Skip to content

Commit

Permalink
Push dev
Browse files Browse the repository at this point in the history
  • Loading branch information
zakirkun committed May 9, 2024
1 parent 939ee45 commit 9bb2473
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 0 deletions.
9 changes: 9 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ type Client struct {
MerchantCode string
ApiKey string
PrivateKey string
Signature string
Mode utils.TRIPAY_MODE
}

Expand All @@ -25,3 +26,11 @@ func (c Client) BaseUrl() string {

return string(utils.URL_PRODUCTION)
}

func (c *Client) SetSignature(s utils.Signature) {
c.Signature = s.CreateSignature()
}

func (c Client) GetSignature() string {
return c.Signature
}
73 changes: 73 additions & 0 deletions client/open_payment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package client

import (
"context"
"encoding/json"
"net/http"

"github.com/zakirkun/go-tripay/internal/requester"
)

type OpenPaymentRequest struct {
Method string `json:"method"`
MerchatReff string `json:"merchant_ref"`
CustomerName string `json:"customer_name"`
Signature string `json:"signature"`
}

type OpenPaymentResponse struct {
Success bool `json:"success"`
Message string `json:"message"`
OpenPaymentData 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"`
QRURL string `json:"qr_url"`
} `json:"data"`
}

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

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

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

p.Signature = c.GetSignature()

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

paramReq := requester.IRequesterParams{
Url: c.BaseUrl() + "open-payment/create",
Method: http.MethodPost,
Body: payloadBody,
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 OpenPaymentResponse
json.Unmarshal(bodyReq.ResponseBody, &successResponse)
return &successResponse, nil
}
35 changes: 35 additions & 0 deletions client/open_payment_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package client

import (
"testing"

"github.com/zakirkun/go-tripay/utils"
)

func TestOpenPaymentSuccess(t *testing.T) {
client := Client{
MerchantCode: "T14302",
ApiKey: "DEV-ZKIDl5gE3AsCDThj7mWX6yvQ8f42NZWJWlZ7TSzS",
PrivateKey: "J2WTm-93avv-w0PZV-ur1t4-4TCjd",
Mode: utils.MODE_DEVELOPMENT,
}

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

payment := OpenPaymentRequest{
Method: "BRIVA",
MerchatReff: "INV345678",
CustomerName: "Fulan",
}

responseOk, responseBad := client.OpenPaymentTransaction(payment)
if responseBad != nil {
t.Errorf("ERROR: %v", responseBad)
}

t.Log("Success: ", responseOk)
}

0 comments on commit 9bb2473

Please sign in to comment.