-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoplata.module
More file actions
252 lines (212 loc) · 7.55 KB
/
oplata.module
File metadata and controls
252 lines (212 loc) · 7.55 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
<?php
/**
* @file
* Oplata.md module.
*/
/**
* @see Oplata
*/
require_once 'oplata.class.inc';
/**
* Implementation of hook_perm().
*/
function oplata_perm() {
return array('access oplata', 'checkout oplata', 'administer oplata');
}
/**
* Implementation of hook_menu().
*/
function oplata_menu() {
$items = array();
$items['admin/settings/oplata'] = array(
'title' => 'Oplata.md gate',
'description' => 'Configure Oplata.md gate.',
'page callback' => 'drupal_get_form',
'page arguments' => array('oplata_settings'),
'access arguments' => array('administer oplata'),
'file' => 'oplata.admin.inc',
'type' => MENU_NORMAL_ITEM,
);
$items['admin/reports/oplata'] = array(
'title' => 'Oplata.md queue',
'description' => 'Show and manage the queue of the Oplata.md transactions',
'page callback' => 'oplata_queue',
'access arguments' => array('administer oplata'),
'file' => 'oplata.admin.inc',
'type' => MENU_NORMAL_ITEM,
);
$items['admin/reports/oplata/%/complete'] = array(
'page callback' => 'drupal_get_form',
'page arguments' => array('oplata_transaction_proceed', 3, 4),
'access arguments' => array('administer oplata'),
'file' => 'oplata.admin.inc',
'type' => MENU_CALLBACK,
);
$items['admin/reports/oplata/%/emulate'] = array(
'page callback' => 'drupal_get_form',
'page arguments' => array('oplata_transaction_proceed', 3, 4),
'access arguments' => array('administer oplata'),
'file' => 'oplata.admin.inc',
'type' => MENU_CALLBACK,
);
$items['oplata/checkout'] = array(
'title' => 'Oplata checkout',
'page callback' => 'drupal_get_form',
'page arguments' => array('oplata_checkout'),
'access arguments' => array('checkout oplata'),
'type' => MENU_CALLBACK
);
$items['oplata/result'] = array(
'title' => 'Transaction result',
'page callback' => 'oplata_result',
'access arguments' => array('access oplata'),
'type' => MENU_CALLBACK
);
return $items;
}
/**
* Implementation of hook_theme().
*/
function oplata_theme() {
return array(
'oplata_checkout' => array(
'arguments' => array('form' => NULL),
),
);
}
/**
* Menu callback: transaction result page.
*/
function oplata_result() {
$post = $_POST;
if ($post['OPL_STATUS']) {
switch ($post['OPL_STATUS']) {
case '2':
$hash = array($post['OPL_QID'], $post['OPL_ACCOUNT'], $post['OPL_STATUS'], $post['OPL_AMOUNT'], $post['OPL_TRANSID']);
if ($post['OPL_SHOPDATA']) $hash[] = $post['OPL_SHOPDATA'];
$hash = Oplata::inst()->hash($hash);
if ($post['OPL_SIGN'] != $hash) {
return t(variable_get('oplata_signature_error_message', 'Signature error! Please contact the site administrator.'));
}
$check = Oplata::inst()->getStatus($post['OPL_ACCOUNT'], $post['OPL_AMOUNT']);
switch ($check) {
case '2':
db_query("UPDATE {oplata_queue} SET transid=%d, status='ok' WHERE qid=%d", $post['OPL_TRANSID'], $post['OPL_ACCOUNT']);
user_deposit_incoming($post['OPL_AMOUNT'], 'Oplata.md', $post['OPL_ACCOUNT']);
return t(variable_get('oplata_success_message', "Transaction succeed!
Transaction Id: !transid
Transaction number: !account
Amount: !amount
Oplata.md shop data: !shopdata"), array(
'!shopdata' => $post['OPL_SHOPDATA'],
'!qid' => $post['OPL_QID'],
'!account' => $post['OPL_ACCOUNT'],
'!status' => $post['OPL_STATUS'],
'!amount' => $post['OPL_AMOUNT'],
'!transid' => $post['OPL_TRANSID'],
));
break;
case '1':
db_query("UPDATE {oplata_queue} SET transid=%d, status='fail' WHERE qid=%d", $post['OPL_TRANSID'], $post['OPL_ACCOUNT']);
return t(variable_get('oplata_confirm_error_message', 'The transaction not confirmed.'));
break;
case '-1':
return t(variable_get('oplata_signature_error_message', 'Signature error! Please contact the site administrator.'));
break;
}
break;
default:
db_query("UPDATE {oplata_queue} SET transid=%d, status='fail' WHERE qid=%d", $post['OPL_TRANSID'], $post['OPL_ACCOUNT']);
return t(variable_get('oplata_error_message', 'Transaction returns error code.'));
break;
}
}
drupal_set_message(t('No post data'), 'error');
return '';
}
/**
* Build the checkout form
*/
function oplata_checkout() {
$form = array();
$form['user_guide'] = array(
'#value' => variable_get('oplata_checkout_user_guide', 'Checkout user guide
Do not close the page until confirmation of payment.'),
);
$rates = Oplata::inst()->getRates();
$lei_rate = $rates[5]; unset($rates[5]); array_unshift($rates, $lei_rate);
$rows = $granulity = array();
$values = explode(';', variable_get('oplata_granulity', "5;10;20;50;75;100"));
foreach ($rates as $rate) {
$row = array();
$row["header"] = array('data' => t($rate['currency']) . "<br/>" . t($rate['name']), 'header' => TRUE);
foreach ($values as $value) {
$amount = $rate['currency']=='LEI'?$value:number_format($value*$rate['rate'], 2, '.', '');
$row["{$rate['id']}:{$value}"] = $granulity["{$rate['id']}:{$value}"] = $amount;
}
$rows[$rate['id']] = $row;
}
$form['amount'] = array(
'#type' => 'radios',
'#required' => true,
'#options' => $granulity
);
$form['#rows'] = $rows;
$form['#rates'] = $rates;
$form['#theme'] = 'oplata_checkout';
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit')
);
return $form;
}
/**
* Implementation of hook_submit().
*/
function oplata_checkout_submit($form, &$form_state) {
global $user, $language;
list($currency, $amount) = explode(':', $form_state['values']['amount']);
$lang = substr(strtolower($language->name), 0, 3);
$action = str_replace('%LANG', $lang, variable_get('oplata_checkout_action', 'https://oplata.md/%LANG/orders/merchant'));
db_query("INSERT INTO {oplata_queue} SET uid=%d, amount=%f", $user->uid, $amount);
$qid = db_result(db_query("SELECT MAX(qid) FROM {oplata_queue} LIMIT 1"));
$oplid = variable_get('oplata_oplid', '');
$mail = $user->mail;
$shopdata = "arg0=1";
$hash = array($amount, $qid, $mail);
$hash = Oplata::inst()->hash($hash);
$form = "<form action=\"{$action}\" method=\"post\" id=\"pay-form\" style=\"display:none;\">
<input type=\"hidden\" name=\"OPL_SYSTEM\" value=\"{$currency}\" />
<input type=\"hidden\" name=\"OPL_ID\" value=\"{$oplid}\" />
<input type=\"hidden\" name=\"OPL_ACCOUNT\" value=\"{$qid}\" />
<input type=\"hidden\" name=\"OPL_AMOUNT\" value=\"{$amount}\" />
<input type=\"hidden\" name=\"OPL_EMAIL\" value=\"{$mail}\" />
<input type=\"hidden\" name=\"OPL_SIGN\" value=\"{$hash}\" />
<input type=\"submit\" id=\"form-submit\" value=\"submit\" />
</form>";
$output = "<html><head><title></title></head></body>{$form}
<script type=\"text/javascript\">
document.getElementById('pay-form').submit();
</script></body></html>";
die($output);
}
/**
* Theme checkout form.
*/
function theme_oplata_checkout($form) {
$output = '';
// $header = $form['#header'];
$rows = $form['#rows'];
$rates = $form['#rates'];
$output .= drupal_render($form['user_guide']);
foreach ($rows as &$row) {
foreach ($row as $key => &$subrow) {
if ($key != 'header') {
$subrow = drupal_render($form['amount'][$key]);
}
}
}
$output .= theme('table', array(), $rows, array(), t('Deposit incoming amounts (available payment systems).'));
$output .= drupal_render($form);
return $output;
}