Skip to content

Commit

Permalink
Add code for transaction not found
Browse files Browse the repository at this point in the history
  • Loading branch information
AchoArnold committed Dec 1, 2024
1 parent 86cfeb5 commit c9aa158
Show file tree
Hide file tree
Showing 4 changed files with 183 additions and 276 deletions.
60 changes: 60 additions & 0 deletions internal/stubs/refund.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,63 @@ package stubs
func RefundInvalidClientResponse() []byte {
return []byte(`{"error":"invalid_client"}`)
}

// RefundResponse is the response when refunding a transaction
func RefundResponse() []byte {
return []byte(`
{
"MD5OfMessageBody": "4b55cf6629b5f0ee3c8ac91435a2eb35",
"MD5OfMessageAttributes": "896f665ac83c778c88943113ee0ccd55",
"MessageId": "993764f9-6b1f-41bd-a7ca-97b8b2167ed7",
"ResponseMetadata": {
"RequestId": "e7b09b6d-0d11-5111-8dc9-c4ab56e3cf7c",
"HTTPStatusCode": 200,
"HTTPHeaders": {
"x-amzn-requestid": "e7b09b6d-0d11-5111-8dc9-c4ab56e3cf7c",
"date": "Sun, 01 Dec 2024 12:42:26 GMT",
"content-type": "application/x-amz-json-1.0",
"content-length": "166",
"connection": "keep-alive"
},
"RetryAttempts": 0
}
}
`)
}

// RefundStatusResponse is the response when checking the status of a refund transaction
func RefundStatusResponse() []byte {
return []byte(`
{
"result": {
"message": "Cash in performed successfully",
"data": {
"createtime": "1733056973",
"subscriberMsisdn": "695xxxxxx",
"amount": 98,
"payToken": "CI24120168FBF65A909F588B4480",
"txnid": "CI241201.1342.C36820",
"txnmode": "rembourse",
"txnstatus": "200",
"orderId": "rembourse",
"status": "SUCCESSFULL",
"channelUserMsisdn": "695xxxxxx",
"description": "Remboursement"
}
},
"parameters": {
"amount": "98",
"xauth": "WU5PVEVIRUFEOllOT1RFSEVBRDIwMjA=",
"channel_user_msisdn": "69xxxxxx",
"customer_key": "2fBAAq_xxxxxxx",
"customer_secret": "34nFkKxxxxxx",
"final_customer_name": "Arnold",
"final_customer_phone": "69xxxxxx",
"final_customer_name_accuracy": "0"
},
"CreateAt": "12-01-2024 12:43:00",
"MessageId": "993764f9-6b1f-41bd-a7ca-97b8b2167ed7",
"RefundStep": "2"
}
`)
}
53 changes: 52 additions & 1 deletion refund.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,57 @@ type RefundParams struct {
FinalCustomerName string `json:"final_customer_name"`
RefundMethod string `json:"refund_method"`
FeesIncluded bool `json:"fees_included"`
DebitPolicy string `json:"debit_policy"`
FinalCustomerNameAccuracy string `json:"final_customer_name_accuracy"`
}

// RefundTransaction is the response from a refund transaction
type RefundTransaction struct {
MD5OfMessageBody string `json:"MD5OfMessageBody"`
MD5OfMessageAttributes string `json:"MD5OfMessageAttributes"`
MessageID string `json:"MessageId"`
ResponseMetadata struct {
RequestID string `json:"RequestId"`
HTTPStatusCode int `json:"HTTPStatusCode"`
HTTPHeaders struct {
XAmznRequestid string `json:"x-amzn-requestid"`
Date string `json:"date"`
ContentType string `json:"content-type"`
ContentLength string `json:"content-length"`
Connection string `json:"connection"`
} `json:"HTTPHeaders"`
RetryAttempts int `json:"RetryAttempts"`
} `json:"ResponseMetadata"`
}

// RefundTransactionStatus is the response from a refund transaction status
type RefundTransactionStatus struct {
Result struct {
Message string `json:"message"`
Data struct {
CreatedAt string `json:"createtime"`
SubscriberMsisdn string `json:"subscriberMsisdn"`
Amount int `json:"amount"`
PayToken string `json:"payToken"`
TransactionID string `json:"txnid"`
TransactionMode string `json:"txnmode"`
TransactionStatus string `json:"txnstatus"`
OrderID string `json:"orderId"`
Status string `json:"status"`
ChannelUserMsisdn string `json:"channelUserMsisdn"`
Description string `json:"description"`
} `json:"data"`
} `json:"result"`
Parameters struct {
Amount string `json:"amount"`
Xauth string `json:"xauth"`
ChannelUserMsisdn string `json:"channel_user_msisdn"`
CustomerKey string `json:"customer_key"`
CustomerSecret string `json:"customer_secret"`
FinalCustomerName string `json:"final_customer_name"`
FinalCustomerPhone string `json:"final_customer_phone"`
FinalCustomerNameAccuracy string `json:"final_customer_name_accuracy"`
} `json:"parameters"`
CreatedAt string `json:"CreateAt"`
MessageID string `json:"MessageId"`
RefundStep string `json:"RefundStep"`
}
23 changes: 16 additions & 7 deletions refund_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,18 @@ import (
type RefundService service

// Status returns the status of an initiated transaction
func (service *RefundService) Status(ctx context.Context, transactionID string) (*map[string]any, *Response, error) {
func (service *RefundService) Status(ctx context.Context, transactionID string) (*RefundTransactionStatus, *Response, error) {
err := service.client.refreshToken(ctx)
if err != nil {
return nil, nil, err
}

request, err := service.client.newRequest(ctx, http.MethodGet, "/prod/refund/status/"+transactionID, nil)
payload := map[string]any{
"customerkey": service.client.customerKey,
"customersecret": service.client.customerSecret,
}

request, err := service.client.newRequest(ctx, http.MethodGet, "/prod/refund/status/"+transactionID, payload)
if err != nil {
return nil, nil, err
}
Expand All @@ -26,7 +31,7 @@ func (service *RefundService) Status(ctx context.Context, transactionID string)
return nil, response, err
}

transaction := new(map[string]any)
transaction := new(RefundTransactionStatus)
if err = json.Unmarshal(*response.Body, transaction); err != nil {
return nil, response, err
}
Expand All @@ -35,12 +40,17 @@ func (service *RefundService) Status(ctx context.Context, transactionID string)
}

// Refund executes an initiated transaction
func (service *RefundService) Refund(ctx context.Context, params *RefundParams) (*map[string]any, *Response, error) {
func (service *RefundService) Refund(ctx context.Context, params *RefundParams) (*RefundTransaction, *Response, error) {
err := service.client.refreshToken(ctx)
if err != nil {
return nil, nil, err
}

feesIncluded := "No"
if params.FeesIncluded {
feesIncluded = "Yes"
}

payload := map[string]any{
"customerkey": service.client.customerKey,
"customersecret": service.client.customerSecret,
Expand All @@ -51,9 +61,8 @@ func (service *RefundService) Refund(ctx context.Context, params *RefundParams)
"final_customer_phone": params.FinalCustomerPhone,
"final_customer_name": params.FinalCustomerName,
"refund_method": params.RefundMethod,
"fees_included": params.FeesIncluded,
"fees_included": feesIncluded,
"final_customer_name_accuracy": params.FinalCustomerNameAccuracy,
"debit_policy": params.DebitPolicy,
}

request, err := service.client.newRequest(ctx, http.MethodPost, "/prod/refund", payload)
Expand All @@ -66,7 +75,7 @@ func (service *RefundService) Refund(ctx context.Context, params *RefundParams)
return nil, response, err
}

transaction := new(map[string]any)
transaction := new(RefundTransaction)
if err = json.Unmarshal(*response.Body, transaction); err != nil {
return nil, response, err
}
Expand Down
Loading

0 comments on commit c9aa158

Please sign in to comment.