mirror of
https://github.com/hanzo-fi/payments.git
synced 2026-10-30 20:31:20 +00:00
- go.mod module path + all self-imports: github.com/formancehq/payments -> github.com/hanzo-fi/payments (1590 .go files / 3152 refs; 20 modules: root, pkg/client, pkg/domain, 18 ce/plugins). External dep kept: formancehq/go-libs. - plugins: each ce/plugins/* consumed a *published* pkg/domain; added local 'replace .../pkg/domain => ../../../pkg/domain' (consistent with root) so every standalone plugin module builds in-monorepo. compile-plugin.sh MOD var rebranded too. - branding: README, LICENSE (dual MIT+EE; upstream copyright retained + Hanzo added), goreleaser (release images -> registry.hanzo.ai/hanzo-fi, OCI labels, homepage), proto go_package, renovate, speakeasy gen.yaml, connector-template *.gotpl. - CI: removed Numary/Formance workflows + composite action; added canonical hanzo.yml + cicd.yml (hanzoai/ci, gated HANZO_CI_ENABLED) + secretless ci.yml (go build/vet). - kept as data/compat keys: wire capability enums ALLOW_FORMANCE_*, generated SDK type client.Formance, domain field FormanceRedirectURL, OTEL_SERVICE_NAME, upstream base image. - verified: go build ./... (default + -tags ee) + all 20 nested modules green.
220 lines
5.2 KiB
Go
220 lines
5.2 KiB
Go
package workflow
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/hanzo-fi/payments/internal/connectors/engine/activities"
|
|
"github.com/hanzo-fi/payments/pkg/domain/models"
|
|
errorsutils "github.com/hanzo-fi/payments/pkg/domain/errors"
|
|
"go.temporal.io/api/enums/v1"
|
|
"go.temporal.io/sdk/client"
|
|
"go.temporal.io/sdk/temporal"
|
|
"go.temporal.io/sdk/workflow"
|
|
)
|
|
|
|
type CreatePayout struct {
|
|
TaskID models.TaskID
|
|
ConnectorID models.ConnectorID
|
|
PaymentInitiationID models.PaymentInitiationID
|
|
}
|
|
|
|
func (w Workflow) runCreatePayout(
|
|
ctx workflow.Context,
|
|
createPayout CreatePayout,
|
|
) error {
|
|
err := w.createPayout(ctx, createPayout)
|
|
if err != nil {
|
|
errUpdateTask := w.updateTasksError(
|
|
ctx,
|
|
createPayout.TaskID,
|
|
&createPayout.ConnectorID,
|
|
err,
|
|
)
|
|
if errUpdateTask != nil {
|
|
return errUpdateTask
|
|
}
|
|
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (w Workflow) createPayout(
|
|
ctx workflow.Context,
|
|
createPayout CreatePayout,
|
|
) error {
|
|
// Get the payment initiation
|
|
pi, err := activities.StoragePaymentInitiationsGet(
|
|
infiniteRetryContext(ctx),
|
|
createPayout.PaymentInitiationID,
|
|
)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// If the payout is scheduled in the future, we need to add a schedule
|
|
// for processing adjustment to the payment initiation, and then sleep until
|
|
// the scheduled time.
|
|
now := workflow.Now(ctx)
|
|
if !pi.ScheduledAt.IsZero() && pi.ScheduledAt.After(now) {
|
|
err = w.addPIAdjustment(
|
|
ctx,
|
|
models.PaymentInitiationAdjustmentID{
|
|
PaymentInitiationID: createPayout.PaymentInitiationID,
|
|
CreatedAt: workflow.Now(ctx),
|
|
Status: models.PAYMENT_INITIATION_ADJUSTMENT_STATUS_SCHEDULED_FOR_PROCESSING,
|
|
},
|
|
pi.Amount,
|
|
&pi.Asset,
|
|
nil,
|
|
map[string]string{
|
|
"scheduledAt": pi.ScheduledAt.String(),
|
|
},
|
|
)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = workflow.Sleep(ctx, pi.ScheduledAt.Sub(now))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
pspPI, err := w.getPSPPI(ctx, pi)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = w.addPIAdjustment(
|
|
ctx,
|
|
models.PaymentInitiationAdjustmentID{
|
|
PaymentInitiationID: createPayout.PaymentInitiationID,
|
|
CreatedAt: workflow.Now(ctx),
|
|
Status: models.PAYMENT_INITIATION_ADJUSTMENT_STATUS_PROCESSING,
|
|
},
|
|
pi.Amount,
|
|
&pi.Asset,
|
|
nil,
|
|
nil,
|
|
)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
createPayoutResponse, errPlugin := activities.PluginCreatePayout(
|
|
infiniteRetryContext(ctx),
|
|
createPayout.ConnectorID,
|
|
models.CreatePayoutRequest{
|
|
PaymentInitiation: pspPI,
|
|
},
|
|
)
|
|
switch errPlugin {
|
|
case nil:
|
|
if createPayoutResponse.Payment != nil {
|
|
payment, err := models.FromPSPPaymentToPayment(*createPayoutResponse.Payment, createPayout.ConnectorID)
|
|
if err != nil {
|
|
return temporal.NewNonRetryableApplicationError(
|
|
"failed to translate psp payment",
|
|
ErrValidation,
|
|
err,
|
|
)
|
|
}
|
|
|
|
if err := w.storePIPaymentWithStatus(
|
|
ctx,
|
|
payment,
|
|
createPayout.PaymentInitiationID,
|
|
getPIStatusFromPayment(payment.Status),
|
|
); err != nil {
|
|
return err
|
|
}
|
|
|
|
return w.updateTaskSuccess(
|
|
ctx,
|
|
createPayout.TaskID,
|
|
&createPayout.ConnectorID,
|
|
payment.ID.String(),
|
|
)
|
|
}
|
|
|
|
if createPayoutResponse.PollingPayoutID != nil {
|
|
pollingPeriod, err := w.connectorPollingPeriod(ctx, createPayout.ConnectorID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
scheduleID := fmt.Sprintf("polling-payout-%s-%s-%s", w.stack, createPayout.ConnectorID.String(), *createPayoutResponse.PollingPayoutID)
|
|
|
|
err = activities.StorageSchedulesStore(
|
|
infiniteRetryContext(ctx),
|
|
models.Schedule{
|
|
ID: scheduleID,
|
|
ConnectorID: createPayout.ConnectorID,
|
|
CreatedAt: workflow.Now(ctx).UTC(),
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = activities.TemporalScheduleCreate(
|
|
infiniteRetryContext(ctx),
|
|
activities.ScheduleCreateOptions{
|
|
ScheduleID: scheduleID,
|
|
Interval: &client.ScheduleIntervalSpec{
|
|
Every: pollingPeriod,
|
|
},
|
|
Action: client.ScheduleWorkflowAction{
|
|
Workflow: RunPollPayout,
|
|
Args: []interface{}{
|
|
PollPayout{
|
|
TaskID: createPayout.TaskID,
|
|
ConnectorID: createPayout.ConnectorID,
|
|
PaymentInitiationID: createPayout.PaymentInitiationID,
|
|
PayoutID: *createPayoutResponse.PollingPayoutID,
|
|
ScheduleID: scheduleID,
|
|
},
|
|
},
|
|
TaskQueue: w.getDefaultTaskQueue(),
|
|
},
|
|
Overlap: enums.SCHEDULE_OVERLAP_POLICY_SKIP,
|
|
TriggerImmediately: true,
|
|
SearchAttributes: w.ScheduleSearchAttributes(ctx, &createPayout.ConnectorID, scheduleID),
|
|
},
|
|
)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
default:
|
|
// Temporal errors do not have a Cause method, so we need to unwrap them
|
|
// to get the underlying error and not store the whole stack trace inside
|
|
// the database.
|
|
cause := errorsutils.Cause(errPlugin)
|
|
err = w.addPIAdjustment(
|
|
ctx,
|
|
models.PaymentInitiationAdjustmentID{
|
|
PaymentInitiationID: createPayout.PaymentInitiationID,
|
|
CreatedAt: workflow.Now(ctx),
|
|
Status: models.PAYMENT_INITIATION_ADJUSTMENT_STATUS_FAILED,
|
|
},
|
|
pi.Amount,
|
|
&pi.Asset,
|
|
cause,
|
|
nil,
|
|
)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return errPlugin
|
|
}
|
|
}
|
|
|
|
const RunCreatePayout = "CreatePayout"
|