mirror of
https://github.com/hanzo-fi/payments.git
synced 2026-10-30 20:31:20 +00:00
* Set webhook ID as payment adjustment ID when adjustment created via webhook * Fetch Column payments oldest to newest * Ensure that webhook secrets are fetched when containers restart * Regenerate plugin code for tests * Add paging to fetch webhook request * Add WorkerPool unit test to increase test coverage * Guard against unhandled webhooks in Column plugin initiation * Rename Timeline.LatestID to Timeline.LastSeenID for clarity * Add metadata column to webhooks_configs table * Persist webhook_configs.metadata * Persist Column webhook secrets as metadata in webhooks_configs * Remove PSPWebhookConfig from InstallResponse * Stop passing context to initWebhookConfig() * Don't pass context to RegisterPlugin func * Don't pass context to plugins.RegisterPlugin
96 lines
2.7 KiB
Go
96 lines
2.7 KiB
Go
package column
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"math/big"
|
|
"time"
|
|
|
|
"github.com/formancehq/go-libs/v3/pointer"
|
|
"github.com/formancehq/payments/internal/connectors/plugins/currency"
|
|
"github.com/formancehq/payments/internal/connectors/plugins/public/column/client"
|
|
"github.com/formancehq/payments/internal/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,
|
|
},
|
|
)
|
|
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
|
|
}
|
|
}
|