8000 fix(currencycloud): fixes after full testing by paul-nicolas · Pull Request #297 · formancehq/payments · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

fix(currencycloud): fixes after full testing #297

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,21 @@
)

type PayoutRequest struct {
OnBehalfOf string `json:"on_behalf_of"`
BeneficiaryID string `json:"beneficiary_id"`
Currency string `json:"currency"`
Amount json.Number `json:"amount"`
Reference string `json:"reference"`
Reason string `json:"reason"`
UniqueRequestID string `json:"unique_request_id"`
}

func (pr *PayoutRequest) ToFormData() url.Values {
form := url.Values{}
form.Set("on_behalf_of", pr.OnBehalfOf)
form.Set("beneficiary_id", pr.BeneficiaryID)
form.Set("currency", pr.Currency)
form.Set("amount", pr.Amount.String())
form.Set("reason", pr.Reason)
form.Set("payment_type", "regular")

Check warning on line 30 in internal/connectors/plugins/public/currencycloud/client/payouts.go

View check run for this annotation

Codecov / codecov/patch

internal/connectors/plugins/public/currencycloud/client/payouts.go#L29-L30

Added lines #L29 - L30 were not covered by tests
form.Set("reference", pr.Reference)
if pr.UniqueRequestID != "" {
form.Set("unique_request_id", pr.UniqueRequestID)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@ import (

//nolint:tagliatelle // allow different styled tags in client
type Transaction struct {
ID string `json:"id"`
AccountID string `json:"account_id"`
Currency string `json:"currency"`
Type string `json:"type"`
Status string `json:"status"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
Action string `json:"action"`
ID string `json:"id"`
AccountID string `json:"account_id"`
Currency string `json:"currency"`
Type 8000 string `json:"type"`
Status string `json:"status"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
Action string `json:"action"`
RelatedEntityType string `json:"related_entity_type"`
RelatedEntityID string `json:"related_entity_id"`

Amount json.Number `json:"amount"`
}
Expand Down
26 changes: 20 additions & 6 deletions internal/connectors/plugins/public/currencycloud/payments.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,14 @@ func transactionToPayment(transaction client.Transaction) (*models.PSPPayment, e
return nil, err
}

paymentType := matchTransactionType(transaction.Type)
paymentType := matchTransactionType(transaction.RelatedEntityType, transaction.Type)

reference := transaction.RelatedEntityID
if reference == "" {
reference = transaction.ID
}
payment := &models.PSPPayment{
Reference: transaction.ID,
Reference: reference,
CreatedAt: transaction.CreatedAt,
Type: paymentType,
Amount: amount,
Expand All @@ -141,13 +145,23 @@ func transactionToPayment(transaction client.Transaction) (*models.PSPPayment, e
return payment, nil
}

func matchTransactionType(transactionType string) models.PaymentType {
switch transactionType {
case "credit":
func matchTransactionType(entityType string, transactionType string) models.PaymentType {
switch entityType {
case "inbound_funds":
return models.PAYMENT_TYPE_PAYIN
case "debit":
case "payment":
return models.PAYMENT_TYPE_PAYOUT
case "transfer", "balance_transfer":
return models.PAYMENT_TYPE_TRANSFER
default:
switch transactionType {
case "credit":
return models.PAYMENT_TYPE_PAYIN
case "debit":
return models.PAYMENT_TYPE_PAYOUT
}
}

return models.PAYMENT_TYPE_OTHER
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -385,3 +385,56 @@ func comparePSPPayments(t *testing.T, a, b models.PSPPayment) {
require.Equal(t, v, b.Metadata[k])
}
}

func TestMatchTransactionType(t *testing.T) {
tests := []struct {
entityType string
transactionType string
expectedPaymentType models.PaymentType
}{
{
entityType: "inbound_funds",
transactionType: "credit",
expectedPaymentType: models.PAYMENT_TYPE_PAYIN,
},
{
entityType: "payment",
transactionType: "debit",
expectedPaymentType: models.PAYMENT_TYPE_PAYOUT,
},
{
entityType: "transfer",
transactionType: "debit",
expectedPaymentType: models.PAYMENT_TYPE_TRANSFER,
},
{
entityType: "balance_transfer",
transactionType: "debit",
expectedPaymentType: models.PAYMENT_TYPE_TRANSFER,
},
{
entityType: "unknown",
transactionType: "unknown",
expectedPaymentType: models.PAYMENT_TYPE_OTHER,
},
{
entityType: "unknown",
transactionType: "credit",
expectedPaymentType: models.PAYMENT_TYPE_PAYIN,
},
{
entityType: "unknown",
transactionType: "debit",
expectedPaymentType: models.PAYMENT_TYPE_PAYOUT,
},
}

for _, test := range tests {
t.Run(test.entityType+"-"+test.transactionType, func(t *testing.T) {
t.Parallel()

paymentType := matchTransactionType(test.entityType, test.transactionType)
require.Equal(t, test.expectedPaymentType, paymentType)
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,12 @@ func (p *Plugin) createPayout(ctx context.Context, pi models.PSPPaymentInitiatio
return models.PSPPayment{}, fmt.Errorf("failed to get string amount from big int: %v: %w", err, models.ErrInvalidRequest)
}

contact, err := p.client.GetContactID(ctx, pi.SourceAccount.Reference)
if err != nil {
return models.PSPPayment{}, err
}

resp, err := p.client.InitiatePayout(ctx, &client.PayoutRequest{
OnBehalfOf: contact.ID,
BeneficiaryID: pi.DestinationAccount.Reference,
Currency: curr,
Amount: json.Number(amount),
Reference: pi.Description,
Reason: pi.Description,
UniqueRequestID: pi.Reference,
})
if err != nil {
Expand Down
< 9E88 td class="blob-num blob-num-deletion empty-cell">
Original file line number Diff line number Diff line change
Expand Up @@ -99,34 +99,17 @@ var _ = Describe("CurrencyCloud Plugin Payouts Creation", func() {
Expect(resp).To(Equal(models.CreatePayoutResponse{}))
})

It("should return an error - get contactID error", func(ctx SpecContext) {
req := models.CreatePayoutRequest{
PaymentInitiation: samplePSPPaymentInitiation,
}

m.EXPECT().GetContactID(gomock.Any(), samplePSPPaymentInitiation.SourceAccount.Reference).
Return(nil, errors.New("test error"))

resp, err := plg.CreatePayout(ctx, req)
Expect(err).ToNot(BeNil())
Expect(err).To(MatchError("test error"))
Expect(resp).To(Equal(models.CreatePayoutResponse{}))
})

It("should return an error - initiate payout error", func(ctx SpecContext) {
req := models.CreatePayoutRequest{
PaymentInitiation: samplePSPPaymentInitiation,
}

m.EXPECT().GetContactID(gomock.Any(), samplePSPPaymentInitiation.SourceAccount.Reference).
Return(&client.Contact{ID: "1"}, nil)

m.EXPECT().InitiatePayout(gomock.Any(), &client.PayoutRequest{
OnBehalfOf: "1",
BeneficiaryID: samplePSPPaymentInitiation.DestinationAccount.Reference,
Currency: "EUR",
Amount: "1.00",
Reference: samplePSPPaymentInitiation.Description,
Reason: samplePSPPaymentInitiation.Description,
UniqueRequestID: samplePSPPaymentInitiation.Reference,
}).Return(nil, errors.New("test error"))

Expand All @@ -141,9 +124,6 @@ var _ = Describe("CurrencyCloud Plugin Payouts Creation", func() {
PaymentInitiation: samplePSPPaymentInitiation,
}

m.EXPECT().GetContactID(gomock.Any(), samplePSPPaymentInitiation.SourceAccount.Reference).
Return(&client.Contact{ID: "1"}, nil)

trResponse := client.PayoutResponse{
ID: "test1",
Amount: "1.00",
Expand All @@ -154,11 +134,11 @@ var _ = Describe("CurrencyCloud Plugin Payouts Creation", func() {
CreatedAt: now,
}
m.EXPECT().InitiatePayout(gomock.Any(), &client.PayoutRequest{
OnBehalfOf: "1",
BeneficiaryID: samplePSPPaymentInitiation.DestinationAccount.Reference,
Currency: "EUR",
Amount: "1.00",
Reference: samplePSPPaymentInitiation.Description,
Reason: samplePSPPaymentInitiation.Description,
UniqueRequestID: samplePSPPaymentInitiation.Reference,
}).Return(&trResponse, nil)

Expand Down
Loading
0