-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathUpdatedCartWithTokenResponse.php
More file actions
76 lines (65 loc) · 2.12 KB
/
Copy pathUpdatedCartWithTokenResponse.php
File metadata and controls
76 lines (65 loc) · 2.12 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
<?php
/**
* PayPal Cart Response (cart updated with new token).
*
* Used when PUT /merchant-cart/{id} creates a new PayPal order because:
* - The cart didn't have a token (POST validation failed)
* - The existing token was expired
*
* @package WooCommerce\PayPalCommerce\StoreSync\Response
*/
declare( strict_types = 1 );
namespace WooCommerce\PayPalCommerce\StoreSync\Response;
use WC_Cart;
use WooCommerce\PayPalCommerce\StoreSync\Schema\PayPalCart;
class UpdatedCartWithTokenResponse extends CartResponse {
/**
* Constructor.
*
* @param PayPalCart $cart The PayPal cart.
* @param string $cart_id The cart ID.
* @param string $token The EC token.
* @param array $applied_coupons Applied coupons data.
* @param WC_Cart|null $wc_cart The WooCommerce cart.
*/
public function __construct(
PayPalCart $cart,
string $cart_id,
string $token,
array $applied_coupons = array(),
?WC_Cart $wc_cart = null
) {
parent::__construct( $cart, $applied_coupons, $cart_id, $wc_cart );
$this->token = $token;
// Updated carts with valid tokens are READY for checkout.
if ( ! $this->cart->issues() && ! empty( $token ) ) {
$this->status = 'READY';
}
}
/**
* Convert to array for API response.
*
* Includes the payment_method.token only when a new token was created.
* Per PayPal spec: "The API response should only mention new token,
* i.e. when no token was generated, the payment_method.token property
* should not exist in the response."
*
* @return array The response array.
*/
public function to_array(): array {
$data = parent::to_array();
// Only include payment_method when a token exists.
if ( ! empty( $this->token ) ) {
$data['payment_method'] = array(
'type' => 'paypal', // hard-coded.
'token' => $this->token,
);
// Add sandbox approval URL for testing.
$data['_testing'] = array(
'sandbox_approval_url' => 'https://www.sandbox.paypal.com/checkoutnow?token=' . $this->token,
'instructions' => 'For sandbox testing: Open this URL in browser, login as buyer, and approve payment before checkout.',
);
}
return $data;
}
}