-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |