From cc999a11d13cd14b35399a277a262f9bde4826eb Mon Sep 17 00:00:00 2001 From: Muhammad Zakir Ramadhan <61570975+zakirkun@users.noreply.github.com> Date: Thu, 9 May 2024 22:31:31 +0700 Subject: [PATCH] finally fix open payment done --- client/open_payment.go | 150 ++++++++++++++++++++++++++++++++++-- client/open_payment_test.go | 11 +-- 2 files changed, 150 insertions(+), 11 deletions(-) diff --git a/client/open_payment.go b/client/open_payment.go index 460363b..74ce9e6 100644 --- a/client/open_payment.go +++ b/client/open_payment.go @@ -3,7 +3,8 @@ package client import ( "context" "encoding/json" - "net/http" + "fmt" + "net/url" "github.com/zakirkun/go-tripay/internal/requester" ) @@ -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(), } @@ -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 + +} diff --git a/client/open_payment_test.go b/client/open_payment_test.go index 5e948de..fb53794 100644 --- a/client/open_payment_test.go +++ b/client/open_payment_test.go @@ -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)