PluginProbe
Pay with Vipps and MobilePay for WooCommerce / 6.3.1
Pay with Vipps and MobilePay for WooCommerce v6.3.1
6.3.1 6.2.6 6.3.0 6.2.5 6.2.4 6.2.3 6.2.2 6.2.1 6.2.0 6.1.10 6.1.9 6.1.8 6.1.7 6.1.6 6.1.5 6.1.4 6.1.3 6.1.2 6.1.1 6.1.0 6.0.5 6.0.4 6.0.3 6.0.2 6.0.1 All 190 releases
woo-vipps / recurring / includes / wc-vipps-recurring-checkout-rest-api.php

wc-vipps-recurring-checkout-rest-api.php in Pay with Vipps and MobilePay for WooCommerce 6.3.1, at recurring/includes/wc-vipps-recurring-checkout-rest-api.php

122 lines 3.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 defined( 'ABSPATH' ) || exit;
4
5 class WC_Vipps_Recurring_Checkout_Rest_Api {
6 public static string $api_namespace = 'vipps-mobilepay-recurring/v1/checkout';
7
8 private static ?WC_Vipps_Recurring_Checkout_Rest_Api $instance = null;
9
10 /**
11 * Returns the *Singleton* instance of this class.
12 *
13 * @return WC_Vipps_Recurring_Checkout_Rest_Api
14 */
15 public static function get_instance(): WC_Vipps_Recurring_Checkout_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( self::$api_namespace, '/session', [
29 'methods' => 'GET',
30 'callback' => [ $this, 'poll_session' ],
31 'permission_callback' => function () {
32 // This route does not work if we don't have a cart.
33 return ! empty( WC()->cart );
34 }
35 ] );
36 }
37
38 /**
39 * @return array
40 * @throws WC_Data_Exception
41 */
42 public function poll_session(): array {
43 $checkout = WC_Vipps_Recurring_Checkout::get_instance();
44
45 $pending_order_id = WC_Vipps_Recurring_Helper::get_checkout_pending_order_id();
46 $order = $pending_order_id ? wc_get_order( $pending_order_id ) : null;
47 $return_url = null;
48
49 if ( $order ) {
50 $return_url = WC_Vipps_Recurring_Helper::get_payment_redirect_url( $order );
51 }
52
53 $session = $order ? $order->get_meta( WC_Vipps_Recurring_Helper::META_ORDER_CHECKOUT_SESSION ) : false;
54 if ( ! $session ) {
55 WC()->session->set( WC_Vipps_Recurring_Helper::SESSION_ADDRESS_HASH, false );
56
57 return [ 'status' => 'EXPIRED', 'redirect_url' => false ];
58 }
59
60 add_filter( 'wc_vipps_recurring_is_vipps_checkout', '__return_true' );
61 $status = $checkout->get_checkout_status( $session );
62
63 $failed = $status === 'ERROR' || $status === 'SessionExpired' || $status === 'PaymentTerminated';
64 if ( $failed ) {
65 WC_Vipps_Recurring_Logger::log( sprintf( "Checkout session %d failed with message %s", $order->get_id(), $status ) );
66 $checkout->abandon_checkout_order( $order );
67
68 return [ 'status' => 'FAILED', 'redirect_url' => $return_url ];
69 }
70
71 if ( $status === 'PaymentSuccessful' ) {
72 // Cancel the session, but do not cancel the order.
73 $checkout->abandon_checkout_order( false );
74
75 return [ 'status' => 'COMPLETED', 'redirect_url' => $return_url ];
76 }
77
78 // Disallow sessions that go on for too long.
79 if ( is_a( $order, "WC_Order" ) ) {
80 $created = $order->get_date_created();
81 $now = time();
82 try {
83 $timestamp = $created->getTimestamp();
84 } catch ( Exception $e ) {
85 // PHP 8 gives ValueError for certain older versions of WooCommerce here.
86 $timestamp = intval( $created->format( 'U' ) );
87
88 }
89 $passed = $now - $timestamp;
90 $minutes = ( $passed / 60 );
91
92 // Expire after 50 minutes
93 if ( $minutes > 50 ) {
94 WC_Vipps_Recurring_Logger::log( sprintf( "Checkout session %s expired after %d minutes (limit 50)", $order->get_id(), $minutes ) );
95 $checkout->abandon_checkout_order( $order );
96
97 return [ 'status' => 'EXPIRED', 'redirect_url' => false ];
98 }
99 }
100
101 // This handles address information data from the poll if present. It is not, currently.
102 $change = false;
103 $vipps_address_hash = WC()->session->get( WC_Vipps_Recurring_Helper::SESSION_ADDRESS_HASH );
104 if ( isset( $status['billingDetails'] ) || isset( $status['shippingDetails'] ) ) {
105 $serialized = sha1( json_encode( @$status['billingDetails'] ) . ':' . json_encode( @$status['shippingDetails'] ) );
106 if ( $serialized != $vipps_address_hash ) {
107 $change = true;
108 WC()->session->set( WC_Vipps_Recurring_Helper::SESSION_ADDRESS_HASH, $serialized );
109 }
110 }
111
112 if ( $change ) {
113 $checkout->maybe_update_order_billing_and_shipping( $order, $status );
114 $order->save();
115
116 return [ 'status' => 'ORDER_CHANGE', 'redirect_url' => false ];
117 }
118
119 return [ 'status' => 'NO_CHANGE', 'redirect_url' => false ];
120 }
121 }
122