From a288a272512e0f00cf48b06753842bb50c1dc8fa Mon Sep 17 00:00:00 2001 From: uchencho Date: Sun, 24 Sep 2023 17:00:47 +0100 Subject: [PATCH] test all refund functionalities --- default.go | 91 ++++++++++++++++++++++++++++++++++++++++-------------- deposit.go | 2 +- refund.go | 61 ++++++++++++++++++++++++++++++++++++ 3 files changed, 130 insertions(+), 24 deletions(-) create mode 100644 refund.go diff --git a/default.go b/default.go index 1824408..09e9e9c 100644 --- a/default.go +++ b/default.go @@ -147,6 +147,7 @@ func (t Payout) IsNotFound() bool { type ResendCallbackRequest struct { PayoutId string `json:"payoutId,omitempty"` DepositId string `json:"depositId,omitempty"` + RefundId string `json:"refundId,omitempty"` } type PayoutStatusResponse struct { @@ -174,11 +175,72 @@ type CreateDepositResponse struct { Annotation APIAnnotation } +type InitiateRefundResponse struct { + RefundId string `json:"depositId"` + Status string `json:"status"` + Created string `json:"created"` + Annotation APIAnnotation +} + +type Deposit struct { + DepositId string `json:"depositId"` + Status string `json:"status"` + RequestedAmount string `json:"requestedAmount"` + DepositedAmount string `json:"depositedAmount"` + Currency string `json:"currency"` + Country string `json:"country"` + Payer Payer `json:"payer"` + Correspondent string `json:"correspondent"` + StatementDescription string `json:"statementDescription"` + CustomerTimestamp string `json:"customerTimestamp"` + Created string `json:"created"` + RespondedByPayer string `json:"respondedByPayer"` + CorrespondentIds map[string]interface{} `json:"correspondentIds"` + SuspiciousActivity map[string]interface{} `json:"suspiciousActivityReport"` + FailureReason FailureReason `json:"failureReason"` + Annotation APIAnnotation +} + +type DepositStatusResponse struct { + DepositId string `json:"depositId"` + Status string `json:"status"` + Annotation APIAnnotation +} + +type RefundRequest struct { + RefundId string `json:"refundId"` + DepositId string `json:"depositId"` + Amount string `json:"amount"` +} + type CreateBulkDepositResponse struct { Result []CreateDepositResponse Annotation APIAnnotation } +type Refund struct { + RefundId string `json:"refundId"` + Status string `json:"status"` + Amount string `json:"amount"` + Currency string `json:"currency"` + Country string `json:"country"` + Recipient Recipient `json:"recipient"` + Correspondent string `json:"correspondent"` + StatementDescription string `json:"statementDescription"` + CustomerTimestamp string `json:"customerTimestamp"` + Created string `json:"created"` + ReceivedByRecipient string `json:"receivedByRecipient"` + CorrespondentIds map[string]interface{} `json:"correspondentIds"` + FailureReason FailureReason `json:"failureReason"` + Annotation APIAnnotation +} + +type RefundStatusResponse struct { + RefundId string `json:"refundId"` + Status string `json:"status"` + Annotation APIAnnotation +} + // TimeProviderFunc represents a provider of time type TimeProviderFunc func() time.Time @@ -327,29 +389,12 @@ func (s *Service) newCreateBulkDepositRequest(timeProvider TimeProviderFunc, req return requests, nil } -type Deposit struct { - DepositId string `json:"depositId"` - Status string `json:"status"` - RequestedAmount string `json:"requestedAmount"` - DepositedAmount string `json:"depositedAmount"` - Currency string `json:"currency"` - Country string `json:"country"` - Payer Payer `json:"recipient"` - Correspondent string `json:"correspondent"` - StatementDescription string `json:"statementDescription"` - CustomerTimestamp string `json:"customerTimestamp"` - Created string `json:"created"` - RespondedByPayer string `json:"respondedByPayer"` - CorrespondentIds map[string]interface{} `json:"correspondentIds"` - SuspiciousActivity map[string]interface{} `json:"suspiciousActivityReport"` - FailureReason FailureReason `json:"failureReason"` - Annotation APIAnnotation -} - -type DepositStatusResponse struct { - DepositId string `json:"depositId"` - Status string `json:"status"` - Annotation APIAnnotation +func (s Service) newRefundRequest(refundId, depositId string, amount Amount) RefundRequest { + return RefundRequest{ + RefundId: refundId, + DepositId: depositId, + Amount: amount.Value, + } } func GetAllCorrespondents() ([]MomoMapping, error) { diff --git a/deposit.go b/deposit.go index 7dcb03e..de355e7 100644 --- a/deposit.go +++ b/deposit.go @@ -72,7 +72,7 @@ func (s *Service) GetDeposit(depositId string) (Deposit, error) { return result, nil } -// ResendCallback provides the functionality of resending a callback (webhook) for a deposit +// ResendDepositCallback provides the functionality of resending a callback (webhook) for a deposit // See docs https://docs.pawapay.co.uk/#operation/depositsResendCallback for more details func (s *Service) ResendDepositCallback(depositId string) (DepositStatusResponse, error) { diff --git a/refund.go b/refund.go new file mode 100644 index 0000000..c005a06 --- /dev/null +++ b/refund.go @@ -0,0 +1,61 @@ +package pawapay + +import ( + "fmt" + "net/http" +) + +// RequestRefund provides the functionality of requesting a refund for an initiated deposit +// See docs https://docs.pawapay.co.uk/#operation/depositWebhook for more details +func (s *Service) RequestRefund(refundId, depositId string, amount Amount) (InitiateRefundResponse, error) { + + resource := "refunds" + payload := s.newRefundRequest(refundId, depositId, amount) + + var response InitiateRefundResponse + annotation, err := s.makeRequest(http.MethodPost, resource, payload, &response) + if err != nil { + return InitiateRefundResponse{}, err + } + response.Annotation = annotation + + return response, nil +} + +// GetRefund provides the functionality of retrieving an initiated refund +// See docs https://docs.pawapay.co.uk/#operation/getRefund for more details +func (s *Service) GetRefund(refundId string) (Refund, error) { + + resource := fmt.Sprintf("refunds/%s", refundId) + var ( + response []Refund + result Refund + ) + + annotation, err := s.makeRequest(http.MethodGet, resource, nil, &response) + if err != nil { + return Refund{}, err + } + if len(response) > 0 { + result = response[0] + } + result.Annotation = annotation + return result, nil +} + +// ResendRefundCallback provides the functionality of resending a callback (webhook) +// See docs https://docs.pawapay.co.uk/#operation/refundsResendCallback for more details +func (s *Service) ResendRefundCallback(refundId string) (RefundStatusResponse, error) { + + resource := "refunds/resend-callback" + payload := ResendCallbackRequest{RefundId: refundId} + + var response RefundStatusResponse + annotation, err := s.makeRequest(http.MethodPost, resource, payload, &response) + if err != nil { + return RefundStatusResponse{}, err + } + response.Annotation = annotation + + return response, nil +}