-
Notifications
You must be signed in to change notification settings - Fork 2
/
commerce_dotpay_main.php
267 lines (250 loc) · 8.76 KB
/
commerce_dotpay_main.php
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
<?php
/**
*
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to tech@dotpay.pl so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Drupal Commerce to newer
* versions in the future. If you wish to customize Drupal Commerce for your
* needs please refer to http://www.dotpay.pl for more information.
*
* @author Dotpay Team <tech@dotpay.pl>
* @copyright Dotpay
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
*
*/
use Dotpay\Dotpay;
use Dotpay\Form;
use Dotpay\Back;
use Dotpay\Card;
use Dotpay\Confirm;
use Dotpay\Instruction;
use Dotpay\Channel\AbstractChannel;
require_once(__DIR__.'/include/bootstrap.php');
require_once(__DIR__.'/commerce_dotpay_channels.inc');
define('DOTPAY_GATEWAY_VERSION', '2.0.2');
/**
* Implemenation of hook_commerce_payment_method_info()
* Returns data of all Dotpay payments gateway supplied by plugin
* @return array
*/
function commerce_dotpay_commerce_payment_method_info() {
drupal_add_css(drupal_get_path('module', 'commerce_dotpay').'/web/css/main.css');
return Dotpay::getInstance()->getChannelList();
}
/**
* Implementation of hook_settings_form()
* Returns data for Drupal API Forms with form of plugin settings
* @param array|null $settings
* @return array
*/
function dotpay_gateway_settings_form($settings = NULL) {
return Dotpay::getInstance()
->getSettings()
->setDefault()
->update($settings)
->getForm();
}
/**
* Implementation of hook_form_FORM_ID_alter()
* Adds channel thumb images to all Dotpay gateways
* @param array $form Form structure
* @param array $form_state Form state
*/
function commerce_dotpay_form_commerce_checkout_form_alter(&$form, &$form_state) {
if (!empty($form['commerce_payment']['payment_method']['#options'])) {
foreach ($form['commerce_payment']['payment_method']['#options'] as $key => &$value) {
list($method_id, $rule_name) = explode('|', $key);
if ($method_id == 'dotpay_standard_channel') {
$value = Dotpay::getInstance()->getStandard()->getThumb().$value;
}
else if ($method_id == 'dotpay_blik_channel') {
$value = Dotpay::getInstance()->getBlik()->getThumb().$value;
}
else if ($method_id == 'dotpay_mp_channel') {
$value = Dotpay::getInstance()->getMasterPass()->getThumb().$value;
}
else if ($method_id == 'dotpay_cc_channel') {
$value = Dotpay::getInstance()->getCC()->getThumb().$value;
}
else if ($method_id == 'dotpay_oc_channel') {
$value = Dotpay::getInstance()->getOneClick()->getThumb().$value;
}
}
}
}
/**
* Implementation of hook_menu().
* Returns pages added by this plugin to site
* @return array
*/
function commerce_dotpay_menu() {
$items = array();
$items['dotpay/%/%/form'] = array(
'title' => t('Payment'),
'page callback' => 'commerce_dotpay_router_form',
'page arguments' => array(1,2),
'type' => MENU_CALLBACK,
'access callback' => true,
);
$items['dotpay/%/back'] = array(
'title' => t('Waiting'),
'page callback' => 'commerce_dotpay_back',
'page arguments' => array(1),
'access callback' => true,
'access arguments' => array('access content'),
);
$items['dotpay/confirm'] = array(
'title' => t('Waiting'),
'page callback' => 'commerce_dotpay_confirm',
'type' => MENU_CALLBACK,
'access callback' => true,
);
$items['user/%user/ocmanage'] = array(
'title' => t('My saved cards'),
'page callback' => 'commerce_dotpay_ocmanage',
'page arguments' => array(1),
'type' => MENU_LOCAL_TASK,
'access callback' => true,
'weight' => 20,
);
$items['dotpay/oc/remove'] = array(
'page callback' => 'commerce_dotpay_ocremove',
'type' => MENU_CALLBACK,
'access callback' => true,
);
$items['dotpay/%/%/instruction'] = array(
'title' => t('Waiting'),
'page callback' => 'commerce_dotpay_instruction',
'type' => MENU_CALLBACK,
'access callback' => true,
'page arguments' => array(1,2),
);
$items['dotpay/%/status'] = array(
'page callback' => 'commerce_dotpay_check_status',
'type' => MENU_CALLBACK,
'access callback' => true,
'page arguments' => array(1),
);
return $items;
}
/**
* Displays hidden form depending on the given order id and Dotpay channel name; implementation of page callback
* @param int $order Order id
* @param string $method Dotpay channel name
*/
function commerce_dotpay_router_form($order, $method) {
$channelDriver = null;
switch($method) {
case 'oc':
$channelDriver = Dotpay::getInstance()->getOneClick();
break;
case 'cc':
$channelDriver = Dotpay::getInstance()->getCC();
break;
case 'blik':
$channelDriver = Dotpay::getInstance()->getBlik();
break;
case 'mp':
$channelDriver = Dotpay::getInstance()->getMasterPass();
break;
case 'standard':
$channelDriver = Dotpay::getInstance()->getStandard();
break;
}
$channel = Dotpay::getParam('channel');
if(!empty($channel) && Dotpay::isChannelInGroup($order, $channel, array(AbstractChannel::cashGroup, AbstractChannel::transferGroup)))
header('Location: '.url('dotpay/'.$order.'/'.Dotpay::getParam('channel').'/instruction'));
if($channelDriver === NULL)
die(t('Channel payment is incorrect. Please try to place your order again.'));
$form = new Form($channelDriver);
echo $form->render($order);
die();
}
/**
* Returns page data, which would be shown on payment back page; implementation of page callback
* @param int $orderId Order id
* @return array
*/
function commerce_dotpay_back($orderId) {
$backPage = new Back($orderId);
$page = array(
'content' => array(
'#type' => 'markup',
'#markup' => $backPage->render(),
)
);
return $page;
}
/**
* Processes request with payment confirmation from Dotpay; implementation of page callback
*/
function commerce_dotpay_confirm() {
$confirm = new Confirm();
echo $confirm->make();
die();
}
/**
* Displays page with list of saved cards for One Click payment method; implementation of page callback
* @global object $user Drupal user object
* @param type $account User uid
* @return type
*/
function commerce_dotpay_ocmanage($account) {
global $user;
drupal_set_title(t('Manage your Credit Cards (for One Click payments)'));
drupal_set_page_content(Dotpay::render(__DIR__.'/templates/ocmanage.phtml', array('cards'=>Card::getUsefulCardsForCustomer($user->uid))));
return array();
}
/**
* Removes saved credit card based on request parameters; implementation of page callback
* @global object $user Drupal user object
*/
function commerce_dotpay_ocremove() {
global $user;
$card = Card::getCardById($_POST['cardId']);
if($card !== false && $user->uid==$card->customer_id) {
Card::removeCard($_POST['cardId']);
die('1');
}
die('0');
}
/**
* Displays page with instruction of payment by transfer or cash channels; implementation of page callback
* @param int $orderId Order id
* @param type $channelId Channel id
* @return type
*/
function commerce_dotpay_instruction($orderId, $channelId) {
drupal_add_css(drupal_get_path('module', 'commerce_dotpay').'/web/css/microBootstrap.css');
$instruction = Dotpay::getInstance()->getStandard()->prepareTransferPayment($orderId, $channelId);
drupal_set_title(t('Instruction of your payment'));
drupal_set_page_content(Dotpay::render(__DIR__.'/templates/instruction.phtml', array('instruction' => $instruction)));
return array();
}
/**
* Checks payment status of order; implementation of page callback
* @param type $orderId order id
*/
function commerce_dotpay_check_status($orderId) {
$transactions = commerce_payment_transaction_load_multiple(array(), array('order_id' => $orderId));
$transaction = array_pop($transactions);
switch($transaction->status) {
case COMMERCE_PAYMENT_STATUS_SUCCESS:
die('1');
case COMMERCE_PAYMENT_STATUS_FAILURE:
die('-1');
default:
die('0');
}
}