/
PaymentStateContext.java
258 lines (210 loc) · 9.87 KB
/
PaymentStateContext.java
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*
* Copyright 2014-2017 Groupon, Inc
* Copyright 2014-2017 The Billing Project, LLC
*
* The Billing Project licenses this file to you under the Apache License, version 2.0
* (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package org.killbill.billing.payment.core.sm;
import java.math.BigDecimal;
import java.util.List;
import java.util.UUID;
import javax.annotation.Nullable;
import org.killbill.automaton.OperationResult;
import org.killbill.billing.account.api.Account;
import org.killbill.billing.callcontext.InternalCallContext;
import org.killbill.billing.catalog.api.Currency;
import org.killbill.billing.payment.api.PluginProperty;
import org.killbill.billing.payment.api.TransactionType;
import org.killbill.billing.payment.dao.PaymentModelDao;
import org.killbill.billing.payment.dao.PaymentTransactionModelDao;
import org.killbill.billing.payment.plugin.api.PaymentTransactionInfoPlugin;
import org.killbill.billing.util.callcontext.CallContext;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableList;
public class PaymentStateContext {
private PaymentModelDao paymentModelDao;
// The following fields (paymentId, transactionId, amount, currency) may take their value from the paymentTransactionModelDao *when they are not already set*
private PaymentTransactionModelDao paymentTransactionModelDao;
// Initialized in CTOR or only set through paymentTransactionModelDao
private UUID paymentId;
private UUID transactionId;
// Can be overriden by control plugin
private BigDecimal amount;
private Currency currency;
private UUID paymentMethodId;
private Iterable<PluginProperty> properties;
// Set in the doOperationCallback when coming back from payment plugin
private PaymentTransactionInfoPlugin paymentTransactionInfoPlugin;
// Set in the control layer in the leavingState callback
private String paymentExternalKey;
private String paymentTransactionExternalKey;
protected UUID paymentIdForNewPayment;
protected UUID paymentTransactionIdForNewPaymentTransaction;
// Set in the control layer after creating the attempt in the enteringState callback
private UUID attemptId;
// This is purely a performance improvement to avoid fetching the existing transactions for that payment throughout the state machine
private List<PaymentTransactionModelDao> onLeavingStateExistingTransactions;
// Immutable
private final Account account;
private final TransactionType transactionType;
private final boolean shouldLockAccountAndDispatch;
private final OperationResult overridePluginOperationResult;
private final InternalCallContext internalCallContext;
private final CallContext callContext;
private final boolean isApiPayment;
@VisibleForTesting
public PaymentStateContext(final boolean isApiPayment, @Nullable final UUID paymentId, @Nullable final String paymentTransactionExternalKey, final TransactionType transactionType,
final Account account, @Nullable final UUID paymentMethodId, final BigDecimal amount, final Currency currency,
final boolean shouldLockAccountAndDispatch, final Iterable<PluginProperty> properties,
final InternalCallContext internalCallContext, final CallContext callContext) {
this(isApiPayment, paymentId, null, null, null, paymentTransactionExternalKey, transactionType, account, paymentMethodId,
amount, currency, null, null, shouldLockAccountAndDispatch, null, properties, internalCallContext, callContext);
}
// Used to create new payment and transactions
public PaymentStateContext(final boolean isApiPayment, @Nullable final UUID paymentId, final UUID transactionId, @Nullable final UUID attemptId, @Nullable final String paymentExternalKey,
@Nullable final String paymentTransactionExternalKey, final TransactionType transactionType,
final Account account, @Nullable final UUID paymentMethodId, final BigDecimal amount, final Currency currency,
@Nullable final UUID paymentIdForNewPayment, @Nullable final UUID paymentTransactionIdForNewPaymentTransaction, final boolean shouldLockAccountAndDispatch, final OperationResult overridePluginOperationResult, final Iterable<PluginProperty> properties,
final InternalCallContext internalCallContext, final CallContext callContext) {
this.isApiPayment = isApiPayment;
this.paymentId = paymentId;
this.transactionId = transactionId;
this.attemptId = attemptId;
this.paymentExternalKey = paymentExternalKey;
this.paymentTransactionExternalKey = paymentTransactionExternalKey;
this.transactionType = transactionType;
this.account = account;
this.paymentMethodId = paymentMethodId;
this.amount = amount;
this.currency = currency;
this.paymentIdForNewPayment = paymentIdForNewPayment;
this.paymentTransactionIdForNewPaymentTransaction = paymentTransactionIdForNewPaymentTransaction;
this.shouldLockAccountAndDispatch = shouldLockAccountAndDispatch;
this.overridePluginOperationResult = overridePluginOperationResult;
this.properties = properties;
this.internalCallContext = internalCallContext;
this.callContext = callContext;
this.onLeavingStateExistingTransactions = ImmutableList.of();
}
public boolean isApiPayment() {
return isApiPayment;
}
public void setPaymentMethodId(final UUID paymentMethodId) {
this.paymentMethodId = paymentMethodId;
}
public PaymentModelDao getPaymentModelDao() {
return paymentModelDao;
}
public void setPaymentModelDao(final PaymentModelDao paymentModelDao) {
this.paymentModelDao = paymentModelDao;
}
public PaymentTransactionModelDao getPaymentTransactionModelDao() {
return paymentTransactionModelDao;
}
public void setPaymentTransactionModelDao(final PaymentTransactionModelDao paymentTransactionModelDao) {
this.paymentTransactionModelDao = paymentTransactionModelDao;
if (paymentId == null) {
this.paymentId = paymentTransactionModelDao.getPaymentId();
}
if (transactionId == null) {
this.transactionId = paymentTransactionModelDao.getId();
}
if (amount == null) {
this.amount = paymentTransactionModelDao.getAmount();
}
if (currency == null) {
this.currency = paymentTransactionModelDao.getCurrency();
}
}
public List<PaymentTransactionModelDao> getOnLeavingStateExistingTransactions() {
return onLeavingStateExistingTransactions;
}
public void setOnLeavingStateExistingTransactions(final List<PaymentTransactionModelDao> onLeavingStateExistingTransactions) {
this.onLeavingStateExistingTransactions = onLeavingStateExistingTransactions;
}
public PaymentTransactionInfoPlugin getPaymentTransactionInfoPlugin() {
return paymentTransactionInfoPlugin;
}
public void setPaymentTransactionInfoPlugin(final PaymentTransactionInfoPlugin paymentTransactionInfoPlugin) {
this.paymentTransactionInfoPlugin = paymentTransactionInfoPlugin;
}
public UUID getPaymentId() {
return paymentId;
}
public UUID getTransactionId() {
return transactionId;
}
public String getPaymentExternalKey() {
return paymentExternalKey;
}
public void setPaymentExternalKey(final String paymentExternalKey) {
this.paymentExternalKey = paymentExternalKey;
}
public String getPaymentTransactionExternalKey() {
return paymentTransactionExternalKey;
}
public void setPaymentTransactionExternalKey(final String paymentTransactionExternalKey) {
this.paymentTransactionExternalKey = paymentTransactionExternalKey;
}
public Account getAccount() {
return account;
}
public UUID getPaymentMethodId() {
return paymentMethodId;
}
public UUID getAttemptId() {
return attemptId;
}
public void setAttemptId(final UUID attemptId) {
this.attemptId = attemptId;
}
public BigDecimal getAmount() {
return amount;
}
public Currency getCurrency() {
return currency;
}
public TransactionType getTransactionType() {
return transactionType;
}
public boolean shouldLockAccountAndDispatch() {
return shouldLockAccountAndDispatch;
}
public OperationResult getOverridePluginOperationResult() {
return overridePluginOperationResult;
}
public Iterable<PluginProperty> getProperties() {
return properties;
}
public InternalCallContext getInternalCallContext() {
return internalCallContext;
}
public CallContext getCallContext() {
return callContext;
}
public void setAmount(final BigDecimal adjustedAmount) {
this.amount = adjustedAmount;
}
public void setCurrency(final Currency currency) {
this.currency = currency;
}
public void setProperties(final Iterable<PluginProperty> properties) {
this.properties = properties;
}
public UUID getPaymentIdForNewPayment() {
return paymentIdForNewPayment;
}
public UUID getPaymentTransactionIdForNewPaymentTransaction() {
return paymentTransactionIdForNewPaymentTransaction;
}
}