mirror of
https://github.com/hanzo-fi/payments.git
synced 2026-10-30 20:31:20 +00:00
119 lines
3.5 KiB
Go
119 lines
3.5 KiB
Go
package moneycorp
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/hanzo-fi/go-libs/v5/pkg/types/pointer"
|
|
"github.com/hanzo-fi/go-libs/v5/pkg/types/currency"
|
|
"github.com/hanzo-fi/payments/ce/plugins/moneycorp/client"
|
|
"github.com/hanzo-fi/payments/pkg/domain/models"
|
|
errorsutils "github.com/hanzo-fi/payments/pkg/domain/errors"
|
|
)
|
|
|
|
func (p *Plugin) createPayout(ctx context.Context, pi models.PSPPaymentInitiation) (*models.PSPPayment, error) {
|
|
if err := p.validateTransferPayoutRequests(pi); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
curr, precision, err := currency.GetCurrencyAndPrecisionFromAsset(supportedCurrenciesWithDecimal, pi.Asset)
|
|
if err != nil {
|
|
return nil, errorsutils.NewWrappedError(
|
|
fmt.Errorf("failed to get currency and precision from asset: %w", err),
|
|
models.ErrInvalidRequest,
|
|
)
|
|
}
|
|
|
|
amount, err := currency.GetStringAmountFromBigIntWithPrecision(pi.Amount, precision)
|
|
if err != nil {
|
|
return nil, errorsutils.NewWrappedError(
|
|
fmt.Errorf("failed to get string amount from big int %v, %w", pi.Amount, err),
|
|
models.ErrInvalidRequest,
|
|
)
|
|
}
|
|
|
|
resp, err := p.client.InitiatePayout(
|
|
ctx,
|
|
&client.PayoutRequest{
|
|
IdempotencyKey: pi.Reference,
|
|
SourceAccountID: pi.SourceAccount.Reference,
|
|
RecipientID: pi.DestinationAccount.Reference,
|
|
PaymentAmount: json.Number(amount),
|
|
PaymentCurrency: curr,
|
|
PaymentMethod: "Standard",
|
|
PaymentReference: pi.Description,
|
|
ClientReference: pi.Description,
|
|
},
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return payoutToPayment(resp)
|
|
}
|
|
|
|
func payoutToPayment(from *client.PayoutResponse) (*models.PSPPayment, error) {
|
|
if from == nil {
|
|
return nil, errorsutils.NewWrappedError(
|
|
fmt.Errorf("cannot convert a nil payout to a payment"),
|
|
models.ErrInvalidRequest,
|
|
)
|
|
}
|
|
|
|
raw, err := json.Marshal(from)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to marshal transaction: %w", err)
|
|
}
|
|
|
|
createdAt, err := time.Parse("2006-01-02T15:04:05.999999999", from.Attributes.CreatedAt)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to parse transaction date: %w", err)
|
|
}
|
|
|
|
c, err := currency.GetPrecision(supportedCurrenciesWithDecimal, from.Attributes.PaymentCurrency)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
amount, err := currency.GetAmountWithPrecisionFromString(from.Attributes.PaymentAmount.String(), c)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &models.PSPPayment{
|
|
Reference: from.ID,
|
|
CreatedAt: createdAt,
|
|
Type: models.PAYMENT_TYPE_PAYOUT,
|
|
Amount: amount,
|
|
Asset: currency.FormatAsset(supportedCurrenciesWithDecimal, from.Attributes.PaymentCurrency),
|
|
Scheme: models.PAYMENT_SCHEME_OTHER,
|
|
Status: matchPaymentStatus(from.Attributes.PaymentStatus),
|
|
SourceAccountReference: pointer.For(fmt.Sprintf("%d", from.Attributes.AccountID)),
|
|
DestinationAccountReference: func() *string {
|
|
if from.Attributes.RecipientDetails.RecipientID == 0 {
|
|
return nil
|
|
}
|
|
return pointer.For(fmt.Sprintf("%d", from.Attributes.RecipientDetails.RecipientID))
|
|
}(),
|
|
Raw: raw,
|
|
}, nil
|
|
}
|
|
|
|
func matchPaymentStatus(status string) models.PaymentStatus {
|
|
// Unauthorised, Awaiting Dispatch, Sent, Cleared, Failed, Cancelled or Query
|
|
switch status {
|
|
case "Unauthorised", "Failed", "Query":
|
|
return models.PAYMENT_STATUS_FAILED
|
|
case "Awaiting Dispatch", "Sent":
|
|
return models.PAYMENT_STATUS_PENDING
|
|
case "Cleared":
|
|
return models.PAYMENT_STATUS_SUCCEEDED
|
|
case "Cancelled":
|
|
return models.PAYMENT_STATUS_FAILED
|
|
}
|
|
|
|
return models.PAYMENT_STATUS_UNKNOWN
|
|
}
|