/
uc_iats.module
218 lines (187 loc) · 7.1 KB
/
uc_iats.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
<?php
// $Id$
/**
* @file
* Defines the IATS payment gateway.
*/
/**
* Implementation of hook_payment_gateway().
*/
function uc_iats_payment_gateway() {
$gateways[] = array(
'id' => 'iats',
'title' => t('IATS'),
'description' => t('Process credit card payments using IATS.'),
'settings' => 'uc_iats_settings_form',
'credit' => 'uc_iats_charge',
'credit_txn_types' => array(UC_CREDIT_AUTH_CAPTURE),
);
return $gateways;
}
/**
* Admin settings form.
*/
function uc_iats_settings_form() {
$form['uc_iats_credentials'] = array(
'#type' => 'fieldset',
'#title' => t('IATS API Credentials'),
);
$form['uc_iats_credentials']['uc_iats_agentcode'] = array(
'#type' => 'textfield',
'#title' => t('Agent Code'),
'#description' => t('Your IATS account Agent Code.'),
'#default_value' => variable_get('uc_iats_agentcode', ''),
);
$form['uc_iats_credentials']['uc_iats_password'] = array(
'#type' => 'textfield',
'#title' => t('Password'),
'#description' => t('Your IATS account password.'),
'#default_value' => variable_get('uc_iats_password', ''),
);
$form['uc_iats_library'] = array(
'#type' => 'fieldset',
'#title' => t('IATSLink Library'),
);
$form['uc_iats_library']['uc_iats_iatslink_library_location'] = array(
'#type' => 'textfield',
'#title' => t('IATSLink Location'),
'#description' => t('The location of the IATSLink library (relative to the uc_iats module directory). Do not include leading or trailing slashes.'),
'#default_value' => variable_get('uc_iats_iatslink_library_location', 'iatslink'),
);
return $form;
}
/**
* Handles credit card transaction.
*/
function uc_iats_charge($order_id, $amount, $data) {
// Load the order.
$order = uc_order_load($order_id);
/**
* TODO: Add switch statement to do other stuff here.
* This space is reserved for future development, including recurring
* payments, etc.
*/
// Pass the info along.
return _uc_iats_charge($order, $amount, $data);
}
/**
* Handles authorizations and captures.
* Does the heavy lifting.
*/
function _uc_iats_charge($order, $amount, $data) {
// Build a description of the order for logging in Auth.Net.
$description = array();
foreach ((array) $order->products as $product) {
$description[] = $product->qty .'x '. $product->model;
}
$billing_country = uc_get_country_data(array('country_id' => $order->billing_country));
$context = array(
'revision' => 'formatted-original',
'type' => 'amount',
);
$options = array(
'sign' => FALSE,
'thou' => FALSE,
'dec' => '.',
);
// Including the IATS library.
$iatslink_location = variable_get('uc_iats_iatslink_library_location', 'iatslink');
module_load_include('php', 'uc_iats', $iatslink_location . '/iatslink');
// Instantiating our object.
$iats = new iatslink();
// Some initialization settings.
$iats->setAgentCode(variable_get('uc_iats_agentcode', ''));
$iats->setPassword(variable_get('uc_iats_password', ''));
$iats->setTestMode(variable_get('uc_iats_testmode', 'TRUE'));
// Setting credit card information.
$iats->setCardNumber($order->payment_details['cc_number']);
$month = str_pad($order->payment_details['cc_exp_month'],2,0,STR_PAD_LEFT);
$year = substr($order->payment_details['cc_exp_year'],2,2);
$iats->setCardExpiry($month .'/'. $year);
$iats->setCVV2($order->payment_details['cc_cvv']);
// Only for people in the US.
$iats->setFirstName($order->billing_first_name);
$iats->setLastName($order->billing_last_name);
$iats->setStreetAddress($order->billing_street1);
$iats->setCity($order->billing_city);
$iats->setState(uc_get_zone_code($order->billing_zone));
$iats->setZipCode($order->billing_postal_code);
// Setting order information.
$iats->setDollarAmount(uc_price($amount, $context, $options));
$iats->setComment(implode(', ', $description));
$iats->setInvoiceNumber($order->order_id);
// Process the card.
$iats->processCreditCard();
// Check the response from IATS for success or failure.
if ($iats->getStatus() == 1) {
$response = $iats->getAuthorizationResult();
$response = explode(':', $response, 2);
$trxn_result = trim($response[0]);
$trxn_id = trim($response[1]);
/**
* Deal with responses.
*/
// Payment accepted.
if ($trxn_result == 'OK') {
$result = _uc_iats_build_result(TRUE);
// Build an admin order comment.
$context = array(
'revision' => 'formatted-original',
'type' => 'amount',
);
$comment = t('<b>@status:</b> @message',
array('@status' => $result['success'] ? t('ACCEPTED') : t('REJECTED'), '@message' => $response[1]));
// Save the comment to the order.
uc_order_comment_save($order->order_id, $result['user'], $comment, 'admin');
}
// This means the call was successful, but the card was rejected, etc.
else {
$result = _uc_iats_build_result(FALSE, _uc_iats_handle_error($trxn_id));
}
// Could not call the API.
} else {
$result = _uc_iats_build_result(FALSE, $iats->getError());
}
return $result;
}
function _uc_iats_build_result($success = FALSE, $message = '') {
global $user;
return array(
'success' => $success,
'message' => $message,
'user' => $user->uid,
);
}
function _uc_iats_handle_error($code) {
$errors = array(
1 => 'Agent Code has not been set up on the authorization system.',
2 => 'Unable to process transaction. Verify and re-enter credit card information.',
3 => 'Charge card expired.',
4 => 'Incorrect expiration date.',
5 => 'Invalid transaction. Verify and re-enter credit card information.',
6 => 'Transaction not supported by institution.',
7 => 'Lost or stolen card.',
8 => 'Invalid card status.',
9 => 'Restricted card status. Usually on corporate cards restricted to specific sales.',
10 => 'Error. Please verify and re-enter credit card information.',
11 => 'General decline code, may have different reasons for each card type. Please have your client call customer service.',
14 => 'This means that the credit card is over the limit.',
15 => 'Decline code, may have different reasons for each card type. Please have your client call customer service.',
16 => 'Invalid charge card number. Verify and re-enter credit card information.',
17 => 'Unable to authorize transaction. Verify card information with customer and re-enter. Could be invalid name or expiry date.',
18 => 'Card not supported by institution.',
19 => 'Incorrect CVV2.',
22 => 'Bank Timeout. Bank lines may be down or busy. Re-try transaction later.',
23 => 'System error. Re-try transaction later.',
24 => 'Charge card expired.',
25 => 'Capture card. Reported lost or stolen.',
27 => 'System error, please re-enter transaction.',
29 => 'Rejected by Ticketmaster.',
31 => 'Manual reject code ',
39 => 'Contact Ticketmaster 1-888-955-5455 ',
40 => 'Card not supported by Ticketmaster. Invalid cc number.',
41 => 'Invalid Expiry date ',
100 => 'Authorization system down. DO NOT REPROCESS.',
);
return $errors[$code];
}