|
1
|
|
|
<?php |
|
2
|
|
|
class WPSC_Payment_Gateway_WorldPay extends WPSC_Payment_Gateway { |
|
3
|
|
|
|
|
4
|
|
|
private $endpoints = array( |
|
5
|
|
|
'sandbox' => 'https://gwapi.demo.securenet.com/api/', |
|
6
|
|
|
'production' => 'https://gwapi.securenet.com/api/', |
|
7
|
|
|
); |
|
8
|
|
|
|
|
9
|
|
|
private $auth; |
|
10
|
|
|
private $payment_capture; |
|
11
|
|
|
private $order_handler; |
|
12
|
|
|
private $secure_net_id; |
|
13
|
|
|
private $secure_key; |
|
14
|
|
|
private $public_key; |
|
15
|
|
|
private $endpoint; |
|
16
|
|
|
private $sandbox; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Constructor of WorldPay Payment Gateway |
|
20
|
|
|
* |
|
21
|
|
|
* @access public |
|
22
|
|
|
* @since 3.9 |
|
23
|
|
|
*/ |
|
24
|
|
|
public function __construct() { |
|
25
|
|
|
|
|
26
|
|
|
parent::__construct(); |
|
27
|
|
|
|
|
28
|
|
|
$this->title = __( 'WorldPay Payment Gateway', 'wp-e-commerce' ); |
|
29
|
|
|
$this->supports = array( 'default_credit_card_form', 'tev1' ); |
|
30
|
|
|
|
|
31
|
|
|
$this->order_handler = WPSC_WorldPay_Payments_Order_Handler::get_instance( $this ); |
|
32
|
|
|
|
|
33
|
|
|
// Define user set variables |
|
34
|
|
|
$this->secure_net_id = $this->setting->get( 'secure_net_id' ); |
|
35
|
|
|
$this->secure_key = $this->setting->get( 'secure_key' ); |
|
36
|
|
|
$this->public_key = $this->setting->get( 'public_key' ); |
|
37
|
|
|
$this->sandbox = $this->setting->get( 'sandbox_mode' ) == '1' ? true : false; |
|
38
|
|
|
$this->endpoint = $this->sandbox ? $this->endpoints['sandbox'] : $this->endpoints['production']; |
|
39
|
|
|
$this->payment_capture = $this->setting->get( 'payment_capture' ) !== null ? $this->setting->get( 'payment_capture' ) : ''; |
|
40
|
|
|
$this->auth = 'Basic ' . base64_encode( $this->setting->get( 'secure_net_id' ) . ':' . $this->setting->get( 'secure_key' ) ); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Settings Form Template |
|
45
|
|
|
* |
|
46
|
|
|
* @since 3.9 |
|
47
|
|
|
*/ |
|
48
|
|
|
public function setup_form() { |
|
49
|
|
|
?> |
|
50
|
|
|
<!-- Account Credentials --> |
|
51
|
|
|
<tr> |
|
52
|
|
|
<td colspan="2"> |
|
53
|
|
|
<h4><?php _e( 'Account Credentials', 'wp-e-commerce' ); ?></h4> |
|
54
|
|
|
</td> |
|
55
|
|
|
</tr> |
|
56
|
|
|
<tr> |
|
57
|
|
|
<td> |
|
58
|
|
|
<label for="wpsc-worldpay-secure-net-id"><?php _e( 'SecureNet ID', 'wp-e-commerce' ); ?></label> |
|
59
|
|
|
</td> |
|
60
|
|
|
<td> |
|
61
|
|
|
<input type="text" name="<?php echo esc_attr( $this->setting->get_field_name( 'secure_net_id' ) ); ?>" value="<?php echo esc_attr( $this->setting->get( 'secure_net_id' ) ); ?>" id="wpsc-worldpay-secure-net-id" /> |
|
62
|
|
|
<br><span class="small description"><?php _e( 'The SecureNet ID can be obtained from the email that you should have received during the sign-up process.', 'wp-e-commerce' ); ?></span> |
|
63
|
|
|
</td> |
|
64
|
|
|
</tr> |
|
65
|
|
|
<tr> |
|
66
|
|
|
<td> |
|
67
|
|
|
<label for="wpsc-worldpay-secure-key"><?php _e( 'Secure Key', 'wp-e-commerce' ); ?></label> |
|
68
|
|
|
</td> |
|
69
|
|
|
<td> |
|
70
|
|
|
<input type="text" name="<?php echo esc_attr( $this->setting->get_field_name( 'secure_key' ) ); ?>" value="<?php echo esc_attr( $this->setting->get( 'secure_key' ) ); ?>" id="wpsc-worldpay-secure-key" /> |
|
71
|
|
|
<br><span class="small description"><?php _e( 'You can obtain the Secure Key by signing into the Virtual Terminal with the login credentials that you were emailed to you during the sign-up process. You will then need to navigate to Settings and click on the Obtain Secure Key link.', 'wp-e-commerce' ); ?></span> |
|
72
|
|
|
</td> |
|
73
|
|
|
</tr> |
|
74
|
|
|
<tr> |
|
75
|
|
|
<td> |
|
76
|
|
|
<label for="wpsc-worldpay-public-key"><?php _e( 'Public Key', 'wp-e-commerce' ); ?></label> |
|
77
|
|
|
</td> |
|
78
|
|
|
<td> |
|
79
|
|
|
<input type="text" name="<?php echo esc_attr( $this->setting->get_field_name( 'public_key' ) ); ?>" value="<?php echo esc_attr( $this->setting->get( 'public_key' ) ); ?>" id="wpsc-worldpay-public-key" /> |
|
80
|
|
|
<br><span class="small description"><?php _e( 'You can obtain the Public Key by signing into the Virtual Terminal. You will then need to navigate to Settings and click on the Obtain Public Key link.', 'wp-e-commerce' ); ?></span> |
|
81
|
|
|
</td> |
|
82
|
|
|
</tr> |
|
83
|
|
|
<tr> |
|
84
|
|
|
<td> |
|
85
|
|
|
<label for="wpsc-worldpay-payment-capture"><?php _e( 'Payment Capture', 'wp-e-commerce' ); ?></label> |
|
86
|
|
|
</td> |
|
87
|
|
|
<td> |
|
88
|
|
|
<select id="wpsc-worldpay-payment-capture" name="<?php echo esc_attr( $this->setting->get_field_name( 'payment_capture' ) ); ?>"> |
|
89
|
|
|
<option value='' <?php selected( '', $this->setting->get( 'payment_capture' ) ); ?>><?php _e( 'Authorize and capture the payment when the order is placed.', 'wp-e-commerce' )?></option> |
|
90
|
|
|
<option value='authorize' <?php selected( 'authorize', $this->setting->get( 'payment_capture' ) ); ?>><?php _e( 'Authorize the payment when the order is placed.', 'wp-e-commerce' )?></option> |
|
91
|
|
|
</select> |
|
92
|
|
|
</td> |
|
93
|
|
|
</tr> |
|
94
|
|
|
<tr> |
|
95
|
|
|
<td> |
|
96
|
|
|
<label><?php _e( 'Sandbox Mode', 'wp-e-commerce' ); ?></label> |
|
97
|
|
|
</td> |
|
98
|
|
|
<td> |
|
99
|
|
|
<label><input <?php checked( $this->setting->get( 'sandbox_mode' ) ); ?> type="radio" name="<?php echo esc_attr( $this->setting->get_field_name( 'sandbox_mode' ) ); ?>" value="1" /> <?php _e( 'Yes', 'wp-e-commerce' ); ?></label> |
|
100
|
|
|
<label><input <?php checked( (bool) $this->setting->get( 'sandbox_mode' ), false ); ?> type="radio" name="<?php echo esc_attr( $this->setting->get_field_name( 'sandbox_mode' ) ); ?>" value="0" /> <?php _e( 'No', 'wp-e-commerce' ); ?></label> |
|
101
|
|
|
</td> |
|
102
|
|
|
</tr> |
|
103
|
|
|
<!-- Error Logging --> |
|
104
|
|
|
<tr> |
|
105
|
|
|
<td colspan="2"> |
|
106
|
|
|
<h4><?php _e( 'Error Logging', 'wp-e-commerce' ); ?></h4> |
|
107
|
|
|
</td> |
|
108
|
|
|
</tr> |
|
109
|
|
|
<tr> |
|
110
|
|
|
<td> |
|
111
|
|
|
<label><?php _e( 'Enable Debugging', 'wp-e-commerce' ); ?></label> |
|
112
|
|
|
</td> |
|
113
|
|
|
<td> |
|
114
|
|
|
<label><input <?php checked( $this->setting->get( 'debugging' ) ); ?> type="radio" name="<?php echo esc_attr( $this->setting->get_field_name( 'debugging' ) ); ?>" value="1" /> <?php _e( 'Yes', 'wp-e-commerce' ); ?></label> |
|
115
|
|
|
<label><input <?php checked( (bool) $this->setting->get( 'debugging' ), false ); ?> type="radio" name="<?php echo esc_attr( $this->setting->get_field_name( 'debugging' ) ); ?>" value="0" /> <?php _e( 'No', 'wp-e-commerce' ); ?></label> |
|
116
|
|
|
</td> |
|
117
|
|
|
</tr> |
|
118
|
|
|
<?php |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Add scripts |
|
123
|
|
|
*/ |
|
124
|
|
|
public function scripts() { |
|
125
|
|
|
|
|
126
|
|
|
$js = $this->sandbox ? 'demo.' : ''; |
|
127
|
|
|
wp_enqueue_script( 'worldpay_payos', 'https://gwapi.'.$js.'securenet.com/v1/PayOS.js', 'jquery', false, true ); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
public function head_script() { |
|
131
|
|
|
?> |
|
132
|
|
|
<script type='text/javascript'> |
|
133
|
|
|
|
|
134
|
|
|
jQuery( document ).ready( function( $ ) { |
|
135
|
|
|
$( '.wpsc_checkout_forms' ).submit( function( e ) { |
|
136
|
|
|
|
|
137
|
|
|
e.preventDefault(); |
|
138
|
|
|
|
|
139
|
|
|
var response = tokenizeCard( |
|
140
|
|
|
{ |
|
141
|
|
|
"publicKey": '<?php echo $this->public_key; ?>', |
|
142
|
|
|
"card": { |
|
143
|
|
|
"number": document.getElementById('card_number').value, |
|
144
|
|
|
"cvv": document.getElementById('card_code').value, |
|
145
|
|
|
"expirationDate": document.getElementById('card_expiry_month').value + '/' + document.getElementById('card_expiry_year').value, |
|
146
|
|
|
"firstName": $( 'input[title="billingfirstname"]' ).val(), |
|
147
|
|
|
"lastName": $( 'input[title="billinglastname"]' ).val(), |
|
148
|
|
|
"address": { |
|
149
|
|
|
"line1": $( 'textarea[title="billingaddress"]' ).text(), |
|
150
|
|
|
"city": $( 'input[title="billingcity"]' ).val(), |
|
151
|
|
|
"state": $( 'input[title="billingstate"]' ).val(), |
|
152
|
|
|
"zip": $( 'input[title="billingpostcode"]' ).val() |
|
153
|
|
|
} |
|
154
|
|
|
}, |
|
155
|
|
|
"addToVault": false, |
|
156
|
|
|
"developerApplication": { |
|
157
|
|
|
"developerId": 10000644, |
|
158
|
|
|
"version": '1.2' |
|
159
|
|
|
|
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
).done(function (result) { |
|
163
|
|
|
|
|
164
|
|
|
var responseObj = $.parseJSON(JSON.stringify(result)); |
|
165
|
|
|
|
|
166
|
|
|
if (responseObj.success) { |
|
167
|
|
|
|
|
168
|
|
|
var $form = $( '.wpsc_checkout_forms' ); |
|
169
|
|
|
|
|
170
|
|
|
var token = responseObj.token; |
|
171
|
|
|
|
|
172
|
|
|
$("#worldpay_pay_token").val(token); |
|
173
|
|
|
// and submit |
|
174
|
|
|
$form.get(0).submit(); |
|
175
|
|
|
|
|
176
|
|
|
// do something with responseObj.token |
|
177
|
|
|
} else { |
|
178
|
|
|
alert("token was not created"); |
|
179
|
|
|
// do something with responseObj.message |
|
180
|
|
|
|
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
}).fail(function ( response ) { |
|
184
|
|
|
$( 'input[type="submit"]', this ).prop( { 'disabled': false } ); |
|
185
|
|
|
console.log( response ); |
|
186
|
|
|
}); |
|
187
|
|
|
}); |
|
188
|
|
|
|
|
189
|
|
|
}); |
|
190
|
|
|
|
|
191
|
|
|
</script> |
|
192
|
|
|
<?php |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
public function te_v1_insert_hidden_field() { |
|
196
|
|
|
echo '<input type="hidden" id="worldpay_pay_token" name="worldpay_pay_token" value="" />'; |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
public function init() { |
|
200
|
|
|
|
|
201
|
|
|
add_action( 'wp_enqueue_scripts', array( $this, 'scripts' ) ); |
|
202
|
|
|
add_action( 'wp_head' , array( $this, 'head_script' ) ); |
|
203
|
|
|
|
|
204
|
|
|
add_action( 'wpsc_inside_shopping_cart', array( $this, 'te_v1_insert_hidden_field' ) ); |
|
205
|
|
|
|
|
206
|
|
|
add_filter( 'wpsc_gateway_checkout_form_worldpay', array( $this, 'payment_fields' ) ); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
public function te_v2_show_payment_fields( $args ) { |
|
210
|
|
|
|
|
211
|
|
|
$default = '<div class="wpsc-form-actions">'; |
|
212
|
|
|
ob_start(); |
|
213
|
|
|
|
|
214
|
|
|
$this->payment_fields(); |
|
215
|
|
|
$fields = ob_get_clean(); |
|
216
|
|
|
|
|
217
|
|
|
$args['before_form_actions'] = $fields . $default; |
|
218
|
|
|
|
|
219
|
|
|
return $args; |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
public function process() { |
|
223
|
|
|
|
|
224
|
|
|
$order = $this->purchase_log; |
|
225
|
|
|
|
|
226
|
|
|
$status = $this->payment_capture === '' ? WPSC_Purchase_Log::ACCEPTED_PAYMENT : WPSC_Purchase_Log::ORDER_RECEIVED; |
|
227
|
|
|
|
|
228
|
|
|
$order->set( 'processed', $status )->save(); |
|
229
|
|
|
|
|
230
|
|
|
$card_token = isset( $_POST['worldpay_pay_token'] ) ? sanitize_text_field( $_POST['worldpay_pay_token'] ) : ''; |
|
231
|
|
|
|
|
232
|
|
|
$this->order_handler->set_purchase_log( $order->get( 'id' ) ); |
|
233
|
|
|
|
|
234
|
|
|
switch ( $this->payment_capture ) { |
|
235
|
|
|
case 'authorize' : |
|
236
|
|
|
|
|
237
|
|
|
// Authorize only |
|
238
|
|
|
$result = $this->authorize_payment( $card_token ); |
|
239
|
|
|
|
|
240
|
|
|
if ( $result ) { |
|
241
|
|
|
// Mark as on-hold |
|
242
|
|
|
$order->set( 'worldpay-status', __( 'WorldPay order opened. Capture the payment below. Authorized payments must be captured within 7 days.', 'wp-e-commerce' ) )->save(); |
|
243
|
|
|
|
|
244
|
|
|
} else { |
|
245
|
|
|
$order->set( 'processed', WPSC_Purchase_Log::PAYMENT_DECLINED )->save(); |
|
246
|
|
|
$order->set( 'worldpay-status', __( 'Could not authorize WorldPay payment.', 'wp-e-commerce' ) )->save(); |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
break; |
|
250
|
|
|
default: |
|
251
|
|
|
|
|
252
|
|
|
// Capture |
|
253
|
|
|
$result = $this->capture_payment( $card_token ); |
|
254
|
|
|
|
|
255
|
|
|
if ( $result ) { |
|
256
|
|
|
// Payment complete |
|
257
|
|
|
$order->set( 'worldpay-status', __( 'WorldPay order completed. Funds have been authorized and captured.', 'wp-e-commerce' ) ); |
|
258
|
|
|
} else { |
|
259
|
|
|
$order->set( 'processed' , WPSC_Purchase_Log::PAYMENT_DECLINED ); |
|
260
|
|
|
$order->set( 'worldpay-status', __( 'Could not authorize WorldPay payment.', 'wp-e-commerce' ) ); |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
break; |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
$order->save(); |
|
267
|
|
|
$this->go_to_transaction_results(); |
|
268
|
|
|
|
|
269
|
|
|
} |
|
270
|
|
|
|
|
271
|
|
|
public function capture_payment( $token ) { |
|
272
|
|
|
|
|
273
|
|
|
if ( $this->purchase_log->get( 'gateway' ) == 'worldpay' ) { |
|
274
|
|
|
|
|
275
|
|
|
$order = $this->purchase_log; |
|
276
|
|
|
|
|
277
|
|
|
$params = array( |
|
278
|
|
|
'amount' => $order->get( 'totalprice' ), |
|
279
|
|
|
'orderId' => $order->get( 'id' ), |
|
280
|
|
|
'invoiceNumber' => $order->get( 'sessionid' ), |
|
281
|
|
|
"addToVault" => false, |
|
282
|
|
|
"paymentVaultToken" => array( |
|
283
|
|
|
"paymentMethodId" => $token, |
|
284
|
|
|
"publicKey" => $this->public_key |
|
285
|
|
|
), |
|
286
|
|
|
"extendedInformation" => array( |
|
287
|
|
|
"typeOfGoods" => $this->type_of_goods( $order->get( 'id' ) ) |
|
288
|
|
|
), |
|
289
|
|
|
); |
|
290
|
|
|
|
|
291
|
|
|
$response = $this->execute( 'Payments/Charge', $params ); |
|
292
|
|
|
|
|
293
|
|
|
if ( is_wp_error( $response ) ) { |
|
294
|
|
|
throw new Exception( $response->get_error_message() ); |
|
295
|
|
|
} |
|
296
|
|
|
|
|
297
|
|
|
if ( isset( $response['ResponseBody']->transaction->transactionId ) ) { |
|
298
|
|
|
$transaction_id = $response['ResponseBody']->transaction->transactionId; |
|
299
|
|
|
$auth_code = $response['ResponseBody']->transaction->authorizationCode; |
|
300
|
|
|
} else { |
|
301
|
|
|
return false; |
|
302
|
|
|
} |
|
303
|
|
|
|
|
304
|
|
|
// Store transaction ID and Auth code in the order |
|
305
|
|
|
$order->set( 'wp_transactionId', $transaction_id )->save(); |
|
306
|
|
|
$order->set( 'wp_order_status' , 'Completed' )->save(); |
|
307
|
|
|
$order->set( 'wp_authcode' , $auth_code )->save(); |
|
308
|
|
|
$order->set( 'transactid' , $transaction_id )->save(); |
|
309
|
|
|
$order->set( 'wp_order_token' , $token )->save(); |
|
310
|
|
|
|
|
311
|
|
|
return true; |
|
312
|
|
|
} |
|
313
|
|
|
|
|
314
|
|
|
return false; |
|
315
|
|
|
} |
|
316
|
|
|
|
|
317
|
|
|
public function authorize_payment( $token ) { |
|
318
|
|
|
|
|
319
|
|
|
if ( $this->purchase_log->get( 'gateway' ) == 'worldpay' ) { |
|
320
|
|
|
|
|
321
|
|
|
$order = $this->purchase_log; |
|
322
|
|
|
|
|
323
|
|
|
$params = array( |
|
324
|
|
|
'amount' => $order->get( 'totalprice' ), |
|
325
|
|
|
'orderId' => $order->get( 'id' ), |
|
326
|
|
|
'invoiceNumber' => $order->get( 'sessionid' ), |
|
327
|
|
|
"addToVault" => false, |
|
328
|
|
|
"paymentVaultToken" => array( |
|
329
|
|
|
"paymentMethodId" => $token, |
|
330
|
|
|
"publicKey" => $this->public_key, |
|
331
|
|
|
), |
|
332
|
|
|
"extendedInformation" => array( |
|
333
|
|
|
"typeOfGoods" => $this->type_of_goods( $order->get( 'id' ) ) |
|
334
|
|
|
), |
|
335
|
|
|
); |
|
336
|
|
|
|
|
337
|
|
|
$response = $this->execute( 'Payments/Authorize', $params ); |
|
338
|
|
|
|
|
339
|
|
|
if ( is_wp_error( $response ) ) { |
|
340
|
|
|
throw new Exception( $response->get_error_message() ); |
|
341
|
|
|
} |
|
342
|
|
|
|
|
343
|
|
|
if ( isset( $response['ResponseBody']->transaction->transactionId ) ) { |
|
344
|
|
|
$transaction_id = $response['ResponseBody']->transaction->transactionId; |
|
345
|
|
|
$auth_code = $response['ResponseBody']->transaction->authorizationCode; |
|
346
|
|
|
} else { |
|
347
|
|
|
return false; |
|
348
|
|
|
} |
|
349
|
|
|
|
|
350
|
|
|
// Store transaction ID and Auth code in the order |
|
351
|
|
|
$order->set( 'wp_transactionId', $transaction_id )->save(); |
|
352
|
|
|
$order->set( 'wp_order_status' , 'Open' )->save(); |
|
353
|
|
|
$order->set( 'wp_authcode' , $auth_code )->save(); |
|
354
|
|
|
$order->set( 'transactid' , $transaction_id )->save(); |
|
355
|
|
|
$order->set( 'wp_order_token' , $token )->save(); |
|
356
|
|
|
|
|
357
|
|
|
return true; |
|
358
|
|
|
} |
|
359
|
|
|
|
|
360
|
|
|
return false; |
|
361
|
|
|
} |
|
362
|
|
|
|
|
363
|
|
|
public function execute( $endpoint, $params = array(), $type = 'POST' ) { |
|
364
|
|
|
|
|
365
|
|
|
// where we make the API petition |
|
366
|
|
|
$endpoint = $this->endpoint . $endpoint; |
|
367
|
|
|
|
|
368
|
|
|
if ( ! is_null( $params ) ) { |
|
369
|
|
|
$params += array( |
|
370
|
|
|
"developerApplication" => array( |
|
371
|
|
|
"developerId" => 10000644, |
|
372
|
|
|
"version" => "1.2" |
|
373
|
|
|
), |
|
374
|
|
|
); |
|
375
|
|
|
} |
|
376
|
|
|
|
|
377
|
|
|
$data = json_encode( $params ); |
|
378
|
|
|
|
|
379
|
|
|
$args = array( |
|
380
|
|
|
'timeout' => 15, |
|
381
|
|
|
'headers' => array( |
|
382
|
|
|
'Authorization' => $this->auth, |
|
383
|
|
|
'Content-Type' => 'application/json', |
|
384
|
|
|
), |
|
385
|
|
|
'sslverify' => false, |
|
386
|
|
|
'body' => $data, |
|
387
|
|
|
); |
|
388
|
|
|
|
|
389
|
|
|
$request = $type == 'GET' ? wp_safe_remote_get( $endpoint, $args ) : wp_safe_remote_post( $endpoint, $args ); |
|
390
|
|
|
$response = wp_remote_retrieve_body( $request ); |
|
391
|
|
|
|
|
392
|
|
|
if ( ! is_wp_error( $request ) ) { |
|
393
|
|
|
|
|
394
|
|
|
$response_object = array(); |
|
395
|
|
|
$response_object['ResponseBody'] = json_decode( $response ); |
|
396
|
|
|
$response_object['Status'] = wp_remote_retrieve_response_code( $request ); |
|
397
|
|
|
|
|
398
|
|
|
$request = $response_object; |
|
399
|
|
|
} |
|
400
|
|
|
|
|
401
|
|
|
return $request; |
|
402
|
|
|
} |
|
403
|
|
|
|
|
404
|
|
|
public function type_of_goods( $log_id ) { |
|
405
|
|
|
$digital = 0; |
|
406
|
|
|
|
|
407
|
|
|
$log = new WPSC_Purchase_Log( $log_id ); |
|
408
|
|
|
$cart = $log->get_cart_contents(); |
|
409
|
|
|
|
|
410
|
|
|
foreach ( $cart as $cartitem ) { |
|
411
|
|
|
$product_meta = get_post_meta( $cartitem->prodid, '_wpsc_product_metadata' ); |
|
412
|
|
|
|
|
413
|
|
|
if ( isset( $product_meta[0]['no_shipping'] ) && $product_meta[0]['no_shipping'] == 1 ) { |
|
414
|
|
|
$digital++; |
|
415
|
|
|
} |
|
416
|
|
|
} |
|
417
|
|
|
|
|
418
|
|
|
return $digital == count( $cart ) ? 'DIGITAL' : 'PHYSICAL'; |
|
419
|
|
|
} |
|
420
|
|
|
} |
|
421
|
|
|
|
|
422
|
|
|
class WPSC_WorldPay_Payments_Order_Handler { |
|
423
|
|
|
|
|
424
|
|
|
private static $instance; |
|
425
|
|
|
private $log; |
|
426
|
|
|
private $gateway; |
|
427
|
|
|
|
|
428
|
|
|
public function __construct( &$gateway ) { |
|
429
|
|
|
|
|
430
|
|
|
$this->log = $gateway->purchase_log; |
|
431
|
|
|
$this->gateway = $gateway; |
|
432
|
|
|
|
|
433
|
|
|
$this->init(); |
|
434
|
|
|
} |
|
435
|
|
|
|
|
436
|
|
|
/** |
|
437
|
|
|
* Constructor |
|
438
|
|
|
*/ |
|
439
|
|
|
public function init() { |
|
440
|
|
|
add_action( 'wpsc_purchlogitem_metabox_start', array( $this, 'meta_box' ), 8 ); |
|
441
|
|
|
add_action( 'wp_ajax_worldpay_order_action' , array( $this, 'order_actions' ) ); |
|
442
|
|
|
|
|
443
|
|
|
} |
|
444
|
|
|
|
|
445
|
|
|
public static function get_instance( $gateway ) { |
|
446
|
|
|
if ( is_null( self::$instance ) ) { |
|
447
|
|
|
self::$instance = new WPSC_WorldPay_Payments_Order_Handler( $gateway ); |
|
448
|
|
|
} |
|
449
|
|
|
|
|
450
|
|
|
return self::$instance; |
|
451
|
|
|
} |
|
452
|
|
|
|
|
453
|
|
|
public function set_purchase_log( $id ) { |
|
454
|
|
|
$this->log = new WPSC_Purchase_Log( $id ); |
|
455
|
|
|
} |
|
456
|
|
|
|
|
457
|
|
|
/** |
|
458
|
|
|
* Perform order actions for amazon |
|
459
|
|
|
*/ |
|
460
|
|
|
public function order_actions() { |
|
461
|
|
|
check_ajax_referer( 'wp_order_action', 'security' ); |
|
462
|
|
|
|
|
463
|
|
|
$order_id = absint( $_POST['order_id'] ); |
|
464
|
|
|
$id = isset( $_POST['worldpay_id'] ) ? sanitize_text_field( $_POST['worldpay_id'] ) : ''; |
|
465
|
|
|
$action = sanitize_title( $_POST['worldpay_action'] ); |
|
466
|
|
|
|
|
467
|
|
|
$this->set_purchase_log( $order_id ); |
|
468
|
|
|
|
|
469
|
|
|
switch ( $action ) { |
|
470
|
|
|
case 'capture' : |
|
471
|
|
|
//Capture an AUTH |
|
472
|
|
|
$this->capture_payment($id); |
|
473
|
|
|
break; |
|
474
|
|
|
|
|
475
|
|
|
case 'void' : |
|
476
|
|
|
// void capture or auth before settled |
|
477
|
|
|
$this->void_payment( $id ); |
|
478
|
|
|
break; |
|
479
|
|
|
|
|
480
|
|
|
case 'refund' : |
|
481
|
|
|
// refund a settled payment |
|
482
|
|
|
$this->refund_payment( $id ); |
|
483
|
|
|
break; |
|
484
|
|
|
|
|
485
|
|
|
case 'void_refund' : |
|
486
|
|
|
// void a refund request |
|
487
|
|
|
$this->void_refund( $id ); |
|
488
|
|
|
break; |
|
489
|
|
|
} |
|
490
|
|
|
|
|
491
|
|
|
echo json_encode( array( 'action' => $action, 'order_id' => $order_id, 'worldpay_id' => $id ) ); |
|
492
|
|
|
|
|
493
|
|
|
die(); |
|
494
|
|
|
} |
|
495
|
|
|
|
|
496
|
|
|
/** |
|
497
|
|
|
* meta_box function. |
|
498
|
|
|
* |
|
499
|
|
|
* @access public |
|
500
|
|
|
* @return void |
|
501
|
|
|
*/ |
|
502
|
|
|
function meta_box( $log_id ) { |
|
503
|
|
|
$this->set_purchase_log( $log_id ); |
|
504
|
|
|
|
|
505
|
|
|
$gateway = $this->log->get( 'gateway' ); |
|
506
|
|
|
|
|
507
|
|
|
if ( $gateway == 'worldpay' ) { |
|
508
|
|
|
$this->authorization_box(); |
|
509
|
|
|
} |
|
510
|
|
|
} |
|
511
|
|
|
|
|
512
|
|
|
/** |
|
513
|
|
|
* pre_auth_box function. |
|
514
|
|
|
* |
|
515
|
|
|
* @access public |
|
516
|
|
|
* @return void |
|
517
|
|
|
*/ |
|
518
|
|
|
public function authorization_box() { |
|
519
|
|
|
|
|
520
|
|
|
$actions = array(); |
|
521
|
|
|
$order_id = $this->log->get( 'id' ); |
|
522
|
|
|
|
|
523
|
|
|
// Get ids |
|
524
|
|
|
$wp_transaction_id = $this->log->get( 'wp_transactionId' ); |
|
525
|
|
|
$wp_auth_code = $this->log->get( 'wp_authcode' ); |
|
526
|
|
|
$wp_order_status = $this->log->get( 'wp_order_status' ); |
|
527
|
|
|
|
|
528
|
|
|
//Don't change order status if a refund has been requested |
|
529
|
|
|
$wp_refund_set = wpsc_get_purchase_meta( $order_id, 'worldpay_refunded', true ); |
|
530
|
|
|
$order_info = $this->refresh_transaction_info( $wp_transaction_id, ! (bool) $wp_refund_set ); |
|
531
|
|
|
?> |
|
532
|
|
|
|
|
533
|
|
|
<div class="metabox-holder"> |
|
534
|
|
|
<div id="wpsc-worldpay-payments" class="postbox"> |
|
535
|
|
|
<h3 class='hndle'><?php _e( 'WorldPay Payments' , 'wp-e-commerce' ); ?></h3> |
|
536
|
|
|
<div class='inside'> |
|
537
|
|
|
<p><?php |
|
538
|
|
|
_e( 'Current status: ', 'wp-e-commerce' ); |
|
539
|
|
|
echo wp_kses_data( $this->log->get( 'worldpay-status' ) ); |
|
540
|
|
|
?> |
|
541
|
|
|
</p> |
|
542
|
|
|
<p><?php |
|
543
|
|
|
_e( 'Transaction ID: ', 'wp-e-commerce' ); |
|
544
|
|
|
echo wp_kses_data( $wp_transaction_id ); |
|
545
|
|
|
?> |
|
546
|
|
|
</p> |
|
547
|
|
|
<?php |
|
548
|
|
|
|
|
549
|
|
|
//Show actions based on order status |
|
550
|
|
|
switch ( $wp_order_status ) { |
|
551
|
|
|
case 'Open' : |
|
552
|
|
|
//Order is only authorized and still not captured/voided |
|
553
|
|
|
$actions['capture'] = array( |
|
554
|
|
|
'id' => $wp_transaction_id, |
|
555
|
|
|
'button' => __( 'Capture funds', 'wp-e-commerce' ) |
|
556
|
|
|
); |
|
557
|
|
|
|
|
558
|
|
|
// |
|
559
|
|
|
if ( ! $order_info['settled'] ) { |
|
560
|
|
|
//Void |
|
561
|
|
|
$actions['void'] = array( |
|
562
|
|
|
'id' => $wp_transaction_id, |
|
563
|
|
|
'button' => __( 'Void order', 'wp-e-commerce' ) |
|
564
|
|
|
); |
|
565
|
|
|
} |
|
566
|
|
|
|
|
567
|
|
|
break; |
|
568
|
|
|
case 'Completed' : |
|
569
|
|
|
//Order has been captured or its a direct payment |
|
570
|
|
|
if ( $order_info['settled'] ) { |
|
571
|
|
|
//Refund |
|
572
|
|
|
$actions['refund'] = array( |
|
573
|
|
|
'id' => $wp_transaction_id, |
|
574
|
|
|
'button' => __( 'Refund order', 'wp-e-commerce' ) |
|
575
|
|
|
); |
|
576
|
|
|
} else { |
|
577
|
|
|
//Void |
|
578
|
|
|
$actions['void'] = array( |
|
579
|
|
|
'id' => $wp_transaction_id, |
|
580
|
|
|
'button' => __( 'Void order', 'wp-e-commerce' ) |
|
581
|
|
|
); |
|
582
|
|
|
} |
|
583
|
|
|
|
|
584
|
|
|
break; |
|
585
|
|
|
case 'Refunded' : |
|
586
|
|
|
//Order is settled and a refund has been requested |
|
587
|
|
|
$wp_refund_id = wpsc_get_purchase_meta( $order_id, 'worldpay_refund_id', true ); |
|
588
|
|
|
|
|
589
|
|
|
if ( $wp_refund_id ) { |
|
590
|
|
|
//Get refund order status to check if its eligible for a void (not settled) |
|
591
|
|
|
$refund_status = $this->refresh_transaction_info( $wp_refund_id, false ); |
|
592
|
|
|
|
|
593
|
|
|
if ( ! $refund_status['settled'] ) { |
|
594
|
|
|
//Show void only if not settled. |
|
595
|
|
|
$actions['void_refund'] = array( |
|
596
|
|
|
'id' => $wp_refund_id, |
|
597
|
|
|
'button' => __( 'Void Refund request', 'wp-e-commerce' ) |
|
598
|
|
|
); |
|
599
|
|
|
} |
|
600
|
|
|
} |
|
601
|
|
|
|
|
602
|
|
|
break; |
|
603
|
|
|
case 'Voided' : |
|
604
|
|
|
break; |
|
605
|
|
|
} |
|
606
|
|
|
|
|
607
|
|
|
if ( ! empty( $actions ) ) { |
|
608
|
|
|
|
|
609
|
|
|
echo '<p class="buttons">'; |
|
610
|
|
|
|
|
611
|
|
|
foreach ( $actions as $action_name => $action ) { |
|
612
|
|
|
echo '<a href="#" class="button" data-action="' . $action_name . '" data-id="' . $action['id'] . '">' . $action['button'] . '</a> '; |
|
613
|
|
|
} |
|
614
|
|
|
|
|
615
|
|
|
echo '</p>'; |
|
616
|
|
|
|
|
617
|
|
|
} |
|
618
|
|
|
?> |
|
619
|
|
|
<script type="text/javascript"> |
|
620
|
|
|
jQuery( document ).ready( function( $ ) { |
|
621
|
|
|
$('#wpsc-worldpay-payments').on( 'click', 'a.button, a.refresh', function( e ) { |
|
622
|
|
|
var $this = $( this ); |
|
623
|
|
|
e.preventDefault(); |
|
624
|
|
|
|
|
625
|
|
|
var data = { |
|
626
|
|
|
action: 'worldpay_order_action', |
|
627
|
|
|
security: '<?php echo wp_create_nonce( "wp_order_action" ); ?>', |
|
628
|
|
|
order_id: '<?php echo $order_id; ?>', |
|
629
|
|
|
worldpay_action: $this.data('action'), |
|
630
|
|
|
worldpay_id: $this.data('id'), |
|
631
|
|
|
worldpay_refund_amount: $('.worldpay_refund_amount').val(), |
|
632
|
|
|
}; |
|
633
|
|
|
|
|
634
|
|
|
// Ajax action |
|
635
|
|
|
$.post( ajaxurl, data, function( result ) { |
|
636
|
|
|
location.reload(); |
|
637
|
|
|
}, 'json' ); |
|
638
|
|
|
|
|
639
|
|
|
return false; |
|
640
|
|
|
}); |
|
641
|
|
|
} ); |
|
642
|
|
|
|
|
643
|
|
|
</script> |
|
644
|
|
|
</div> |
|
645
|
|
|
</div> |
|
646
|
|
|
</div> |
|
647
|
|
|
<?php |
|
648
|
|
|
} |
|
649
|
|
|
|
|
650
|
|
|
/** |
|
651
|
|
|
* Get the order status from API |
|
652
|
|
|
* |
|
653
|
|
|
* @param string $transaction_id |
|
654
|
|
|
*/ |
|
655
|
|
|
public function refresh_transaction_info( $transaction_id, $update = true ) { |
|
656
|
|
|
|
|
657
|
|
|
if ( $this->log->get( 'gateway' ) == 'worldpay' ) { |
|
658
|
|
|
|
|
659
|
|
|
$response = $this->gateway->execute( 'transactions/'. $transaction_id, null, 'GET' ); |
|
660
|
|
|
|
|
661
|
|
|
if ( is_wp_error( $response ) ) { |
|
662
|
|
|
throw new Exception( $response->get_error_message() ); |
|
663
|
|
|
} |
|
664
|
|
|
|
|
665
|
|
|
$response_object = array(); |
|
666
|
|
|
$response_object['trans_type'] = $response['ResponseBody']->transactions[0]->transactionType; |
|
667
|
|
|
$response_object['settled'] = isset( $response['ResponseBody']->transactions[0]->settlementData ) ? true : false; |
|
668
|
|
|
|
|
669
|
|
|
//Recheck status and update if required |
|
670
|
|
|
if ( $update ) { |
|
671
|
|
|
switch ( $response_object['trans_type'] ) { |
|
672
|
|
|
case 'AUTH_ONLY' : |
|
673
|
|
|
$this->log->set( 'wp_order_status', 'Open' )->save(); |
|
674
|
|
|
break; |
|
675
|
|
|
|
|
676
|
|
|
case 'VOID' : |
|
677
|
|
|
$this->log->set( 'wp_order_status', 'Voided' )->save(); |
|
678
|
|
|
break; |
|
679
|
|
|
|
|
680
|
|
|
case 'REFUND' : |
|
681
|
|
|
case 'CREDIT' : |
|
682
|
|
|
$this->log->set( 'wp_order_status', 'Refunded' )->save(); |
|
683
|
|
|
break; |
|
684
|
|
|
|
|
685
|
|
|
case 'AUTH_CAPTURE' : |
|
686
|
|
|
case 'PRIOR_AUTH_CAPTURE' : |
|
687
|
|
|
$this->log->set( 'wp_order_status', 'Completed' )->save(); |
|
688
|
|
|
break; |
|
689
|
|
|
} |
|
690
|
|
|
} |
|
691
|
|
|
|
|
692
|
|
|
return $response_object; |
|
693
|
|
|
} |
|
694
|
|
|
} |
|
695
|
|
|
|
|
696
|
|
|
|
|
697
|
|
|
/** |
|
698
|
|
|
* Void auth/capture |
|
699
|
|
|
* |
|
700
|
|
|
* @param string $transaction_id |
|
701
|
|
|
*/ |
|
702
|
|
|
public function void_payment( $transaction_id ) { |
|
703
|
|
|
|
|
704
|
|
|
if ( $this->log->get( 'gateway' ) == 'worldpay' ) { |
|
705
|
|
|
|
|
706
|
|
|
$params = array( |
|
707
|
|
|
'amount' => $this->log->get( 'totalprice' ), |
|
|
|
|
|
|
708
|
|
|
'transactionId' => $transaction_id, |
|
709
|
|
|
); |
|
710
|
|
|
|
|
711
|
|
|
$response = $this->gateway->execute( 'Payments/Void', $params ); |
|
712
|
|
|
|
|
713
|
|
|
if ( is_wp_error( $response ) ) { |
|
714
|
|
|
throw new Exception( $response->get_error_message() ); |
|
715
|
|
|
} |
|
716
|
|
|
|
|
717
|
|
|
$this->log->set( 'wp_order_status', 'Voided' )->save(); |
|
718
|
|
|
$this->log->set( 'worldpay-status', sprintf( __( 'Authorization voided (Auth ID: %s)', 'wp-e-commerce' ), $response['ResponseBody']->transaction->authorizationCode ) )->save(); |
|
719
|
|
|
$this->log->set( 'processed' , WPSC_Purchase_Log::INCOMPLETE_SALE )->save(); |
|
720
|
|
|
$this->log->set( 'transactid' , $response['ResponseBody']->transaction->transactionId )->save(); |
|
721
|
|
|
} |
|
722
|
|
|
} |
|
723
|
|
|
|
|
724
|
|
|
/** |
|
725
|
|
|
* Refund payment |
|
726
|
|
|
* |
|
727
|
|
|
* @param string $transaction_id |
|
728
|
|
|
*/ |
|
729
|
|
|
public function refund_payment( $transaction_id ) { |
|
730
|
|
|
|
|
731
|
|
|
if ( $this->log->get( 'gateway' ) == 'worldpay' ) { |
|
732
|
|
|
|
|
733
|
|
|
$params = array( |
|
734
|
|
|
'amount' => $this->log->get( 'totalprice' ), |
|
735
|
|
|
'transactionId' => $transaction_id, |
|
736
|
|
|
|
|
737
|
|
|
); |
|
738
|
|
|
|
|
739
|
|
|
$response = $this->gateway->execute( 'Payments/Refund', $params ); |
|
740
|
|
|
|
|
741
|
|
|
if ( is_wp_error( $response ) ) { |
|
742
|
|
|
throw new Exception( $response->get_error_message() ); |
|
743
|
|
|
} |
|
744
|
|
|
|
|
745
|
|
|
wpsc_add_purchase_meta( $this->log->get( 'id' ), 'worldpay_refunded', true ); |
|
746
|
|
|
wpsc_add_purchase_meta( $this->log->get( 'id' ), 'worldpay_refund_id', $response['ResponseBody']->transaction->transactionId ); |
|
747
|
|
|
|
|
748
|
|
|
$this->log->set( 'worldpay-status', sprintf( __( 'Refunded (Transaction ID: %s)', 'wp-e-commerce' ), $response['ResponseBody']->transaction->transactionId ) )->save(); |
|
749
|
|
|
$this->log->set( 'processed' , WPSC_Purchase_Log::REFUNDED )->save(); |
|
750
|
|
|
$this->log->set( 'wp_order_status', 'Refunded' )->save(); |
|
751
|
|
|
$this->log->set( 'transactid' , $response['ResponseBody']->transaction->transactionId )->save(); |
|
752
|
|
|
} |
|
753
|
|
|
} |
|
754
|
|
|
|
|
755
|
|
|
/** |
|
756
|
|
|
* Capture authorized payment |
|
757
|
|
|
* |
|
758
|
|
|
* @param string $transaction_id |
|
759
|
|
|
*/ |
|
760
|
|
|
public function capture_payment( $transaction_id ) { |
|
761
|
|
|
|
|
762
|
|
|
if ( $this->log->get( 'gateway' ) == 'worldpay' ) { |
|
763
|
|
|
|
|
764
|
|
|
$params = array( |
|
765
|
|
|
'amount' => $this->log->get( 'totalprice' ), |
|
766
|
|
|
'transactionId' => $transaction_id, |
|
767
|
|
|
); |
|
768
|
|
|
|
|
769
|
|
|
$response = $this->gateway->execute( 'Payments/Capture', $params ); |
|
770
|
|
|
|
|
771
|
|
|
if ( is_wp_error( $response ) ) { |
|
772
|
|
|
throw new Exception( $response->get_error_message() ); |
|
773
|
|
|
} |
|
774
|
|
|
|
|
775
|
|
|
$this->log->set( 'wp_order_status', 'Completed' )->save(); |
|
776
|
|
|
$this->log->set( 'worldpay-status', sprintf( __( 'Authorization Captured (Auth ID: %s)', 'wp-e-commerce' ), $response['ResponseBody']->transaction->authorizationCode ) )->save(); |
|
777
|
|
|
$this->log->set( 'processed' , WPSC_Purchase_Log::ACCEPTED_PAYMENT )->save(); |
|
778
|
|
|
$this->log->set( 'transactid' , $response['ResponseBody']->transaction->transactionId )->save(); |
|
779
|
|
|
} |
|
780
|
|
|
} |
|
781
|
|
|
|
|
782
|
|
|
/** |
|
783
|
|
|
* Void a refund request |
|
784
|
|
|
* |
|
785
|
|
|
* @param string $transaction_id |
|
786
|
|
|
*/ |
|
787
|
|
|
public function void_refund( $transaction_id ) { |
|
788
|
|
|
|
|
789
|
|
|
if ( $this->log->get( 'gateway' ) == 'worldpay' ) { |
|
790
|
|
|
|
|
791
|
|
|
$params = array( |
|
792
|
|
|
'amount' => $this->log->get( 'totalprice' ), |
|
793
|
|
|
'transactionId' => $transaction_id, |
|
794
|
|
|
); |
|
795
|
|
|
|
|
796
|
|
|
$response = $this->gateway->execute( 'Payments/Void', $params ); |
|
797
|
|
|
|
|
798
|
|
|
if ( is_wp_error( $response ) ) { |
|
799
|
|
|
throw new Exception( $response->get_error_message() ); |
|
800
|
|
|
} |
|
801
|
|
|
|
|
802
|
|
|
wpsc_delete_purchase_meta( $this->log->get( 'id' ), 'worldpay_refunded' ); |
|
803
|
|
|
wpsc_delete_purchase_meta( $this->log->get( 'id' ), 'worldpay_refund_id' ); |
|
804
|
|
|
|
|
805
|
|
|
$this->log->set( 'processed' , WPSC_Purchase_Log::ACCEPTED_PAYMENT )->save(); |
|
806
|
|
|
$this->log->set( 'wp_order_status', 'Completed' )->save(); |
|
807
|
|
|
$this->log->set( 'worldpay-status', sprintf( __( 'Refund Voided (Transaction ID: %s)', 'wp-e-commerce' ), $response['ResponseBody']->transaction->transactionId ) )->save(); |
|
808
|
|
|
$this->log->set( 'transactid' , $response['ResponseBody']->transaction->transactionId )->save(); |
|
809
|
|
|
} |
|
810
|
|
|
} |
|
811
|
|
|
} |
|
812
|
|
|
|