| 1 |
<?php |
| 2 |
/** |
| 3 |
* Abstract class for payment methods. |
| 4 |
* |
| 5 |
* @since 4.16.0 |
| 6 |
* @package SimplePay\Core\PaymentMethods |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace SimplePay\Core\PaymentMethods; |
| 10 |
|
| 11 |
use SimplePay\Core\Payments\Payment_Confirmation; |
| 12 |
|
| 13 |
/** |
| 14 |
* Abstract class for payment methods. |
| 15 |
* |
| 16 |
* @since 4.16.0 |
| 17 |
* @package SimplePay\Core\PaymentMethods |
| 18 |
*/ |
| 19 |
abstract class AbstractPaymentMethod { |
| 20 |
|
| 21 |
/** |
| 22 |
* The payment method ID. |
| 23 |
* |
| 24 |
* @var string |
| 25 |
* @since 4.16.0 |
| 26 |
*/ |
| 27 |
const PAYMENT_METHOD_ID = ''; |
| 28 |
|
| 29 |
/** |
| 30 |
* Register the payment method. |
| 31 |
* |
| 32 |
* @since 4.16.0 |
| 33 |
* |
| 34 |
* @param Collection $payment_methods Payment methods collection. |
| 35 |
* @return void |
| 36 |
*/ |
| 37 |
abstract public function register( $payment_methods ): void; |
| 38 |
|
| 39 |
/** |
| 40 |
* Validates the Payment Confirmation data. |
| 41 |
* |
| 42 |
* If the data includes an invalid or incomplete PaymentIntent |
| 43 |
* redirect to the form's failure page. |
| 44 |
* |
| 45 |
* @since 4.2.0 |
| 46 |
*/ |
| 47 |
public function redirect_failed_payment(): void { |
| 48 |
// Ensure we can retrieve a PaymentIntent. |
| 49 |
if ( ! isset( $_GET['payment_intent'] ) ) { |
| 50 |
return; |
| 51 |
} |
| 52 |
|
| 53 |
// Ensure we paid with the correct payment method. |
| 54 |
if ( ! isset( $_GET['source_type'] ) || static::PAYMENT_METHOD_ID !== $_GET['source_type'] ) { |
| 55 |
return; |
| 56 |
} |
| 57 |
|
| 58 |
// Ensure we have a customer so `Payment_Confirmation\get_confirmation_data()` doesn't fail. |
| 59 |
if ( ! isset( $_GET['customer_id'] ) ) { |
| 60 |
return; |
| 61 |
} |
| 62 |
|
| 63 |
$payment_confirmation_data = Payment_Confirmation\get_confirmation_data(); |
| 64 |
|
| 65 |
// Ensure we have a Payment Form to reference. |
| 66 |
if ( ! isset( $payment_confirmation_data['form'] ) ) { |
| 67 |
return; |
| 68 |
} |
| 69 |
|
| 70 |
$payment_intent = isset( $payment_confirmation_data['paymentintents'] ) |
| 71 |
? current( $payment_confirmation_data['paymentintents'] ) |
| 72 |
: false; |
| 73 |
|
| 74 |
$failure_page = $payment_confirmation_data['form']->payment_cancelled_page; |
| 75 |
|
| 76 |
// Redirect to failure if PaymentIntent cannot be found. |
| 77 |
if ( false === $payment_intent ) { |
| 78 |
wp_safe_redirect( $failure_page ); |
| 79 |
} |
| 80 |
|
| 81 |
// Do nothing if the Intent has succeeded. |
| 82 |
if ( 'succeeded' === $payment_intent->status ) { |
| 83 |
return; |
| 84 |
} |
| 85 |
|
| 86 |
// Do nothing if the Intent did not have an error. |
| 87 |
if ( ! isset( $payment_intent->last_payment_error ) ) { |
| 88 |
return; |
| 89 |
} |
| 90 |
|
| 91 |
// Do nothing if the Intent is not from the correct payment method. |
| 92 |
if ( static::PAYMENT_METHOD_ID !== $payment_intent->last_payment_error->payment_method->type ) { |
| 93 |
return; |
| 94 |
} |
| 95 |
|
| 96 |
// Redirect to failure page. |
| 97 |
wp_safe_redirect( $failure_page ); |
| 98 |
} |
| 99 |
} |
| 100 |
|