| 1 |
<?php |
| 2 |
|
| 3 |
defined( 'ABSPATH' ) || exit; |
| 4 |
|
| 5 |
class WC_Vipps_Recurring_Rest_Api { |
| 6 |
private string $api_namespace = 'vipps-mobilepay-recurring/v1'; |
| 7 |
|
| 8 |
private static ?WC_Vipps_Recurring_Rest_Api $instance = null; |
| 9 |
|
| 10 |
/** |
| 11 |
* Returns the *Singleton* instance of this class. |
| 12 |
* |
| 13 |
* @return WC_Vipps_Recurring_Rest_Api |
| 14 |
*/ |
| 15 |
public static function get_instance(): WC_Vipps_Recurring_Rest_Api { |
| 16 |
if ( null === self::$instance ) { |
| 17 |
self::$instance = new self(); |
| 18 |
} |
| 19 |
|
| 20 |
return self::$instance; |
| 21 |
} |
| 22 |
|
| 23 |
public function __construct() { |
| 24 |
add_action( 'rest_api_init', [ $this, 'init' ] ); |
| 25 |
} |
| 26 |
|
| 27 |
public function init() { |
| 28 |
register_rest_route( $this->api_namespace, '/orders/status/(?P<order_id>[0-9]+)', [ |
| 29 |
'methods' => 'GET', |
| 30 |
'callback' => [ $this, 'order_status' ], |
| 31 |
'permission_callback' => '__return_true' |
| 32 |
] ); |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* @throws WC_Vipps_Recurring_Exception |
| 37 |
* @throws WC_Vipps_Recurring_Temporary_Exception |
| 38 |
* @throws WC_Vipps_Recurring_Config_Exception |
| 39 |
* @throws WC_Data_Exception |
| 40 |
*/ |
| 41 |
public function order_status( WP_REST_Request $request ) { |
| 42 |
$order_id = $request->get_param( 'order_id' ); |
| 43 |
$order_key = $request->get_param( 'key' ); |
| 44 |
|
| 45 |
$order = wc_get_order( $order_id ); |
| 46 |
|
| 47 |
if ( ! $order || ! is_string( $order_key ) || ! hash_equals( $order->get_order_key(), $order_key ) ) { |
| 48 |
return new WP_Error( |
| 49 |
'not_found', |
| 50 |
'Order not found.', |
| 51 |
[ 'status' => 404 ] |
| 52 |
); |
| 53 |
} |
| 54 |
|
| 55 |
$gateway = WC_Vipps_Recurring::get_instance()->gateway(); |
| 56 |
|
| 57 |
do_action( 'wc_vipps_recurring_before_rest_api_check_order_status', $order_id ); |
| 58 |
|
| 59 |
// Skip the lock here, otherwise we get a false result |
| 60 |
$gateway->check_charge_status( $order_id, true ); |
| 61 |
$order = wc_get_order( $order_id ); |
| 62 |
$agreement_id = WC_Vipps_Recurring_Helper::get_agreement_id_from_order( $order ); |
| 63 |
|
| 64 |
if ( ! $agreement_id ) { |
| 65 |
return new WP_REST_Response( [ |
| 66 |
'status' => 'PENDING', |
| 67 |
'redirect_url' => false |
| 68 |
], 202 ); |
| 69 |
} |
| 70 |
|
| 71 |
try { |
| 72 |
$agreement = $gateway->api->get_agreement( $agreement_id ); |
| 73 |
} catch ( WC_Vipps_Recurring_Exception $exception ) { |
| 74 |
if ( $exception->response_code !== 404 ) { |
| 75 |
throw $exception; |
| 76 |
} |
| 77 |
|
| 78 |
return new WP_REST_Response( [ |
| 79 |
'status' => 'PENDING', |
| 80 |
'redirect_url' => false |
| 81 |
], 202 ); |
| 82 |
} |
| 83 |
|
| 84 |
do_action( 'wc_vipps_recurring_after_rest_api_check_order_status', $order_id ); |
| 85 |
|
| 86 |
$return_url = false; |
| 87 |
if ( $agreement->status !== 'PENDING' ) { |
| 88 |
if ( $agreement->status === 'ACTIVE' ) { |
| 89 |
WC_Vipps_Recurring_Checkout::get_instance()->maybe_login_checkout_user( $order->get_id(), $order_key ); |
| 90 |
} |
| 91 |
$return_url = $order->get_checkout_order_received_url(); |
| 92 |
} |
| 93 |
|
| 94 |
return [ |
| 95 |
'status' => $agreement->status, |
| 96 |
'redirect_url' => $return_url |
| 97 |
]; |
| 98 |
} |
| 99 |
} |
| 100 |
|