Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AchoArnold committed Dec 8, 2022
1 parent e5960fd commit 63a3206
Show file tree
Hide file tree
Showing 5 changed files with 419 additions and 4 deletions.
2 changes: 1 addition & 1 deletion client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestClient_Token(t *testing.T) {

// Arrange
requests := make([]http.Request, 0)
server := helpers.MakeRequestCapturingTestServer(http.StatusOK, [][]byte{stubs.TokenResponse()}, &requests)
server := helpers.MakeRequestCapturingTestServer([]int{http.StatusOK}, [][]byte{stubs.TokenResponse()}, &requests)

client := New(
WithBaseURL(server.URL),
Expand Down
4 changes: 2 additions & 2 deletions internal/helpers/test_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func MakeTestServer(responseCode int, body []byte) *httptest.Server {
}

// MakeRequestCapturingTestServer creates an api server that captures the request object
func MakeRequestCapturingTestServer(responseCode int, responses [][]byte, requests *[]http.Request) *httptest.Server {
func MakeRequestCapturingTestServer(responseCodes []int, responses [][]byte, requests *[]http.Request) *httptest.Server {
index := 0
return httptest.NewServer(http.HandlerFunc(func(responseWriter http.ResponseWriter, req *http.Request) {
clonedRequest := req.Clone(context.Background())
Expand All @@ -35,7 +35,7 @@ func MakeRequestCapturingTestServer(responseCode int, responses [][]byte, reques

*requests = append(*requests, *clonedRequest)

responseWriter.WriteHeader(responseCode)
responseWriter.WriteHeader(responseCodes[index])
_, err = responseWriter.Write(responses[index])
index++
if err != nil {
Expand Down
130 changes: 130 additions & 0 deletions internal/stubs/merchant_payment.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,133 @@ func MerchantPaymentInitResponse() []byte {
}
`)
}

// MerchantPaymentPayResponseWithInsufficientFunds is the response when the user has insufficient funds
func MerchantPaymentPayResponseWithInsufficientFunds() []byte {
return []byte(`
{
"message":"60019 :: Le solde du compte du payeur est insuffisant",
"data":{
"id":48462449,
"createtime":"1670442106",
"subscriberMsisdn":"69XXXXXXX",
"amount":100,
"payToken":"MP22120771FEB7B21FD2381C3786",
"txnid":null,
"txnmode":"12345",
"inittxnmessage":"Le solde du compte du payeur est insuffisant",
"inittxnstatus":"60019",
"confirmtxnstatus":null,
"confirmtxnmessage":null,
"status":"FAILED",
"notifUrl":"https://example.com/payment-notification",
"description":"Payment Description",
"channelUserMsisdn":"69XXXXXXX"
}
}
`)
}

// MerchantPaymentPayResponse is the response after executing a payment
func MerchantPaymentPayResponse() []byte {
return []byte(`
{
"message":"Merchant payment successfully initiated",
"data":{
"id":48463325,
"createtime":"1670442691",
"subscriberMsisdn":"69XXXXXXX",
"amount":100,
"payToken":"MP22120771FEB7B21FD2381C3786",
"txnid":"MP221207.2051.B56929",
"txnmode":"12345",
"inittxnmessage":"Paiement e la clientele done.The devrez confirmer le paiement en saisissant son code PIN et vous recevrez alors un SMS. Merci dutiliser des services Orange Money.",
"inittxnstatus":"200",
"confirmtxnstatus":null,
"confirmtxnmessage":null,
"status":"PENDING",
"notifUrl":"https://example.com/payment-notification",
"description":"Payment Description",
"channelUserMsisdn":"69XXXXXXX"
}
}
`)
}

// MerchantPaymentPushResponse is the response after sending a push notification
func MerchantPaymentPushResponse() []byte {
return []byte(`
{
"message":"Push sent to customer",
"data":{
"id":48463325,
"createtime":"1670442691",
"subscriberMsisdn":"69XXXXXXX",
"amount":100,
"payToken":"MP22120771FEB7B21FD2381C3786",
"txnid":"MP221207.2051.B56929",
"txnmode":"12345",
"inittxnmessage":"Paiement e la clientele done.The devrez confirmer le paiement en saisissant son code PIN et vous recevrez alors un SMS. Merci dutiliser des services Orange Money.",
"inittxnstatus":"200",
"confirmtxnstatus":null,
"confirmtxnmessage":null,
"status":"PENDING",
"notifUrl":"https://example.com/payment-notification",
"description":"Payment Description",
"channelUserMsisdn":"69XXXXXXX"
}
}
`)
}

// MerchantPaymentTransactionStatusResponse is the transaction status response for a confirmed payment
func MerchantPaymentTransactionStatusResponse() []byte {
return []byte(`
{
"message":"Transaction retrieved successfully",
"data":{
"id":48463325,
"createtime":"1670442691",
"subscriberMsisdn":"69XXXXXXX",
"amount":100,
"payToken":"MP22120771FEB7B21FD2381C3786",
"txnid":"MP221207.2051.B56929",
"txnmode":"12345",
"inittxnmessage":"Paiement e la clientele done.The devrez confirmer le paiement en saisissant son code PIN et vous recevrez alors un SMS. Merci dutiliser des services Orange Money.",
"inittxnstatus":"200",
"confirmtxnstatus":"200",
"confirmtxnmessage":"Successful Payment of COMPANY_NAME from 69XXXXXXX CUSTOMER_NAME. Transaction ID:MP221207.2051.B56929, Amount:100, New balance:1103.5.",
"status":"SUCCESSFULL",
"notifUrl":"https://example.com/payment-notification",
"description":"Payment Description",
"channelUserMsisdn":"69XXXXXXX"
}
}
`)
}

// MerchantPaymentTransactionStatusResponseWithExpired is the transaction status response for an expired payment
func MerchantPaymentTransactionStatusResponseWithExpired() []byte {
return []byte(`
{
"message":"Transaction retrieved successfully",
"data":{
"id":48445436,
"createtime":"1670436067",
"subscriberMsisdn":null,
"amount":null,
"payToken":"MP22120771FEB7B21FD2381C3786",
"txnid":null,
"txnmode":null,
"inittxnmessage":null,
"inittxnstatus":null,
"confirmtxnstatus":null,
"confirmtxnmessage":null,
"status":"EXPIRED",
"notifUrl":null,
"description":null,
"channelUserMsisdn":null
}
}
`)
}
17 changes: 17 additions & 0 deletions merchant_payment.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,20 @@ type MerchantPaymentTransaction struct {
Description string `json:"description"`
ChannelUserMSISDN string `json:"channelUserMsisdn"`
}

// IsExpired checks if a transaction is expired
func (transaction *MerchantPaymentTransaction) IsExpired() bool {
return transaction.Status == "EXPIRED"
}

// IsPending checks if a transaction is pending
func (transaction *MerchantPaymentTransaction) IsPending() bool {
return transaction.Status == "PENDING"
}

// IsConfirmed checks if a transaction is confirmed by the user
func (transaction *MerchantPaymentTransaction) IsConfirmed() bool {
return transaction.Status == "SUCCESSFULL" &&
transaction.ConfirmTransactionStatus != nil &&
*transaction.ConfirmTransactionStatus == "200"
}
Loading

0 comments on commit 63a3206

Please sign in to comment.