Skip to content

Commit

Permalink
fix moneycorp
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-nicolas committed Sep 25, 2023
1 parent 57ce1ad commit 7f05faa
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,19 @@ import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
)

type payoutRequest struct {
Payout *PayoutRequest `json:"data"`
Payout struct {
Attributes *PayoutRequest `json:"attributes"`
} `json:"data"`
}

type PayoutRequest struct {
SourceAccountID string `json:"-"`
IdempotencyKey string `json:"-"`
RecipientID string `json:"recipientId"`
PaymentDate string `json:"paymentDate"`
PaymentAmount float64 `json:"paymentAmount"`
Expand Down Expand Up @@ -54,9 +58,9 @@ type PayoutResponse struct {
func (c *Client) InitiatePayout(ctx context.Context, pr *PayoutRequest) (*PayoutResponse, error) {
endpoint := fmt.Sprintf("%s/accounts/%s/payments", c.endpoint, pr.SourceAccountID)

body, err := json.Marshal(&payoutRequest{
Payout: pr,
})
reqBody := &payoutRequest{}
reqBody.Payout.Attributes = pr
body, err := json.Marshal(reqBody)
if err != nil {
return nil, fmt.Errorf("failed to marshal payout request: %w", err)
}
Expand All @@ -66,6 +70,7 @@ func (c *Client) InitiatePayout(ctx context.Context, pr *PayoutRequest) (*Payout
return nil, fmt.Errorf("failed to create login request: %w", err)
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Idempotency-Key", pr.IdempotencyKey)

resp, err := c.httpClient.Do(req)
if err != nil {
Expand All @@ -74,6 +79,9 @@ func (c *Client) InitiatePayout(ctx context.Context, pr *PayoutRequest) (*Payout
defer resp.Body.Close()

if resp.StatusCode != http.StatusCreated {
body, _ := io.ReadAll(resp.Body)
fmt.Println(string(body))

return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@ import (
)

type transferRequest struct {
Transfer *TransferRequest `json:"data"`
Transfer struct {
Attributes *TransferRequest `json:"attributes"`
} `json:"data"`
}

type TransferRequest struct {
SourceAccountID string `json:"-"`
IdempotencyKey string `json:"-"`
ReceivingAccountID string `json:"receivingAccountId"`
TransferAmount float64 `json:"transferAmount"`
TransferCurrency string `json:"transferCurrency"`
Expand All @@ -28,9 +31,9 @@ type transferResponse struct {
type TransferResponse struct {
ID string `json:"id"`
Attributes struct {
SendingAccountID string `json:"sendingAccountId"`
SendingAccountID int64 `json:"sendingAccountId"`
SendingAccountName string `json:"sendingAccountName"`
ReceivingAccountID string `json:"receivingAccountId"`
ReceivingAccountID int64 `json:"receivingAccountId"`
ReceivingAccountName string `json:"receivingAccountName"`
CreatedAt string `json:"createdAt"`
CreatedBy string `json:"createdBy"`
Expand All @@ -47,9 +50,9 @@ type TransferResponse struct {
func (c *Client) InitiateTransfer(ctx context.Context, tr *TransferRequest) (*TransferResponse, error) {
endpoint := fmt.Sprintf("%s/accounts/%s/transfers", c.endpoint, tr.SourceAccountID)

body, err := json.Marshal(&transferRequest{
Transfer: tr,
})
reqBody := &transferRequest{}
reqBody.Transfer.Attributes = tr
body, err := json.Marshal(reqBody)
if err != nil {
return nil, fmt.Errorf("failed to marshal transfer request: %w", err)
}
Expand All @@ -59,6 +62,7 @@ func (c *Client) InitiateTransfer(ctx context.Context, tr *TransferRequest) (*Tr
return nil, fmt.Errorf("failed to create transfer request: %w", err)
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Idempotency-Key", tr.IdempotencyKey)

resp, err := c.httpClient.Do(req)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ func taskInitiatePayment(logger logging.Logger, moneycorpClient *client.Client,
var resp *client.TransferResponse
resp, err = moneycorpClient.InitiateTransfer(ctx, &client.TransferRequest{
SourceAccountID: transfer.SourceAccount.Reference,
IdempotencyKey: transfer.ID.UniqueRequestID,
ReceivingAccountID: transfer.DestinationAccount.Reference,
TransferAmount: amount,
TransferCurrency: curr,
Expand All @@ -116,6 +117,7 @@ func taskInitiatePayment(logger logging.Logger, moneycorpClient *client.Client,
var resp *client.PayoutResponse
resp, err = moneycorpClient.InitiatePayout(ctx, &client.PayoutRequest{
SourceAccountID: transfer.SourceAccount.Reference,
IdempotencyKey: transfer.ID.UniqueRequestID,
RecipientID: transfer.DestinationAccount.Reference,
PaymentAmount: amount,
PaymentCurrency: curr,
Expand Down Expand Up @@ -182,7 +184,7 @@ func taskUpdatePaymentStatus(
metricsRegistry metrics.MetricsRegistry,
) error {
transferInitiationID := models.MustTransferInitiationIDFromString(transferID)
transfer, err := getTransfer(ctx, storageReader, transferInitiationID, false)
transfer, err := getTransfer(ctx, storageReader, transferInitiationID, true)
if err != nil {
return err
}
Expand Down

0 comments on commit 7f05faa

Please sign in to comment.