Repository navigation
Expand file tree
/
Copy pathrefundflow.go
More file actions
157 lines (143 loc) · 5.48 KB
/
Copy pathrefundflow.go
File metadata and controls
157 lines (143 loc) · 5.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// Package refundflow runs each order refund as a durable Temporal workflow so a
// refund completes exactly once even when the worker executing it crashes.
//
// The workflow ID and the payment provider's idempotency key are both derived
// from the tenant and order. Temporal guarantees at most one workflow per ID,
// and the provider returns the original receipt for a repeated key, so neither
// a retried activity nor a repeated request can refund an order twice.
package refundflow
import (
"context"
"errors"
"fmt"
"time"
genpayments "github.com/goadesign/surla/examples/orders/gen/payments"
"github.com/goadesign/surla/examples/orders/internal/orders"
enumspb "go.temporal.io/api/enums/v1"
"go.temporal.io/api/serviceerror"
"go.temporal.io/sdk/activity"
"go.temporal.io/sdk/client"
"go.temporal.io/sdk/temporal"
"go.temporal.io/sdk/worker"
"go.temporal.io/sdk/workflow"
)
type (
// Runner starts refund workflows and waits for their receipts. It
// implements orders.RefundRunner.
Runner struct {
// temporal starts and reads refund workflows.
temporal client.Client
// taskQueue is the queue the refund worker polls.
taskQueue string
}
// Activities holds the refund workflow's activities.
Activities struct {
// payments calls the payment provider.
payments Refunder
}
// Refunder issues refunds at the payment provider. The generated payments
// client satisfies it.
Refunder interface {
// Refund refunds an order once per idempotency key.
Refund(ctx context.Context, p *genpayments.RefundPayload) (*genpayments.Receipt, error)
}
// Input is the refund workflow's argument.
Input struct {
// TenantID is the merchant tenant that owns the order.
TenantID string
// OrderID is the refunded order.
OrderID string
// AmountCents is the refunded amount in cents.
AmountCents int64
// Method is how the money is returned: "store_credit" or "card".
Method string
// IdempotencyKey identifies this refund at the payment provider.
IdempotencyKey string
}
)
const (
// WorkflowName is the registered name of the refund workflow.
WorkflowName = "orders.refund"
// chargeActivityName is the registered name of the payment activity.
chargeActivityName = "orders.refund.charge"
)
// NewRunner returns a Runner that starts refund workflows on taskQueue.
func NewRunner(c client.Client, taskQueue string) *Runner {
return &Runner{temporal: c, taskQueue: taskQueue}
}
// NewActivities returns the refund activities backed by the payment provider.
func NewActivities(payments Refunder) *Activities {
return &Activities{payments: payments}
}
// Register registers the refund workflow and its activity on a worker.
func Register(w worker.Worker, a *Activities) {
w.RegisterWorkflowWithOptions(Workflow, workflow.RegisterOptions{Name: WorkflowName})
w.RegisterActivityWithOptions(a.Charge, activity.RegisterOptions{Name: chargeActivityName})
}
// Refund runs the refund workflow for the order and returns its receipt. A
// refund already running for the order is joined; a refund that already
// completed returns its original receipt; a refund that failed may be retried.
func (r *Runner) Refund(ctx context.Context, refund orders.Refund) (*genpayments.Receipt, error) {
id := refundKey(refund.TenantID, refund.OrderID)
in := Input{
TenantID: refund.TenantID,
OrderID: refund.OrderID,
AmountCents: refund.AmountCents,
Method: refund.Method,
IdempotencyKey: id,
}
run, err := r.temporal.ExecuteWorkflow(ctx, client.StartWorkflowOptions{
ID: id,
TaskQueue: r.taskQueue,
WorkflowIDReusePolicy: enumspb.WORKFLOW_ID_REUSE_POLICY_ALLOW_DUPLICATE_FAILED_ONLY,
WorkflowIDConflictPolicy: enumspb.WORKFLOW_ID_CONFLICT_POLICY_USE_EXISTING,
}, WorkflowName, in)
if err != nil {
var started *serviceerror.WorkflowExecutionAlreadyStarted
if !errors.As(err, &started) {
return nil, fmt.Errorf("start refund workflow %s: %w", id, err)
}
run = r.temporal.GetWorkflow(ctx, id, "")
}
var receipt genpayments.Receipt
if err := run.Get(ctx, &receipt); err != nil {
return nil, fmt.Errorf("refund workflow %s: %w", id, err)
}
return &receipt, nil
}
// Workflow refunds one order by calling the payment provider through an
// activity that Temporal retries until it succeeds.
func Workflow(ctx workflow.Context, in Input) (*genpayments.Receipt, error) {
ctx = workflow.WithActivityOptions(ctx, workflow.ActivityOptions{
StartToCloseTimeout: 30 * time.Second,
RetryPolicy: &temporal.RetryPolicy{
InitialInterval: time.Second,
BackoffCoefficient: 2,
MaximumInterval: 30 * time.Second,
},
})
var receipt genpayments.Receipt
if err := workflow.ExecuteActivity(ctx, chargeActivityName, in).Get(ctx, &receipt); err != nil {
return nil, err
}
return &receipt, nil
}
// Charge asks the payment provider for the refund. Retrying it is safe: the
// provider returns the original receipt for a repeated idempotency key.
func (a *Activities) Charge(ctx context.Context, in Input) (*genpayments.Receipt, error) {
receipt, err := a.payments.Refund(ctx, &genpayments.RefundPayload{
TenantID: in.TenantID,
OrderID: in.OrderID,
AmountCents: in.AmountCents,
Method: in.Method,
IdempotencyKey: in.IdempotencyKey,
})
if err != nil {
return nil, fmt.Errorf("payment provider refund for order %s: %w", in.OrderID, err)
}
return receipt, nil
}
// refundKey identifies the single refund an order may receive.
func refundKey(tenantID, orderID string) string {
return "refund/" + tenantID + "/" + orderID
}