Repository navigation
Expand file tree
/
Copy pathwebhook.php
More file actions
561 lines (465 loc) · 22.3 KB
/
Copy pathwebhook.php
File metadata and controls
561 lines (465 loc) · 22.3 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
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
//set this in your wp-config.php for debugging
//define( 'PMPRO_CCBILL_DEBUG', true );
global $wpdb, $gateway_environment, $logstr;
$logstr = ''; //will put debug info here and write to ccbill-webhook.txt
if ( ! function_exists( 'pmpro_getParam' ) ){
return;
}
$event_type = pmpro_getParam('eventType', 'REQUEST');
$response = array();
foreach ( $_REQUEST as $key => $value ) {
$response[ $key ] = sanitize_text_field( $value );
}
// Make sure that the response matches the account number saved to ensure it's for the same account/subscription.
$stored_account_number = trim( (string) get_option( 'pmpro_ccbill_account_number' ) );
$received_account_number = trim( (string) $response['clientAccnum'] );
if ( empty( $received_account_number ) || $received_account_number !== $stored_account_number ) {
pmpro_ccbill_webhook_log( sprintf(
"There was an error processing your CCBill webhook. Account number doesn't match the one on record. Received: '%s', Expected: '%s'.",
$received_account_number,
$stored_account_number
) );
pmpro_ccbill_Exit();
}
// CCBill doesn't sign webhooks, so make sure that this request really came from CCBill before processing it.
$verification_error = pmpro_ccbill_verify_webhook_source( $event_type, $response );
if ( ! empty( $verification_error ) ) {
pmpro_ccbill_webhook_log( $verification_error );
pmpro_ccbill_Exit();
}
// Full reference of event types and responses:
// https://ccbill.com/doc/webhooks-overview
switch ( $event_type ) {
case 'NewSaleSuccess':
$order_id = sanitize_text_field( $response['X-pmpro_orderid'] );
$morder = new MemberOrder( $order_id );
pmpro_ccbill_webhook_log( sprintf( 'NewSaleSuccess received. X-pmpro_orderid: %s, Loaded order ID: %s, Loaded order user_id: %s', $order_id, $morder->id, $morder->user_id ) );
if ( empty( $morder->id ) ) {
pmpro_ccbill_webhook_log( sprintf( 'Order %s could not be found. Skipping.', $order_id ) );
pmpro_ccbill_Exit();
}
// Only complete orders that are still waiting for payment. CCBill can resend NewSaleSuccess, and completing
// the checkout again would cancel the member's new subscription at CCBill as a "previous" subscription.
if ( ! in_array( $morder->status, array( 'token', 'pending' ), true ) ) {
pmpro_ccbill_webhook_log( sprintf( 'Order #%s has already been processed (status: %s). Skipping duplicate NewSaleSuccess for subscription %s.', $morder->id, $morder->status, isset( $response['subscriptionId'] ) ? $response['subscriptionId'] : '(not set)' ) );
pmpro_ccbill_Exit();
}
// Let's save the order data that may be needed. Ensure that there is a recurring amount passed in, sandbox passes this even for one-time payments.
if ( ! empty( $response['subscriptionId'] ) && ( isset( $response['subscriptionRecurringPrice'] ) && intval( $response['subscriptionRecurringPrice'] ) > 0 ) ) {
$morder->subscription_transaction_id = sanitize_text_field( $response['subscriptionId'] );
}
if ( ! empty( $response['transactionId'] ) ) {
$morder->payment_transaction_id = sanitize_text_field( $response['transactionId'] );
}
$morder->saveOrder();
// Remember the CCBill subscription ID even for one-time payments so that it can't be reused for another order.
if ( ! empty( $morder->id ) && ! empty( $response['subscriptionId'] ) ) {
update_pmpro_membership_order_meta( $morder->id, 'ccbill_subscription_id', $response['subscriptionId'] );
}
//run the function to complete checkout
if ( pmpro_ccbill_ChangeMembershipLevel( $morder, $response ) ) {
//Log the event
pmpro_ccbill_webhook_log( sprintf( __( "Checkout processed (%s) success!", 'pmpro_ccbill'), $morder->code ) );
} else {
// Named fields for a quick diagnosis; pmpro_ccbill_Exit() already dumps the full $_REQUEST below.
pmpro_ccbill_webhook_log( sprintf(
'Checkout FAILED to assign a membership level. Order ID: %s, code: %s, user_id: %s, membership_id: %s, X-pmpro_levelid: %s.',
$morder->id,
$morder->code,
$morder->user_id,
$morder->membership_id,
isset( $response['X-pmpro_levelid'] ) ? $response['X-pmpro_levelid'] : '(not set)'
) );
}
pmpro_ccbill_Exit();
break;
case 'Expiration':
case 'Cancellation':
$subscription_id = sanitize_text_field( $response['subscriptionId'] );
pmpro_ccbill_webhook_log( pmpro_handle_subscription_cancellation_at_gateway( $subscription_id, 'ccbill', 'live' ) );
pmpro_ccbill_Exit();
break;
case 'RenewalSuccess':
$status = 'success';
pmpro_ccbill_AddRenewal( $response, $status );
pmpro_ccbill_Exit();
break;
case 'RenewalFailure':
$status = 'error';
pmpro_ccbill_AddRenewal( $response, $status );
pmpro_ccbill_Exit();
break;
default:
do_action('pmpro_ccbill_other_webhook_events', $event_type);
pmpro_ccbill_Exit();
break;
}
/**
* Make sure that a webhook request came from CCBill.
*
* Requests from CCBill's webhook IP addresses are accepted. Otherwise (e.g. for sites behind a proxy or CDN),
* the event must be confirmed through DataLink. If DataLink can't confirm it, the request is rejected.
*
* @since 1.2
*
* @param string $event_type The webhook event type.
* @param array $response The sanitized webhook postback data.
* @return string An error message if the request could not be verified, or an empty string if it was.
*/
function pmpro_ccbill_verify_webhook_source( $event_type, $response ) {
global $wpdb;
// Only REMOTE_ADDR is checked. Forwarding headers can be set by anyone.
$remote_ip = isset( $_SERVER['REMOTE_ADDR'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ) ) : '';
// CCBill's webhook IP ranges are x.x.x.1-254 in these blocks: https://ccbill.com/doc/webhooks-user-guide
if ( filter_var( $remote_ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 ) ) {
$octets = explode( '.', $remote_ip );
$block = $octets[0] . '.' . $octets[1] . '.' . $octets[2];
if ( in_array( $block, array( '64.38.212', '64.38.215', '64.38.240', '64.38.241' ), true ) && $octets[3] >= 1 && $octets[3] <= 254 ) {
return '';
}
}
// Not from a CCBill IP address, so confirm the subscription through DataLink instead.
$error_prefix = sprintf( 'Webhook rejected. The request came from %s, which is not a CCBill IP address, and could not be confirmed through DataLink.', $remote_ip );
$subscription_id = isset( $response['subscriptionId'] ) ? $response['subscriptionId'] : '';
if ( empty( get_option( 'pmpro_ccbill_datalink_username' ) ) || empty( get_option( 'pmpro_ccbill_datalink_password' ) ) ) {
return $error_prefix . ' DataLink credentials are not set.';
}
if ( empty( $subscription_id ) ) {
return $error_prefix . ' No subscriptionId was sent.';
}
$data = PMProGateway_CCBill::get_datalink_subscription_status( $subscription_id );
if ( is_wp_error( $data ) ) {
return $error_prefix . ' ' . $data->get_error_message();
}
// get_datalink_subscription_status() returns a WP_Error for DataLink error codes, so this only catches a response without a status.
if ( ! isset( $data['subscriptionStatus'] ) ) {
return sprintf( '%s Subscription %s was not found for this CCBill account.', $error_prefix, $subscription_id );
}
if ( 'NewSaleSuccess' === $event_type ) {
// The sale must not have been voided, refunded or charged back.
foreach ( array( 'voidsIssued', 'refundsIssued', 'chargebacksIssued' ) as $key ) {
if ( ! isset( $data[ $key ] ) || 0 !== intval( $data[ $key ] ) ) {
return sprintf( '%s Subscription %s has %s: %s.', $error_prefix, $subscription_id, $key, isset( $data[ $key ] ) ? $data[ $key ] : '(missing)' );
}
}
$morder = new MemberOrder( isset( $response['X-pmpro_orderid'] ) ? $response['X-pmpro_orderid'] : 0 );
if ( empty( $morder->id ) ) {
return $error_prefix . ' The order could not be found.';
}
// The subscription must not already be used by another order.
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$other_order_id = $wpdb->get_var(
$wpdb->prepare(
"SELECT o.id FROM $wpdb->pmpro_membership_orders o
LEFT JOIN $wpdb->pmpro_membership_ordermeta om ON om.pmpro_membership_order_id = o.id AND om.meta_key = 'ccbill_subscription_id'
WHERE o.id <> %d AND ( o.subscription_transaction_id = %s OR o.payment_transaction_id = %s OR om.meta_value = %s )
LIMIT 1",
$morder->id,
$subscription_id,
$subscription_id,
$subscription_id
)
);
if ( ! empty( $other_order_id ) ) {
return sprintf( '%s Subscription %s is already used by order #%s.', $error_prefix, $subscription_id, $other_order_id );
}
// The subscription must have been created around the time of the order. CCBill's signupDate (YYYYMMDD) isn't in UTC,
// so allow the day before the order's date (time zones) through 2 days after it (slow checkouts or delayed webhooks).
$signup_date = ( isset( $data['signupDate'] ) && preg_match( '/^\d{8}$/', $data['signupDate'] ) ) ? strtotime( $data['signupDate'] ) : false;
$order_date = strtotime( gmdate( 'Y-m-d', $morder->getTimestamp( true ) ) );
if ( empty( $signup_date ) || $signup_date < $order_date - DAY_IN_SECONDS || $signup_date > $order_date + 2 * DAY_IN_SECONDS ) {
return sprintf( '%s Subscription %s signupDate (%s) is not close to the date of order #%s (%s).', $error_prefix, $subscription_id, isset( $data['signupDate'] ) ? $data['signupDate'] : '(missing)', $morder->id, gmdate( 'Ymd', $order_date ) );
}
} elseif ( 'RenewalSuccess' === $event_type ) {
// CCBill must have rebilled the subscription more times than we have recorded renewals for it,
// so that a subscription can't be replayed to add renewals that weren't paid. The first paid order is the initial sale.
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$paid_orders = (int) $wpdb->get_var(
$wpdb->prepare(
"SELECT COUNT(*) FROM $wpdb->pmpro_membership_orders
WHERE subscription_transaction_id = %s AND status NOT IN ( 'error', 'token', 'pending', 'review' )",
$subscription_id
)
);
$recorded_renewals = max( 0, $paid_orders - 1 );
if ( ! isset( $data['timesRebilled'] ) || intval( $data['timesRebilled'] ) <= $recorded_renewals ) {
return sprintf( '%s Subscription %s has been rebilled %s times and %d renewals are already recorded.', $error_prefix, $subscription_id, isset( $data['timesRebilled'] ) ? $data['timesRebilled'] : '(missing)', $recorded_renewals );
}
}
return '';
}
/**
* Change Membership Level for CCBill.
* * @param MemberOrder $morder
* @param array $response The sanitized webhook postback data.
* @return bool
* @since 0.1
*/
function pmpro_ccbill_ChangeMembershipLevel( $morder, $response = array() ) {
global $pmpro_level;
pmpro_pull_checkout_data_from_order( $morder );
// Never trust the level ID out of the request itself ($response['X-pmpro_levelid']) as the
// source of the level to grant -- a webhook postback can be spoofed by anyone who can guess/
// obtain an order ID, and it's only used below as a tamper/mismatch check for logging.
// The order's `membership_id` column is set server-side at checkout, before the user is ever
// sent to CCBill, and can't be influenced by the postback -- so it's the trustworthy fallback.
if ( empty( $pmpro_level->id ) && ! empty( $morder->membership_id ) ) {
$fallback_level = pmpro_getLevel( intval( $morder->membership_id ) );
if ( ! empty( $fallback_level ) ) {
pmpro_ccbill_webhook_log( sprintf( 'checkout_level order meta was missing for order #%s. Falling back to the order\'s stored membership_id (%s).', $morder->id, $morder->membership_id ) );
$pmpro_level = $fallback_level;
}
}
if ( empty( $pmpro_level->id ) ) {
pmpro_ccbill_webhook_log( sprintf( 'No membership level could be determined for order #%s (user #%s). Aborting level change.', $morder->id, $morder->user_id ) );
return false;
}
// Sanity/fraud check: the level ID CCBill echoed back should match what the order was
// actually created for. A mismatch doesn't change what we grant (we always grant based on
// the order's own trusted data above), but it's worth flagging for investigation.
if ( ! empty( $response['X-pmpro_levelid'] ) && intval( $response['X-pmpro_levelid'] ) !== intval( $pmpro_level->id ) ) {
pmpro_ccbill_webhook_log( sprintf( 'WARNING: X-pmpro_levelid (%s) in the postback does not match the level being granted (%s) for order #%s. Possible tampered/replayed request -- investigate.', $response['X-pmpro_levelid'], $pmpro_level->id, $morder->id ) );
}
return pmpro_complete_async_checkout( $morder );
}
/**
* Add Renewal Order
*
* @param array $response
* @param string $status
* @return void
*/
function pmpro_ccbill_AddRenewal( array $response, $status = 'success' ) : void {
$transaction_id = $response['transactionId'];
$subscription_id = $response['subscriptionId'];
$timestamp = is_numeric( $response['timestamp'] ) ? $response['timestamp'] : strtotime( $response['timestamp'] ); // Convert to a timestamp if it's not already passed through.
$payment_type = $response['paymentType'];
$card_type = $response['cardType'];
$renewal_date = $response['renewalDate'];
$morder = new MemberOrder();
$morder->getMemberOrderByPaymentTransactionID( $transaction_id );
if ( empty( $morder->id ) ) {
/**
* Order doesn't exist
*/
$old_order = new MemberOrder();
$old_order->getLastMemberOrderBySubscriptionTransactionID( $subscription_id );
pmpro_ccbill_webhook_log( 'old_order ID: ' . $old_order->id );
// Original subscription order cannot be found. Let's Bail.
if ( empty( $old_order ) || empty( $old_order->id ) ) {
pmpro_ccbill_webhook_log( sprintf( __( "Couldn't find the original subscription: (%s).", 'pmpro_ccbill' ), $subscription_id ) );
pmpro_ccbill_Exit();
}
$user_id = $old_order->user_id;
$user = get_userdata( $user_id );
// No user found for this order anymore.
if ( empty( $user ) ) {
pmpro_ccbill_webhook_log( sprintf( __( "Couldn't find the old order's user. Order ID (%s).", 'pmpro_ccbill' ), $old_order->id ) );
pmpro_ccbill_Exit();
}
$user->membership_level = pmpro_getMembershipLevelForUser( $user_id );
// Log the user ID only for security reasons.
pmpro_ccbill_webhook_log( 'WP User ID: ' . $user_id );
// Create a new order now.
$order = new MemberOrder();
$order->user_id = $user_id;
$order->status = $status;
$order->membership_id = $user->membership_level->id;
$order->payment_transaction_id = $transaction_id;
$order->subscription_transaction_id = $subscription_id;
$order->gateway = get_option( 'pmpro_gateway' );
$order->gateway_environment = get_option( 'pmpro_gateway_environment' );
$order->timestamp = $timestamp;
$order->payment_type = $payment_type;
$order->cardtype = $card_type;
$order->find_billing_address();
if ( 'error' === $status ) {
do_action( 'pmpro_subscription_payment_failed', $old_order );
$order->notes = sprintf( __( 'Renewal failed: %s (%s). Retry on %s.', 'pmpro_ccbill' ) , $response['failureReason'], $response['failureCode'], $response['nextRetryDate'] );
$order->saveOrder();
// Email the customer about this failure.
$pmproemail = new PMProEmail();
$pmproemail->sendBillingFailureEmail( $user, $order );
// Email admin so they are aware of the failure
$pmproemail = new PMProEmail();
$pmproemail->sendBillingFailureAdminEmail(get_bloginfo("admin_email"), $order);
// Write to the log
pmpro_ccbill_webhook_log( sprintf( __( 'Renewal failed (%s) for subscription # (%s).', 'pmpro_ccbill' ), $response['failureReason'], $subscription_id ) );
pmpro_ccbill_Exit();
} else {
$card_number = $response['last4'];
$card_exp = $response['expDate'];
$total = $response['accountingAmount'];
$card_exp_month = substr( $card_exp, 0, 2 );
$card_exp_year = '20' . substr( $card_exp, 2 );
$order->accountnumber = 'XXXXXXXXXXXX' . $card_number;
$order->expirationmonth = $card_exp_month;
$order->expirationyear = $card_exp_year;
$order->subtotal = $total;
$order->total = $total;
// CCBill rebills include tax, but the webhook only sends the total. If the total is the subscription's
// billing amount plus tax, record the tax separately. Otherwise, such as for subscriptions that were
// created without tax, the whole amount stays as the subtotal.
$subscription = $old_order->get_subscription();
if ( ! empty( $subscription ) ) {
$billing_amount = pmpro_round_price( $subscription->get_billing_amount() );
$tax = pmpro_round_price( $old_order->getTaxForPrice( $billing_amount ) );
if ( $tax > 0 && pmpro_round_price_as_string( $billing_amount + $tax ) === pmpro_round_price_as_string( $total ) ) {
$order->subtotal = $billing_amount;
$order->tax = $tax;
}
}
}
// Save the order before sending the email.
$order->saveOrder();
if ( $order->id ) {
$order->getMemberOrderByID( $order->id );
/**
* Send customer email
*/
$email = new PMProEmail();
$email->sendInvoiceEmail( $user, $order );
pmpro_ccbill_webhook_log( sprintf( __( 'Order created (%1$s) for subscription # (%2$s).', 'pmpro_ccbill' ), $order->id, $subscription_id ) );
do_action( 'pmpro_subscription_payment_completed', $order, $response );
}
} else {
/**
* Order exists, log and exit
*/
pmpro_ccbill_webhook_log( sprintf( __( 'An order with that payment ID (%s) already exists.', 'pmpro_ccbill' ), $transaction_id ) );
}
pmpro_ccbill_Exit();
}
/**
* Cancel level for a subscription. Deprecated, call pmpro_handle_subscription_cancellation_at_gateway instead.
*
* @param MemberOrder $morder The order object.
* return bool True if the cancellation was successful, false otherwise.
* @since TBD
* @deprecated TBD
*/
function pmpro_ccbill_RecurringCancel( $morder ) {
_deprecated_function( __FUNCTION__, 'TBD', 'pmpro_handle_subscription_cancellation_at_gateway' );
global $pmpro_error;
$worked = pmpro_cancelMembershipLevel( $morder->membership_level->id, $morder->user_id, 'inactive' );
if ( $worked === true ) {
//send an email to the member
$myemail = new PMProEmail();
$myemail->sendCancelEmail();
//send an email to the admin
$myemail = new PMProEmail();
$myemail->sendCancelAdminEmail( $morder->user, $morder->membership_level->id );
pmpro_ccbill_webhook_log( sprintf( __( "Subscription Cancelled (%s)", 'pmpro-ccbill'), $morder->csubscription_transaction_id ) );
return true;
} else {
return false;
}
}
/*
Add message to webhook string
*/
function pmpro_ccbill_webhook_log( $s ) {
global $logstr;
$logstr .= "\t" . $s . "\n";
}
/**
* Mask the customer's personal data in webhook data before it is logged. The email and last name are only partially masked.
*
* Event types, IDs, amounts and the X-pmpro_* fields are kept so that webhooks can still be debugged.
*
* @since 1.2
*
* @param array $data The webhook request data.
* @return array The webhook data with personal data masked.
*/
function pmpro_ccbill_redact_webhook_data( $data ) {
/**
* Filter the webhook fields that are masked in the CCBill webhook log.
*
* @since 1.2
*
* @param array $keys The webhook field names to mask.
*/
$redacted_keys = apply_filters( 'pmpro_ccbill_webhook_log_redacted_keys', array(
'firstName',
'lastName',
'email',
'address1',
'address2',
'city',
'state',
'postalCode',
'phoneNumber',
'ipAddress',
'username',
'bin',
'last4',
'expDate',
) );
$redacted_keys = array_map( 'strtolower', $redacted_keys );
foreach ( $data as $key => $value ) {
// Never log passwords.
if ( 'password' === strtolower( $key ) ) {
unset( $data[ $key ] );
continue;
}
if ( ! in_array( strtolower( $key ), $redacted_keys, true ) || '' === $value ) {
continue;
}
// Keep part of the email and last name so that log entries can be matched to a customer.
$at = is_string( $value ) ? strpos( $value, '@' ) : false;
if ( ! is_string( $value ) ) {
$data[ $key ] = '[redacted]';
} elseif ( 'email' === strtolower( $key ) && false !== $at ) {
$visible = min( 2, (int) floor( $at / 2 ) );
$data[ $key ] = substr( $value, 0, $visible ) . '***' . substr( $value, $at );
} elseif ( 'lastname' === strtolower( $key ) ) {
$data[ $key ] = mb_substr( $value, 0, 1 ) . '***';
} else {
$data[ $key ] = '[redacted]';
}
}
return $data;
}
/*
Output webhook log and exit;
*/
function pmpro_ccbill_Exit( $redirect = false ) {
global $logstr;
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only: the raw request is only written to the debug log.
$logstr = var_export( pmpro_ccbill_redact_webhook_data( $_REQUEST ), true ) . sprintf( __( 'Logged On: %s', 'pmpro-ccbill' ), date_i18n("m/d/Y H:i:s") ) . "\n" . $logstr . "\n-------------\n";
//log in file or email?
if ( defined( 'PMPRO_CCBILL_DEBUG' ) && PMPRO_CCBILL_DEBUG === 'log' ) {
//file, in PMPro's restricted uploads directory so it can't be fetched directly.
if ( function_exists( 'pmpro_get_restricted_file_path' ) ) {
$logfile = apply_filters( 'pmpro_ccbill_webhook_logfile', pmpro_get_restricted_file_path( 'logs', 'ccbill-webhook.txt' ) );
$loghandle = fopen( $logfile, "a+" );
fwrite( $loghandle, $logstr );
fclose( $loghandle );
}
} elseif ( defined( 'PMPRO_CCBILL_DEBUG' ) && false !== PMPRO_CCBILL_DEBUG ) {
//email
if ( strpos( PMPRO_CCBILL_DEBUG, "@" ) ) {
$log_email = PMPRO_CCBILL_DEBUG; //constant defines a specific email address
} else {
$log_email = get_option("admin_email");
}
wp_mail($log_email, get_option("blogname") . ' ' . __( "CCBill Webhook Log", 'pmpro-ccbill' ), nl2br($logstr));
}
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only: only used to build the confirmation page redirect.
if ( !empty( $_REQUEST['pmpro_orderid'] ) ){
//Coming back from the gateway, lets redirect back to membership confirmation
$morder = new MemberOrder( intval( $_REQUEST['pmpro_orderid'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only: only used to build the confirmation page redirect.
if ( !empty( $morder ) ) {
$redirect = pmpro_url( "confirmation", "?level=" . $morder->membership_id );
}
}
if ( ! empty( $redirect ) ) {
wp_redirect( $redirect ); // phpcs:ignore WordPress.Security.SafeRedirect.wp_redirect_wp_redirect -- pmpro_url() is filterable and may legitimately point to another host (e.g. Network Subsite).
}
exit;
}