This repository has been archived by the owner on Mar 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
migs.php
371 lines (318 loc) · 17.3 KB
/
migs.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
<?php
/* MIGS Payment Gateway Class */
class MIGS extends WC_Payment_Gateway {
// Setup our Gateway's id, description and other values
function __construct() {
// The global ID for this Payment method
$this->id = "migs";
// The Title shown on the top of the Payment Gateways Page next to all the other Payment Gateways
$this->method_title = __("MIGS", 'migs');
// The description for this Payment Gateway, shown on the actual Payment options page on the backend
$this->method_description = __("MIGS Payment Gateway Plug-in for WooCommerce", 'migs');
// The title to be used for the vertical tabs that can be ordered top to bottom
$this->title = __("MIGS", 'migs');
// If you want to show an image next to the gateway's name on the frontend, enter a URL to an image.
$this->icon = MIGS_PLUGIN_URL . 'images/migs_icon.jpg';
// Bool. Can be set to true if you want payment fields to show on the checkout
// if doing a direct integration, which we are doing in this case
$this->has_fields = false;
// Supports the default credit card form
//$this->supports = array('default_credit_card_form');
// This basically defines your settings which are then loaded with init_settings()
$this->init_form_fields();
// After init_settings() is called, you can get the settings and load them into variables, e.g:
// $this->title = $this->get_option( 'title' );
$this->init_settings();
// Turn these settings into variables we can use
foreach ($this->settings as $setting_key => $value) {
$this->$setting_key = $value;
}
add_action('woocommerce_api_' . strtolower(get_class($this)), array($this, 'migs_response_handler'));
// Lets check for SSL
//add_action('admin_notices', array($this, 'do_ssl_check'));
// Save settings
if (is_admin()) {
// Versions over 2.0
// Save our administration options. Since we are not going to be doing anything special
// we have not defined 'process_admin_options' in this class so the method in the parent
// class will be used instead
add_action('woocommerce_update_options_payment_gateways_' . $this->id, array($this, 'process_admin_options'));
}
add_action('woocommerce_receipt_migs', array($this, 'migs_receipt_page'));
//add_action('woocommerce_thankyou_migs', array($this, 'migs_response_handler'));
}
// End __construct()
// Build the administration fields for this specific Gateway
public function init_form_fields() {
$this->form_fields = array(
'enabled' => array(
'title' => __('Enable / Disable', 'migs'),
'label' => __('Enable this payment gateway', 'migs'),
'type' => 'checkbox',
'default' => 'no',
),
'title' => array(
'title' => __('Title', 'migs'),
'type' => 'text',
'desc_tip' => __('Payment title the customer will see during the checkout process.', 'migs'),
'default' => __('Master card', 'migs'),
),
'description' => array(
'title' => __('Description', 'migs'),
'type' => 'textarea',
'desc_tip' => __('Payment description the customer will see during the checkout process.', 'migs'),
'default' => __('Pay securely using your master card.', 'migs'),
'css' => 'max-width:350px;'
),
'access_code' => array(
'title' => __('MIGS Access Code', 'migs'),
'type' => 'text',
'desc_tip' => __('This is the Access Code MIGS when you signed up for an account.', 'migs'),
),
'merchant_id' => array(
'title' => __('MIGS Merchant ID', 'migs'),
'type' => 'text',
'desc_tip' => __('This is the Merchant ID when you signed up for an account.', 'migs'),
),
'merchant_secret_key' => array(
'title' => __('MIGS Secret Key', 'migs'),
'type' => 'password',
'desc_tip' => __('This is Mertchant Secret Key when you signed up for an account.', 'migs'),
),
'environment' => array(
'title' => __('MIGS Test Mode', 'migs'),
'label' => __('Enable Test Mode', 'migs'),
'type' => 'checkbox',
'description' => __('Place the payment gateway in test mode.', 'migs'),
'default' => 'no',
)
);
}
//Submit payment and handle response
public function process_payment($order_id) {
global $woocommerce;
//Get this Order's information so that we know
//who to charge and how much
$customer_order = new WC_Order($order_id);
/* $payment_fields = array(
'vpc_AccessCode' => trim($this->access_code),
'vpc_Amount' => '100',//$this->get_exact_amount($customer_order->order_total),
'vpc_Command' => 'pay',
'vpc_Locale' => 'en',
'vpc_MerchTxnRef' => $order_id,
'vpc_Merchant' => trim($this->merchant_id),
'vpc_OrderInfo' => 'This is for test',
'vpc_ReturnURL' => $this->get_return_url($customer_order),
'vpc_Version' => 1
);
$hashData = $this->merchant_secret_key;
foreach ($payment_fields as $key => $value) {
$hashData .= $value;
}
$config_params = http_build_query($payment_fields);
$config_params .= '&vpc_SecureHash=' . strtoupper(md5($hashData));
$url = 'https://migs.mastercard.com.au/vpcpay?' . $config_params; */
// Redirect to thank you page
return array(
'result' => 'success',
'redirect' => $customer_order->get_checkout_payment_url(true)
);
//,
}
// Validate fields
public function validate_fields() {
return true;
}
public function do_ssl_check() {
if ($this->enabled == "yes") {
if (get_option('woocommerce_force_ssl_checkout') == "no") {
echo "<div class=\"error\"><p>" . sprintf(__("<strong>%s</strong> is enabled and WooCommerce is not forcing the SSL certificate on your checkout page. Please ensure that you have a valid SSL certificate and that you are <a href=\"%s\">forcing the checkout pages to be secured.</a>"), $this->method_title, admin_url('admin.php?page=wc-settings&tab=checkout')) . "</p></div>";
}
}
}
public function get_exact_amount($amount) {
return($amount * 100);
}
public function migs_receipt_page($order_id) {
global $woocommerce;
$customer_order = new WC_Order($order_id);
$redirect_url = add_query_arg('wc-api', get_class($this), site_url());
$currency = 'QAR';
//'vpc_Currency' => $currency,
$payment_fields = array(
'vpc_AccessCode' => trim($this->access_code),
'vpc_Amount' => $this->get_exact_amount($customer_order->order_total),
'vpc_Command' => 'pay',
'vpc_Locale' => 'en',
'vpc_MerchTxnRef' => $order_id,
'vpc_Merchant' => trim($this->merchant_id),
'vpc_OrderInfo' => 'This is for test',
'vpc_ReturnURL' => $redirect_url, //$this->get_return_url($customer_order),
'vpc_Version' => 1
);
$hashData = $this->merchant_secret_key;
foreach ($payment_fields as $key => $value) {
$hashData .= $value;
}
$config_params = http_build_query($payment_fields);
$config_params .= '&vpc_SecureHash=' . strtoupper(md5($hashData));
$url = 'https://migs.mastercard.com.au/vpcpay?' . $config_params;
$payment_form = '<form id="migs_frm" action="' . $url . '" method="get">';
$payment_form .= '<label><input type="checkbox" name="migs_terms_cond" required="true" /></label><a href="">Terms & conditions</a>';
$payment_form .= '<input type="submit" name="migs_btn_submit" value="Pay" />';
$payment_form .= '</form>';
/*$script = '<script type="text/javascript">
jQuery(document).ready(function(){ jQuery("#migs_frm").on("submit", function(e){e.preventDefault(); var URL = jQuery("#migs_frm").prop("action"); window.location.href = URL; })});
</script>';*/
echo $payment_form ;
}
public function migs_response_handler() {
global $woocommerce;
$response = $_REQUEST;
$order_id = $response['vpc_MerchTxnRef'];
$customer_order = new WC_Order($order_id);
$amount = $this->null2unknown($_GET["vpc_Amount"]);
$locale = $this->null2unknown($_GET["vpc_Locale"]);
$batchNo = $this->null2unknown($_GET["vpc_BatchNo"]);
$command = $this->null2unknown($_GET["vpc_Command"]);
$message = $this->null2unknown($_GET["vpc_Message"]);
$version = $this->null2unknown($_GET["vpc_Version"]);
$cardType = $this->null2unknown($_GET["vpc_Card"]);
$orderInfo = $this->null2unknown($_GET["vpc_OrderInfo"]);
$receiptNo = $this->null2unknown($_GET["vpc_ReceiptNo"]);
$merchantID = $this->null2unknown($_GET["vpc_Merchant"]);
$authorizeID = $this->null2unknown($_GET["vpc_AuthorizeId"]);
$merchTxnRef = $this->null2unknown($_GET["vpc_MerchTxnRef"]);
$transactionNo = $this->null2unknown($_GET["vpc_TransactionNo"]);
$acqResponseCode = $this->null2unknown($_GET["vpc_AcqResponseCode"]);
$txnResponseCode = $this->null2unknown($_GET["vpc_TxnResponseCode"]);
// 3-D Secure Data
$verType = array_key_exists("vpc_VerType", $_GET) ? $_GET["vpc_VerType"] : "No Value Returned";
$verStatus = array_key_exists("vpc_VerStatus", $_GET) ? $_GET["vpc_VerStatus"] : "No Value Returned";
$token = array_key_exists("vpc_VerToken", $_GET) ? $_GET["vpc_VerToken"] : "No Value Returned";
$verSecurLevel = array_key_exists("vpc_VerSecurityLevel", $_GET) ? $_GET["vpc_VerSecurityLevel"] : "No Value Returned";
$enrolled = array_key_exists("vpc_3DSenrolled", $_GET) ? $_GET["vpc_3DSenrolled"] : "No Value Returned";
$xid = array_key_exists("vpc_3DSXID", $_GET) ? $_GET["vpc_3DSXID"] : "No Value Returned";
$acqECI = array_key_exists("vpc_3DSECI", $_GET) ? $_GET["vpc_3DSECI"] : "No Value Returned";
$authStatus = array_key_exists("vpc_3DSstatus", $_GET) ? $_GET["vpc_3DSstatus"] : "No Value Returned";
if ($txnResponseCode == 0) {
$customer_order->add_order_note(__('MIGS payment completed.', 'migs'));
// Mark order as Paid
$customer_order->payment_complete();
// Empty the cart (Very important step)
$woocommerce->cart->empty_cart();
update_post_meta($order_id, 'unique_3d_transaction_identifier', $xid);
update_post_meta($order_id, '3d_authentication_value', $token);
update_post_meta($order_id, '3d_electronics_commerce', $acqECI);
update_post_meta($order_id, '3d_authentication_schema', $verType);
update_post_meta($order_id, '3d_security_level', $verSecurLevel);
update_post_meta($order_id, '3d_enrolled', $enrolled);
update_post_meta($order_id, '3d_auth_status', $authStatus);
// Redirect to thank you page
wp_redirect($this->get_return_url($customer_order));
exit;
} else {
wc_add_notice('Message: ' . $this->getResponseDescription($txnResponseCode) . '', 'error');
// Add note to the order for your reference
$customer_order->add_order_note('Error: ' . $this->getResponseDescription($txnResponseCode));
wp_redirect($customer_order->get_checkout_payment_url(true));
exit;
}
}
public function null2unknown($data) {
if ($data == "") {
return "No Value Returned";
} else {
return $data;
}
}
public function getStatusDescription($statusResponse) {
if ($statusResponse == "" || $statusResponse == "No Value Returned") {
$result = "3DS not supported or there was no 3DS data provided";
} else {
switch ($statusResponse) {
Case "Y" : $result = "The cardholder was successfully authenticated.";
break;
Case "E" : $result = "The cardholder is not enrolled.";
break;
Case "N" : $result = "The cardholder was not verified.";
break;
Case "U" : $result = "The cardholder's Issuer was unable to authenticate due to some system error at the Issuer.";
break;
Case "F" : $result = "There was an error in the format of the request from the merchant.";
break;
Case "A" : $result = "Authentication of your Merchant ID and Password to the ACS Directory Failed.";
break;
Case "D" : $result = "Error communicating with the Directory Server.";
break;
Case "C" : $result = "The card type is not supported for authentication.";
break;
Case "S" : $result = "The signature on the response received from the Issuer could not be validated.";
break;
Case "P" : $result = "Error parsing input from Issuer.";
break;
Case "I" : $result = "Internal Payment Server system error.";
break;
default : $result = "Unable to be determined";
break;
}
}
return $result;
}
public function getResponseDescription($responseCode) {
switch ($responseCode) {
case "0" : $result = "Transaction Successful";
break;
case "?" : $result = "Transaction status is unknown";
break;
case "1" : $result = "Unknown Error";
break;
case "2" : $result = "Bank Declined Transaction";
break;
case "3" : $result = "No Reply from Bank";
break;
case "4" : $result = "Expired Card";
break;
case "5" : $result = "Insufficient funds";
break;
case "6" : $result = "Error Communicating with Bank";
break;
case "7" : $result = "Payment Server System Error";
break;
case "8" : $result = "Transaction Type Not Supported";
break;
case "9" : $result = "Bank declined transaction (Do not contact Bank)";
break;
case "A" : $result = "Transaction Aborted";
break;
case "C" : $result = "Transaction Cancelled";
break;
case "D" : $result = "Deferred transaction has been received and is awaiting processing";
break;
case "F" : $result = "3D Secure Authentication failed";
break;
case "I" : $result = "Card Security Code verification failed";
break;
case "L" : $result = "Shopping Transaction Locked (Please try the transaction again later)";
break;
case "N" : $result = "Cardholder is not enrolled in Authentication scheme";
break;
case "P" : $result = "Transaction has been received by the Payment Adaptor and is being processed";
break;
case "R" : $result = "Transaction was not processed - Reached limit of retry attempts allowed";
break;
case "S" : $result = "Duplicate SessionID (OrderInfo)";
break;
case "T" : $result = "Address Verification Failed";
break;
case "U" : $result = "Card Security Code Failed";
break;
case "V" : $result = "Address Verification and Card Security Code Failed";
break;
default : $result = "Unable to be determined";
}
return $result;
}
}
// End of SPYR_AuthorizeNet_AIM
//6768?key=wc_order_55003e06463a4&vpc_3DSECI=01&vpc_3DSXID=N8IoWqjHuqErMRVFb4H%2FOsqNnLo%3D&vpc_3DSenrolled=Y&vpc_3DSstatus=A&vpc_AVSRequestCode=Z&vpc_AVSResultCode=Unsupported&vpc_AcqAVSRespCode=Unsupported&vpc_AcqCSCRespCode=Unsupported&vpc_AcqResponseCode=00&vpc_Amount=200&vpc_AuthorizeId=640799&vpc_BatchNo=20150312&vpc_CSCResultCode=Unsupported&vpc_Card=MC&vpc_Command=pay&vpc_Locale=en&vpc_MerchTxnRef=6768&vpc_Merchant=TESTDB91249&vpc_Message=Approved&vpc_OrderInfo=This+is+for+test&vpc_ReceiptNo=507101640799&vpc_SecureHash=91CE93046800BBC99997E5DABC8957C1&vpc_TransactionNo=1144&vpc_TxnResponseCode=0&vpc_VerSecurityLevel=06&vpc_VerStatus=M&vpc_VerToken=htLerxW6QIujYwAAAG6TAyUAAAA%3D&vpc_VerType=3DS&vpc_Version=1