mirror of
https://github.com/hanzo-fi/payments.git
synced 2026-10-30 20:31:20 +00:00
119 lines
3.8 KiB
Go
119 lines
3.8 KiB
Go
package moneycorp
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/hanzo-fi/go-libs/v5/pkg/types/currency"
|
|
"github.com/hanzo-fi/go-libs/v5/pkg/types/pointer"
|
|
"github.com/hanzo-fi/payments/ce/plugins/moneycorp/client"
|
|
errorsutils "github.com/hanzo-fi/payments/pkg/domain/errors"
|
|
"github.com/hanzo-fi/payments/pkg/domain/models"
|
|
"github.com/hanzo-fi/payments/pkg/domain/plugins"
|
|
)
|
|
|
|
func (p *Plugin) createTransfer(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 amount %v: %w", pi.Amount, err),
|
|
models.ErrInvalidRequest,
|
|
)
|
|
}
|
|
|
|
resp, err := p.client.InitiateTransfer(
|
|
ctx,
|
|
&client.TransferRequest{
|
|
IdempotencyKey: pi.Reference,
|
|
SourceAccountID: pi.SourceAccount.Reference,
|
|
ReceivingAccountID: pi.DestinationAccount.Reference,
|
|
TransferAmount: json.Number(amount),
|
|
TransferCurrency: curr,
|
|
TransferReference: pi.Description,
|
|
ClientReference: pi.Description,
|
|
},
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return transferToPayment(resp)
|
|
}
|
|
|
|
func transferToPayment(transfer *client.TransferResponse) (*models.PSPPayment, error) {
|
|
if transfer == nil {
|
|
return nil, errorsutils.NewWrappedError(
|
|
fmt.Errorf("cannot convert a nil transfer to a payment"),
|
|
models.ErrInvalidRequest,
|
|
)
|
|
}
|
|
|
|
raw, err := json.Marshal(transfer)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to marshal transaction: %w", err)
|
|
}
|
|
|
|
createdAt, err := time.Parse("2006-01-02T15:04:05.999999999", transfer.Attributes.CreatedAt)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to parse transaction date: %w", err)
|
|
}
|
|
|
|
c, err := currency.GetPrecision(supportedCurrenciesWithDecimal, transfer.Attributes.TransferCurrency)
|
|
if err != nil {
|
|
if errors.Is(err, currency.ErrMissingCurrencies) {
|
|
// Wrap so the fetch loop (toPSPPayments) skips the transfer instead of
|
|
// freezing ingestion. transferToPayment is on the fetch path via
|
|
// fetchAndTranslateTransfer, not only the create path.
|
|
return nil, fmt.Errorf("%w: %w", plugins.ErrCurrencyNotSupported, err)
|
|
}
|
|
return nil, err
|
|
}
|
|
|
|
amount, err := currency.GetAmountWithPrecisionFromString(transfer.Attributes.TransferAmount.String(), c)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &models.PSPPayment{
|
|
Reference: transfer.ID,
|
|
CreatedAt: createdAt,
|
|
Type: models.PAYMENT_TYPE_TRANSFER,
|
|
Amount: amount,
|
|
Asset: currency.FormatAsset(supportedCurrenciesWithDecimal, transfer.Attributes.TransferCurrency),
|
|
Scheme: models.PAYMENT_SCHEME_OTHER,
|
|
Status: matchTransferStatus(transfer.Attributes.TransferStatus),
|
|
SourceAccountReference: pointer.For(fmt.Sprintf("%d", transfer.Attributes.SendingAccountID)),
|
|
DestinationAccountReference: pointer.For(fmt.Sprintf("%d", transfer.Attributes.ReceivingAccountID)),
|
|
Raw: raw,
|
|
}, nil
|
|
}
|
|
|
|
func matchTransferStatus(status string) models.PaymentStatus {
|
|
// Awaiting Dispatch, Cleared, or Cancelled
|
|
switch status {
|
|
case "Awaiting Dispatch":
|
|
return models.PAYMENT_STATUS_PENDING
|
|
case "Cleared":
|
|
return models.PAYMENT_STATUS_SUCCEEDED
|
|
case "Cancelled":
|
|
return models.PAYMENT_STATUS_FAILED
|
|
}
|
|
|
|
return models.PAYMENT_STATUS_UNKNOWN
|
|
}
|