-
Notifications
You must be signed in to change notification settings - Fork 0
/
commerce_vipps.module
91 lines (85 loc) · 3.63 KB
/
commerce_vipps.module
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
<?php
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Url;
/**
* @file
* This module provides Vipps payment gateway integration to Commerce.
*/
use Drupal\commerce_payment\Entity\PaymentGateway;
use Drupal\commerce_order\Entity\Order;
use Drupal\views\Form\ViewsForm;
use Drupal\Core\Form\FormStateInterface;
/**
* Implements hook_form_FORM_ID_alter().
*/
function commerce_vipps_form_views_form_commerce_cart_form_default_alter(&$form, FormStateInterface $form_state, $form_id) {
if ($form_state->getFormObject() instanceof ViewsForm) {
/** @var \Drupal\views\ViewExecutable $view */
$view = reset($form_state->getBuildInfo()['args']);
// Only add the Checkout button if the cart form view has order items.
if ($view->storage->get('tag') == 'commerce_cart_form' && !empty($view->result)) {
$payment_gateway_storage = \Drupal::entityTypeManager()->getStorage('commerce_payment_gateway');
/** @var \Drupal\commerce_payment\Entity\PaymentGatewayInterface[] $payment_gateways */
$payment_gateways = $payment_gateway_storage->loadByProperties(['plugin' => 'vipps_express', 'status' => TRUE]);
$order = Order::load($view->args[0]);
// Evaluate conditions for the remaining ones.
foreach ($payment_gateways as $payment_gateway_id => $payment_gateway) {
if (!$payment_gateway->applies($order)) {
unset($payment_gateways[$payment_gateway_id]);
}
}
if (!$payment_gateways) {
return;
}
foreach ($payment_gateways as $payment_gateway) {
$form['actions']['express'] = [
'#type' => 'submit',
'#value' => t('Vipps Hurtigkasse'),
'#weight' => 10,
'#access' => \Drupal::currentUser()->hasPermission('access checkout'),
'#submit' => array_merge($form['#submit'],
['commerce_vipps_cart_form_submit']),
'#order_id' => $view->argument['order_id']->value[0],
'#payment_gateway' => $payment_gateway->id(),
'#update_cart' => TRUE,
'#show_update_message' => FALSE,
'#attached' => ['library' => ['commerce_vipps/hurtigkasse']],
];
if (($style = $payment_gateway->getPlugin()->getConfiguration()['button_style']) !== 'default') {
$form['actions']['express']['#attributes']['class'] = ['vipps-hurtigkasse--button-style', 'vipps-hurtigkasse--button-style--' . $style];
}
}
}
}
}
/**
* Submit handler used to redirect to the checkout page.
*/
function commerce_vipps_cart_form_submit($form, FormStateInterface $form_state) {
$order_id = $form_state->getTriggeringElement()['#order_id'];
$payment_gateway_id = $form_state->getTriggeringElement()['#payment_gateway'];
$order = Order::load($order_id);
/** @var \Drupal\commerce_checkout\CheckoutOrderManagerInterface $checkout_manager */
$order->set('payment_gateway', PaymentGateway::load($payment_gateway_id));
$order->set('payment_method', NULL);
$order->set('checkout_step', 'payment');
$order->lock();
$order->save();
$form_state->setRedirect('commerce_checkout.form', ['commerce_order' => $order_id]);
}
/**
* Implements hook_entity_operation_alter().
*/
function commerce_vipps_entity_operation_alter(array &$operations, EntityInterface $entity) {
if ($entity->getEntityTypeId() !== 'commerce_payment') {
return;
}
if (!in_array($entity->getPaymentGateway()->getPluginId(), ['vipps', 'vipps_express'])) {
return;
}
$operations['remote-log'] = [
'title' => t('Remote Transaction Log'),
'url' => $url = Url::fromRoute('commerce_vipps.remote_log', ['order' => $entity->getOrderId(), 'payment' => $entity->id()]),
'access' => $url->access(),
];
}