Skip to content

Commit

Permalink
finally fix open payment done
Browse files Browse the repository at this point in the history
  • Loading branch information
zakirkun committed May 9, 2024
1 parent 9bb2473 commit cc999a1
Show file tree
Hide file tree
Showing 2 changed files with 150 additions and 11 deletions.
150 changes: 144 additions & 6 deletions client/open_payment.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package client
import (
"context"
"encoding/json"
"net/http"
"fmt"
"net/url"

"github.com/zakirkun/go-tripay/internal/requester"
)
Expand All @@ -30,26 +31,94 @@ type OpenPaymentResponse struct {
} `json:"data"`
}

type OpenPaymentDetailResponse struct {
Success bool `json:"success"`
Message string `json:"message"`
OpenPaymentDetailData struct {
UUID string `json:"uuid"`
MerchantRef string `json:"merchant_ref"`
CustomerName string `json:"customer_name"`
PaymentName string `json:"payment_name"`
PaymentMethod string `json:"payment_method"`
PayCode string `json:"pay_code"`
QRString string `json:"qr_string,omitempty"`
QRURL string `json:"qr_url,omitempty"`
} `json:"data"`
}

type OpenPaymentListParams struct {
Page int
PerPage int
Sort string
Reference string
MerchantRef string
Method string
Status string
}

type OpenPaymentListResponse struct {
Success bool `json:"success"`
Message string `json:"message"`
OpenPaymentTransaction []struct {
Reference string `json:"reference"`
MerchantRef string `json:"merchant_ref"`
PaymentMethod string `json:"payment_method"`
PaymentName string `json:"payment_name"`
CustomerName string `json:"customer_name"`
Amount int `json:"amount"`
FeeMerchant int `json:"fee_merchant"`
FeeCustomer int `json:"fee_customer"`
TotalFee int `json:"total_fee"`
AmountReceived int `json:"amount_received"`
CheckoutURL string `json:"checkout_url"`
Status string `json:"status"`
PaidAt int64 `json:"paid_at"`
} `json:"data"`
Pagination struct {
Total int `json:"total"`
DataFrom int `json:"data_from"`
DataTo int `json:"data_to"`
PerPage int `json:"per_page"`
CurrentPage int `json:"current_page"`
LastPage int `json:"last_page"`
NextPage *int `json:"next_page"`
} `json:"pagination"`
}

func (c Client) OpenPaymentDetail(uuid string) (*OpenPaymentDetailResponse, error) {
return openPaymentDetail(c, uuid, nil)
}

func (c Client) OpenPaymentDetailWithContext(uuid string, ctx context.Context) (*OpenPaymentDetailResponse, error) {
return openPaymentDetail(c, uuid, ctx)
}

func (c Client) OpenPaymentTransaction(p OpenPaymentRequest) (*OpenPaymentResponse, error) {
return openPayment(c, p, nil)
}

func (c Client) OpenPaymentTransactionWithConext(p OpenPaymentRequest, ctx context.Context) (*OpenPaymentResponse, error) {
func (c Client) OpenPaymentTransactionWithContext(p OpenPaymentRequest, ctx context.Context) (*OpenPaymentResponse, error) {
return openPayment(c, p, ctx)
}

func openPayment(c Client, p OpenPaymentRequest, ctx context.Context) (*OpenPaymentResponse, error) {
func (c Client) OpenPaymentList(uuid string, p ...OpenPaymentListParams) (*OpenPaymentListResponse, error) {
return openPaymentList(c, uuid, nil, p...)
}

p.Signature = c.GetSignature()
func (c Client) OpenPaymentListWithContext(uuid string, ctx context.Context, p ...OpenPaymentListParams) (*OpenPaymentListResponse, error) {
return openPaymentList(c, uuid, ctx, p...)
}

func openPayment(c Client, p OpenPaymentRequest, ctx context.Context) (*OpenPaymentResponse, error) {

payloadBody, err := json.Marshal(p)
payloadBody, err := json.Marshal(&p)
if err != nil {
return nil, err
}

paramReq := requester.IRequesterParams{
Url: c.BaseUrl() + "open-payment/create",
Method: http.MethodPost,
Method: "POST",
Body: payloadBody,
Header: c.HeaderRequest(),
}
Expand All @@ -71,3 +140,72 @@ func openPayment(c Client, p OpenPaymentRequest, ctx context.Context) (*OpenPaym
json.Unmarshal(bodyReq.ResponseBody, &successResponse)
return &successResponse, nil
}

func openPaymentDetail(c Client, uuid string, ctx context.Context) (*OpenPaymentDetailResponse, error) {

paramReq := requester.IRequesterParams{
Url: c.BaseUrl() + "open-payment/" + uuid + "/detail",
Method: "GET",
Body: nil,
Header: c.HeaderRequest(),
}

req := requester.NewRequester(paramReq)
bodyReq := new(requester.IResponseBody)
var errReq error
if ctx != nil {
bodyReq, errReq = req.DOWithContext(ctx)
} else {
bodyReq, errReq = req.DO()
}

if errReq != nil {
return nil, errReq
}

var successResponse OpenPaymentDetailResponse
json.Unmarshal(bodyReq.ResponseBody, &successResponse)
return &successResponse, nil
}

func openPaymentList(c Client, uuid string, ctx context.Context, p ...OpenPaymentListParams) (*OpenPaymentListResponse, error) {

var paymentParams OpenPaymentListParams
for _, m := range p {
paymentParams = m
}

urlParam := url.Values{}
urlParam.Set("page", fmt.Sprintf("%d", paymentParams.Page))
urlParam.Set("per_page", fmt.Sprintf("%d", paymentParams.PerPage))
urlParam.Set("sort", paymentParams.Sort)
urlParam.Set("reference", paymentParams.Reference)
urlParam.Set("merchant_ref", paymentParams.MerchantRef)
urlParam.Set("method", paymentParams.Method)
urlParam.Set("status", paymentParams.Status)

paramReq := requester.IRequesterParams{
Url: c.BaseUrl() + "open-payment/" + uuid + "/transactions?" + urlParam.Encode(),
Method: "GET",
Body: nil,
Header: c.HeaderRequest(),
}

req := requester.NewRequester(paramReq)
bodyReq := new(requester.IResponseBody)
var errReq error
if ctx != nil {
bodyReq, errReq = req.DOWithContext(ctx)
} else {
bodyReq, errReq = req.DO()
}

if errReq != nil {
return nil, errReq
}

var response OpenPaymentListResponse
json.Unmarshal(bodyReq.ResponseBody, &response)
return &response, nil

}
11 changes: 6 additions & 5 deletions client/open_payment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ func TestOpenPaymentSuccess(t *testing.T) {

client.SetSignature(utils.Signature{
MerchantCode: "T14302",
Channel: "BRIVA",
MerchanReff: "INV345678",
Channel: "BCAVA",
MerchanReff: "INV345675",
})

payment := OpenPaymentRequest{
Method: "BRIVA",
MerchatReff: "INV345678",
CustomerName: "Fulan",
Method: "BCAVA",
MerchatReff: "INV345675",
CustomerName: "Fulan Fulan",
Signature: client.GetSignature(),
}

responseOk, responseBad := client.OpenPaymentTransaction(payment)
Expand Down

0 comments on commit cc999a1

Please sign in to comment.