class-wc-commweb-api.php
3.99 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
<?php
if (!defined('ABSPATH')) {
exit;
}
class WC_COMMWEB_HOSTED_API {
private static $log;
public static $_live_url = "https://paymentgateway.commbank.com.au/api/nvp/version/36";
public static $_checkout_url_js = 'https://paymentgateway.commbank.com.au/checkout/version/36/checkout.js';
public static function log($message) {
if (empty(self::$log)) {
self::$log = new WC_Logger();
}
self::$log->add('commweb-hosted-checkout', $message);
if (defined('WP_DEBUG') && WP_DEBUG) {
error_log($message);
}
}
public static function getSetting() {
$options = get_option('woocommerce_commweb_hosted_checkout_settings');
return $options;
}
public static function getMerchantId() {
$option = self::getSetting();
return $option['merchant_id'];
}
public static function getApiPassword() {
$option = self::getSetting();
return $option['api_password'];
}
public static function getApiUsername() {
$merchant_id = self::getMerchantId();
return 'merchant.' . $merchant_id;
}
public static function getDebug() {
$option = self::getSetting();
return $option['debug'];
}
public static function getCheckoutSession($order_id, $id_for_commweb) {
$order = new WC_Order($order_id);
$amount = number_format($order->order_total, 2, '.', '');
$merchant = self::getMerchantId();
$apiPassword = self::getApiPassword();
$url = self::$_live_url;
$fields = array(
'apiOperation' => urlencode('CREATE_CHECKOUT_SESSION'),
'apiPassword' => urlencode($apiPassword),
'apiUsername' => urlencode(self::getApiUsername()),
'merchant' => urlencode($merchant),
'order.id' => urlencode($id_for_commweb),
'order.amount' => urlencode($amount),
'order.currency' => urlencode('AUD')
);
$fields_string = '';
if (self::getDebug() == 'yes') {
self::log('Checkout session request: ' . print_r($fields, true));
}
foreach ($fields as $key => $value) {
$fields_string .= $key . '=' . $value . '&';
}
rtrim($fields_string, '&');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
$error = curl_error($ch);
curl_close($ch);
if ($result != '') {
$arr_session_id = null;
parse_str(html_entity_decode($result), $arr_session_id);
if (self::getDebug() == 'yes') {
self::log('Checkout session response: ' . print_r($arr_session_id, true));
}
if (isset($arr_session_id['result']) && $arr_session_id['result'] == 'ERROR') {
$session_id = '';
} else {
$session_id = $arr_session_id['session_id'];
$_SESSION['SuccessIndicator'] = $arr_session_id['successIndicator'];
$_SESSION['CurrentOrderId'] = $order_id;
}
} else {
self::log('Error: ' . print_r($error, true));
$session_id = '';
}
return $session_id;
}
public static function getOrderCommwebDetail($id_for_commweb) {
$url = self::$_live_url;
$fields = array(
'apiOperation' => urlencode('RETRIEVE_ORDER'),
'apiPassword' => urlencode(self::getApiPassword()),
'apiUsername' => urlencode(self::getApiUsername()),
'merchant' => urlencode(self::getMerchantId()),
'order.id' => urlencode($id_for_commweb)
);
$fields_string = '';
foreach ($fields as $key => $value) {
$fields_string .= $key . '=' . $value . '&';
}
rtrim($fields_string, '&');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
curl_close($ch);
$result = str_replace("%5B0%5D", '', $result);
$output = null;
parse_str(html_entity_decode($result), $output);
if (self::getDebug() == 'yes') {
self::log('Order detail from commweb: ' . print_r($output, true));
}
return $output;
}
}