|
1
|
|
|
<?php |
|
2
|
|
|
require_once( 'paypal-express-checkout.php' ); |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* The PayPal Express Checkout Gateway class |
|
6
|
|
|
* |
|
7
|
|
|
*/ |
|
8
|
|
|
class WPSC_Payment_Gateway_Paypal_Digital_Goods extends WPSC_Payment_Gateway_Paypal_Express_Checkout { |
|
9
|
|
|
const SANDBOX_URL = 'https://www.sandbox.paypal.com/incontext?token='; |
|
10
|
|
|
const LIVE_URL = 'https://www.paypal.com/incontext?token='; |
|
11
|
|
|
protected $gateway; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Constructor of PayPal Express Checkout Gateway |
|
15
|
|
|
* |
|
16
|
|
|
* @param array $options |
|
17
|
|
|
* |
|
18
|
|
|
* @since 3.9 |
|
19
|
|
|
*/ |
|
20
|
|
|
public function __construct( $options ) { |
|
21
|
|
|
require_once( 'php-merchant/gateways/paypal-digital-goods.php' ); |
|
22
|
|
|
// Now that the gateway is created, call parent constructor |
|
23
|
|
|
parent::__construct( $options, true ); |
|
24
|
|
|
|
|
25
|
|
|
$this->gateway = new PHP_Merchant_Paypal_Digital_Goods( $options ); |
|
26
|
|
|
$this->title = __( 'PayPal Digital Goods for Express Checkout', 'wp-e-commerce' ); |
|
27
|
|
|
|
|
28
|
|
|
$this->gateway->set_options( array( |
|
29
|
|
|
'api_username' => $this->setting->get( 'api_username' ), |
|
30
|
|
|
'api_password' => $this->setting->get( 'api_password' ), |
|
31
|
|
|
'api_signature' => $this->setting->get( 'api_signature' ), |
|
32
|
|
|
'cancel_url' => $this->get_cancel_url(), |
|
33
|
|
|
'currency' => $this->get_currency_code(), |
|
34
|
|
|
'test' => (bool) $this->setting->get( 'sandbox_mode' ), |
|
35
|
|
|
'address_override' => 1, |
|
36
|
|
|
'solution_type' => 'mark', |
|
37
|
|
|
'cart_logo' => $this->setting->get( 'cart_logo' ), |
|
38
|
|
|
'cart_border' => $this->setting->get( 'cart_border' ), |
|
39
|
|
|
) ); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Run the gateway hooks |
|
44
|
|
|
* |
|
45
|
|
|
* @access public |
|
46
|
|
|
* @since 4.0 |
|
47
|
|
|
* |
|
48
|
|
|
* @return void |
|
49
|
|
|
*/ |
|
50
|
|
|
public function init() { |
|
51
|
|
|
// Express Checkout for DG Button |
|
52
|
|
|
add_action( 'wpsc_cart_item_table_form_actions_left', array( $this, 'add_ecs_button' ), 2, 2 ); |
|
53
|
|
|
|
|
54
|
|
|
// Filter Digital Goods option on checkout |
|
55
|
|
|
add_filter( 'wpsc_payment_method_form_fields', array( &$this, 'dg_option_removal' ), 100 ); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Toggles Digital Goods option based on whether or not shipping is being used on the given cart. |
|
60
|
|
|
* |
|
61
|
|
|
* @since 4.0 |
|
62
|
|
|
* |
|
63
|
|
|
* @param array $fields Payment method form fields |
|
64
|
|
|
* |
|
65
|
|
|
* @return array $fields Modified payment method form fields |
|
66
|
|
|
*/ |
|
67
|
|
|
public function dg_option_removal( $fields ) { |
|
68
|
|
|
if ( wpsc_uses_shipping() ) { |
|
69
|
|
|
// Remove DG option |
|
70
|
|
|
foreach( $fields as $index => $field ) { |
|
71
|
|
|
if ( $field['value'] === 'paypal-digital-goods' ) { |
|
72
|
|
|
unset( $fields[ $index] ); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
} else { |
|
76
|
|
|
// Remove Normal option |
|
77
|
|
|
foreach( $fields as $index => $field ) { |
|
78
|
|
|
if ( $field['value'] === 'paypal-express-checkout' ) { |
|
79
|
|
|
unset( $fields[ $index ] ); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
return $fields; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Insert the ExpessCheckout Shortcut Button |
|
88
|
|
|
* |
|
89
|
|
|
* @return void |
|
90
|
|
|
*/ |
|
91
|
|
|
public function add_ecs_button( $cart_table, $context ) { |
|
92
|
|
|
|
|
93
|
|
|
if ( wpsc_uses_shipping() || ! wpsc_is_gateway_active( 'paypal-digital-goods' ) ) { |
|
94
|
|
|
return; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
if ( 'bottom' == $context ) { |
|
98
|
|
|
return; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
if ( _wpsc_get_current_controller_name() === 'cart' ) { |
|
102
|
|
|
$url = $this->get_shortcut_url(); |
|
103
|
|
|
echo '<a class="express-checkout-button" id="pp-ecs-dg" href="'. esc_url ( $url ) .'"><img src="https://www.paypalobjects.com/webstatic/en_US/i/buttons/checkout-logo-large.png" alt="' . __( 'Check out with PayPal', 'wp-e-commerce' ) . '" /></a>'; |
|
|
|
|
|
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Return the ExpressCheckout Shortcut redirection URL |
|
109
|
|
|
* |
|
110
|
|
|
* @return void |
|
111
|
|
|
*/ |
|
112
|
|
|
public function get_shortcut_url() { |
|
113
|
|
|
$location = add_query_arg( array( |
|
114
|
|
|
'payment_gateway' => 'paypal-digital-goods', |
|
115
|
|
|
'payment_gateway_callback' => 'shortcut_process', |
|
116
|
|
|
), home_url( 'index.php' ) ); |
|
117
|
|
|
|
|
118
|
|
|
return apply_filters( 'wpsc_paypal_digital_goods_shortcut_url', $location ); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Sets the Review Callback for Review Order page. |
|
123
|
|
|
* |
|
124
|
|
|
* @param string $url |
|
125
|
|
|
* @return string |
|
126
|
|
|
*/ |
|
127
|
|
|
public function review_order_callback( $url ) { |
|
128
|
|
|
$args = array( |
|
129
|
|
|
'payment_gateway_callback' => 'review_transaction', |
|
130
|
|
|
'payment_gateway' => 'paypal-digital-goods', |
|
131
|
|
|
); |
|
132
|
|
|
$url = add_query_arg( $args, $url ); |
|
133
|
|
|
|
|
134
|
|
|
return $url; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Run the gateway hooks |
|
139
|
|
|
* |
|
140
|
|
|
* @access public |
|
141
|
|
|
* @since 4.0 |
|
142
|
|
|
* |
|
143
|
|
|
* @return void |
|
144
|
|
|
*/ |
|
145
|
|
|
public function init() { |
|
146
|
|
|
// Disable default selection |
|
147
|
|
|
add_filter( |
|
148
|
|
|
'wpsc_payment_method_form_fields', |
|
149
|
|
|
array( 'WPSC_Payment_Gateway_Paypal_Digital_Goods', 'filter_unselect_default' ), 100 , 1 |
|
150
|
|
|
); |
|
151
|
|
|
|
|
152
|
|
|
// Load DG scripts and styles |
|
153
|
|
|
add_action( 'wp_enqueue_scripts', array( 'WPSC_Payment_Gateway_Paypal_Digital_Goods', 'dg_script' ) ); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* WordPress Enqueue for the Dgital Goods Script and CSS file |
|
158
|
|
|
* |
|
159
|
|
|
* @return void |
|
160
|
|
|
* |
|
161
|
|
|
* @since 3.9 |
|
162
|
|
|
*/ |
|
163
|
|
|
public static function dg_script() { |
|
164
|
|
|
$dg_loc = array( |
|
165
|
|
|
'spinner_url' => wpsc_get_ajax_spinner(), |
|
166
|
|
|
'loading' => __( 'Loading...', 'wp-e-commerce' ), |
|
167
|
|
|
); |
|
168
|
|
|
// Checkout Page |
|
169
|
|
|
if ( wpsc_is_checkout() ) { |
|
170
|
|
|
wp_enqueue_script( 'dg-script', 'https://www.paypalobjects.com/js/external/dg.js' ); |
|
171
|
|
|
wp_enqueue_script( 'dg-script-internal', WPSC_URL . '/wpsc-components/merchant-core-v3/gateways/dg.js', array( 'jquery' ) ); |
|
172
|
|
|
wp_localize_script( 'dg-script', 'dg_loc', $dg_loc ); |
|
173
|
|
|
} |
|
174
|
|
|
// Cart Page |
|
175
|
|
|
if ( function_exists( 'wpsc_is_cart' ) && wpsc_is_cart() ) { |
|
176
|
|
|
wp_enqueue_script( 'dg-script', 'https://www.paypalobjects.com/js/external/dg.js' ); |
|
177
|
|
|
wp_enqueue_script( 'dg-script-internal', WPSC_URL . '/wpsc-components/merchant-core-v3/gateways/dgs.js', array( 'jquery' ) ); |
|
178
|
|
|
wp_localize_script( 'dg-script', 'dg_loc', $dg_loc ); |
|
179
|
|
|
} |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* No payment gateway is selected by default |
|
184
|
|
|
* |
|
185
|
|
|
* @access public |
|
186
|
|
|
* @since 3.9 |
|
187
|
|
|
* |
|
188
|
|
|
* @param array $fields |
|
189
|
|
|
* |
|
190
|
|
|
* @return array |
|
191
|
|
|
*/ |
|
192
|
|
|
public static function filter_unselect_default( $fields ) { |
|
193
|
|
|
foreach ( $fields as $i => $field ) { |
|
194
|
|
|
$fields[ $i ][ 'checked' ] = false; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
return $fields; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* Return the PayPal return URL |
|
202
|
|
|
* |
|
203
|
|
|
* @return string |
|
204
|
|
|
* |
|
205
|
|
|
* @since 3.9 |
|
206
|
|
|
*/ |
|
207
|
|
|
protected function get_return_url() { |
|
208
|
|
|
$redirect = add_query_arg( array( |
|
209
|
|
|
'sessionid' => $this->purchase_log->get( 'sessionid' ), |
|
210
|
|
|
'payment_gateway' => 'paypal-digital-goods', |
|
211
|
|
|
'payment_gateway_callback' => 'return_url_redirect', |
|
212
|
|
|
), |
|
213
|
|
|
get_option( 'transact_url' ) |
|
214
|
|
|
); |
|
215
|
|
|
return apply_filters( 'wpsc_paypal_digital_goods_return_url_redirect', $redirect, $this ); |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* PayPal Lightbox Form redirection for the Return URL |
|
220
|
|
|
* |
|
221
|
|
|
* @return void |
|
222
|
|
|
* |
|
223
|
|
|
* @since 3.9 |
|
224
|
|
|
*/ |
|
225
|
|
|
public function callback_return_url_redirect() { |
|
226
|
|
|
// Session id |
|
227
|
|
|
if ( ! isset( $_GET['sessionid'] ) ) { |
|
228
|
|
|
return; |
|
229
|
|
|
} else { |
|
230
|
|
|
$sessionid = $_GET['sessionid']; |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
// Page Styles |
|
234
|
|
|
wp_register_style( 'ppdg-iframe', plugins_url( 'dg.css', __FILE__ ) ); |
|
235
|
|
|
|
|
236
|
|
|
// Apply any filters |
|
237
|
|
|
if ( wpsc_get_customer_meta( 'ecs-' . $sessionid ) ) { |
|
238
|
|
|
add_filter( 'wpsc_paypal_express_checkout_transact_url', array( &$this, 'review_order_url' ) ); |
|
239
|
|
|
add_filter( 'wpsc_paypal_express_checkout_return_url', array( &$this, 'review_order_callback' ) ); |
|
240
|
|
|
|
|
241
|
|
|
wpsc_delete_customer_meta( 'esc-' . $sessionid ); |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
// Return a redirection page |
|
245
|
|
|
?> |
|
246
|
|
|
<html> |
|
247
|
|
|
<head> |
|
248
|
|
|
<title><?php _e( 'Processing...', 'wp-e-commerce' ); ?></title> |
|
249
|
|
|
<?php wp_print_styles( 'ppdg-iframe' ); ?> |
|
250
|
|
|
</head> |
|
251
|
|
|
<body> |
|
252
|
|
|
<div id="left_frame"> |
|
253
|
|
|
<div id="right_frame"> |
|
254
|
|
|
<p id="message"> |
|
255
|
|
|
<?php _e( 'Processing Order', 'wp-e-commerce'); ?> |
|
256
|
|
|
|
|
257
|
|
|
</p> |
|
258
|
|
|
<img src="https://www.paypal.com/en_US/i/icon/icon_animated_prog_42wx42h.gif" alt="Processing..." /> |
|
259
|
|
|
<div id="right_bottom"> |
|
260
|
|
|
<div id="left_bottom"> |
|
261
|
|
|
</div> |
|
262
|
|
|
</div> |
|
263
|
|
|
</div> |
|
264
|
|
|
</div> |
|
265
|
|
|
<script type="text/javascript"> |
|
266
|
|
|
<?php $location = $this->get_original_return_url( $sessionid ); ?> |
|
267
|
|
|
setTimeout('if (window!=top) {top.location.replace("<?php echo $location; ?>");}else{location.replace("<?php echo $location; ?>");}', 1500); |
|
268
|
|
|
</script> |
|
269
|
|
|
</body> |
|
270
|
|
|
</html> |
|
271
|
|
|
<?php |
|
272
|
|
|
exit(); |
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
|
|
/** |
|
276
|
|
|
* Return the original (real) Return URL |
|
277
|
|
|
* |
|
278
|
|
|
* @param integer $session_id |
|
279
|
|
|
* |
|
280
|
|
|
* @return string |
|
281
|
|
|
* |
|
282
|
|
|
* @since 3.9 |
|
283
|
|
|
*/ |
|
284
|
|
|
protected function get_original_return_url( $session_id ) { |
|
285
|
|
|
$transact_url = get_option( 'transact_url' ); |
|
286
|
|
|
$transact_url = apply_filters( 'wpsc_paypal_digital_goods_transact_url', $transact_url ); |
|
287
|
|
|
$transact_url = apply_filters( 'wpsc_paypal_express_checkout_transact_url', $transact_url ); |
|
288
|
|
|
|
|
289
|
|
|
$location = add_query_arg( array( |
|
290
|
|
|
'sessionid' => $session_id, |
|
291
|
|
|
'token' => $_REQUEST['token'], |
|
292
|
|
|
'PayerID' => $_REQUEST['PayerID'], |
|
293
|
|
|
'payment_gateway' => 'paypal-digital-goods', |
|
294
|
|
|
'payment_gateway_callback' => 'confirm_transaction', |
|
295
|
|
|
), |
|
296
|
|
|
$transact_url |
|
297
|
|
|
); |
|
298
|
|
|
|
|
299
|
|
|
$location = wp_validate_redirect( $location ); |
|
300
|
|
|
$location = apply_filters( 'wpsc_paypal_express_checkout_return_url', $location ); |
|
301
|
|
|
|
|
302
|
|
|
return apply_filters( 'wpsc_paypal_digital_goods_return_url', $location ); |
|
303
|
|
|
} |
|
304
|
|
|
|
|
305
|
|
|
/** |
|
306
|
|
|
* Return the Cancel URL |
|
307
|
|
|
* |
|
308
|
|
|
* @return string |
|
309
|
|
|
* |
|
310
|
|
|
* @since 3.9 |
|
311
|
|
|
*/ |
|
312
|
|
|
protected function get_cancel_url() { |
|
313
|
|
|
$redirect = add_query_arg( array( |
|
314
|
|
|
'payment_gateway' => 'paypal-digital-goods', |
|
315
|
|
|
'payment_gateway_callback' => 'cancel_url_redirect', |
|
316
|
|
|
), |
|
317
|
|
|
get_option( 'transact_url' ) |
|
318
|
|
|
); |
|
319
|
|
|
return apply_filters( 'wpsc_paypal_digital_goods_cancel_url_redirect', $redirect ); |
|
320
|
|
|
} |
|
321
|
|
|
|
|
322
|
|
|
/** |
|
323
|
|
|
* PayPal Lightbox Form redirection for the Cancel URL |
|
324
|
|
|
* |
|
325
|
|
|
* @return void |
|
326
|
|
|
* |
|
327
|
|
|
* @since 3.9 |
|
328
|
|
|
*/ |
|
329
|
|
|
public function callback_cancel_url_redirect() { |
|
330
|
|
|
// Page Styles |
|
331
|
|
|
wp_register_style( 'ppdg-iframe', plugins_url( 'dg.css', __FILE__ ) ); |
|
332
|
|
|
|
|
333
|
|
|
// Return a redirection page |
|
334
|
|
|
?> |
|
335
|
|
|
<html> |
|
336
|
|
|
<head> |
|
337
|
|
|
<title><?php _e( 'Processing...', 'wp-e-commerce' ); ?></title> |
|
338
|
|
|
<?php wp_print_styles( 'ppdg-iframe' ); ?> |
|
339
|
|
|
</head> |
|
340
|
|
|
<body> |
|
341
|
|
|
<div id="left_frame"> |
|
342
|
|
|
<div id="right_frame"> |
|
343
|
|
|
<p id="message"> |
|
344
|
|
|
<?php _e( 'Cancelling Order', 'wp-e-commerce' ); ?> |
|
345
|
|
|
</p> |
|
346
|
|
|
<img src="https://www.paypal.com/en_US/i/icon/icon_animated_prog_42wx42h.gif" alt="Processing..." /> |
|
347
|
|
|
<div id="right_bottom"> |
|
348
|
|
|
<div id="left_bottom"> |
|
349
|
|
|
</div> |
|
350
|
|
|
</div> |
|
351
|
|
|
</div> |
|
352
|
|
|
</div> |
|
353
|
|
|
<script type="text/javascript"> |
|
354
|
|
|
<?php $location = $this->get_original_cancel_url() ; ?> |
|
|
|
|
|
|
355
|
|
|
setTimeout('if (window!=top) {top.location.replace("<?php echo $location; ?>");}else{location.replace("<?php echo $location; ?>");}', 1500); |
|
356
|
|
|
</script> |
|
357
|
|
|
</body> |
|
358
|
|
|
</html> |
|
359
|
|
|
<?php |
|
360
|
|
|
exit(); |
|
361
|
|
|
} |
|
362
|
|
|
|
|
363
|
|
|
/** |
|
364
|
|
|
* Return the original (real) Cancel URL |
|
365
|
|
|
* |
|
366
|
|
|
* @return string |
|
367
|
|
|
* |
|
368
|
|
|
* @since 3.9 |
|
369
|
|
|
*/ |
|
370
|
|
|
protected function get_original_cancel_url() { |
|
371
|
|
|
return apply_filters( 'wpsc_paypal_digital_goods_cancel_url', $this->get_shopping_cart_payment_url() ); |
|
372
|
|
|
} |
|
373
|
|
|
|
|
374
|
|
|
/** |
|
375
|
|
|
* Return the notify URL |
|
376
|
|
|
* |
|
377
|
|
|
* @return string |
|
378
|
|
|
* |
|
379
|
|
|
* @since 3.9 |
|
380
|
|
|
*/ |
|
381
|
|
|
protected function get_notify_url() { |
|
382
|
|
|
$location = add_query_arg( array( |
|
383
|
|
|
'payment_gateway' => 'paypal-digital-goods', |
|
384
|
|
|
'payment_gateway_callback' => 'ipn', |
|
385
|
|
|
), home_url( 'index.php' ) ); |
|
386
|
|
|
|
|
387
|
|
|
return apply_filters( 'wpsc_paypal_express_checkout_notify_url', $location ); |
|
388
|
|
|
} |
|
389
|
|
|
|
|
390
|
|
|
/** |
|
391
|
|
|
* IPN Callback function |
|
392
|
|
|
* |
|
393
|
|
|
* @return void |
|
394
|
|
|
* |
|
395
|
|
|
* @since 3.9 |
|
396
|
|
|
*/ |
|
397
|
|
|
public function callback_ipn() { |
|
398
|
|
|
$ipn = new PHP_Merchant_Paypal_IPN( false, (bool) $this->setting->get( 'sandbox_mode', false ) ); |
|
399
|
|
|
|
|
400
|
|
|
if ( $ipn->is_verified() ) { |
|
401
|
|
|
$sessionid = $ipn->get( 'message_id' ); |
|
402
|
|
|
$this->set_purchase_log_for_callbacks( $sessionid ); |
|
403
|
|
|
|
|
404
|
|
|
if ( $ipn->is_payment_denied() ) { |
|
405
|
|
|
$this->purchase_log->set( 'processed', WPSC_Purchase_Log::PAYMENT_DECLINED ); |
|
406
|
|
|
} elseif ( $ipn->is_payment_refunded() ) { |
|
407
|
|
|
$this->purchase_log->set( 'processed', WPSC_Purchase_Log::REFUNDED ); |
|
408
|
|
|
} elseif ( $ipn->is_payment_completed() ) { |
|
409
|
|
|
$this->purchase_log->set( 'processed', WPSC_Purchase_Log::ACCEPTED_PAYMENT ); |
|
410
|
|
|
} elseif ( $ipn->is_payment_pending() ) { |
|
411
|
|
|
if ( $ipn->is_payment_refund_pending() ) |
|
412
|
|
|
$this->purchase_log->set( 'processed', WPSC_Purchase_Log::REFUND_PENDING ); |
|
413
|
|
|
else |
|
414
|
|
|
$this->purchase_log->set( 'processed', WPSC_Purchase_Log::ORDER_RECEIVED ); |
|
415
|
|
|
} |
|
416
|
|
|
|
|
417
|
|
|
$this->purchase_log->save(); |
|
418
|
|
|
transaction_results( $sessionid, false ); |
|
419
|
|
|
} |
|
420
|
|
|
|
|
421
|
|
|
exit; |
|
422
|
|
|
} |
|
423
|
|
|
|
|
424
|
|
|
|
|
425
|
|
|
/** |
|
426
|
|
|
* Confirm Transaction Callback |
|
427
|
|
|
* |
|
428
|
|
|
* @return null |
|
429
|
|
|
* |
|
430
|
|
|
* @since 3.9 |
|
431
|
|
|
*/ |
|
432
|
|
|
public function callback_confirm_transaction() { |
|
433
|
|
|
|
|
434
|
|
|
if ( ! isset( $_GET['sessionid'] ) || ! isset( $_GET['token'] ) || ! isset( $_GET['PayerID'] ) ) { |
|
435
|
|
|
return; |
|
436
|
|
|
} |
|
437
|
|
|
|
|
438
|
|
|
$this->set_purchase_log_for_callbacks(); |
|
439
|
|
|
|
|
440
|
|
|
$this->callback_process_confirmed_payment(); |
|
441
|
|
|
} |
|
442
|
|
|
|
|
443
|
|
|
/** |
|
444
|
|
|
* Process the transaction through the PayPal APIs |
|
445
|
|
|
* |
|
446
|
|
|
* @since 3.9 |
|
447
|
|
|
*/ |
|
448
|
|
|
public function callback_process_confirmed_payment() { |
|
449
|
|
|
$args = array_map( 'urldecode', $_GET ); |
|
450
|
|
|
extract( $args, EXTR_SKIP ); |
|
|
|
|
|
|
451
|
|
|
|
|
452
|
|
|
if ( ! isset( $sessionid ) || ! isset( $token ) || ! isset( $PayerID ) ) { |
|
453
|
|
|
return; |
|
454
|
|
|
} |
|
455
|
|
|
|
|
456
|
|
|
$this->set_purchase_log_for_callbacks(); |
|
457
|
|
|
|
|
458
|
|
|
$total = $this->convert( $this->purchase_log->get( 'totalprice' ) ); |
|
459
|
|
|
$options = array( |
|
460
|
|
|
'token' => $token, |
|
461
|
|
|
'payer_id' => $PayerID, |
|
462
|
|
|
'message_id' => $this->purchase_log->get( 'id' ), |
|
463
|
|
|
'invoice' => $this->purchase_log->get( 'sessionid' ), |
|
464
|
|
|
); |
|
465
|
|
|
$options += $this->checkout_data->get_gateway_data(); |
|
466
|
|
|
$options += $this->purchase_log->get_gateway_data( parent::get_currency_code(), $this->get_currency_code() ); |
|
|
|
|
|
|
467
|
|
|
|
|
468
|
|
|
if ( $this->setting->get( 'ipn', false ) ) { |
|
469
|
|
|
$options['notify_url'] = $this->get_notify_url(); |
|
470
|
|
|
} |
|
471
|
|
|
|
|
472
|
|
|
// GetExpressCheckoutDetails |
|
473
|
|
|
$details = $this->gateway->get_details_for( $token ); |
|
474
|
|
|
$this->log_payer_details( $details ); |
|
475
|
|
|
|
|
476
|
|
|
$response = $this->gateway->purchase( $options ); |
|
477
|
|
|
$this->log_protection_status( $response ); |
|
478
|
|
|
$location = remove_query_arg( 'payment_gateway_callback' ); |
|
479
|
|
|
|
|
480
|
|
|
if ( $response->has_errors() ) { |
|
481
|
|
|
wpsc_update_customer_meta( 'paypal_express_checkout_errors', $response->get_errors() ); |
|
482
|
|
|
$location = add_query_arg( array( 'payment_gateway_callback' => 'display_paypal_error' ) ); |
|
483
|
|
|
} elseif ( $response->is_payment_completed() || $response->is_payment_pending() ) { |
|
484
|
|
|
$location = remove_query_arg( 'payment_gateway' ); |
|
485
|
|
|
|
|
486
|
|
|
if ( $response->is_payment_completed() ) { |
|
487
|
|
|
$this->purchase_log->set( 'processed', WPSC_Purchase_Log::ACCEPTED_PAYMENT ); |
|
488
|
|
|
} else { |
|
489
|
|
|
$this->purchase_log->set( 'processed', WPSC_Purchase_Log::ORDER_RECEIVED ); |
|
490
|
|
|
} |
|
491
|
|
|
|
|
492
|
|
|
$this->purchase_log->set( 'transactid', $response->get( 'transaction_id' ) ) |
|
493
|
|
|
->set( 'date', time() ) |
|
494
|
|
|
->save(); |
|
495
|
|
|
} else { |
|
496
|
|
|
$location = add_query_arg( array( 'payment_gateway_callback' => 'display_generic_error' ) ); |
|
497
|
|
|
} |
|
498
|
|
|
|
|
499
|
|
|
wp_redirect( esc_url_raw( $location ) ); |
|
500
|
|
|
exit; |
|
501
|
|
|
|
|
502
|
|
|
} |
|
503
|
|
|
|
|
504
|
|
|
/** |
|
505
|
|
|
* PayPal Lightbox Form redirection for the Error Page |
|
506
|
|
|
* |
|
507
|
|
|
* @return void |
|
508
|
|
|
* |
|
509
|
|
|
* @since 3.9 |
|
510
|
|
|
*/ |
|
511
|
|
|
public function callback_display_paypal_error_redirect() { |
|
512
|
|
|
// Redirect Location |
|
513
|
|
|
$location = esc_url( add_query_arg( array( |
|
514
|
|
|
'payment_gateway' => 'paypal-digital-goods', |
|
515
|
|
|
'payment_gateway_callback' => 'display_paypal_error', |
|
516
|
|
|
), base64_decode( $_GET['return_url'] ) ) ); |
|
517
|
|
|
|
|
518
|
|
|
// Page Styles |
|
519
|
|
|
wp_register_style( 'ppdg-iframe', plugins_url( 'dg.css', __FILE__ ) ); |
|
520
|
|
|
|
|
521
|
|
|
// Return a redirection page |
|
522
|
|
|
?> |
|
523
|
|
|
<html> |
|
524
|
|
|
<head> |
|
525
|
|
|
<title><?php __( 'Processing...', 'wp-e-commerce' ); ?></title> |
|
526
|
|
|
<?php wp_print_styles( 'ppdg-iframe' ); ?> |
|
527
|
|
|
</head> |
|
528
|
|
|
<body> |
|
529
|
|
|
<div id="left_frame"> |
|
530
|
|
|
<div id="right_frame"> |
|
531
|
|
|
<p id="message"> |
|
532
|
|
|
<?php _e( 'Processing Order', 'wp-e-commerce'); ?> |
|
533
|
|
|
</p> |
|
534
|
|
|
<img src="https://www.paypal.com/en_US/i/icon/icon_animated_prog_42wx42h.gif" alt="Processing..." /> |
|
535
|
|
|
<div id="right_bottom"> |
|
536
|
|
|
<div id="left_bottom"> |
|
537
|
|
|
</div> |
|
538
|
|
|
</div> |
|
539
|
|
|
</div> |
|
540
|
|
|
</div> |
|
541
|
|
|
<script type="text/javascript"> |
|
542
|
|
|
setTimeout('if (window!=top) {top.location.replace("<?php echo $location; ?>");}else{location.replace("<?php echo $location; ?>");}', 1500); |
|
543
|
|
|
</script> |
|
544
|
|
|
</body> |
|
545
|
|
|
</html> |
|
546
|
|
|
<?php |
|
547
|
|
|
exit(); |
|
548
|
|
|
|
|
|
|
|
|
|
549
|
|
|
|
|
550
|
|
|
} |
|
551
|
|
|
|
|
552
|
|
|
public function callback_display_paypal_error() { |
|
553
|
|
|
add_filter( 'wpsc_get_transaction_html_output', array( $this, 'filter_paypal_error_page' ) ); |
|
554
|
|
|
} |
|
555
|
|
|
|
|
556
|
|
|
public function callback_display_generic_error() { |
|
557
|
|
|
add_filter( 'wpsc_get_transaction_html_output', array( $this, 'filter_generic_error_page' ) ); |
|
558
|
|
|
} |
|
559
|
|
|
|
|
560
|
|
|
/** |
|
561
|
|
|
* Error Page Template |
|
562
|
|
|
* |
|
563
|
|
|
* @since 3.9 |
|
564
|
|
|
*/ |
|
565
|
|
|
public function filter_paypal_error_page() { |
|
566
|
|
|
$errors = wpsc_get_customer_meta( 'paypal_express_checkout_errors' ); |
|
567
|
|
|
ob_start(); |
|
568
|
|
|
?> |
|
569
|
|
|
<p> |
|
570
|
|
|
<?php _e( 'Sorry, your transaction could not be processed by PayPal. Please contact the site administrator. The following errors are returned:', 'wp-e-commerce' ); ?> |
|
571
|
|
|
</p> |
|
572
|
|
|
<ul> |
|
573
|
|
|
<?php foreach ( $errors as $error ): ?> |
|
574
|
|
|
<li><?php echo esc_html( $error['details'] ) ?> (<?php echo esc_html( $error['code'] ); ?>)</li> |
|
575
|
|
|
<?php endforeach; ?> |
|
576
|
|
|
</ul> |
|
577
|
|
|
<p><a href="<?php echo esc_url( $this->get_shopping_cart_payment_url() ); ?>"><?php _e( 'Click here to go back to the checkout page.', 'wp-e-commerce' ) ?></a></p> |
|
578
|
|
|
<?php |
|
579
|
|
|
$output = apply_filters( 'wpsc_paypal_express_checkout_gateway_error_message', ob_get_clean(), $errors ); |
|
580
|
|
|
return $output; |
|
581
|
|
|
} |
|
582
|
|
|
|
|
583
|
|
|
/** |
|
584
|
|
|
* Generic Error Page Template |
|
585
|
|
|
* |
|
586
|
|
|
* @since 3.9 |
|
587
|
|
|
*/ |
|
588
|
|
|
public function filter_generic_error_page() { |
|
589
|
|
|
ob_start(); |
|
590
|
|
|
?> |
|
591
|
|
|
<p><?php _e( 'Sorry, but your transaction could not be processed by PayPal for some reason. Please contact the site administrator.', 'wp-e-commerce' ); ?></p> |
|
592
|
|
|
<p><a href="<?php echo esc_attr( $this->get_shopping_cart_payment_url() ); ?>"><?php _e( 'Click here to go back to the checkout page.', 'wp-e-commerce' ) ?></a></p> |
|
593
|
|
|
<?php |
|
594
|
|
|
$output = apply_filters( 'wpsc_paypal_express_checkout_generic_error_message', ob_get_clean() ); |
|
595
|
|
|
return $output; |
|
596
|
|
|
} |
|
597
|
|
|
|
|
598
|
|
|
/** |
|
599
|
|
|
* Settings Form Template |
|
600
|
|
|
* |
|
601
|
|
|
* @since 3.9 |
|
602
|
|
|
*/ |
|
603
|
|
|
public function setup_form() { |
|
604
|
|
|
$paypal_currency = $this->get_currency_code(); |
|
605
|
|
|
?> |
|
606
|
|
|
<!-- Account Credentials --> |
|
607
|
|
|
<tr> |
|
608
|
|
|
<td colspan="2"> |
|
609
|
|
|
<h4><?php _e( 'Account Credentials', 'wp-e-commerce' ); ?></h4> |
|
610
|
|
|
</td> |
|
611
|
|
|
</tr> |
|
612
|
|
|
<tr> |
|
613
|
|
|
<td> |
|
614
|
|
|
<label for="wpsc-paypal-express-api-username"><?php _e( 'API Username', 'wp-e-commerce' ); ?></label> |
|
615
|
|
|
</td> |
|
616
|
|
|
<td> |
|
617
|
|
|
<input type="text" name="<?php echo esc_attr( $this->setting->get_field_name( 'api_username' ) ); ?>" value="<?php echo esc_attr( $this->setting->get( 'api_username' ) ); ?>" id="wpsc-paypal-express-api-username" /> |
|
618
|
|
|
</td> |
|
619
|
|
|
</tr> |
|
620
|
|
|
<tr> |
|
621
|
|
|
<td> |
|
622
|
|
|
<label for="wpsc-paypal-express-api-password"><?php _e( 'API Password', 'wp-e-commerce' ); ?></label> |
|
623
|
|
|
</td> |
|
624
|
|
|
<td> |
|
625
|
|
|
<input type="text" name="<?php echo esc_attr( $this->setting->get_field_name( 'api_password' ) ); ?>" value="<?php echo esc_attr( $this->setting->get( 'api_password' ) ); ?>" id="wpsc-paypal-express-api-password" /> |
|
626
|
|
|
</td> |
|
627
|
|
|
</tr> |
|
628
|
|
|
<tr> |
|
629
|
|
|
<td> |
|
630
|
|
|
<label for="wpsc-paypal-express-api-signature"><?php _e( 'API Signature', 'wp-e-commerce' ); ?></label> |
|
631
|
|
|
</td> |
|
632
|
|
|
<td> |
|
633
|
|
|
<input type="text" name="<?php echo esc_attr( $this->setting->get_field_name( 'api_signature' ) ); ?>" value="<?php echo esc_attr( $this->setting->get( 'api_signature' ) ); ?>" id="wpsc-paypal-express-api-signature" /> |
|
634
|
|
|
</td> |
|
635
|
|
|
</tr> |
|
636
|
|
|
<tr> |
|
637
|
|
|
<td> |
|
638
|
|
|
<label><?php _e( 'Sandbox Mode', 'wp-e-commerce' ); ?></label> |
|
639
|
|
|
</td> |
|
640
|
|
|
<td> |
|
641
|
|
|
<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> |
|
642
|
|
|
<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> |
|
643
|
|
|
</td> |
|
644
|
|
|
</tr> |
|
645
|
|
|
<tr> |
|
646
|
|
|
<td> |
|
647
|
|
|
<label><?php _e( 'IPN', 'wp-e-commerce' ); ?></label> |
|
648
|
|
|
</td> |
|
649
|
|
|
<td> |
|
650
|
|
|
<label><input <?php checked( $this->setting->get( 'ipn' ) ); ?> type="radio" name="<?php echo esc_attr( $this->setting->get_field_name( 'ipn' ) ); ?>" value="1" /> <?php _e( 'Yes', 'wp-e-commerce' ); ?></label> |
|
651
|
|
|
<label><input <?php checked( (bool) $this->setting->get( 'ipn' ), false ); ?> type="radio" name="<?php echo esc_attr( $this->setting->get_field_name( 'ipn' ) ); ?>" value="0" /> <?php _e( 'No', 'wp-e-commerce' ); ?></label> |
|
652
|
|
|
</td> |
|
653
|
|
|
</tr> |
|
654
|
|
|
|
|
655
|
|
|
<!-- Cart Customization --> |
|
656
|
|
|
<tr> |
|
657
|
|
|
<td colspan="2"> |
|
658
|
|
|
<label><h4><?php _e( 'Cart Customization', 'wp-e-commerce'); ?></h4></label> |
|
659
|
|
|
</td> |
|
660
|
|
|
</tr> |
|
661
|
|
|
<tr> |
|
662
|
|
|
<td> |
|
663
|
|
|
<label for="wpsc-paypal-express-cart-logo"><?php _e( 'Merchant Logo', 'wp-e-commerce' ); ?></label> |
|
664
|
|
|
</td> |
|
665
|
|
|
<td> |
|
666
|
|
|
<input type="text" name="<?php echo esc_attr( $this->setting->get_field_name( 'cart_logo' ) ); ?>" value="<?php echo esc_attr( $this->setting->get( 'cart_logo' ) ); ?>" id="wpsc-paypal-express-cart-logo" /><br><span class="small description"><?php _e( 'The image must be stored in a HTTPS Server. Limit the image to 190 pixels wide by 60 pixels high.', 'wp-e-commerce' ); ?></span> |
|
667
|
|
|
</td> |
|
668
|
|
|
</tr> |
|
669
|
|
|
<tr> |
|
670
|
|
|
<td> |
|
671
|
|
|
<label for="wpsc-paypal-express-cart-border"><?php _e( 'Cart Border Color', 'wp-e-commerce' ); ?></label> |
|
672
|
|
|
</td> |
|
673
|
|
|
<td> |
|
674
|
|
|
<input type="text" name="<?php echo esc_attr( $this->setting->get_field_name( 'cart_border' ) ); ?>" value="<?php echo esc_attr( $this->setting->get( 'cart_border' ) ); ?>" id="wpsc-paypal-express-cart-border" /> |
|
675
|
|
|
</td> |
|
676
|
|
|
</tr> |
|
677
|
|
|
|
|
678
|
|
|
<!-- Currency Conversion --> |
|
679
|
|
|
<?php if ( ! $this->is_currency_supported() ): ?> |
|
680
|
|
|
<tr> |
|
681
|
|
|
<td colspan="2"> |
|
682
|
|
|
<h4><?php _e( 'Currency Conversion', 'wp-e-commerce' ); ?></h4> |
|
683
|
|
|
</td> |
|
684
|
|
|
</tr> |
|
685
|
|
|
<tr> |
|
686
|
|
|
<td colspan="2"> |
|
687
|
|
|
<p><?php _e( "Your base currency is currently not accepted by PayPal. As a result, before a payment request is sent to PayPal, WP eCommerce has to convert the amounts into one of PayPal's supported currencies. Please select your preferred currency below.", 'wp-e-commerce' ); ?></p> |
|
688
|
|
|
</td> |
|
689
|
|
|
</tr> |
|
690
|
|
|
<tr> |
|
691
|
|
|
<td> |
|
692
|
|
|
<label for "wpsc-paypal-express-currency"><?php _e( 'PayPal Currency', 'wp-e-commerce' ); ?></label> |
|
693
|
|
|
</td> |
|
694
|
|
|
<td> |
|
695
|
|
|
<select name="<?php echo esc_attr( $this->setting->get_field_name( 'currency' ) ); ?>" id="wpsc-paypal-express-currency"> |
|
696
|
|
|
<?php foreach ($this->gateway->get_supported_currencies() as $currency): ?> |
|
697
|
|
|
<option <?php selected( $currency, $paypal_currency ); ?> value="<?php echo esc_attr( $currency ); ?>"><?php echo esc_html( $currency ); ?></option> |
|
698
|
|
|
<?php endforeach ?> |
|
699
|
|
|
</select> |
|
700
|
|
|
</td> |
|
701
|
|
|
</tr> |
|
702
|
|
|
<?php endif ?> |
|
703
|
|
|
|
|
704
|
|
|
<!-- Checkout Shortcut --> |
|
705
|
|
|
<tr> |
|
706
|
|
|
<td colspan="2"> |
|
707
|
|
|
<h4><?php _e( 'Express Checkout Shortcut', 'wp-e-commerce' ); ?></h4> |
|
708
|
|
|
</td> |
|
709
|
|
|
</tr> |
|
710
|
|
|
<tr> |
|
711
|
|
|
<td> |
|
712
|
|
|
<label><?php _e( 'Enable Shortcut', 'wp-e-commerce' ); ?></label> |
|
713
|
|
|
</td> |
|
714
|
|
|
<td> |
|
715
|
|
|
<label><input <?php checked( $this->setting->get( 'shortcut' ) ); ?> type="radio" name="<?php echo esc_attr( $this->setting->get_field_name( 'shortcut' ) ); ?>" value="1" /> <?php _e( 'Yes', 'wp-e-commerce' ); ?></label> |
|
716
|
|
|
<label><input <?php checked( (bool) $this->setting->get( 'shortcut' ), false ); ?> type="radio" name="<?php echo esc_attr( $this->setting->get_field_name( 'shortcut' ) ); ?>" value="0" /> <?php _e( 'No', 'wp-e-commerce' ); ?></label> |
|
717
|
|
|
</td> |
|
718
|
|
|
</tr> |
|
719
|
|
|
|
|
720
|
|
|
<!-- Error Logging --> |
|
721
|
|
|
<tr> |
|
722
|
|
|
<td colspan="2"> |
|
723
|
|
|
<h4><?php _e( 'Error Logging', 'wp-e-commerce' ); ?></h4> |
|
724
|
|
|
</td> |
|
725
|
|
|
</tr> |
|
726
|
|
|
<tr> |
|
727
|
|
|
<td> |
|
728
|
|
|
<label><?php _e( 'Enable Debugging', 'wp-e-commerce' ); ?></label> |
|
729
|
|
|
</td> |
|
730
|
|
|
<td> |
|
731
|
|
|
<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> |
|
732
|
|
|
<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> |
|
733
|
|
|
</td> |
|
734
|
|
|
</tr> |
|
735
|
|
|
<?php |
|
736
|
|
|
} |
|
737
|
|
|
|
|
738
|
|
|
/** |
|
739
|
|
|
* Process the SetExpressCheckout API Call |
|
740
|
|
|
* |
|
741
|
|
|
* @param array $args |
|
742
|
|
|
* @return void |
|
743
|
|
|
* |
|
744
|
|
|
* @since 3.9 |
|
745
|
|
|
*/ |
|
746
|
|
|
public function process( $args = array() ) { |
|
747
|
|
|
$total = $this->convert( $this->purchase_log->get( 'totalprice' ) ); |
|
748
|
|
|
|
|
749
|
|
|
$options = array( |
|
750
|
|
|
'return_url' => $this->get_return_url(), |
|
751
|
|
|
'message_id' => $this->purchase_log->get( 'id' ), |
|
752
|
|
|
'invoice' => $this->purchase_log->get( 'sessionid' ), |
|
753
|
|
|
'address_override' => 1, |
|
754
|
|
|
); |
|
755
|
|
|
$options += $this->checkout_data->get_gateway_data(); |
|
756
|
|
|
$options += $this->purchase_log->get_gateway_data( parent::get_currency_code(), $this->get_currency_code() ); |
|
|
|
|
|
|
757
|
|
|
|
|
758
|
|
|
if ( $this->setting->get( 'ipn', false ) ) { |
|
759
|
|
|
$options['notify_url'] = $this->get_notify_url(); |
|
760
|
|
|
} |
|
761
|
|
|
|
|
762
|
|
|
$response = $this->gateway->setup_purchase( $options ); |
|
763
|
|
|
|
|
764
|
|
|
if ( $response->is_successful() ) { |
|
765
|
|
|
$url = ( $this->setting->get( 'sandbox_mode' ) ? self::SANDBOX_URL : self::LIVE_URL ) . $response->get( 'token' ); |
|
766
|
|
|
} else { |
|
767
|
|
|
|
|
768
|
|
|
// SetExpressCheckout Failure |
|
769
|
|
|
$this->log_error( $response ); |
|
770
|
|
|
wpsc_update_customer_meta( 'paypal_digital_goods_errors', $response->get_errors() ); |
|
771
|
|
|
|
|
772
|
|
|
$url = add_query_arg( array( |
|
773
|
|
|
'payment_gateway' => 'paypal-digital-goods', |
|
774
|
|
|
'payment_gateway_callback' => 'display_paypal_error_redirect', |
|
775
|
|
|
'return_url' => base64_encode( $this->get_return_url() ), |
|
776
|
|
|
), $this->get_return_url() ); |
|
777
|
|
|
} |
|
778
|
|
|
|
|
779
|
|
|
if( ! isset( $args['return_only'] ) || $args['return_only'] !== true ) { |
|
780
|
|
|
echo( $url ); |
|
781
|
|
|
exit; |
|
782
|
|
|
} |
|
783
|
|
|
|
|
784
|
|
|
return $url; |
|
785
|
|
|
} |
|
786
|
|
|
} |
|
787
|
|
|
|