-
Notifications
You must be signed in to change notification settings - Fork 2
/
commerce_omnipay.module
222 lines (187 loc) · 6.78 KB
/
commerce_omnipay.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
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
<?php
include_once 'includes/commerce_omnipay.payment_method.inc';
/**
* Implements hook_menu().
*/
function commerce_omnipay_menu() {
$items = array();
$items['admin/commerce/payment-gateways'] = array(
'title' => 'Payment Gateways',
'description' => 'Configure payment gateways',
'page callback' => 'commerce_omnipay_settings_page',
'access arguments' => array('administer payment gateways'),
'type' => MENU_NORMAL_ITEM,
'file' => 'includes/commerce_omnipay.admin.inc',
);
return $items;
}
/**
* Implements hook_access().
*/
function commerce_omnipay_permission() {
return array(
'administer payment gateways' => array(
'title' => t('Administer Payment Gateways'),
'description' => t('Administer Commerce Omnipay Payment Gateway Settings.'),
),
);
}
/**
* Implements hook_commerce_payment_method_info().
*/
function commerce_omnipay_commerce_payment_method_info() {
$payment_methods['omnipay'] = array(
'base' => 'commerce_omnipay_omnipay',
'title' => t('Omnipay'),
'active' => TRUE,
'short_title' => t('Omnipay'),
'description' => t('Omnipay'),
'terminal' => FALSE,
'offsite' => FALSE,
'offsite_autoredirect' => FALSE,
'file' => 'includes/commerce_omnipay.payment_method.inc',
);
return $payment_methods;
}
/**
* Implements hook_entity_update().
*/
function commerce_omnipay_entity_update($entity, $type) {
switch ($type) {
case 'commerce_order':
if ($entity->status == "canceled" && $entity->original->status != "canceled") {
if (isset($entity->data['payment_method']) &&
(FALSE !== stripos($entity->data['payment_method'], 'commerce_payment_omnipay'))) {
commerce_omnipay_order_cancelled($entity);
}
}
if ($entity->status == "completed" && $entity->original->status != "completed") {
if (isset($entity->data['payment_method']) &&
(FALSE !== stripos($entity->data['payment_method'], 'commerce_payment_omnipay'))) {
commerce_omnipay_order_completed($entity);
}
}
break;
}
}
/**
* List supported gateways.
* @return array
*/
function commerce_omnipay_supported_gateways() {
$gateways = array();
$gateways['stripe'] = array(
'label' => "Stripe",
'gateway' => "Stripe",
'description' => "Stripe is a suite of APIs that powers commerce for businesses of all sizes.",
'website' => "http://stripe.com",
);
$gateways['PayPal_Pro'] = array(
'label' => "PayPal Pro",
'gateway' => "PayPal_Pro",
'description' => "PayPal Payments Pro has the customization capability, technical maturity, and proven security that is needed to build professional-grade eCommerce sites.",
'website' => "https://www.paypal.com/webapps/mpp/paypal-payments-pro",
);
$gateways['WorldPay'] = array(
'label' => "WorldPay",
'gateway' => "WorldPay",
'description' => "At Worldpay, we can provide you with card processing services for your business from card machines, online payments to mail and telephone orders.",
'website' => "http://www.worldpay.com/us",
);
$gateways['AuthorizeNet_AIM'] = array(
'label' => "Authorize.net",
'gateway' => "AuthorizeNet_AIM",
'description' => "Authorize.net payment gateway enables internet merchants to accept online payments via credit card and e-check.",
'website' => "http://www.authorize.net/",
);
drupal_alter('commerce_omnipay_gateways', $gateways);
return $gateways;
}
function commerce_omnipay_get_gateway_settings($gateway) {
$settings = variable_get("commerce_omnipay_gateway_settings_" . $gateway, array());
drupal_alter('commerce_omnipay_settings', $settings, $gateway);
return $settings;
}
function commerce_omnipay_set_gateway_settings($gateway, $settings) {
variable_set("commerce_omnipay_gateway_settings_" . $gateway, $settings);
}
/**
* @param $order
*/
function commerce_omnipay_order_cancelled($order) {
$payment_transactions = commerce_omnipay_get_categorized_transactions($order);
//check transactions
if (isset($payment_transactions['purchase'])) {
if (!isset($payment_transactions['refund'])) {
commerce_omnipay_refund_void('refund', $payment_transactions['purchase'], $payment_transactions['purchase']->amount/100.0);
}
else {
// This should never get executed but might be needed in the future.
}
}
elseif (isset($payment_transactions['authorize'])) {
commerce_omnipay_refund_void('void', $payment_transactions['authorize'], $payment_transactions['authorize']->amount/100.0);
}
}
/**
* @param $order
*/
function commerce_omnipay_order_completed($order) {
$payment_transactions = commerce_omnipay_get_categorized_transactions($order);
// get order total amount
$order_total = $order->commerce_order_total[LANGUAGE_NONE][0]['amount'];
$currency_code = $order->commerce_order_total[LANGUAGE_NONE][0]['currency_code'];
// Has transaction been captured?
if (isset($payment_transactions['capture']) or isset($payment_transactions['purchase'])) {
// get amount captured
if (isset($payment_transactions['capture'])) {
$capture_transaction = $payment_transactions['capture'];
}
elseif (isset($payment_transactions['purchase'])) {
$capture_transaction = $payment_transactions['purchase'];
}
$amount_captured = $capture_transaction->amount;
// if amount captured is greater than order total amount
// then refund the difference
if ($amount_captured > $order_total) {
$refund_amount = commerce_currency_amount_to_decimal($amount_captured - $order_total, $currency_code);
commerce_omnipay_refund_void('refund', $capture_transaction, $refund_amount);
}
}
elseif (isset($payment_transactions['authorize'])) {
// capture order total amount
// Note: The order total could be less than the amount authorized
// if line items have been removed from the order.
commerce_omnipay_capture($payment_transactions['authorize'], commerce_currency_amount_to_decimal($order_total, $currency_code));
}
}
/**
* @param $order
* @return array
*/
function commerce_omnipay_get_categorized_transactions($order) {
$transactions = commerce_payment_transaction_load_multiple([], $conditions = ['order_id' => $order->order_id]);
$payment_transactions = [];
foreach ($transactions as $trans) {
switch ($trans->remote_status) {
case 'purchase':
$payment_transactions['purchase'] = $trans;
break;
case 'authorize':
$payment_transactions['authorize'] = $trans;
break;
case 'capture':
$payment_transactions['capture'] = $trans;
break;
case 'void';
$payment_transactions['void'] = $trans;
break;
case 'refund':
$payment_transactions['refund'] = $trans;
break;
default:
break;
}
}
return $payment_transactions;
}