mirror of
https://github.com/hanzo-fi/payments.git
synced 2026-10-30 20:31:20 +00:00
* Implement Balances Webhooks in Stripe to reduce polling workflows * Create webhooks on install * Remove webhook at uninstall time * Add verifyWebhook function to Stripe * Pin APIVersion in Stripe Webhook Create call * Default to root account when receiving balance.update webhook * Only make call do delete webhooks if webhooks were setup to begin with * Set up two webhook endpoints - one for connect and one for root account * Use event.Created to determine balance time in Stripe webhooks * Start recording root account ID instead of dummy 'root' value * Create unit test for Stripe create webhooks * Add tests for Stripe Translate webhook * Share logic between Stripe balance polling and webhook handle * Use supported webhooks map * Update existing stripe webhooks instead of creating new ones at app start time * Run install sequence for each connector at app start of payments api pods * Turn of balances periodically for Stripe * Add nil checks for balance.Available * Remove potentially sensitive information from error message * Add migration tool to remove FETCH BALANCES schedules from stripe
80 lines
2.4 KiB
Go
80 lines
2.4 KiB
Go
package stripe
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"math/big"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/formancehq/go-libs/v3/currency"
|
|
"github.com/formancehq/payments/internal/connectors/plugins/public/stripe/client"
|
|
"github.com/formancehq/payments/internal/models"
|
|
errorsutils "github.com/formancehq/payments/internal/utils/errors"
|
|
"github.com/stripe/stripe-go/v80"
|
|
)
|
|
|
|
func (p *Plugin) createTransfer(ctx context.Context, pi models.PSPPaymentInitiation) (models.PSPPayment, error) {
|
|
if err := p.validatePayoutTransferRequest(pi); err != nil {
|
|
return models.PSPPayment{}, err
|
|
}
|
|
|
|
curr, _, err := currency.GetCurrencyAndPrecisionFromAsset(supportedCurrenciesWithDecimal, pi.Asset)
|
|
if err != nil {
|
|
return models.PSPPayment{}, errorsutils.NewWrappedError(
|
|
fmt.Errorf("failed to get currency and precision from asset: %w", err),
|
|
models.ErrInvalidRequest,
|
|
)
|
|
}
|
|
|
|
var source *string = nil
|
|
if pi.SourceAccount != nil && pi.SourceAccount.Reference != p.client.GetRootAccountID() {
|
|
source = &pi.SourceAccount.Reference
|
|
}
|
|
|
|
resp, err := p.client.CreateTransfer(
|
|
ctx,
|
|
&client.CreateTransferRequest{
|
|
IdempotencyKey: pi.Reference,
|
|
Amount: pi.Amount.Int64(),
|
|
Currency: curr,
|
|
Source: source,
|
|
Destination: pi.DestinationAccount.Reference,
|
|
Description: pi.Description,
|
|
Metadata: pi.Metadata,
|
|
},
|
|
)
|
|
if err != nil {
|
|
return models.PSPPayment{}, err
|
|
}
|
|
|
|
payment, err := fromTransferToPayment(resp, source, &pi.DestinationAccount.Reference)
|
|
if err != nil {
|
|
return models.PSPPayment{}, err
|
|
}
|
|
|
|
return payment, nil
|
|
}
|
|
|
|
func fromTransferToPayment(from *stripe.Transfer, source, destination *string) (models.PSPPayment, error) {
|
|
raw, err := json.Marshal(from)
|
|
if err != nil {
|
|
return models.PSPPayment{}, err
|
|
}
|
|
|
|
return models.PSPPayment{
|
|
Reference: from.BalanceTransaction.ID,
|
|
CreatedAt: time.Unix(from.Created, 0),
|
|
Type: models.PAYMENT_TYPE_TRANSFER,
|
|
Amount: big.NewInt(from.Amount),
|
|
Asset: currency.FormatAsset(supportedCurrenciesWithDecimal, strings.ToUpper(string(from.Currency))),
|
|
Scheme: models.PAYMENT_SCHEME_OTHER,
|
|
Status: models.PAYMENT_STATUS_SUCCEEDED,
|
|
SourceAccountReference: source,
|
|
DestinationAccountReference: destination,
|
|
Metadata: from.Metadata,
|
|
Raw: raw,
|
|
}, nil
|
|
}
|