forked from linnovate/tranzilla
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommerce_tranzilla.module
238 lines (196 loc) · 7.24 KB
/
commerce_tranzilla.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<?php
/**
* @file
* Implements Authorize.Net payment services for use in Drupal Commerce.
*/
/**
* Implements hook_commerce_payment_method_info().
*/
function commerce_tranzilla_commerce_payment_method_info() {
$payment_methods = array();
$payment_methods['tranzilla'] = array(
'base' => 'commerce_tranzilla',
'title' => t('Tranzilla - Credit Card'),
'short_title' => t('Tranzilla CC'),
'display_title' => t('Credit card'),
'description' => t('Integrates Tranzilla Payment Gateway'),
);
return $payment_methods;
}
/**
* Payment method callback: settings form.
*/
function commerce_tranzilla_settings_form($settings = NULL) {
$form = array();
$form['supplier'] = array(
'#type' => 'textfield',
'#title' => t('Supplier'),
'#description' => t('Your Supplier Login, Provided by Tranzilla'),
'#required' => TRUE,
);
$form['system'] = array(
'#type' => 'select',
'#title' => t('Tranzilla System'),
'#description' => t('Tranzilla URL'),
'#options' => array(
'secure5.tranzila.com' => t('Secure5.Tranzila.com'),
'secure.tranzila.com' => t('Secure.Tranzila.com'),
),
'#default_value' => variable_get('uc_tranzila_system', 'secure5.tranzila.com'),
'#required' => TRUE,
);
$form['transactions'] =array (
'#type' => 'select',
'#title' => t('Installments'),
'#description' => t('Maxiumum Number of Installments. Selecting 1 will not display the form for the users.'),
'#options' => array (1,2,3,4,5,6,7,8,9,10,11,12),
'#default_value' => 1,
'#required' => TRUE,
);
return $form;
}
/**
* Payment method callback: checkout form.
*/
function commerce_tranzilla_submit_form($payment_method, $pane_values, $checkout_pane, $order) {
module_load_include('inc', 'commerce_payment', 'includes/commerce_payment.credit_card');
return commerce_payment_credit_card_form(array('code' => '', 'owner' => ''));
}
/**
* Payment method callback: checkout form validation.
*/
function commerce_tranzilla_submit_form_validate($payment_method, $pane_form, $pane_values, $order, $form_parents = array()) {
module_load_include('inc', 'commerce_payment', 'includes/commerce_payment.credit_card');
// Validate the credit card fields.
$settings = array(
'form_parents' => array_merge($form_parents, array('credit_card')),
);
if (!commerce_payment_credit_card_validate($pane_values['credit_card'], $settings)) {
return FALSE;
}
}
/**
* Payment method callback: checkout form submission.
*/
function commerce_tranzilla_submit_form_submit($payment_method, $pane_form, $pane_values, $order, $charge) {
// Build a name-value pair array for this transaction.
$nvp = array(
'cred_type' => 1,
'supplier' => $payment_method['settings']['supplier'],
'sum' => commerce_currency_amount_to_decimal($charge['amount'], $charge['currency_code']),
'ccno'=> $pane_values['credit_card']['number'],
'expmonth' => $pane_values['credit_card']['exp_month'],
'expyear' => substr($pane_values['credit_card']['exp_year'],2,2),
'mycvv' => $pane_values['credit_card']['code'],
'currency' => $charge['currency_code'],
'order_id' => $order->order_id,
);
// Submit the request to Tranzilla
$response = commerce_tranzilla_request($payment_method, $nvp);
// Prepare a transaction object to log the API response.
$transaction = commerce_payment_transaction_new('tranzilla', $order->order_id);
$transaction->instance_id = $payment_method['instance_id'];
//$transaction->remote_id = $response[6];
$transaction->amount = $charge['amount'];
$transaction->currency_code = $charge['currency_code'];
$transaction->payload[REQUEST_TIME] = "";
//dpm ($response, "response");
$resp_hash = array();
foreach(explode('&', $response->data) as $r) {
list($key, $value) = explode("=", $r);
$resp_hash[$key] = $value;
}
if($resp_hash['Response'] != "000") {
// Error.
// Create a failed transaction with the error message.
$transaction->status = COMMERCE_PAYMENT_STATUS_FAILURE;
}
else {
$transaction->remote_id = $resp_hash['TransID'];
$transaction->status = COMMERCE_PAYMENT_STATUS_SUCCESS;
}
// Save the transaction information.
commerce_payment_transaction_save($transaction);
// If the payment failed, display an error and rebuild the form.
if ($resp_hash['Response'] != "000") {
$error = commerce_tranzilla_error_message($resp_hash['Response']);
drupal_set_message(t('There was an error processing the transaction for order @order_id.<br />Full response from server: @response', array('@order_id' => $order->order_id,
'@response' => $error)), 'error');
return FALSE;
}
else {
drupal_set_message(t('Credit card payment processed successfully.'), 'success');
}
}
/**
* Submits an request to Tranzilla
*
* @param $payment_method
* The payment method instance array associated with this API request.
*/
function commerce_tranzilla_request($payment_method, $nvp = array()) {
// Get the API endpoint URL for the method's transaction mode.
$get_url = 'https://'.$payment_method['settings']['system'].'/cgi-bin/tranzila31.cgi';
$get_fields = array();
foreach ($nvp as $key => $value) {
// Replace spaces with %20, so URL will be valid.
$get_fields[] = $key .'='. str_replace(' ', '%20', $value);
}
//dpm($get_fields, "get fields");
//dpm($get_fields);
$respose = drupal_http_request($get_url .'?'. implode('&' ,$get_fields));
return $respose;
}
function commerce_tranzilla_error_message($error_code) {
$lang = array(
'ccerror004' => t('Credit Card was declined'),
'ccerror006' => t('CVV Number is invalid'),
'ccerror033' => t('Credit Card Number is invalid'),
'ccerror036' => t('Credit Card expired'),
'ccerror057' => t('Please specify credit card number'),
'ccerror062' => t('Transaction type is invalid'),
'ccerror063' => t('Transaction Code is invalid'),
'ccerror064' => t('Credit card type is not supported'),
'ccerror065' => t('Currency type is invalid'),
);
switch($error_code)
{
case '004':
return $lang['ccerror004'];
break;
case '006':
case '017':
case '058':
case '059':
return $lang['ccerror006'];
break;
case '033':
return $lang['ccerror033'];
break;
case '036':
return $lang['ccerror036'];
break;
case '057':
case '059':
return $lang['ccerror057'];
break;
case '062':
return $lang['ccerror062'];
break;
case '063':
return $lang['ccerror063'];
break;
case '064':
return $lang['ccerror064'];
break;
case '065':
return $lang['ccerror065'];
break;
case '000':
return true;
break;
default:
return $lang['ccerror004'];
break;
}
}