-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathResponseFactory.php
More file actions
129 lines (114 loc) · 4.17 KB
/
Copy pathResponseFactory.php
File metadata and controls
129 lines (114 loc) · 4.17 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
<?php
/**
* Factory service for the REST response objects.
*
* @package WooCommerce\PayPalCommerce\StoreSync\Response
*/
declare( strict_types = 1 );
namespace WooCommerce\PayPalCommerce\StoreSync\Response;
use WC_Cart;
use WC_Order;
use WooCommerce\PayPalCommerce\StoreSync\CartValidation\CouponValidator\AppliedCouponsBuilder;
use WooCommerce\PayPalCommerce\StoreSync\Schema\PayPalCart;
use WooCommerce\PayPalCommerce\StoreSync\Helper\AgenticCartBuilder;
class ResponseFactory {
/**
* Cart builder service.
*
* @var AgenticCartBuilder
*/
private AgenticCartBuilder $cart_builder;
/**
* Applied coupons builder service.
*
* @var AppliedCouponsBuilder
*/
private AppliedCouponsBuilder $applied_coupons_builder;
/**
* Constructor.
*
* @param AgenticCartBuilder $cart_builder Cart builder service.
* @param AppliedCouponsBuilder $applied_coupons_builder Applied coupons builder service.
*/
public function __construct( AgenticCartBuilder $cart_builder, AppliedCouponsBuilder $applied_coupons_builder ) {
$this->cart_builder = $cart_builder;
$this->applied_coupons_builder = $applied_coupons_builder;
}
/**
* Create a new cart response (status: CREATED).
*
* @param PayPalCart $cart The cart object.
* @param string $cart_id The cart ID.
* @param string $token The payment token.
* @return NewCartResponse The response object.
*/
public function new_cart( PayPalCart $cart, string $cart_id, string $token ): NewCartResponse {
$wc_cart = $this->build_wc_cart_or_null( $cart );
$applied_coupons = $this->build_applied_coupons( $cart );
return new NewCartResponse( $cart, $cart_id, $token, $applied_coupons, $wc_cart );
}
/**
* Create a cart response with token (for PUT when a new token was created).
*
* Used for PUT /merchant-cart/{id} when the endpoint creates a new PayPal order
* because the cart didn't have a token or the existing token was expired.
*
* @param PayPalCart $cart The cart object.
* @param string $cart_id The cart ID.
* @param string $token The payment token.
* @return UpdatedCartWithTokenResponse The response object.
*/
public function cart_with_token( PayPalCart $cart, string $cart_id, string $token ): UpdatedCartWithTokenResponse {
$wc_cart = $this->build_wc_cart_or_null( $cart );
$applied_coupons = $this->build_applied_coupons( $cart );
return new UpdatedCartWithTokenResponse( $cart, $cart_id, $token, $applied_coupons, $wc_cart );
}
/**
* Create a paid cart response.
*
* @param WC_Order $order The WooCommerce order.
* @param PayPalCart $cart The cart object.
* @param string $cart_id The cart ID.
* @return PaidCartResponse The response object.
*/
public function from_order( WC_Order $order, PayPalCart $cart, string $cart_id ): PaidCartResponse {
$wc_cart = $this->build_wc_cart_or_null( $cart );
$applied_coupons = $this->build_applied_coupons( $cart );
return new PaidCartResponse( $cart, $cart_id, $order, $applied_coupons, $wc_cart );
}
/**
* Create a basic cart response.
*
* @param PayPalCart $cart The cart object.
* @param string $cart_id The cart ID.
* @return CartResponse The response object.
*/
public function from_cart( PayPalCart $cart, string $cart_id ): CartResponse {
$wc_cart = $this->build_wc_cart_or_null( $cart );
$applied_coupons = $this->build_applied_coupons( $cart );
return new CartResponse( $cart, $applied_coupons, $cart_id, $wc_cart );
}
/**
* Build WC_Cart from PayPalCart.
*
* @param PayPalCart $cart The PayPal cart.
* @return WC_Cart|null The WooCommerce cart or null.
*/
private function build_wc_cart_or_null( PayPalCart $cart ): ?WC_Cart {
$wc_cart = $this->cart_builder->paypal_cart_to_wc_cart( $cart );
if ( $wc_cart instanceof WC_Cart ) {
return $wc_cart;
}
return null;
}
/**
* Build applied coupons data for a cart.
*
* @param PayPalCart $cart The cart object.
* @return array Applied coupons data.
*/
private function build_applied_coupons( PayPalCart $cart ): array {
$validation_status = $cart->issues() ? 'INVALID' : 'VALID';
return $this->applied_coupons_builder->build_applied_coupons_array( $cart, $validation_status );
}
}