-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass-edd-simple-after-payment-redirect.php
401 lines (340 loc) · 12.1 KB
/
class-edd-simple-after-payment-redirect.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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
<?php
/**
* Plugin Name: EDD Redirect after payment
* Plugin URI: https://github.com/brainstormforce/edd-simple-after-payment-redirect
* Description: Redirect to a custom URL after successful purchase.
* Author: Pratik Chaskar
* Author URI: https://pratikchaskar.com/
* Text Domain: edd-simple-after-payment-redirect
* Domain Path: /languages
* Version: 1.0.5
*
* PHP version 7
*
* @category PHP
* @package Edd_Simple_After_Payment_Redirect
* @author Display Name <[email protected]>
* @license GPLv2 or later https://brainstormforce.com
* @link https://brainstormforce.com
*/
/**
* Exit if accessed directly.
*/
defined( 'ABSPATH' ) || exit;
/**
* Edd_Simple_After_Payment_Redirect
*
* @category PHP
* @package Edd_Simple_After_Payment_Redirect
* @author Display Name <[email protected]>
* @license GPLv2 or later https://brainstormforce.com
* @link https://brainstormforce.com
*/
class Edd_Simple_After_Payment_Redirect {
/**
* Member Variable
*
* @var instance
*/
private static $instance = null;
/**
* Redirect
*
* @var bool
*/
private $redirect = false;
/**
* Instance
*
* @return object
*/
public static function instance() {
if ( ! isset( self::$instance ) ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* __construct
*
* @return void
*/
private function __construct() {
add_action( 'plugins_loaded', array( $this, 'maybe_load_plugin' ), 9999, 0 );
}
/**
* Load plugin only when EDD is active.
*
* @return void
*/
public function maybe_load_plugin() {
if ( ! class_exists( 'Easy_Digital_Downloads' ) ) {
add_action(
'admin_notices',
function() {
?>
<div class="notice notice-error is-dismissible">
<p><?php esc_html_e( 'EDD Redirect after payment requires Easy Digital Downloads to be installed and activated.', 'edd-simple-after-payment-redirect' ); ?></p>
</div>
<?php
}
);
} else {
add_action( 'edd_complete_purchase', array( $this, 'process_standard_payment' ) );
add_filter( 'edd_payment_confirm_paypal', array( $this, 'process_paypal_standard' ) );
add_action( 'template_redirect', array( $this, 'process_offsite_payment' ) );
add_action( 'edd_meta_box_fields', array( $this, 'edd_external_product_render_field' ), 90 );
add_filter( 'edd_metabox_fields_save', array( $this, 'edd_external_product_save' ) );
add_filter( 'edd_metabox_save__edd_after_payment_redirect', array( $this, 'edd_external_product_metabox_save' ) );
}
}
/**
* Process_standard_payment
*
* @param mixed $payment_id get cart items from payment ID.
*
* @return object
*/
public function process_standard_payment( $payment_id ) {
// get cart items from payment ID.
$cart_items = edd_get_payment_meta_cart_details( $payment_id );
// get the download ID from cart items array.
if ( $cart_items ) {
foreach ( $cart_items as $download ) {
$download_id = $download['id'];
}
}
// return if more than one item exists in cart. The default purchase confirmation will be used.
if ( count( $cart_items ) > 1 ) {
return;
}
$payment_status = edd_get_payment_status( absint( $payment_id ), true );
if ( 'Pending' === $payment_status || 'private' === $payment_status ) {
return false;
}
// redirect by default to the normal EDD success page.
$this->redirect = apply_filters( 'edd_csr_redirect', get_permalink( edd_get_option( 'success_page' ) ), $download_id );
$this->redirect = get_post_meta( $download_id, '_edd_after_payment_redirect', true );
if ( false === $this->redirect || '' === $this->redirect ) {
return;
}
$customer_id = edd_get_payment_customer_id( $payment_id );
$customer = new EDD_Customer( $customer_id );
$this->redirect = add_query_arg(
array(
'username' => $customer->name,
'useremail' => $customer->email,
'payment_id' => $payment_id,
),
$this->redirect
);
add_filter( 'edd_get_success_page_uri', array( $this, 'get_redirect_url' ) );
add_filter( 'edd_success_page_url', array( $this, 'get_redirect_url' ) );
}
/**
* Process_paypal_standard
*
* @param mixed $content return if no payment-id query string or purchase session.
*
* @return string
*/
public function process_paypal_standard( $content ) {
// return if no payment-id query string or purchase session.
if ( ! isset( $_GET['payment-id'] ) && ! edd_get_purchase_session() ) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended
return $content;
}
// get payment ID from the query string.
$payment_id = isset( $_GET['payment-id'] ) ? absint( $_GET['payment-id'] ) : false; //phpcs:ignore WordPress.Security.NonceVerification.Recommended
// no query string, get the payment ID from the purchase session.
if ( ! $payment_id ) {
$session = edd_get_purchase_session();
$payment_id = edd_get_purchase_id_by_key( $session['purchase_key'] );
}
// get cart items from payment ID.
$cart_items = edd_get_payment_meta_cart_details( $payment_id );
// get the download ID from cart items array.
if ( $cart_items ) {
foreach ( $cart_items as $download ) {
$download_id = $download['id'];
}
}
// return if more than one item exists in cart. The default purchase confirmation will be used.
if ( count( $cart_items ) > 1 ) {
return;
}
// redirect by default to the normal EDD success page.
$this->redirect = apply_filters( 'edd_csr_redirect', get_permalink( edd_get_option( 'success_page' ) ), $download_id );
$this->redirect = get_post_meta( $download_id, '_edd_after_payment_redirect', true );
if ( false === $this->redirect || '' === $this->redirect ) {
return;
}
$customer_id = edd_get_payment_customer_id( $payment_id );
$customer = new EDD_Customer( $customer_id );
$this->redirect = add_query_arg(
array(
'username' => $customer->name,
'useremail' => $customer->email,
),
$this->redirect
);
// if payment is pending or private (buy now button behavior), load the payment processing template.
if ( $payment && ( 'pending' === $payment->post_status || 'private' === $payment->post_status ) ) {
return false;
} elseif ( $payment && 'publish' === $payment->post_status ) {
// payment is complete, it can redirect straight away.
wp_safe_redirect( $this->get_redirect_url(), 301 );
exit;
}
return $content;
}
/**
* Payment processing template
* The idea here is to give the website enough time to receive instructions from PayPal as per https://github.com/easydigitaldownloads/Easy-Digital-Downloads/issues/1839
* You should always add the neccessary checks on the redirected page if you are going to show the customer sensitive information
*
* Similar to EDD's /templates/payment-processing.php file
*
* @credits - https://github.com/easydigitaldownloads/edd-conditional-success-redirects/blob/master/includes/class-process-redirects.php#L68-L89
*
* @since 1.0.4
* @return void
*/
public function payment_processing() {
$redirect = $this->get_redirect_url();
?>
<div id="edd-payment-processing">
<p><?php printf( wp_kses_post( 'Your purchase is processing. This page will reload automatically in 8 seconds. If it does not, click <a href="%s">here</a>.', 'edd' ), esc_url( $redirect ) ); ?>
<span class="edd-cart-ajax"><i class="edd-icon-spinner edd-icon-spin"></i></span>
<script type="text/javascript">setTimeout(function(){ window.location = '<?php echo esc_url( $redirect ); ?>'; }, 8000);</script>
</div>
<?php
}
/**
* Process_offsite_payment
*
* @return void
*/
public function process_offsite_payment() {
// check if we have query string and on purchase confirmation page.
if ( ! is_page( edd_get_option( 'success_page' ) ) ) {
return;
}
// get the purchase session.
$purchase_session = edd_get_purchase_session();
if ( ! $purchase_session ) {
return false;
}
$cart_items = $purchase_session['downloads'];
// get the download ID from cart items array.
if ( $cart_items ) {
foreach ( $cart_items as $download ) {
$download_id = $download['id'];
}
}
// get payment ID from the query string.
$payment_id = isset( $_GET['payment_id'] ) ? absint( $_GET['payment_id'] ) : false; //phpcs:ignore WordPress.Security.NonceVerification.Recommended
// no query string, get the payment ID from the purchase session.
if ( ! $payment_id ) {
$session = edd_get_purchase_session();
$payment_id = edd_get_purchase_id_by_key( $session['purchase_key'] );
}
$payment_status = edd_get_payment_status( $payment_id, true );
if ( 'Pending' === $payment_status || 'private' === $payment_status ) {
return false;
}
// return if more than one item exists in cart. The default purchase confirmation will be used.
if ( count( $cart_items ) > 1 ) {
return false;
}
// redirect by default to the normal EDD success page.
$this->redirect = apply_filters( 'edd_csr_redirect', get_permalink( edd_get_option( 'success_page' ) ), $download_id );
$this->redirect = get_post_meta( $download_id, '_edd_after_payment_redirect', true );
$customer_id = edd_get_payment_customer_id( $payment_id );
$customer = new EDD_Customer( $customer_id );
$this->redirect = add_query_arg(
array(
'username' => $customer->name,
'useremail' => $customer->email,
),
$this->redirect
);
// normal offsite redirect.
if ( isset( $_GET['payment-confirmation'] ) && $_GET['payment-confirmation'] ) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended
// return if using PayPal express. Customer needs to "confirm" the payment first before redirecting.
// also redirects if paypal standard was used. It has its own processing function.
if ( 'paypalexpress' === $_GET['payment-confirmation'] || 'paypal' === $_GET['payment-confirmation'] ) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended
return;
}
// redirect.
wp_safe_redirect( $this->get_redirect_url(), 301 );
exit;
}
// PayPal Express.
// Customer must "confirm" purchase.
if ( isset( $_GET['token'] ) && $_GET['token'] && ! isset( $_GET['payment-confirmation'] ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended
// redirect.
wp_safe_redirect( $this->get_redirect_url(), 301 );
exit;
}
}
/**
* Get_redirect_url
*
* @return object
*/
public function get_redirect_url() {
return $this->redirect;
}
/**
* After Purchase redirect URL Field.
*
* Adds field do the EDD Downloads meta box for specifying the "After Purchase redirect URL".
*
* @param integer $post_id Download (Post) ID.
*
* @since 1.0.0
*
* @return void
*/
public function edd_external_product_render_field( $post_id ) {
$edd_after_payment_redirect = get_post_meta( $post_id, '_edd_after_payment_redirect', true );
?>
<p><strong><?php esc_html_e( 'After successful purchase redirect:', 'edd-simple-after-payment-redirect' ); ?></strong></p>
<label for="edd_after_payment_redirect">
<input type="text" name="_edd_after_payment_redirect" id="edd_after_payment_redirect" value="<?php echo esc_attr( $edd_after_payment_redirect ); ?>" size="80" placeholder="http://"/>
<br/><?php esc_html_e( 'The external URL (including http://) to redirect to a URL after successful purchase. Leave blank if the product redirect not required.', 'edd-simple-after-payment-redirect' ); ?>
</label>
<?php
}
/**
* Add the _edd_after_payment_redirect field to the list of saved product fields.
*
* @param array $fields The default product fields list.
*
* @since 1.0.0
*
* @return array The updated product fields list.
*/
public function edd_external_product_save( $fields ) {
// Add our field.
$fields[] = '_edd_after_payment_redirect';
// Return the fields array.
return $fields;
}
/**
* Sanitize metabox field to only accept URLs.
*
* @param mixed $new Convert to raw URL to save into wp_postmeta table.
*
* @since 1.0.0
*
* @return object
*/
public function edd_external_product_metabox_save( $new ) {
// Convert to raw URL to save into wp_postmeta table.
$new = esc_url_raw( $_POST['_edd_after_payment_redirect'] ); //phpcs:ignore
// Return URL.
return $new;
}
}
add_action( 'plugins_loaded', 'Edd_Simple_After_Payment_Redirect::instance' );