mirror of
https://github.com/hanzo-fi/payments.git
synced 2026-10-30 20:31:20 +00:00
97 lines
2.7 KiB
Go
97 lines
2.7 KiB
Go
package column
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"math/big"
|
|
"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/column/client"
|
|
"github.com/hanzo-fi/payments/pkg/domain/models"
|
|
)
|
|
|
|
func (p *Plugin) createPayout(ctx context.Context, pi models.PSPPaymentInitiation) (models.CreatePayoutResponse, error) {
|
|
if err := p.validatePayoutRequests(pi); err != nil {
|
|
return models.CreatePayoutResponse{}, err
|
|
}
|
|
var curr string
|
|
if pi.Asset != "" {
|
|
|
|
currency, _, err := currency.GetCurrencyAndPrecisionFromAsset(supportedCurrenciesWithDecimal, pi.Asset)
|
|
if err != nil {
|
|
return models.CreatePayoutResponse{}, fmt.Errorf("failed to get currency and precision from asset: %v: %w", err, models.ErrInvalidRequest)
|
|
}
|
|
curr = currency
|
|
}
|
|
|
|
resp, err := p.client.InitiatePayout(
|
|
ctx,
|
|
&client.PayoutRequest{
|
|
Amount: pi.Amount.Int64(),
|
|
CurrencyCode: curr,
|
|
Metadata: pi.Metadata,
|
|
SourceAccount: pi.SourceAccount.Reference,
|
|
DestinationAccount: pi.DestinationAccount.Reference,
|
|
Description: pi.Description,
|
|
Reference: pi.Reference,
|
|
},
|
|
)
|
|
if err != nil {
|
|
return models.CreatePayoutResponse{}, err
|
|
}
|
|
|
|
payment, err := p.payoutToPayment(resp.ID, resp)
|
|
if err != nil {
|
|
return models.CreatePayoutResponse{}, err
|
|
}
|
|
|
|
return models.CreatePayoutResponse{
|
|
Payment: payment,
|
|
}, nil
|
|
}
|
|
|
|
func (p *Plugin) payoutToPayment(id string, from *client.PayoutResponse) (*models.PSPPayment, error) {
|
|
raw, err := json.Marshal(from)
|
|
if err != nil {
|
|
return &models.PSPPayment{}, err
|
|
}
|
|
|
|
createdAt := time.Time{}
|
|
if from.CreatedAt != "" {
|
|
createdAt, err = ParseColumnTimestamp(from.CreatedAt)
|
|
if err != nil {
|
|
return &models.PSPPayment{}, err
|
|
}
|
|
}
|
|
|
|
curr := ""
|
|
if from.CurrencyCode != "" {
|
|
curr = currency.FormatAsset(supportedCurrenciesWithDecimal, from.CurrencyCode)
|
|
}
|
|
|
|
return &models.PSPPayment{
|
|
Reference: id,
|
|
ParentReference: from.ID,
|
|
Amount: big.NewInt(from.Amount),
|
|
Asset: curr,
|
|
Status: p.mapTransactionStatus(from.Status),
|
|
Raw: raw,
|
|
Type: mapPayoutType(from),
|
|
SourceAccountReference: pointer.For(from.BankAccountID),
|
|
DestinationAccountReference: pointer.For(from.CounterpartyId),
|
|
CreatedAt: createdAt,
|
|
Metadata: from.Metadata,
|
|
}, nil
|
|
}
|
|
|
|
func mapPayoutType(payout *client.PayoutResponse) models.PaymentType {
|
|
if payout.IsIncoming {
|
|
return models.PAYMENT_TYPE_PAYIN
|
|
} else {
|
|
return models.PAYMENT_TYPE_PAYOUT
|
|
}
|
|
}
|