-
Notifications
You must be signed in to change notification settings - Fork 1
/
commerce_midtrans.module
executable file
·193 lines (172 loc) · 7.7 KB
/
commerce_midtrans.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
<?php
/**
* @file
* Midtrans payment method for Drupal Commerce for testing and development.
*/
// load library
$rootplugin = dirname(__FILE__);
require_once($rootplugin.'/libraries/midtrans-php/Midtrans.php');
/**
* Implements hook_libraries_info().
*/
function commerce_midtrans_libraries_info() {
$libraries['midtrans'] = array(
'name' => 'PHP client library for Midtrans',
'vendor url' => 'https://github.com/Midtrans/midtrans-php',
'download url' => 'https://github.com/Midtrans/midtrans-php/archive/master.zip',
'path' => 'Midtrans',
'version arguments' => array(
'file' => 'README.md',
'pattern' => '/Midtrans (\d+)/',
'lines' => 6,
),
'files' => array(
'php' => array(
'ApiRequestor.php',
'Config.php',
'CoreApi.php',
'Notification.php',
'Sanitizer.php',
'Snap.php',
'SnapApiRequestor.php',
'Transaction.php',
),
),
);
return $libraries;
}
/**
* Implements hook_menu().
*/
function commerce_midtrans_menu() {
$items = array();
// Define an always accessible path to receive notification/IPNs.
$items['commerce_midtrans/ipn'] = array(
'page callback' => 'commerce_midtrans_process_ipn',
'page arguments' => array(),
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Processes an incoming notification.
* @return bool
* TRUE or FALSE indicating whether the notification was successfully processed or not.
*/
function commerce_midtrans_process_ipn() {
$raw_notification = json_decode(file_get_contents('php://input'), true);
$field = $raw_notification['custom_field1'];
if ($raw_notification['custom_field1'] == 'online_installment'){
$payment = commerce_payment_method_instance_load('commerce_midtrans_online_installment|commerce_payment_commerce_midtrans_online_installment');
$method = 'commerce_midtrans_online_installment';
}
else if ($raw_notification['custom_field1'] == 'offline_installment'){
$payment = commerce_payment_method_instance_load('commerce_midtrans_offline_installment|commerce_payment_commerce_midtrans_offline_installment');
$method = 'commerce_midtrans_offline_installment';
}
else {
$payment = commerce_payment_method_instance_load('commerce_midtrans_snap|commerce_payment_commerce_midtrans_snap');
$method = 'commerce_midtrans_snap';
}
if ($payment['settings']['payment_mode'] == 'TEST'){
// set sandbox credentials here
\Midtrans\Config::$serverKey = $payment['settings']['sandbox_server_key'];
\Midtrans\Config::$isProduction = FALSE;
}
else if ($payment['settings']['payment_mode'] == 'LIVE'){
// set production credentials here
\Midtrans\Config::$serverKey = $payment['settings']['production_server_key'];
\Midtrans\Config::$isProduction = TRUE;
}
$response = new \Midtrans\Notification();
// error_log('exit point response '.print_r($response, TRUE)); //debugan
// Exit now if the $_POST was empty.
if (empty($response)) {
watchdog('commerce_midtrans', 'Notification URL accessed with no POST data submitted.', array(), WATCHDOG_WARNING);
return FALSE;
}
watchdog('commerce_midtrans', 'Response from Midtrans ! Response: !Response :.', array('!Response' => '<pre>' . print_r($response, TRUE) . '</pre>'), WATCHDOG_DEBUG);
if (isset($response) && !empty($response)) {
// Prepare a transaction object to log the API response.
$transaction = commerce_payment_transaction_new($method, $response->order_id);
$order = commerce_order_load($response->order_id);
$transaction->uid = $order->uid;
$transaction->currency_code = $order->commerce_order_total[LANGUAGE_NONE][0]['currency_code'];
$transaction->instance_id = $order->data['payment_method'];
$transaction->remote_id = $response->transaction_id;
$transaction->remote_status = $response->transaction_status;
$transaction->payload = $response;
$transaction->amount = commerce_currency_decimal_to_amount($response->gross_amount / $payment['settings']['conversion'], $transaction->currency_code);
$transaction->message = $response->status_message;
// Set the transaction status based on SUCCESS or FAIL.
$transaction_status = $response->transaction_status;
$fraud = $response->fraud_status;
$payment_type = $response->payment_type;
// error_log('VTtransaction status: '.$transaction_status ); //debugan
// error_log('transaction->status: '.$transaction->status ); //debugan
if ($transaction_status == 'capture') {
if ($fraud == 'challenge') {
$transaction->status = COMMERCE_PAYMENT_STATUS_PENDING;
commerce_order_status_update($order, $name = 'pending', $skip_save = FALSE, $revision = TRUE, t('Midtrans-' . $payment_type . ': challenged payment'));
}
elseif ($fraud == 'accept') {
$transaction->status = COMMERCE_PAYMENT_STATUS_SUCCESS;
commerce_order_status_update($order, $name = 'processing', $skip_save = FALSE, $revision = TRUE, t('Midtrans-' . $payment_type . ': payment completed'));
}
}
elseif ($transaction_status == 'settlement') {
// error_log($payment_type); //debugan
// error_log(print_r($response,true)); //debugan
if($payment_type != 'credit_card'){
$transaction->status = COMMERCE_PAYMENT_STATUS_SUCCESS;
commerce_order_status_update($order, $name = 'processing', $skip_save = FALSE, $revision = TRUE, t('Midtrans-' . $payment_type . ': payment completed'));
} else{
// error_log('do nothing'); //debugan
return;
}
}
elseif ($transaction_status == 'pending') {
$transaction->status = COMMERCE_PAYMENT_STATUS_PENDING;
commerce_order_status_update($order, $name = 'pending', $skip_save = FALSE, $revision = TRUE, t('Midtrans-'. $payment_type . ': awaiting payment'));
}
elseif ($transaction_status == 'cancel') {
$transaction->status = COMMERCE_PAYMENT_STATUS_SUCCESS;
$transaction->amount = -($transaction->amount);
commerce_order_status_update($order, $name = 'canceled', $skip_save = FALSE, $revision = TRUE, t('Midtrans-' . $payment_type . ': cancelled payment'));
$transaction->message = 'Cancel, payment canceled';
}
elseif ($transaction_status == 'expire') {
$transaction->status = COMMERCE_PAYMENT_STATUS_SUCCESS;
$transaction->amount = -($transaction->amount);
commerce_order_status_update($order, $name = 'canceled', $skip_save = FALSE, $revision = TRUE, t('Midtrans-' . $payment_type . ': expired payment'));
$transaction->message = 'Expire, payment expired';
}
elseif ($transaction_status == 'deny') {
$transaction->status = COMMERCE_PAYMENT_STATUS_FAILURE;
commerce_order_status_update($order, $name = 'canceled', $skip_save = FALSE, $revision = TRUE, t('Midtrans-' . $payment_type . ': denied payment'));
$transaction->message = 'Denied, payment denied by Midtrans';
}else{
$transaction->status = COMMERCE_PAYMENT_STATUS_FAILURE;
}
// error_log('transaction->status: '.$transaction->status ); //debugan
// error_log('transaction: '.print_r($transaction,true) ); //debugan
commerce_payment_transaction_save($transaction);
}
}
/**
* Convert 2 digits coundry code to 3 digit country code.
*
* @param string $country_code
* Country code which will be converted.
*/
function commerce_midtrans_convert_country_code($country_code) {
// 3 digits country codes.
module_load_include('inc', 'commerce_midtrans', 'commerce_midtrans.data');
$cc_three = commerce_midtrans_country_code();
// Check if country code exists.
if (isset($cc_three[$country_code]) && $cc_three[$country_code] != '') {
$country_code = $cc_three[$country_code];
}
return $country_code;
}