-
Notifications
You must be signed in to change notification settings - Fork 252
Expand file tree
/
Copy pathPaymentProcess.php
More file actions
297 lines (269 loc) · 10.4 KB
/
PaymentProcess.php
File metadata and controls
297 lines (269 loc) · 10.4 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
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
<?php
namespace Drupal\commerce_payment\Plugin\Commerce\CheckoutPane;
use Drupal\commerce_checkout\Plugin\Commerce\CheckoutFlow\CheckoutFlowInterface;
use Drupal\commerce_checkout\Plugin\Commerce\CheckoutPane\CheckoutPaneBase;
use Drupal\commerce_payment\Exception\DeclineException;
use Drupal\commerce_payment\Exception\PaymentGatewayException;
use Drupal\commerce_payment\Plugin\Commerce\PaymentGateway\ManualPaymentGatewayInterface;
use Drupal\commerce_payment\Plugin\Commerce\PaymentGateway\OffsitePaymentGatewayInterface;
use Drupal\commerce_payment\Plugin\Commerce\PaymentGateway\OnsitePaymentGatewayInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Link;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides the payment process pane.
*
* @CommerceCheckoutPane(
* id = "payment_process",
* label = @Translation("Payment process"),
* default_step = "payment",
* wrapper_element = "container",
* )
*/
class PaymentProcess extends CheckoutPaneBase {
/**
* The messenger.
*
* @var \Drupal\Core\Messenger\MessengerInterface
*/
protected $messenger;
/**
* Constructs a new PaymentProcess object.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\commerce_checkout\Plugin\Commerce\CheckoutFlow\CheckoutFlowInterface $checkout_flow
* The parent checkout flow.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Drupal\Core\Messenger\MessengerInterface $messenger
* The messenger.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, CheckoutFlowInterface $checkout_flow, EntityTypeManagerInterface $entity_type_manager, MessengerInterface $messenger) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $checkout_flow, $entity_type_manager);
$this->messenger = $messenger;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, CheckoutFlowInterface $checkout_flow = NULL) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$checkout_flow,
$container->get('entity_type.manager'),
$container->get('messenger')
);
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [
'capture' => TRUE,
] + parent::defaultConfiguration();
}
/**
* {@inheritdoc}
*/
public function buildConfigurationSummary() {
if (!empty($this->configuration['capture'])) {
$summary = $this->t('Transaction mode: Authorize and capture');
}
else {
$summary = $this->t('Transaction mode: Authorize only');
}
return $summary;
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
$form['capture'] = [
'#type' => 'radios',
'#title' => $this->t('Transaction mode'),
'#description' => $this->t('This setting is only respected if the chosen payment gateway supports authorizations.'),
'#options' => [
TRUE => $this->t('Authorize and capture'),
FALSE => $this->t('Authorize only (requires manual capture after checkout)'),
],
'#default_value' => (int) $this->configuration['capture'],
];
return $form;
}
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
parent::submitConfigurationForm($form, $form_state);
if (!$form_state->getErrors()) {
$values = $form_state->getValue($form['#parents']);
$this->configuration['capture'] = !empty($values['capture']);
}
}
/**
* {@inheritdoc}
*/
public function isVisible() {
if ($this->order->getTotalPrice()->isZero()) {
// Hide the pane for free orders, since they don't need a payment.
return FALSE;
}
$payment_info_pane = $this->checkoutFlow->getPane('payment_information');
if (!$payment_info_pane->isVisible() || $payment_info_pane->getStepId() == '_disabled') {
// Hide the pane if the PaymentInformation pane has been disabled.
return FALSE;
}
return TRUE;
}
/**
* {@inheritdoc}
*/
public function buildPaneForm(array $pane_form, FormStateInterface $form_state, array &$complete_form) {
// The payment gateway is currently always required to be set.
if ($this->order->get('payment_gateway')->isEmpty()) {
$this->messenger->addError($this->t('No payment gateway selected.'));
$this->redirectToPreviousStep();
}
/** @var \Drupal\commerce_payment\Entity\PaymentGatewayInterface $payment_gateway */
$payment_gateway = $this->order->payment_gateway->entity;
$payment_gateway_plugin = $payment_gateway->getPlugin();
$payment_storage = $this->entityTypeManager->getStorage('commerce_payment');
/** @var \Drupal\commerce_payment\Entity\PaymentInterface $payment */
$payment = $payment_storage->create([
'state' => 'new',
'amount' => $this->order->getTotalPrice(),
'payment_gateway' => $payment_gateway->id(),
'order_id' => $this->order->id(),
]);
$next_step_id = $this->checkoutFlow->getNextStepId($this->getStepId());
if ($payment_gateway_plugin instanceof OnsitePaymentGatewayInterface) {
/** @var \Drupal\commerce_payment\Entity\PaymentMethodInterface $payment_method */
$payment_method = $this->order->payment_method->entity;
try {
$payment->payment_method = $payment_method;
$payment_gateway_plugin->createPayment($payment, $this->configuration['capture']);
$this->redirectToStep($next_step_id);
}
catch (DeclineException $e) {
$message = $this->t('"We encountered an error processing your payment method. Please re-enter your payment information.');
$this->messenger->addError($message);
$this->order->get('payment_gateway')->setValue(NULL);
$this->order->get('payment_method')->setValue(NULL);
if (!$payment_method->isReusable() || $payment_method->getOwner()->isAnonymous()) {
$payment_method->delete();
}
$this->redirectToPreviousStep();
}
catch (PaymentGatewayException $e) {
\Drupal::logger('commerce_payment')->error($e->getMessage());
$message = $this->t('We encountered an unexpected error processing your payment method. Please try again later.');
$this->messenger->addError($message);
$this->redirectToPreviousStep();
}
}
elseif ($payment_gateway_plugin instanceof OffsitePaymentGatewayInterface) {
$pane_form['offsite_payment'] = [
'#type' => 'commerce_payment_gateway_form',
'#operation' => 'offsite-payment',
'#default_value' => $payment,
'#return_url' => $this->buildReturnUrl()->toString(),
'#cancel_url' => $this->buildCancelUrl()->toString(),
'#exception_url' => $this->buildPaymentInformationStepUrl()->toString(),
'#exception_message' => $this->t('We encountered an unexpected error processing your payment. Please try again later.'),
'#capture' => $this->configuration['capture'],
];
$complete_form['actions']['next']['#value'] = $this->t('Proceed to @gateway', [
'@gateway' => $payment_gateway_plugin->getDisplayLabel(),
]);
// Make sure that the payment gateway's onCancel() method is invoked,
// by pointing the "Go back" link to the cancel URL.
$complete_form['actions']['next']['#suffix'] = Link::fromTextAndUrl($this->t('Go back'), $this->buildCancelUrl())->toString();
// Hide the actions by default, they are not needed by gateways that
// embed iframes or redirect via GET. The offsite-payment form can
// choose to show them when needed (redirect via POST).
$complete_form['actions']['#access'] = FALSE;
return $pane_form;
}
elseif ($payment_gateway_plugin instanceof ManualPaymentGatewayInterface) {
try {
$payment_gateway_plugin->createPayment($payment);
$this->redirectToStep($next_step_id);
}
catch (PaymentGatewayException $e) {
\Drupal::logger('commerce_payment')->error($e->getMessage());
$message = $this->t('We encountered an unexpected error processing your payment. Please try again later.');
$this->messenger->addError($message);
$this->redirectToPreviousStep();
}
}
else {
$this->redirectToStep($next_step_id);
}
}
/**
* Builds the URL to the "return" page.
*
* @return \Drupal\Core\Url
* The "return" page URL.
*/
protected function buildReturnUrl() {
return Url::fromRoute('commerce_payment.checkout.return', [
'commerce_order' => $this->order->id(),
'step' => 'payment',
], ['absolute' => TRUE]);
}
/**
* Builds the URL to the "cancel" page.
*
* @return \Drupal\Core\Url
* The "cancel" page URL.
*/
protected function buildCancelUrl() {
return Url::fromRoute('commerce_payment.checkout.cancel', [
'commerce_order' => $this->order->id(),
'step' => 'payment',
], ['absolute' => TRUE]);
}
/**
* Builds the URL to the payment information checkout step.
*
* @return \Drupal\Core\Url
* The URL to the payment information checkout step.
*/
protected function buildPaymentInformationStepUrl() {
return Url::fromRoute('commerce_checkout.form', [
'commerce_order' => $this->order->id(),
'step' => $this->checkoutFlow->getPane('payment_information')->getStepId(),
], ['absolute' => TRUE]);
}
/**
* Redirects to a specific checkout step.
*
* @param string $step_id
* The step ID to redirect to.
*
* @throws \Drupal\commerce\Response\NeedsRedirectException
*/
protected function redirectToStep($step_id) {
$this->checkoutFlow->redirectToStep($step_id);
}
/**
* Redirects to a previous checkout step on error.
*
* @throws \Drupal\commerce\Response\NeedsRedirectException
*/
protected function redirectToPreviousStep() {
$step_id = $this->checkoutFlow->getPane('payment_information')->getStepId();
return $this->redirectToStep($step_id);
}
}