@@ -13,127 +13,127 @@ |
||
13 | 13 | */ |
14 | 14 | class ThirdPartyPayPalApi extends PayPalApi |
15 | 15 | { |
16 | - /** |
|
17 | - * Partner access token used to process payments. |
|
18 | - * |
|
19 | - * @var string |
|
20 | - */ |
|
21 | - protected string $access_token = ''; |
|
16 | + /** |
|
17 | + * Partner access token used to process payments. |
|
18 | + * |
|
19 | + * @var string |
|
20 | + */ |
|
21 | + protected string $access_token = ''; |
|
22 | 22 | |
23 | - /** |
|
24 | - * Partner Client ID. |
|
25 | - * |
|
26 | - * @var string |
|
27 | - */ |
|
28 | - protected string $partner_client_id = ''; |
|
23 | + /** |
|
24 | + * Partner Client ID. |
|
25 | + * |
|
26 | + * @var string |
|
27 | + */ |
|
28 | + protected string $partner_client_id = ''; |
|
29 | 29 | |
30 | - /** |
|
31 | - * Merchant (seller) merchant ID. |
|
32 | - * |
|
33 | - * @var string |
|
34 | - */ |
|
35 | - protected string $payer_id = ''; |
|
30 | + /** |
|
31 | + * Merchant (seller) merchant ID. |
|
32 | + * |
|
33 | + * @var string |
|
34 | + */ |
|
35 | + protected string $payer_id = ''; |
|
36 | 36 | |
37 | - /** |
|
38 | - * BN Code. Partner-Attribution-Id. |
|
39 | - * |
|
40 | - * @var string |
|
41 | - */ |
|
42 | - protected string $bn_code = ''; |
|
37 | + /** |
|
38 | + * BN Code. Partner-Attribution-Id. |
|
39 | + * |
|
40 | + * @var string |
|
41 | + */ |
|
42 | + protected string $bn_code = ''; |
|
43 | 43 | |
44 | 44 | |
45 | - /** |
|
46 | - * @param string $access_token |
|
47 | - * @param string $bn_code |
|
48 | - * @param string $partner_client_id |
|
49 | - * @param string $payer_id |
|
50 | - * @param bool $sandbox_mode |
|
51 | - */ |
|
52 | - public function __construct( |
|
53 | - string $access_token, |
|
54 | - string $bn_code, |
|
55 | - string $partner_client_id = '', |
|
56 | - string $payer_id = '', |
|
57 | - bool $sandbox_mode = true |
|
58 | - ) { |
|
59 | - parent::__construct($sandbox_mode); |
|
60 | - $this->access_token = $access_token; |
|
61 | - $this->partner_client_id = $partner_client_id; |
|
62 | - $this->payer_id = $payer_id; |
|
63 | - $this->bn_code = $bn_code; |
|
64 | - } |
|
45 | + /** |
|
46 | + * @param string $access_token |
|
47 | + * @param string $bn_code |
|
48 | + * @param string $partner_client_id |
|
49 | + * @param string $payer_id |
|
50 | + * @param bool $sandbox_mode |
|
51 | + */ |
|
52 | + public function __construct( |
|
53 | + string $access_token, |
|
54 | + string $bn_code, |
|
55 | + string $partner_client_id = '', |
|
56 | + string $payer_id = '', |
|
57 | + bool $sandbox_mode = true |
|
58 | + ) { |
|
59 | + parent::__construct($sandbox_mode); |
|
60 | + $this->access_token = $access_token; |
|
61 | + $this->partner_client_id = $partner_client_id; |
|
62 | + $this->payer_id = $payer_id; |
|
63 | + $this->bn_code = $bn_code; |
|
64 | + } |
|
65 | 65 | |
66 | 66 | |
67 | - /** |
|
68 | - * Send an API request. |
|
69 | - * |
|
70 | - * @param array $body_parameters |
|
71 | - * @param string $endpoint |
|
72 | - * @param string $method |
|
73 | - * @param array $headers |
|
74 | - * @return Object|array |
|
75 | - */ |
|
76 | - public function sendRequest(array $body_parameters, string $endpoint, string $method = 'POST', array $headers = []) |
|
77 | - { |
|
78 | - $request_parameters = $this->getRequestParameters($body_parameters, $method, $headers); |
|
79 | - return $this->response($endpoint, $request_parameters); |
|
80 | - } |
|
67 | + /** |
|
68 | + * Send an API request. |
|
69 | + * |
|
70 | + * @param array $body_parameters |
|
71 | + * @param string $endpoint |
|
72 | + * @param string $method |
|
73 | + * @param array $headers |
|
74 | + * @return Object|array |
|
75 | + */ |
|
76 | + public function sendRequest(array $body_parameters, string $endpoint, string $method = 'POST', array $headers = []) |
|
77 | + { |
|
78 | + $request_parameters = $this->getRequestParameters($body_parameters, $method, $headers); |
|
79 | + return $this->response($endpoint, $request_parameters); |
|
80 | + } |
|
81 | 81 | |
82 | 82 | |
83 | - /** |
|
84 | - * Build the request parameters. |
|
85 | - * |
|
86 | - * @param array $body_parameters |
|
87 | - * @param string $method |
|
88 | - * @param array $headers |
|
89 | - * @return array |
|
90 | - */ |
|
91 | - private function getRequestParameters(array $body_parameters, string $method, array $headers): array |
|
92 | - { |
|
93 | - $request_parameters = [ |
|
94 | - 'method' => $method, |
|
95 | - ]; |
|
96 | - $default_headers = [ |
|
97 | - 'User-Agent' => sanitize_text_field($_SERVER['HTTP_USER_AGENT']), |
|
98 | - 'PayPal-Partner-Attribution-Id' => $this->bnCode(), |
|
99 | - 'Content-Type' => 'application/json', |
|
100 | - 'Authorization' => 'Bearer ' . $this->accessToken(), |
|
101 | - ]; |
|
102 | - // If we have merchant credentials then we are onboard and can do requests on behalf of the seller. |
|
103 | - if ($this->partner_client_id && $this->payer_id) { |
|
104 | - $assertion1 = [ |
|
105 | - 'alg' => 'none', |
|
106 | - ]; |
|
107 | - $assertion2 = [ |
|
108 | - 'iss' => $this->partner_client_id, |
|
109 | - 'payer_id' => $this->payer_id, |
|
110 | - ]; |
|
111 | - $default_headers['PayPal-Auth-Assertion'] = base64_encode(json_encode($assertion1, JSON_HEX_APOS)) . '.' . |
|
112 | - base64_encode(json_encode($assertion2, JSON_HEX_APOS)) . '.'; |
|
113 | - } |
|
114 | - $request_parameters['headers'] = array_merge($default_headers, $headers); |
|
115 | - // Add body if this is a POST request. |
|
116 | - if ($body_parameters && ($method === 'POST' || $method === 'PUT')) { |
|
117 | - $request_parameters['body'] = json_encode($body_parameters); |
|
118 | - } |
|
119 | - return $request_parameters; |
|
120 | - } |
|
83 | + /** |
|
84 | + * Build the request parameters. |
|
85 | + * |
|
86 | + * @param array $body_parameters |
|
87 | + * @param string $method |
|
88 | + * @param array $headers |
|
89 | + * @return array |
|
90 | + */ |
|
91 | + private function getRequestParameters(array $body_parameters, string $method, array $headers): array |
|
92 | + { |
|
93 | + $request_parameters = [ |
|
94 | + 'method' => $method, |
|
95 | + ]; |
|
96 | + $default_headers = [ |
|
97 | + 'User-Agent' => sanitize_text_field($_SERVER['HTTP_USER_AGENT']), |
|
98 | + 'PayPal-Partner-Attribution-Id' => $this->bnCode(), |
|
99 | + 'Content-Type' => 'application/json', |
|
100 | + 'Authorization' => 'Bearer ' . $this->accessToken(), |
|
101 | + ]; |
|
102 | + // If we have merchant credentials then we are onboard and can do requests on behalf of the seller. |
|
103 | + if ($this->partner_client_id && $this->payer_id) { |
|
104 | + $assertion1 = [ |
|
105 | + 'alg' => 'none', |
|
106 | + ]; |
|
107 | + $assertion2 = [ |
|
108 | + 'iss' => $this->partner_client_id, |
|
109 | + 'payer_id' => $this->payer_id, |
|
110 | + ]; |
|
111 | + $default_headers['PayPal-Auth-Assertion'] = base64_encode(json_encode($assertion1, JSON_HEX_APOS)) . '.' . |
|
112 | + base64_encode(json_encode($assertion2, JSON_HEX_APOS)) . '.'; |
|
113 | + } |
|
114 | + $request_parameters['headers'] = array_merge($default_headers, $headers); |
|
115 | + // Add body if this is a POST request. |
|
116 | + if ($body_parameters && ($method === 'POST' || $method === 'PUT')) { |
|
117 | + $request_parameters['body'] = json_encode($body_parameters); |
|
118 | + } |
|
119 | + return $request_parameters; |
|
120 | + } |
|
121 | 121 | |
122 | 122 | |
123 | - /** |
|
124 | - * @return string |
|
125 | - */ |
|
126 | - public function accessToken(): string |
|
127 | - { |
|
128 | - return $this->access_token; |
|
129 | - } |
|
123 | + /** |
|
124 | + * @return string |
|
125 | + */ |
|
126 | + public function accessToken(): string |
|
127 | + { |
|
128 | + return $this->access_token; |
|
129 | + } |
|
130 | 130 | |
131 | 131 | |
132 | - /** |
|
133 | - * @return string |
|
134 | - */ |
|
135 | - public function bnCode(): string |
|
136 | - { |
|
137 | - return $this->bn_code; |
|
138 | - } |
|
132 | + /** |
|
133 | + * @return string |
|
134 | + */ |
|
135 | + public function bnCode(): string |
|
136 | + { |
|
137 | + return $this->bn_code; |
|
138 | + } |
|
139 | 139 | } |
@@ -97,7 +97,7 @@ discard block |
||
97 | 97 | 'User-Agent' => sanitize_text_field($_SERVER['HTTP_USER_AGENT']), |
98 | 98 | 'PayPal-Partner-Attribution-Id' => $this->bnCode(), |
99 | 99 | 'Content-Type' => 'application/json', |
100 | - 'Authorization' => 'Bearer ' . $this->accessToken(), |
|
100 | + 'Authorization' => 'Bearer '.$this->accessToken(), |
|
101 | 101 | ]; |
102 | 102 | // If we have merchant credentials then we are onboard and can do requests on behalf of the seller. |
103 | 103 | if ($this->partner_client_id && $this->payer_id) { |
@@ -108,8 +108,8 @@ discard block |
||
108 | 108 | 'iss' => $this->partner_client_id, |
109 | 109 | 'payer_id' => $this->payer_id, |
110 | 110 | ]; |
111 | - $default_headers['PayPal-Auth-Assertion'] = base64_encode(json_encode($assertion1, JSON_HEX_APOS)) . '.' . |
|
112 | - base64_encode(json_encode($assertion2, JSON_HEX_APOS)) . '.'; |
|
111 | + $default_headers['PayPal-Auth-Assertion'] = base64_encode(json_encode($assertion1, JSON_HEX_APOS)).'.'. |
|
112 | + base64_encode(json_encode($assertion2, JSON_HEX_APOS)).'.'; |
|
113 | 113 | } |
114 | 114 | $request_parameters['headers'] = array_merge($default_headers, $headers); |
115 | 115 | // Add body if this is a POST request. |
@@ -23,254 +23,254 @@ |
||
23 | 23 | */ |
24 | 24 | class CreateOrder extends OrdersApi |
25 | 25 | { |
26 | - /** |
|
27 | - * Line items total. |
|
28 | - * |
|
29 | - * @var float |
|
30 | - */ |
|
31 | - protected $items_total = 0; |
|
26 | + /** |
|
27 | + * Line items total. |
|
28 | + * |
|
29 | + * @var float |
|
30 | + */ |
|
31 | + protected $items_total = 0; |
|
32 | 32 | |
33 | - /** |
|
34 | - * Promotions total. |
|
35 | - * |
|
36 | - * @var float |
|
37 | - */ |
|
38 | - protected $promos_total = 0; |
|
33 | + /** |
|
34 | + * Promotions total. |
|
35 | + * |
|
36 | + * @var float |
|
37 | + */ |
|
38 | + protected $promos_total = 0; |
|
39 | 39 | |
40 | - /** |
|
41 | - * Tax total. |
|
42 | - * |
|
43 | - * @var float |
|
44 | - */ |
|
45 | - protected $tax_total = 0; |
|
40 | + /** |
|
41 | + * Tax total. |
|
42 | + * |
|
43 | + * @var float |
|
44 | + */ |
|
45 | + protected $tax_total = 0; |
|
46 | 46 | |
47 | - /** |
|
48 | - * Currency. |
|
49 | - * |
|
50 | - * @var string |
|
51 | - */ |
|
52 | - protected string $currency_code; |
|
47 | + /** |
|
48 | + * Currency. |
|
49 | + * |
|
50 | + * @var string |
|
51 | + */ |
|
52 | + protected string $currency_code; |
|
53 | 53 | |
54 | - /** |
|
55 | - * Billing info. |
|
56 | - * |
|
57 | - * @var array |
|
58 | - */ |
|
59 | - protected array $billing_info; |
|
54 | + /** |
|
55 | + * Billing info. |
|
56 | + * |
|
57 | + * @var array |
|
58 | + */ |
|
59 | + protected array $billing_info; |
|
60 | 60 | |
61 | - /** |
|
62 | - * Transaction this order is for. |
|
63 | - * |
|
64 | - * @var EE_Transaction |
|
65 | - */ |
|
66 | - protected EE_Transaction $transaction; |
|
61 | + /** |
|
62 | + * Transaction this order is for. |
|
63 | + * |
|
64 | + * @var EE_Transaction |
|
65 | + */ |
|
66 | + protected EE_Transaction $transaction; |
|
67 | 67 | |
68 | 68 | |
69 | - /** |
|
70 | - * CreateOrder constructor. |
|
71 | - * |
|
72 | - * @param PayPalApi $api |
|
73 | - * @param EE_Transaction $transaction |
|
74 | - * @param array $billing_info |
|
75 | - */ |
|
76 | - public function __construct(PayPalApi $api, EE_Transaction $transaction, array $billing_info) |
|
77 | - { |
|
78 | - parent::__construct($api); |
|
79 | - $this->transaction = $transaction; |
|
80 | - $this->currency_code = CurrencyManager::currencyCode(); |
|
81 | - $this->sanitizeRequestParameters($billing_info); |
|
82 | - } |
|
69 | + /** |
|
70 | + * CreateOrder constructor. |
|
71 | + * |
|
72 | + * @param PayPalApi $api |
|
73 | + * @param EE_Transaction $transaction |
|
74 | + * @param array $billing_info |
|
75 | + */ |
|
76 | + public function __construct(PayPalApi $api, EE_Transaction $transaction, array $billing_info) |
|
77 | + { |
|
78 | + parent::__construct($api); |
|
79 | + $this->transaction = $transaction; |
|
80 | + $this->currency_code = CurrencyManager::currencyCode(); |
|
81 | + $this->sanitizeRequestParameters($billing_info); |
|
82 | + } |
|
83 | 83 | |
84 | 84 | |
85 | - /** |
|
86 | - * Sanitize the array of billing form data. |
|
87 | - * |
|
88 | - * @param array $billing_info |
|
89 | - * @return void |
|
90 | - */ |
|
91 | - public function sanitizeRequestParameters(array $billing_info): void |
|
92 | - { |
|
93 | - $email_validator = new Basic(); |
|
94 | - $sanitizer = new RequestSanitizer($email_validator); |
|
95 | - foreach ($billing_info as $item => $value) |
|
96 | - { |
|
97 | - $this->billing_info[ $item ] = $sanitizer->clean($value); |
|
98 | - } |
|
99 | - } |
|
85 | + /** |
|
86 | + * Sanitize the array of billing form data. |
|
87 | + * |
|
88 | + * @param array $billing_info |
|
89 | + * @return void |
|
90 | + */ |
|
91 | + public function sanitizeRequestParameters(array $billing_info): void |
|
92 | + { |
|
93 | + $email_validator = new Basic(); |
|
94 | + $sanitizer = new RequestSanitizer($email_validator); |
|
95 | + foreach ($billing_info as $item => $value) |
|
96 | + { |
|
97 | + $this->billing_info[ $item ] = $sanitizer->clean($value); |
|
98 | + } |
|
99 | + } |
|
100 | 100 | |
101 | 101 | |
102 | - /** |
|
103 | - * Create PayPal Order. |
|
104 | - * |
|
105 | - * @return array |
|
106 | - * @throws EE_Error |
|
107 | - * @throws ReflectionException |
|
108 | - */ |
|
109 | - public function create(): array |
|
110 | - { |
|
111 | - $order_parameters = $this->getParameters(); |
|
112 | - // Create Order request. |
|
113 | - $create_response = $this->api->sendRequest($order_parameters, $this->request_url); |
|
114 | - return $this->validateOrder($create_response, $order_parameters); |
|
115 | - } |
|
102 | + /** |
|
103 | + * Create PayPal Order. |
|
104 | + * |
|
105 | + * @return array |
|
106 | + * @throws EE_Error |
|
107 | + * @throws ReflectionException |
|
108 | + */ |
|
109 | + public function create(): array |
|
110 | + { |
|
111 | + $order_parameters = $this->getParameters(); |
|
112 | + // Create Order request. |
|
113 | + $create_response = $this->api->sendRequest($order_parameters, $this->request_url); |
|
114 | + return $this->validateOrder($create_response, $order_parameters); |
|
115 | + } |
|
116 | 116 | |
117 | 117 | |
118 | - /** |
|
119 | - * Form order parameters. |
|
120 | - * |
|
121 | - * @return array |
|
122 | - * @throws EE_Error |
|
123 | - * @throws ReflectionException |
|
124 | - */ |
|
125 | - protected function getParameters(): array |
|
126 | - { |
|
127 | - $registrant = $this->transaction->primary_registration(); |
|
128 | - $attendee = $registrant->attendee(); |
|
129 | - $event = $registrant->event(); |
|
130 | - $description = $event->name() ?: sprintf( |
|
131 | - esc_html__('Tickets for an event at %1$s', 'event_espresso'), |
|
132 | - get_bloginfo('name') |
|
133 | - ); |
|
134 | - return [ |
|
135 | - 'intent' => 'CAPTURE', |
|
136 | - 'purchase_units' => [ |
|
137 | - [ |
|
138 | - 'custom_id' => $this->transaction->ID(), |
|
139 | - 'description' => substr(wp_strip_all_tags($description), 0, 125), |
|
140 | - 'items' => $this->getLineItems(), |
|
141 | - 'amount' => [ |
|
142 | - 'value' => $this->transaction->total(), |
|
143 | - 'currency_code' => $this->currency_code, |
|
144 | - 'breakdown' => $this->getBreakdown(), |
|
145 | - ], |
|
146 | - ], |
|
147 | - ], |
|
148 | - 'application_context' => [ |
|
149 | - 'shipping_preference' => 'NO_SHIPPING', |
|
150 | - 'user_action' => 'PAY_NOW', |
|
151 | - ], |
|
152 | - 'payer' => [ |
|
153 | - 'email_address' => $attendee->email(), |
|
154 | - 'name' => [ |
|
155 | - 'given_name' => $attendee->fname(), |
|
156 | - 'surname' => $attendee->lname(), |
|
118 | + /** |
|
119 | + * Form order parameters. |
|
120 | + * |
|
121 | + * @return array |
|
122 | + * @throws EE_Error |
|
123 | + * @throws ReflectionException |
|
124 | + */ |
|
125 | + protected function getParameters(): array |
|
126 | + { |
|
127 | + $registrant = $this->transaction->primary_registration(); |
|
128 | + $attendee = $registrant->attendee(); |
|
129 | + $event = $registrant->event(); |
|
130 | + $description = $event->name() ?: sprintf( |
|
131 | + esc_html__('Tickets for an event at %1$s', 'event_espresso'), |
|
132 | + get_bloginfo('name') |
|
133 | + ); |
|
134 | + return [ |
|
135 | + 'intent' => 'CAPTURE', |
|
136 | + 'purchase_units' => [ |
|
137 | + [ |
|
138 | + 'custom_id' => $this->transaction->ID(), |
|
139 | + 'description' => substr(wp_strip_all_tags($description), 0, 125), |
|
140 | + 'items' => $this->getLineItems(), |
|
141 | + 'amount' => [ |
|
142 | + 'value' => $this->transaction->total(), |
|
143 | + 'currency_code' => $this->currency_code, |
|
144 | + 'breakdown' => $this->getBreakdown(), |
|
145 | + ], |
|
146 | + ], |
|
147 | + ], |
|
148 | + 'application_context' => [ |
|
149 | + 'shipping_preference' => 'NO_SHIPPING', |
|
150 | + 'user_action' => 'PAY_NOW', |
|
151 | + ], |
|
152 | + 'payer' => [ |
|
153 | + 'email_address' => $attendee->email(), |
|
154 | + 'name' => [ |
|
155 | + 'given_name' => $attendee->fname(), |
|
156 | + 'surname' => $attendee->lname(), |
|
157 | 157 | |
158 | - ], |
|
159 | - ], |
|
160 | - ]; |
|
161 | - } |
|
158 | + ], |
|
159 | + ], |
|
160 | + ]; |
|
161 | + } |
|
162 | 162 | |
163 | 163 | |
164 | - /** |
|
165 | - * Itemize the payment. List all the line items, discounts and taxes. |
|
166 | - * |
|
167 | - * @return array |
|
168 | - * @throws EE_Error|ReflectionException |
|
169 | - */ |
|
170 | - protected function getLineItems(): array |
|
171 | - { |
|
172 | - // Order line items. |
|
173 | - $line_items = []; |
|
174 | - $event_line_items = $this->transaction->items_purchased(); |
|
175 | - // List actual line items. |
|
176 | - foreach ($event_line_items as $line_item) { |
|
177 | - if ($line_item instanceof EE_Line_Item && $line_item->OBJ_type() !== 'Promotion') { |
|
178 | - $item_money = $line_item->unit_price(); |
|
179 | - $li_description = $line_item->desc() ?? esc_html__('Event Ticket', 'event_espresso'); |
|
180 | - $line_items [] = [ |
|
181 | - 'name' => substr(wp_strip_all_tags($line_item->name()), 0, 126), |
|
182 | - 'quantity' => $line_item->quantity(), |
|
183 | - 'description' => substr(wp_strip_all_tags($li_description), 0, 125), |
|
184 | - 'unit_amount' => [ |
|
185 | - 'currency_code' => $this->currency_code, |
|
186 | - 'value' => $item_money, |
|
187 | - ], |
|
188 | - 'category' => 'DIGITAL_GOODS', |
|
189 | - ]; |
|
190 | - // Line item total. |
|
191 | - $this->items_total += $line_item->pretaxTotal(); |
|
192 | - } elseif ($line_item->OBJ_type() === 'Promotion') { |
|
193 | - // Promotions total. |
|
194 | - $this->promos_total += $line_item->total(); |
|
195 | - } |
|
196 | - } |
|
197 | - // Make sure we have an absolute number with only two decimal laces. |
|
198 | - $this->items_total = CurrencyManager::normalizeValue($this->items_total); |
|
199 | - $this->promos_total = CurrencyManager::normalizeValue($this->promos_total); |
|
200 | - $this->countTaxTotal(); |
|
201 | - return $line_items; |
|
202 | - } |
|
164 | + /** |
|
165 | + * Itemize the payment. List all the line items, discounts and taxes. |
|
166 | + * |
|
167 | + * @return array |
|
168 | + * @throws EE_Error|ReflectionException |
|
169 | + */ |
|
170 | + protected function getLineItems(): array |
|
171 | + { |
|
172 | + // Order line items. |
|
173 | + $line_items = []; |
|
174 | + $event_line_items = $this->transaction->items_purchased(); |
|
175 | + // List actual line items. |
|
176 | + foreach ($event_line_items as $line_item) { |
|
177 | + if ($line_item instanceof EE_Line_Item && $line_item->OBJ_type() !== 'Promotion') { |
|
178 | + $item_money = $line_item->unit_price(); |
|
179 | + $li_description = $line_item->desc() ?? esc_html__('Event Ticket', 'event_espresso'); |
|
180 | + $line_items [] = [ |
|
181 | + 'name' => substr(wp_strip_all_tags($line_item->name()), 0, 126), |
|
182 | + 'quantity' => $line_item->quantity(), |
|
183 | + 'description' => substr(wp_strip_all_tags($li_description), 0, 125), |
|
184 | + 'unit_amount' => [ |
|
185 | + 'currency_code' => $this->currency_code, |
|
186 | + 'value' => $item_money, |
|
187 | + ], |
|
188 | + 'category' => 'DIGITAL_GOODS', |
|
189 | + ]; |
|
190 | + // Line item total. |
|
191 | + $this->items_total += $line_item->pretaxTotal(); |
|
192 | + } elseif ($line_item->OBJ_type() === 'Promotion') { |
|
193 | + // Promotions total. |
|
194 | + $this->promos_total += $line_item->total(); |
|
195 | + } |
|
196 | + } |
|
197 | + // Make sure we have an absolute number with only two decimal laces. |
|
198 | + $this->items_total = CurrencyManager::normalizeValue($this->items_total); |
|
199 | + $this->promos_total = CurrencyManager::normalizeValue($this->promos_total); |
|
200 | + $this->countTaxTotal(); |
|
201 | + return $line_items; |
|
202 | + } |
|
203 | 203 | |
204 | 204 | |
205 | - /** |
|
206 | - * Count the tax total. |
|
207 | - * |
|
208 | - * @return void |
|
209 | - * @throws EE_Error|ReflectionException |
|
210 | - */ |
|
211 | - protected function countTaxTotal(): void |
|
212 | - { |
|
213 | - // List taxes. |
|
214 | - $this->tax_total = 0; |
|
215 | - $tax_items = $this->transaction->tax_items(); |
|
216 | - foreach ($tax_items as $tax_item) { |
|
217 | - $this->tax_total += $tax_item->total(); |
|
218 | - } |
|
219 | - $this->tax_total = CurrencyManager::normalizeValue($this->tax_total); |
|
220 | - } |
|
205 | + /** |
|
206 | + * Count the tax total. |
|
207 | + * |
|
208 | + * @return void |
|
209 | + * @throws EE_Error|ReflectionException |
|
210 | + */ |
|
211 | + protected function countTaxTotal(): void |
|
212 | + { |
|
213 | + // List taxes. |
|
214 | + $this->tax_total = 0; |
|
215 | + $tax_items = $this->transaction->tax_items(); |
|
216 | + foreach ($tax_items as $tax_item) { |
|
217 | + $this->tax_total += $tax_item->total(); |
|
218 | + } |
|
219 | + $this->tax_total = CurrencyManager::normalizeValue($this->tax_total); |
|
220 | + } |
|
221 | 221 | |
222 | 222 | |
223 | - /** |
|
224 | - * Itemize the payment the breakdown list. |
|
225 | - * |
|
226 | - * @return array |
|
227 | - */ |
|
228 | - protected function getBreakdown(): array |
|
229 | - { |
|
230 | - $breakdown['item_total'] = [ |
|
231 | - 'currency_code' => $this->currency_code, |
|
232 | - 'value' => $this->items_total, |
|
233 | - ]; |
|
234 | - $breakdown['tax_total'] = [ |
|
235 | - 'currency_code' => $this->currency_code, |
|
236 | - 'value' => $this->tax_total, |
|
237 | - ]; |
|
238 | - $breakdown['discount'] = [ |
|
239 | - 'currency_code' => $this->currency_code, |
|
240 | - 'value' => abs($this->promos_total), |
|
241 | - ]; |
|
242 | - return $breakdown; |
|
243 | - } |
|
223 | + /** |
|
224 | + * Itemize the payment the breakdown list. |
|
225 | + * |
|
226 | + * @return array |
|
227 | + */ |
|
228 | + protected function getBreakdown(): array |
|
229 | + { |
|
230 | + $breakdown['item_total'] = [ |
|
231 | + 'currency_code' => $this->currency_code, |
|
232 | + 'value' => $this->items_total, |
|
233 | + ]; |
|
234 | + $breakdown['tax_total'] = [ |
|
235 | + 'currency_code' => $this->currency_code, |
|
236 | + 'value' => $this->tax_total, |
|
237 | + ]; |
|
238 | + $breakdown['discount'] = [ |
|
239 | + 'currency_code' => $this->currency_code, |
|
240 | + 'value' => abs($this->promos_total), |
|
241 | + ]; |
|
242 | + return $breakdown; |
|
243 | + } |
|
244 | 244 | |
245 | 245 | |
246 | - /** |
|
247 | - * Makes sure that we have received an Order back from the API call. |
|
248 | - * |
|
249 | - * @param $response |
|
250 | - * @param $parameters |
|
251 | - * @return array |
|
252 | - */ |
|
253 | - public function validateOrder($response, $parameters): array |
|
254 | - { |
|
255 | - if (! empty($response['error'])) { |
|
256 | - return $response; |
|
257 | - } |
|
258 | - if (! isset($response['id'])) { |
|
259 | - $message = esc_html__('Unexpected response. Unable to find the order.', 'event_espresso'); |
|
260 | - try { |
|
261 | - PayPalLogger::errorLog( |
|
262 | - $message, |
|
263 | - [$this->request_url, $parameters, $response], |
|
264 | - $this->transaction->payment_method() |
|
265 | - ); |
|
266 | - } catch (EE_Error | ReflectionException $e) { |
|
267 | - // Just continue. |
|
268 | - } |
|
269 | - return [ |
|
270 | - 'error' => $response['error'] ?? 'missing_order', |
|
271 | - 'message' => $response['message'] ?? $message, |
|
272 | - ]; |
|
273 | - } |
|
274 | - return $response; |
|
275 | - } |
|
246 | + /** |
|
247 | + * Makes sure that we have received an Order back from the API call. |
|
248 | + * |
|
249 | + * @param $response |
|
250 | + * @param $parameters |
|
251 | + * @return array |
|
252 | + */ |
|
253 | + public function validateOrder($response, $parameters): array |
|
254 | + { |
|
255 | + if (! empty($response['error'])) { |
|
256 | + return $response; |
|
257 | + } |
|
258 | + if (! isset($response['id'])) { |
|
259 | + $message = esc_html__('Unexpected response. Unable to find the order.', 'event_espresso'); |
|
260 | + try { |
|
261 | + PayPalLogger::errorLog( |
|
262 | + $message, |
|
263 | + [$this->request_url, $parameters, $response], |
|
264 | + $this->transaction->payment_method() |
|
265 | + ); |
|
266 | + } catch (EE_Error | ReflectionException $e) { |
|
267 | + // Just continue. |
|
268 | + } |
|
269 | + return [ |
|
270 | + 'error' => $response['error'] ?? 'missing_order', |
|
271 | + 'message' => $response['message'] ?? $message, |
|
272 | + ]; |
|
273 | + } |
|
274 | + return $response; |
|
275 | + } |
|
276 | 276 | } |
@@ -94,7 +94,7 @@ discard block |
||
94 | 94 | $sanitizer = new RequestSanitizer($email_validator); |
95 | 95 | foreach ($billing_info as $item => $value) |
96 | 96 | { |
97 | - $this->billing_info[ $item ] = $sanitizer->clean($value); |
|
97 | + $this->billing_info[$item] = $sanitizer->clean($value); |
|
98 | 98 | } |
99 | 99 | } |
100 | 100 | |
@@ -231,11 +231,11 @@ discard block |
||
231 | 231 | 'currency_code' => $this->currency_code, |
232 | 232 | 'value' => $this->items_total, |
233 | 233 | ]; |
234 | - $breakdown['tax_total'] = [ |
|
234 | + $breakdown['tax_total'] = [ |
|
235 | 235 | 'currency_code' => $this->currency_code, |
236 | 236 | 'value' => $this->tax_total, |
237 | 237 | ]; |
238 | - $breakdown['discount'] = [ |
|
238 | + $breakdown['discount'] = [ |
|
239 | 239 | 'currency_code' => $this->currency_code, |
240 | 240 | 'value' => abs($this->promos_total), |
241 | 241 | ]; |
@@ -252,10 +252,10 @@ discard block |
||
252 | 252 | */ |
253 | 253 | public function validateOrder($response, $parameters): array |
254 | 254 | { |
255 | - if (! empty($response['error'])) { |
|
255 | + if ( ! empty($response['error'])) { |
|
256 | 256 | return $response; |
257 | 257 | } |
258 | - if (! isset($response['id'])) { |
|
258 | + if ( ! isset($response['id'])) { |
|
259 | 259 | $message = esc_html__('Unexpected response. Unable to find the order.', 'event_espresso'); |
260 | 260 | try { |
261 | 261 | PayPalLogger::errorLog( |
@@ -20,74 +20,74 @@ |
||
20 | 20 | */ |
21 | 21 | class CaptureOrder extends OrdersApi |
22 | 22 | { |
23 | - /** |
|
24 | - * Currency. |
|
25 | - * |
|
26 | - * @var string |
|
27 | - */ |
|
28 | - protected string $currency_code; |
|
23 | + /** |
|
24 | + * Currency. |
|
25 | + * |
|
26 | + * @var string |
|
27 | + */ |
|
28 | + protected string $currency_code; |
|
29 | 29 | |
30 | - /** |
|
31 | - * Transaction this order is for. |
|
32 | - * |
|
33 | - * @var EE_Transaction |
|
34 | - */ |
|
35 | - protected EE_Transaction $transaction; |
|
30 | + /** |
|
31 | + * Transaction this order is for. |
|
32 | + * |
|
33 | + * @var EE_Transaction |
|
34 | + */ |
|
35 | + protected EE_Transaction $transaction; |
|
36 | 36 | |
37 | 37 | |
38 | - /** |
|
39 | - * CaptureOrder constructor. |
|
40 | - * |
|
41 | - * @param PayPalApi $api |
|
42 | - * @param EE_Transaction $transaction |
|
43 | - * @param string $order_id |
|
44 | - */ |
|
45 | - public function __construct(PayPalApi $api, EE_Transaction $transaction, string $order_id) |
|
46 | - { |
|
47 | - parent::__construct($api); |
|
48 | - $this->transaction = $transaction; |
|
49 | - $this->currency_code = CurrencyManager::currencyCode(); |
|
50 | - $this->request_url = $this->request_url . $order_id . '/capture'; |
|
51 | - } |
|
38 | + /** |
|
39 | + * CaptureOrder constructor. |
|
40 | + * |
|
41 | + * @param PayPalApi $api |
|
42 | + * @param EE_Transaction $transaction |
|
43 | + * @param string $order_id |
|
44 | + */ |
|
45 | + public function __construct(PayPalApi $api, EE_Transaction $transaction, string $order_id) |
|
46 | + { |
|
47 | + parent::__construct($api); |
|
48 | + $this->transaction = $transaction; |
|
49 | + $this->currency_code = CurrencyManager::currencyCode(); |
|
50 | + $this->request_url = $this->request_url . $order_id . '/capture'; |
|
51 | + } |
|
52 | 52 | |
53 | 53 | |
54 | - /** |
|
55 | - * Capture payment for PayPal Order. |
|
56 | - * |
|
57 | - * @return array |
|
58 | - */ |
|
59 | - public function capture(): array |
|
60 | - { |
|
61 | - // Create Order request. |
|
62 | - $capture_response = $this->api->sendRequest([], $this->request_url); |
|
63 | - return $this->validateOrder($capture_response); |
|
64 | - } |
|
54 | + /** |
|
55 | + * Capture payment for PayPal Order. |
|
56 | + * |
|
57 | + * @return array |
|
58 | + */ |
|
59 | + public function capture(): array |
|
60 | + { |
|
61 | + // Create Order request. |
|
62 | + $capture_response = $this->api->sendRequest([], $this->request_url); |
|
63 | + return $this->validateOrder($capture_response); |
|
64 | + } |
|
65 | 65 | |
66 | 66 | |
67 | - /** |
|
68 | - * Makes sure that we have received an Order back from the API call. |
|
69 | - * |
|
70 | - * @param $response |
|
71 | - * @return array |
|
72 | - */ |
|
73 | - public function validateOrder($response): array |
|
74 | - { |
|
75 | - if (! empty($response['error'])) { |
|
76 | - return $response; |
|
77 | - } |
|
78 | - if (! isset($response['id'])) { |
|
79 | - $message = esc_html__('Unexpected response. No order returned.', 'event_espresso'); |
|
80 | - try { |
|
81 | - PayPalLogger::errorLog($message, [$this->request_url, $response], $this->transaction->payment_method()); |
|
82 | - } catch (EE_Error | ReflectionException $e) { |
|
83 | - // Just continue. |
|
84 | - } |
|
85 | - return [ |
|
86 | - 'error' => $response['error'] ?? 'missing_order', |
|
87 | - 'message' => $response['message'] ?? $message, |
|
88 | - 'name' => $response['name'] ?? 'UNKNOWN_ERROR', |
|
89 | - ]; |
|
90 | - } |
|
91 | - return $response; |
|
92 | - } |
|
67 | + /** |
|
68 | + * Makes sure that we have received an Order back from the API call. |
|
69 | + * |
|
70 | + * @param $response |
|
71 | + * @return array |
|
72 | + */ |
|
73 | + public function validateOrder($response): array |
|
74 | + { |
|
75 | + if (! empty($response['error'])) { |
|
76 | + return $response; |
|
77 | + } |
|
78 | + if (! isset($response['id'])) { |
|
79 | + $message = esc_html__('Unexpected response. No order returned.', 'event_espresso'); |
|
80 | + try { |
|
81 | + PayPalLogger::errorLog($message, [$this->request_url, $response], $this->transaction->payment_method()); |
|
82 | + } catch (EE_Error | ReflectionException $e) { |
|
83 | + // Just continue. |
|
84 | + } |
|
85 | + return [ |
|
86 | + 'error' => $response['error'] ?? 'missing_order', |
|
87 | + 'message' => $response['message'] ?? $message, |
|
88 | + 'name' => $response['name'] ?? 'UNKNOWN_ERROR', |
|
89 | + ]; |
|
90 | + } |
|
91 | + return $response; |
|
92 | + } |
|
93 | 93 | } |
@@ -15,25 +15,25 @@ |
||
15 | 15 | */ |
16 | 16 | abstract class OrdersApi |
17 | 17 | { |
18 | - /** |
|
19 | - * @var PayPalApi |
|
20 | - */ |
|
21 | - protected PayPalApi $api; |
|
18 | + /** |
|
19 | + * @var PayPalApi |
|
20 | + */ |
|
21 | + protected PayPalApi $api; |
|
22 | 22 | |
23 | - /** |
|
24 | - * @var string |
|
25 | - */ |
|
26 | - protected string $request_url; |
|
23 | + /** |
|
24 | + * @var string |
|
25 | + */ |
|
26 | + protected string $request_url; |
|
27 | 27 | |
28 | 28 | |
29 | - /** |
|
30 | - * Orders API constructor. |
|
31 | - * |
|
32 | - * @param PayPalApi $api |
|
33 | - */ |
|
34 | - public function __construct(PayPalApi $api) |
|
35 | - { |
|
36 | - $this->api = $api; |
|
37 | - $this->request_url = $this->api->apiEndpoint() . 'checkout/orders/'; |
|
38 | - } |
|
29 | + /** |
|
30 | + * Orders API constructor. |
|
31 | + * |
|
32 | + * @param PayPalApi $api |
|
33 | + */ |
|
34 | + public function __construct(PayPalApi $api) |
|
35 | + { |
|
36 | + $this->api = $api; |
|
37 | + $this->request_url = $this->api->apiEndpoint() . 'checkout/orders/'; |
|
38 | + } |
|
39 | 39 | } |
@@ -15,72 +15,72 @@ |
||
15 | 15 | */ |
16 | 16 | abstract class PayPalApi |
17 | 17 | { |
18 | - /** |
|
19 | - * @var bool Debug mode enabled ? |
|
20 | - */ |
|
21 | - protected bool $sandbox_mode; |
|
18 | + /** |
|
19 | + * @var bool Debug mode enabled ? |
|
20 | + */ |
|
21 | + protected bool $sandbox_mode; |
|
22 | 22 | |
23 | - /** |
|
24 | - * API request/response validation helper. |
|
25 | - * |
|
26 | - * @var ResponseInspector |
|
27 | - */ |
|
28 | - protected ResponseInspector $inspector; |
|
23 | + /** |
|
24 | + * API request/response validation helper. |
|
25 | + * |
|
26 | + * @var ResponseInspector |
|
27 | + */ |
|
28 | + protected ResponseInspector $inspector; |
|
29 | 29 | |
30 | - /** |
|
31 | - * @var string PayPal API endpoint. |
|
32 | - */ |
|
33 | - protected string $api_endpoint = ''; |
|
30 | + /** |
|
31 | + * @var string PayPal API endpoint. |
|
32 | + */ |
|
33 | + protected string $api_endpoint = ''; |
|
34 | 34 | |
35 | 35 | |
36 | - /** |
|
37 | - * @param bool $sandbox_mode |
|
38 | - */ |
|
39 | - public function __construct(bool $sandbox_mode = true) { |
|
40 | - $this->sandbox_mode = $sandbox_mode; |
|
41 | - // Is this a sandbox request. |
|
42 | - $this->api_endpoint = $this->sandbox_mode |
|
43 | - ? 'https://api-m.sandbox.paypal.com/v2/' |
|
44 | - : 'https://api-m.paypal.com/v2/'; |
|
45 | - $this->inspector = new ResponseInspector(); |
|
46 | - } |
|
36 | + /** |
|
37 | + * @param bool $sandbox_mode |
|
38 | + */ |
|
39 | + public function __construct(bool $sandbox_mode = true) { |
|
40 | + $this->sandbox_mode = $sandbox_mode; |
|
41 | + // Is this a sandbox request. |
|
42 | + $this->api_endpoint = $this->sandbox_mode |
|
43 | + ? 'https://api-m.sandbox.paypal.com/v2/' |
|
44 | + : 'https://api-m.paypal.com/v2/'; |
|
45 | + $this->inspector = new ResponseInspector(); |
|
46 | + } |
|
47 | 47 | |
48 | 48 | |
49 | - /** |
|
50 | - * Send an API request. |
|
51 | - * |
|
52 | - * @param string $endpoint |
|
53 | - * @param array $request_parameters |
|
54 | - * @return Object|array |
|
55 | - */ |
|
56 | - public function response(string $endpoint, array $request_parameters) |
|
57 | - { |
|
58 | - // Sent the API request. |
|
59 | - $response = wp_remote_request($endpoint, $request_parameters); |
|
60 | - // Validate the response. |
|
61 | - $this->inspector->validateResponse($response); |
|
62 | - if (! $this->inspector->isValid()) { |
|
63 | - PayPalLogger::errorLog($this->inspector->error()['message'], [$endpoint, $request_parameters, $response]); |
|
64 | - return $this->inspector->error(); |
|
65 | - } |
|
66 | - // Decode the parameters. |
|
67 | - $api_response = json_decode($response['body'], true); |
|
68 | - // Validate parameters. |
|
69 | - $this->inspector->validateParameters($api_response); |
|
70 | - if (! $this->inspector->isValid()) { |
|
71 | - PayPalLogger::errorLog($this->inspector->error()['message'], [$endpoint, $request_parameters, $response]); |
|
72 | - return $this->inspector->error(); |
|
73 | - } |
|
74 | - // All seem ok, return the response. |
|
75 | - return $api_response; |
|
76 | - } |
|
49 | + /** |
|
50 | + * Send an API request. |
|
51 | + * |
|
52 | + * @param string $endpoint |
|
53 | + * @param array $request_parameters |
|
54 | + * @return Object|array |
|
55 | + */ |
|
56 | + public function response(string $endpoint, array $request_parameters) |
|
57 | + { |
|
58 | + // Sent the API request. |
|
59 | + $response = wp_remote_request($endpoint, $request_parameters); |
|
60 | + // Validate the response. |
|
61 | + $this->inspector->validateResponse($response); |
|
62 | + if (! $this->inspector->isValid()) { |
|
63 | + PayPalLogger::errorLog($this->inspector->error()['message'], [$endpoint, $request_parameters, $response]); |
|
64 | + return $this->inspector->error(); |
|
65 | + } |
|
66 | + // Decode the parameters. |
|
67 | + $api_response = json_decode($response['body'], true); |
|
68 | + // Validate parameters. |
|
69 | + $this->inspector->validateParameters($api_response); |
|
70 | + if (! $this->inspector->isValid()) { |
|
71 | + PayPalLogger::errorLog($this->inspector->error()['message'], [$endpoint, $request_parameters, $response]); |
|
72 | + return $this->inspector->error(); |
|
73 | + } |
|
74 | + // All seem ok, return the response. |
|
75 | + return $api_response; |
|
76 | + } |
|
77 | 77 | |
78 | 78 | |
79 | - /** |
|
80 | - * @return string |
|
81 | - */ |
|
82 | - public function apiEndpoint(): string |
|
83 | - { |
|
84 | - return $this->api_endpoint; |
|
85 | - } |
|
79 | + /** |
|
80 | + * @return string |
|
81 | + */ |
|
82 | + public function apiEndpoint(): string |
|
83 | + { |
|
84 | + return $this->api_endpoint; |
|
85 | + } |
|
86 | 86 | } |
@@ -42,7 +42,7 @@ discard block |
||
42 | 42 | $this->api_endpoint = $this->sandbox_mode |
43 | 43 | ? 'https://api-m.sandbox.paypal.com/v2/' |
44 | 44 | : 'https://api-m.paypal.com/v2/'; |
45 | - $this->inspector = new ResponseInspector(); |
|
45 | + $this->inspector = new ResponseInspector(); |
|
46 | 46 | } |
47 | 47 | |
48 | 48 | |
@@ -59,7 +59,7 @@ discard block |
||
59 | 59 | $response = wp_remote_request($endpoint, $request_parameters); |
60 | 60 | // Validate the response. |
61 | 61 | $this->inspector->validateResponse($response); |
62 | - if (! $this->inspector->isValid()) { |
|
62 | + if ( ! $this->inspector->isValid()) { |
|
63 | 63 | PayPalLogger::errorLog($this->inspector->error()['message'], [$endpoint, $request_parameters, $response]); |
64 | 64 | return $this->inspector->error(); |
65 | 65 | } |
@@ -67,7 +67,7 @@ discard block |
||
67 | 67 | $api_response = json_decode($response['body'], true); |
68 | 68 | // Validate parameters. |
69 | 69 | $this->inspector->validateParameters($api_response); |
70 | - if (! $this->inspector->isValid()) { |
|
70 | + if ( ! $this->inspector->isValid()) { |
|
71 | 71 | PayPalLogger::errorLog($this->inspector->error()['message'], [$endpoint, $request_parameters, $response]); |
72 | 72 | return $this->inspector->error(); |
73 | 73 | } |
@@ -13,115 +13,115 @@ |
||
13 | 13 | */ |
14 | 14 | class FirstPartyPayPalApi extends PayPalApi |
15 | 15 | { |
16 | - /** |
|
17 | - * Client ID. Used to process payments. |
|
18 | - * |
|
19 | - * @var string |
|
20 | - */ |
|
21 | - protected string $client_id = ''; |
|
22 | - |
|
23 | - /** |
|
24 | - * Client secret. Used to process payments. |
|
25 | - * |
|
26 | - * @var string |
|
27 | - */ |
|
28 | - protected string $client_secret = ''; |
|
29 | - |
|
30 | - /** |
|
31 | - * BN Code. Partner-Attribution-Id. |
|
32 | - * |
|
33 | - * @var string |
|
34 | - */ |
|
35 | - protected string $bn_code = ''; |
|
36 | - |
|
37 | - |
|
38 | - /** |
|
39 | - * @param string $client_id |
|
40 | - * @param string $client_secret |
|
41 | - * @param string $bn_code |
|
42 | - * @param bool $sandbox_mode |
|
43 | - */ |
|
44 | - public function __construct(string $client_id, string $client_secret, string $bn_code, bool $sandbox_mode = true) |
|
45 | - { |
|
46 | - parent::__construct($sandbox_mode); |
|
47 | - $this->client_id = $client_id; |
|
48 | - $this->client_secret = $client_secret; |
|
49 | - $this->bn_code = $bn_code; |
|
50 | - } |
|
51 | - |
|
52 | - |
|
53 | - /** |
|
54 | - * Send an API request. |
|
55 | - * |
|
56 | - * @param array $body_parameters |
|
57 | - * @param string $endpoint |
|
58 | - * @param string $method |
|
59 | - * @param array $headers |
|
60 | - * @return Object|array |
|
61 | - */ |
|
62 | - public function sendRequest(array $body_parameters, string $endpoint, string $method = 'POST', array $headers = []) |
|
63 | - { |
|
64 | - $request_parameters = $this->getRequestParameters($body_parameters, $method, $headers); |
|
65 | - return $this->response($endpoint, $request_parameters); |
|
66 | - } |
|
67 | - |
|
68 | - |
|
69 | - /** |
|
70 | - * Build the request parameters. |
|
71 | - * |
|
72 | - * @param array $body_parameters |
|
73 | - * @param string $method |
|
74 | - * @param array $headers |
|
75 | - * @return array |
|
76 | - */ |
|
77 | - private function getRequestParameters(array $body_parameters, string $method, array $headers): array |
|
78 | - { |
|
79 | - $request_parameters = [ |
|
80 | - 'method' => $method, |
|
81 | - 'timeout' => 60, |
|
82 | - 'redirection' => 5, |
|
83 | - 'blocking' => true, |
|
84 | - ]; |
|
85 | - $default_headers = [ |
|
86 | - 'User-Agent' => sanitize_text_field($_SERVER['HTTP_USER_AGENT']), |
|
87 | - 'PayPal-Partner-Attribution-Id' => $this->bn_code, |
|
88 | - 'Content-Type' => 'application/json', |
|
89 | - 'Authorization' => 'Basic ' . base64_encode( |
|
90 | - $this->client_id . ':' . $this->client_secret |
|
91 | - ), |
|
92 | - ]; |
|
93 | - $request_parameters['headers'] = array_merge($default_headers, $headers); |
|
94 | - // Add body if this is a POST request. |
|
95 | - if ($body_parameters && ($method === 'POST' || $method === 'PUT')) { |
|
96 | - $request_parameters['body'] = json_encode($body_parameters); |
|
97 | - } |
|
98 | - return $request_parameters; |
|
99 | - } |
|
100 | - |
|
101 | - |
|
102 | - /** |
|
103 | - * @return string |
|
104 | - */ |
|
105 | - public function clientId(): string |
|
106 | - { |
|
107 | - return $this->client_id; |
|
108 | - } |
|
109 | - |
|
110 | - |
|
111 | - /** |
|
112 | - * @return string |
|
113 | - */ |
|
114 | - public function clientSecret(): string |
|
115 | - { |
|
116 | - return $this->client_secret; |
|
117 | - } |
|
118 | - |
|
119 | - |
|
120 | - /** |
|
121 | - * @return string |
|
122 | - */ |
|
123 | - public function bnCode(): string |
|
124 | - { |
|
125 | - return $this->bn_code; |
|
126 | - } |
|
16 | + /** |
|
17 | + * Client ID. Used to process payments. |
|
18 | + * |
|
19 | + * @var string |
|
20 | + */ |
|
21 | + protected string $client_id = ''; |
|
22 | + |
|
23 | + /** |
|
24 | + * Client secret. Used to process payments. |
|
25 | + * |
|
26 | + * @var string |
|
27 | + */ |
|
28 | + protected string $client_secret = ''; |
|
29 | + |
|
30 | + /** |
|
31 | + * BN Code. Partner-Attribution-Id. |
|
32 | + * |
|
33 | + * @var string |
|
34 | + */ |
|
35 | + protected string $bn_code = ''; |
|
36 | + |
|
37 | + |
|
38 | + /** |
|
39 | + * @param string $client_id |
|
40 | + * @param string $client_secret |
|
41 | + * @param string $bn_code |
|
42 | + * @param bool $sandbox_mode |
|
43 | + */ |
|
44 | + public function __construct(string $client_id, string $client_secret, string $bn_code, bool $sandbox_mode = true) |
|
45 | + { |
|
46 | + parent::__construct($sandbox_mode); |
|
47 | + $this->client_id = $client_id; |
|
48 | + $this->client_secret = $client_secret; |
|
49 | + $this->bn_code = $bn_code; |
|
50 | + } |
|
51 | + |
|
52 | + |
|
53 | + /** |
|
54 | + * Send an API request. |
|
55 | + * |
|
56 | + * @param array $body_parameters |
|
57 | + * @param string $endpoint |
|
58 | + * @param string $method |
|
59 | + * @param array $headers |
|
60 | + * @return Object|array |
|
61 | + */ |
|
62 | + public function sendRequest(array $body_parameters, string $endpoint, string $method = 'POST', array $headers = []) |
|
63 | + { |
|
64 | + $request_parameters = $this->getRequestParameters($body_parameters, $method, $headers); |
|
65 | + return $this->response($endpoint, $request_parameters); |
|
66 | + } |
|
67 | + |
|
68 | + |
|
69 | + /** |
|
70 | + * Build the request parameters. |
|
71 | + * |
|
72 | + * @param array $body_parameters |
|
73 | + * @param string $method |
|
74 | + * @param array $headers |
|
75 | + * @return array |
|
76 | + */ |
|
77 | + private function getRequestParameters(array $body_parameters, string $method, array $headers): array |
|
78 | + { |
|
79 | + $request_parameters = [ |
|
80 | + 'method' => $method, |
|
81 | + 'timeout' => 60, |
|
82 | + 'redirection' => 5, |
|
83 | + 'blocking' => true, |
|
84 | + ]; |
|
85 | + $default_headers = [ |
|
86 | + 'User-Agent' => sanitize_text_field($_SERVER['HTTP_USER_AGENT']), |
|
87 | + 'PayPal-Partner-Attribution-Id' => $this->bn_code, |
|
88 | + 'Content-Type' => 'application/json', |
|
89 | + 'Authorization' => 'Basic ' . base64_encode( |
|
90 | + $this->client_id . ':' . $this->client_secret |
|
91 | + ), |
|
92 | + ]; |
|
93 | + $request_parameters['headers'] = array_merge($default_headers, $headers); |
|
94 | + // Add body if this is a POST request. |
|
95 | + if ($body_parameters && ($method === 'POST' || $method === 'PUT')) { |
|
96 | + $request_parameters['body'] = json_encode($body_parameters); |
|
97 | + } |
|
98 | + return $request_parameters; |
|
99 | + } |
|
100 | + |
|
101 | + |
|
102 | + /** |
|
103 | + * @return string |
|
104 | + */ |
|
105 | + public function clientId(): string |
|
106 | + { |
|
107 | + return $this->client_id; |
|
108 | + } |
|
109 | + |
|
110 | + |
|
111 | + /** |
|
112 | + * @return string |
|
113 | + */ |
|
114 | + public function clientSecret(): string |
|
115 | + { |
|
116 | + return $this->client_secret; |
|
117 | + } |
|
118 | + |
|
119 | + |
|
120 | + /** |
|
121 | + * @return string |
|
122 | + */ |
|
123 | + public function bnCode(): string |
|
124 | + { |
|
125 | + return $this->bn_code; |
|
126 | + } |
|
127 | 127 | } |
@@ -86,8 +86,8 @@ |
||
86 | 86 | 'User-Agent' => sanitize_text_field($_SERVER['HTTP_USER_AGENT']), |
87 | 87 | 'PayPal-Partner-Attribution-Id' => $this->bn_code, |
88 | 88 | 'Content-Type' => 'application/json', |
89 | - 'Authorization' => 'Basic ' . base64_encode( |
|
90 | - $this->client_id . ':' . $this->client_secret |
|
89 | + 'Authorization' => 'Basic '.base64_encode( |
|
90 | + $this->client_id.':'.$this->client_secret |
|
91 | 91 | ), |
92 | 92 | ]; |
93 | 93 | $request_parameters['headers'] = array_merge($default_headers, $headers); |
@@ -17,96 +17,96 @@ |
||
17 | 17 | */ |
18 | 18 | class TrackSellerOnboarding extends PartnersApi |
19 | 19 | { |
20 | - /** |
|
21 | - * Partner ID. |
|
22 | - * |
|
23 | - * @var string |
|
24 | - */ |
|
25 | - protected string $partner_id; |
|
20 | + /** |
|
21 | + * Partner ID. |
|
22 | + * |
|
23 | + * @var string |
|
24 | + */ |
|
25 | + protected string $partner_id; |
|
26 | 26 | |
27 | - /** |
|
28 | - * Seller ID. |
|
29 | - * |
|
30 | - * @var string |
|
31 | - */ |
|
32 | - protected string $seller_id; |
|
27 | + /** |
|
28 | + * Seller ID. |
|
29 | + * |
|
30 | + * @var string |
|
31 | + */ |
|
32 | + protected string $seller_id; |
|
33 | 33 | |
34 | 34 | |
35 | - /** |
|
36 | - * TrackSellerOnboarding constructor. |
|
37 | - * |
|
38 | - * @param PayPalApi $api |
|
39 | - * @param string $partner_id |
|
40 | - * @param string $seller_id |
|
41 | - * @param bool $sandbox_mode |
|
42 | - */ |
|
43 | - public function __construct( |
|
44 | - PayPalApi $api, |
|
45 | - string $partner_id, |
|
46 | - string $seller_id, |
|
47 | - bool $sandbox_mode |
|
48 | - ) { |
|
49 | - parent::__construct($api, $sandbox_mode); |
|
50 | - $this->partner_id = $partner_id; |
|
51 | - $this->seller_id = $seller_id; |
|
52 | - $this->request_url = $this->request_url . "/{$partner_id}/merchant-integrations/{$seller_id}"; |
|
53 | - } |
|
35 | + /** |
|
36 | + * TrackSellerOnboarding constructor. |
|
37 | + * |
|
38 | + * @param PayPalApi $api |
|
39 | + * @param string $partner_id |
|
40 | + * @param string $seller_id |
|
41 | + * @param bool $sandbox_mode |
|
42 | + */ |
|
43 | + public function __construct( |
|
44 | + PayPalApi $api, |
|
45 | + string $partner_id, |
|
46 | + string $seller_id, |
|
47 | + bool $sandbox_mode |
|
48 | + ) { |
|
49 | + parent::__construct($api, $sandbox_mode); |
|
50 | + $this->partner_id = $partner_id; |
|
51 | + $this->seller_id = $seller_id; |
|
52 | + $this->request_url = $this->request_url . "/{$partner_id}/merchant-integrations/{$seller_id}"; |
|
53 | + } |
|
54 | 54 | |
55 | 55 | |
56 | - /** |
|
57 | - * Get the onboarding status and validate it. |
|
58 | - * |
|
59 | - * @return array |
|
60 | - */ |
|
61 | - public function isValid(): array |
|
62 | - { |
|
63 | - // Send GET request. |
|
64 | - $response = $this->api->sendRequest([], $this->request_url, 'GET'); |
|
65 | - return $this->validateStatus($response); |
|
66 | - } |
|
56 | + /** |
|
57 | + * Get the onboarding status and validate it. |
|
58 | + * |
|
59 | + * @return array |
|
60 | + */ |
|
61 | + public function isValid(): array |
|
62 | + { |
|
63 | + // Send GET request. |
|
64 | + $response = $this->api->sendRequest([], $this->request_url, 'GET'); |
|
65 | + return $this->validateStatus($response); |
|
66 | + } |
|
67 | 67 | |
68 | 68 | |
69 | - /** |
|
70 | - * Makes sure that we have received proper parameters and the status is valid. |
|
71 | - * |
|
72 | - * @param array $response |
|
73 | - * @return array |
|
74 | - */ |
|
75 | - public function validateStatus(array $response): array |
|
76 | - { |
|
77 | - if (! empty($response['error'])) { |
|
78 | - return $response; |
|
79 | - } |
|
80 | - if (! empty($response['name']) && ! empty($response['message'])) { |
|
81 | - return ['error' => $response['name'], 'message' => $response['message']]; |
|
82 | - } |
|
83 | - // Check the data we received. |
|
84 | - if ( |
|
85 | - empty($response[ Domain::API_PARAM_TRACK_MERCHANT_ID ]) |
|
86 | - || ! isset($response[ Domain::API_PARAM_PAYMENTS_RECEIVABLE ]) |
|
87 | - || ! isset($response[ Domain::API_PARAM_PRIM_EMAIL_CONFIRMED ]) |
|
88 | - ) { |
|
89 | - $err_msg = esc_html__('Missing required data for validating the onboarding status.', 'event_espresso'); |
|
90 | - PayPalLogger::errorLog($err_msg, $response); |
|
91 | - return ['error' => 'ONBOARDING_MISSING_REQUIRED_DATA', 'message' => $err_msg]; |
|
92 | - } |
|
93 | - // Now validate the onboarding status. |
|
94 | - if (! $response[ Domain::API_PARAM_PAYMENTS_RECEIVABLE ]) { |
|
95 | - $err_msg = esc_html__( |
|
96 | - 'Your Account has been limited by PayPal. Please check your PayPal account inbox for an email from PayPal to determine the next steps for this.', |
|
97 | - 'event_espresso' |
|
98 | - ); |
|
99 | - PayPalLogger::errorLog($err_msg, $response); |
|
100 | - return ['error' => 'ONBOARDING_LIMITED_BY_PAYPAL', 'message' => $err_msg]; |
|
101 | - } |
|
102 | - if (! $response[ Domain::API_PARAM_PRIM_EMAIL_CONFIRMED ]) { |
|
103 | - $err_msg = esc_html__('Email address not confirmed. Please confirm your email address.', 'event_espresso'); |
|
104 | - PayPalLogger::errorLog($err_msg, $response); |
|
105 | - return ['error' => 'ONBOARDING_CONFIRM_EMAIL', 'message' => $err_msg]; |
|
106 | - } |
|
107 | - return [ |
|
108 | - 'valid' => true, |
|
109 | - 'response' => $response, |
|
110 | - ]; |
|
111 | - } |
|
69 | + /** |
|
70 | + * Makes sure that we have received proper parameters and the status is valid. |
|
71 | + * |
|
72 | + * @param array $response |
|
73 | + * @return array |
|
74 | + */ |
|
75 | + public function validateStatus(array $response): array |
|
76 | + { |
|
77 | + if (! empty($response['error'])) { |
|
78 | + return $response; |
|
79 | + } |
|
80 | + if (! empty($response['name']) && ! empty($response['message'])) { |
|
81 | + return ['error' => $response['name'], 'message' => $response['message']]; |
|
82 | + } |
|
83 | + // Check the data we received. |
|
84 | + if ( |
|
85 | + empty($response[ Domain::API_PARAM_TRACK_MERCHANT_ID ]) |
|
86 | + || ! isset($response[ Domain::API_PARAM_PAYMENTS_RECEIVABLE ]) |
|
87 | + || ! isset($response[ Domain::API_PARAM_PRIM_EMAIL_CONFIRMED ]) |
|
88 | + ) { |
|
89 | + $err_msg = esc_html__('Missing required data for validating the onboarding status.', 'event_espresso'); |
|
90 | + PayPalLogger::errorLog($err_msg, $response); |
|
91 | + return ['error' => 'ONBOARDING_MISSING_REQUIRED_DATA', 'message' => $err_msg]; |
|
92 | + } |
|
93 | + // Now validate the onboarding status. |
|
94 | + if (! $response[ Domain::API_PARAM_PAYMENTS_RECEIVABLE ]) { |
|
95 | + $err_msg = esc_html__( |
|
96 | + 'Your Account has been limited by PayPal. Please check your PayPal account inbox for an email from PayPal to determine the next steps for this.', |
|
97 | + 'event_espresso' |
|
98 | + ); |
|
99 | + PayPalLogger::errorLog($err_msg, $response); |
|
100 | + return ['error' => 'ONBOARDING_LIMITED_BY_PAYPAL', 'message' => $err_msg]; |
|
101 | + } |
|
102 | + if (! $response[ Domain::API_PARAM_PRIM_EMAIL_CONFIRMED ]) { |
|
103 | + $err_msg = esc_html__('Email address not confirmed. Please confirm your email address.', 'event_espresso'); |
|
104 | + PayPalLogger::errorLog($err_msg, $response); |
|
105 | + return ['error' => 'ONBOARDING_CONFIRM_EMAIL', 'message' => $err_msg]; |
|
106 | + } |
|
107 | + return [ |
|
108 | + 'valid' => true, |
|
109 | + 'response' => $response, |
|
110 | + ]; |
|
111 | + } |
|
112 | 112 | } |
@@ -49,7 +49,7 @@ discard block |
||
49 | 49 | parent::__construct($api, $sandbox_mode); |
50 | 50 | $this->partner_id = $partner_id; |
51 | 51 | $this->seller_id = $seller_id; |
52 | - $this->request_url = $this->request_url . "/{$partner_id}/merchant-integrations/{$seller_id}"; |
|
52 | + $this->request_url = $this->request_url."/{$partner_id}/merchant-integrations/{$seller_id}"; |
|
53 | 53 | } |
54 | 54 | |
55 | 55 | |
@@ -74,24 +74,24 @@ discard block |
||
74 | 74 | */ |
75 | 75 | public function validateStatus(array $response): array |
76 | 76 | { |
77 | - if (! empty($response['error'])) { |
|
77 | + if ( ! empty($response['error'])) { |
|
78 | 78 | return $response; |
79 | 79 | } |
80 | - if (! empty($response['name']) && ! empty($response['message'])) { |
|
80 | + if ( ! empty($response['name']) && ! empty($response['message'])) { |
|
81 | 81 | return ['error' => $response['name'], 'message' => $response['message']]; |
82 | 82 | } |
83 | 83 | // Check the data we received. |
84 | 84 | if ( |
85 | - empty($response[ Domain::API_PARAM_TRACK_MERCHANT_ID ]) |
|
86 | - || ! isset($response[ Domain::API_PARAM_PAYMENTS_RECEIVABLE ]) |
|
87 | - || ! isset($response[ Domain::API_PARAM_PRIM_EMAIL_CONFIRMED ]) |
|
85 | + empty($response[Domain::API_PARAM_TRACK_MERCHANT_ID]) |
|
86 | + || ! isset($response[Domain::API_PARAM_PAYMENTS_RECEIVABLE]) |
|
87 | + || ! isset($response[Domain::API_PARAM_PRIM_EMAIL_CONFIRMED]) |
|
88 | 88 | ) { |
89 | 89 | $err_msg = esc_html__('Missing required data for validating the onboarding status.', 'event_espresso'); |
90 | 90 | PayPalLogger::errorLog($err_msg, $response); |
91 | 91 | return ['error' => 'ONBOARDING_MISSING_REQUIRED_DATA', 'message' => $err_msg]; |
92 | 92 | } |
93 | 93 | // Now validate the onboarding status. |
94 | - if (! $response[ Domain::API_PARAM_PAYMENTS_RECEIVABLE ]) { |
|
94 | + if ( ! $response[Domain::API_PARAM_PAYMENTS_RECEIVABLE]) { |
|
95 | 95 | $err_msg = esc_html__( |
96 | 96 | 'Your Account has been limited by PayPal. Please check your PayPal account inbox for an email from PayPal to determine the next steps for this.', |
97 | 97 | 'event_espresso' |
@@ -99,7 +99,7 @@ discard block |
||
99 | 99 | PayPalLogger::errorLog($err_msg, $response); |
100 | 100 | return ['error' => 'ONBOARDING_LIMITED_BY_PAYPAL', 'message' => $err_msg]; |
101 | 101 | } |
102 | - if (! $response[ Domain::API_PARAM_PRIM_EMAIL_CONFIRMED ]) { |
|
102 | + if ( ! $response[Domain::API_PARAM_PRIM_EMAIL_CONFIRMED]) { |
|
103 | 103 | $err_msg = esc_html__('Email address not confirmed. Please confirm your email address.', 'event_espresso'); |
104 | 104 | PayPalLogger::errorLog($err_msg, $response); |
105 | 105 | return ['error' => 'ONBOARDING_CONFIRM_EMAIL', 'message' => $err_msg]; |
@@ -18,161 +18,161 @@ |
||
18 | 18 | */ |
19 | 19 | class PayPalExtraMeta |
20 | 20 | { |
21 | - /** |
|
22 | - * Payment method instance. |
|
23 | - * |
|
24 | - * @var EE_Payment_Method |
|
25 | - */ |
|
26 | - public $pm; |
|
27 | - |
|
28 | - /** |
|
29 | - * Metadata key. |
|
30 | - * |
|
31 | - * @var EE_Payment_Method |
|
32 | - */ |
|
33 | - public $metadata_key; |
|
34 | - |
|
35 | - |
|
36 | - /** |
|
37 | - * Class constructor. |
|
38 | - * |
|
39 | - * @param EE_Payment_Method $pm_instance |
|
40 | - */ |
|
41 | - public function __construct(EE_Payment_Method $pm_instance) |
|
42 | - { |
|
43 | - $this->pm = $pm_instance; |
|
44 | - $this->metadata_key = $pm_instance->debug_mode() |
|
45 | - ? Domain::META_KEY_PAYPAL_DATA_SANDBOX |
|
46 | - : Domain::META_KEY_PAYPAL_DATA; |
|
47 | - } |
|
48 | - |
|
49 | - |
|
50 | - /** |
|
51 | - * Save/update the provided data to the PM extra meta. |
|
52 | - * |
|
53 | - * @param array $data |
|
54 | - * @return bool |
|
55 | - */ |
|
56 | - public function saveBatch(array $data): bool |
|
57 | - { |
|
58 | - // Update the PM data. |
|
59 | - try { |
|
60 | - $paypal_data = $this->pm->get_extra_meta($this->metadata_key, true, []); |
|
61 | - $data_to_save = array_replace_recursive($paypal_data, $data); |
|
62 | - $this->saveMetaData($data_to_save); |
|
63 | - } catch (Exception $e) { |
|
64 | - $err_msg = sprintf( |
|
65 | - esc_html__('Could not save merchant data. %1$s', 'event_espresso'), |
|
66 | - $e->getMessage() |
|
67 | - ); |
|
68 | - PayPalLogger::errorLog($err_msg, $data, $this->pm); |
|
69 | - return false; |
|
70 | - } |
|
71 | - return true; |
|
72 | - } |
|
73 | - |
|
74 | - |
|
75 | - /** |
|
76 | - * Get PM extra meta by meta name/option. |
|
77 | - * |
|
78 | - * @param string $option_name |
|
79 | - * @return mixed |
|
80 | - */ |
|
81 | - public function getOption(string $option_name) |
|
82 | - { |
|
83 | - $meta_data = $this->getMetaData(); |
|
84 | - return $meta_data[ $option_name ] ?? false; |
|
85 | - } |
|
86 | - |
|
87 | - |
|
88 | - /** |
|
89 | - * Get PM metadata. |
|
90 | - * Return the metadata array if all good. False otherwise. |
|
91 | - * |
|
92 | - * @return array|bool |
|
93 | - */ |
|
94 | - public function getMetaData() |
|
95 | - { |
|
96 | - try { |
|
97 | - return $this->pm->get_extra_meta($this->metadata_key, true, []); |
|
98 | - } catch (EE_Error | ReflectionException $e) { |
|
99 | - $err_msg = sprintf( |
|
100 | - esc_html__('Error getting the PM meta data: %1$s', 'event_espresso'), |
|
101 | - $e->getMessage() |
|
102 | - ); |
|
103 | - PayPalLogger::errorLog($err_msg, [], $this->pm); |
|
104 | - return false; |
|
105 | - } |
|
106 | - } |
|
107 | - |
|
108 | - |
|
109 | - /** |
|
110 | - * Save/update the provided option to the PM extra meta. |
|
111 | - * |
|
112 | - * @param string $name |
|
113 | - * @param $value |
|
114 | - * @return bool |
|
115 | - */ |
|
116 | - public function saveOption(string $name, $value): bool |
|
117 | - { |
|
118 | - $meta_data = $this->getMetaData(); |
|
119 | - if (! $meta_data) { |
|
120 | - $meta_data = []; |
|
121 | - } |
|
122 | - |
|
123 | - $meta_data[ $name ] = $value; |
|
124 | - return $this->saveMetaData($meta_data); |
|
125 | - } |
|
126 | - |
|
127 | - |
|
128 | - /** |
|
129 | - * Save/update the PM extra meta. |
|
130 | - * |
|
131 | - * @param array $data |
|
132 | - * @return bool |
|
133 | - */ |
|
134 | - public function saveMetaData(array $data): bool |
|
135 | - { |
|
136 | - try { |
|
137 | - $this->pm->update_extra_meta($this->metadata_key, $data); |
|
138 | - } catch (Exception $e) { |
|
139 | - return false; |
|
140 | - } |
|
141 | - return true; |
|
142 | - } |
|
143 | - |
|
144 | - |
|
145 | - /** |
|
146 | - * Delete PM option. |
|
147 | - * |
|
148 | - * @param string $name |
|
149 | - * @return bool |
|
150 | - */ |
|
151 | - public function deleteOption(string $name): bool |
|
152 | - { |
|
153 | - $meta_data = $this->getMetaData(); |
|
154 | - if (! $meta_data) { |
|
155 | - return false; |
|
156 | - } |
|
157 | - unset($meta_data[ $name ]); |
|
158 | - return $this->saveMetaData($meta_data); |
|
159 | - } |
|
160 | - |
|
161 | - |
|
162 | - /** |
|
163 | - * Delete the PM extra meta. |
|
164 | - * |
|
165 | - * @return bool |
|
166 | - */ |
|
167 | - public function deleteMetaData(): bool |
|
168 | - { |
|
169 | - try { |
|
170 | - // Live and sandbox data. |
|
171 | - $this->pm->delete_extra_meta(Domain::META_KEY_PAYPAL_DATA); |
|
172 | - $this->pm->delete_extra_meta(Domain::META_KEY_PAYPAL_DATA_SANDBOX); |
|
173 | - } catch (Exception $e) { |
|
174 | - return false; |
|
175 | - } |
|
176 | - return true; |
|
177 | - } |
|
21 | + /** |
|
22 | + * Payment method instance. |
|
23 | + * |
|
24 | + * @var EE_Payment_Method |
|
25 | + */ |
|
26 | + public $pm; |
|
27 | + |
|
28 | + /** |
|
29 | + * Metadata key. |
|
30 | + * |
|
31 | + * @var EE_Payment_Method |
|
32 | + */ |
|
33 | + public $metadata_key; |
|
34 | + |
|
35 | + |
|
36 | + /** |
|
37 | + * Class constructor. |
|
38 | + * |
|
39 | + * @param EE_Payment_Method $pm_instance |
|
40 | + */ |
|
41 | + public function __construct(EE_Payment_Method $pm_instance) |
|
42 | + { |
|
43 | + $this->pm = $pm_instance; |
|
44 | + $this->metadata_key = $pm_instance->debug_mode() |
|
45 | + ? Domain::META_KEY_PAYPAL_DATA_SANDBOX |
|
46 | + : Domain::META_KEY_PAYPAL_DATA; |
|
47 | + } |
|
48 | + |
|
49 | + |
|
50 | + /** |
|
51 | + * Save/update the provided data to the PM extra meta. |
|
52 | + * |
|
53 | + * @param array $data |
|
54 | + * @return bool |
|
55 | + */ |
|
56 | + public function saveBatch(array $data): bool |
|
57 | + { |
|
58 | + // Update the PM data. |
|
59 | + try { |
|
60 | + $paypal_data = $this->pm->get_extra_meta($this->metadata_key, true, []); |
|
61 | + $data_to_save = array_replace_recursive($paypal_data, $data); |
|
62 | + $this->saveMetaData($data_to_save); |
|
63 | + } catch (Exception $e) { |
|
64 | + $err_msg = sprintf( |
|
65 | + esc_html__('Could not save merchant data. %1$s', 'event_espresso'), |
|
66 | + $e->getMessage() |
|
67 | + ); |
|
68 | + PayPalLogger::errorLog($err_msg, $data, $this->pm); |
|
69 | + return false; |
|
70 | + } |
|
71 | + return true; |
|
72 | + } |
|
73 | + |
|
74 | + |
|
75 | + /** |
|
76 | + * Get PM extra meta by meta name/option. |
|
77 | + * |
|
78 | + * @param string $option_name |
|
79 | + * @return mixed |
|
80 | + */ |
|
81 | + public function getOption(string $option_name) |
|
82 | + { |
|
83 | + $meta_data = $this->getMetaData(); |
|
84 | + return $meta_data[ $option_name ] ?? false; |
|
85 | + } |
|
86 | + |
|
87 | + |
|
88 | + /** |
|
89 | + * Get PM metadata. |
|
90 | + * Return the metadata array if all good. False otherwise. |
|
91 | + * |
|
92 | + * @return array|bool |
|
93 | + */ |
|
94 | + public function getMetaData() |
|
95 | + { |
|
96 | + try { |
|
97 | + return $this->pm->get_extra_meta($this->metadata_key, true, []); |
|
98 | + } catch (EE_Error | ReflectionException $e) { |
|
99 | + $err_msg = sprintf( |
|
100 | + esc_html__('Error getting the PM meta data: %1$s', 'event_espresso'), |
|
101 | + $e->getMessage() |
|
102 | + ); |
|
103 | + PayPalLogger::errorLog($err_msg, [], $this->pm); |
|
104 | + return false; |
|
105 | + } |
|
106 | + } |
|
107 | + |
|
108 | + |
|
109 | + /** |
|
110 | + * Save/update the provided option to the PM extra meta. |
|
111 | + * |
|
112 | + * @param string $name |
|
113 | + * @param $value |
|
114 | + * @return bool |
|
115 | + */ |
|
116 | + public function saveOption(string $name, $value): bool |
|
117 | + { |
|
118 | + $meta_data = $this->getMetaData(); |
|
119 | + if (! $meta_data) { |
|
120 | + $meta_data = []; |
|
121 | + } |
|
122 | + |
|
123 | + $meta_data[ $name ] = $value; |
|
124 | + return $this->saveMetaData($meta_data); |
|
125 | + } |
|
126 | + |
|
127 | + |
|
128 | + /** |
|
129 | + * Save/update the PM extra meta. |
|
130 | + * |
|
131 | + * @param array $data |
|
132 | + * @return bool |
|
133 | + */ |
|
134 | + public function saveMetaData(array $data): bool |
|
135 | + { |
|
136 | + try { |
|
137 | + $this->pm->update_extra_meta($this->metadata_key, $data); |
|
138 | + } catch (Exception $e) { |
|
139 | + return false; |
|
140 | + } |
|
141 | + return true; |
|
142 | + } |
|
143 | + |
|
144 | + |
|
145 | + /** |
|
146 | + * Delete PM option. |
|
147 | + * |
|
148 | + * @param string $name |
|
149 | + * @return bool |
|
150 | + */ |
|
151 | + public function deleteOption(string $name): bool |
|
152 | + { |
|
153 | + $meta_data = $this->getMetaData(); |
|
154 | + if (! $meta_data) { |
|
155 | + return false; |
|
156 | + } |
|
157 | + unset($meta_data[ $name ]); |
|
158 | + return $this->saveMetaData($meta_data); |
|
159 | + } |
|
160 | + |
|
161 | + |
|
162 | + /** |
|
163 | + * Delete the PM extra meta. |
|
164 | + * |
|
165 | + * @return bool |
|
166 | + */ |
|
167 | + public function deleteMetaData(): bool |
|
168 | + { |
|
169 | + try { |
|
170 | + // Live and sandbox data. |
|
171 | + $this->pm->delete_extra_meta(Domain::META_KEY_PAYPAL_DATA); |
|
172 | + $this->pm->delete_extra_meta(Domain::META_KEY_PAYPAL_DATA_SANDBOX); |
|
173 | + } catch (Exception $e) { |
|
174 | + return false; |
|
175 | + } |
|
176 | + return true; |
|
177 | + } |
|
178 | 178 | } |
@@ -22,295 +22,295 @@ |
||
22 | 22 | */ |
23 | 23 | class PayPalExtraMetaManager |
24 | 24 | { |
25 | - /** |
|
26 | - * Get payment method option/extra meta |
|
27 | - * |
|
28 | - * @param EE_Payment_Method $paypal_pm |
|
29 | - * @param string $option_name |
|
30 | - * @return mixed |
|
31 | - */ |
|
32 | - public static function getPmOption(EE_Payment_Method $paypal_pm, string $option_name): mixed |
|
33 | - { |
|
34 | - $pp_meta_data = self::extraMeta($paypal_pm); |
|
35 | - $option_value = $pp_meta_data->getOption($option_name); |
|
36 | - // Decrypt the encrypted options. |
|
37 | - if ( |
|
38 | - $option_name === Domain::META_KEY_ACCESS_TOKEN |
|
39 | - || $option_name === Domain::META_KEY_PARTNER_MERCHANT_ID |
|
40 | - || $option_name === Domain::META_KEY_CLIENT_SECRET |
|
41 | - ) { |
|
42 | - $option_value = self::decryptString($option_value, $paypal_pm); |
|
43 | - } |
|
44 | - return $option_value; |
|
45 | - } |
|
25 | + /** |
|
26 | + * Get payment method option/extra meta |
|
27 | + * |
|
28 | + * @param EE_Payment_Method $paypal_pm |
|
29 | + * @param string $option_name |
|
30 | + * @return mixed |
|
31 | + */ |
|
32 | + public static function getPmOption(EE_Payment_Method $paypal_pm, string $option_name): mixed |
|
33 | + { |
|
34 | + $pp_meta_data = self::extraMeta($paypal_pm); |
|
35 | + $option_value = $pp_meta_data->getOption($option_name); |
|
36 | + // Decrypt the encrypted options. |
|
37 | + if ( |
|
38 | + $option_name === Domain::META_KEY_ACCESS_TOKEN |
|
39 | + || $option_name === Domain::META_KEY_PARTNER_MERCHANT_ID |
|
40 | + || $option_name === Domain::META_KEY_CLIENT_SECRET |
|
41 | + ) { |
|
42 | + $option_value = self::decryptString($option_value, $paypal_pm); |
|
43 | + } |
|
44 | + return $option_value; |
|
45 | + } |
|
46 | 46 | |
47 | 47 | |
48 | - /** |
|
49 | - * Save payment method option/extra meta |
|
50 | - * |
|
51 | - * @param EE_Payment_Method $paypal_pm |
|
52 | - * @param string $option_name |
|
53 | - * @param $option_value |
|
54 | - * @return bool |
|
55 | - */ |
|
56 | - public static function savePmOption(EE_Payment_Method $paypal_pm, string $option_name, $option_value): bool |
|
57 | - { |
|
58 | - $pp_meta_data = self::extraMeta($paypal_pm); |
|
59 | - return $pp_meta_data->saveOption($option_name, $option_value); |
|
60 | - } |
|
48 | + /** |
|
49 | + * Save payment method option/extra meta |
|
50 | + * |
|
51 | + * @param EE_Payment_Method $paypal_pm |
|
52 | + * @param string $option_name |
|
53 | + * @param $option_value |
|
54 | + * @return bool |
|
55 | + */ |
|
56 | + public static function savePmOption(EE_Payment_Method $paypal_pm, string $option_name, $option_value): bool |
|
57 | + { |
|
58 | + $pp_meta_data = self::extraMeta($paypal_pm); |
|
59 | + return $pp_meta_data->saveOption($option_name, $option_value); |
|
60 | + } |
|
61 | 61 | |
62 | 62 | |
63 | - /** |
|
64 | - * Save a list of payment method options/extra meta. |
|
65 | - * |
|
66 | - * @param EE_Payment_Method $paypal_pm |
|
67 | - * @param array $options_list |
|
68 | - * @return bool |
|
69 | - */ |
|
70 | - public static function savePmOptions(EE_Payment_Method $paypal_pm, array $options_list): bool |
|
71 | - { |
|
72 | - $pp_meta_data = self::extraMeta($paypal_pm); |
|
73 | - return $pp_meta_data->saveBatch($options_list); |
|
74 | - } |
|
63 | + /** |
|
64 | + * Save a list of payment method options/extra meta. |
|
65 | + * |
|
66 | + * @param EE_Payment_Method $paypal_pm |
|
67 | + * @param array $options_list |
|
68 | + * @return bool |
|
69 | + */ |
|
70 | + public static function savePmOptions(EE_Payment_Method $paypal_pm, array $options_list): bool |
|
71 | + { |
|
72 | + $pp_meta_data = self::extraMeta($paypal_pm); |
|
73 | + return $pp_meta_data->saveBatch($options_list); |
|
74 | + } |
|
75 | 75 | |
76 | 76 | |
77 | - /** |
|
78 | - * Delete payment method option/extra meta |
|
79 | - * |
|
80 | - * @param EE_Payment_Method $paypal_pm |
|
81 | - * @param string $option_name |
|
82 | - * @return bool |
|
83 | - */ |
|
84 | - public static function deletePmOption(EE_Payment_Method $paypal_pm, string $option_name): bool |
|
85 | - { |
|
86 | - $pp_meta_data = self::extraMeta($paypal_pm); |
|
87 | - return $pp_meta_data->deleteOption($option_name); |
|
88 | - } |
|
77 | + /** |
|
78 | + * Delete payment method option/extra meta |
|
79 | + * |
|
80 | + * @param EE_Payment_Method $paypal_pm |
|
81 | + * @param string $option_name |
|
82 | + * @return bool |
|
83 | + */ |
|
84 | + public static function deletePmOption(EE_Payment_Method $paypal_pm, string $option_name): bool |
|
85 | + { |
|
86 | + $pp_meta_data = self::extraMeta($paypal_pm); |
|
87 | + return $pp_meta_data->deleteOption($option_name); |
|
88 | + } |
|
89 | 89 | |
90 | 90 | |
91 | - /** |
|
92 | - * Get all options for payment method. |
|
93 | - * |
|
94 | - * @param EE_Payment_Method $paypal_pm |
|
95 | - * @return array|bool |
|
96 | - */ |
|
97 | - public static function getAllData(EE_Payment_Method $paypal_pm) |
|
98 | - { |
|
99 | - $pp_meta_data = self::extraMeta($paypal_pm); |
|
100 | - return $pp_meta_data->getMetaData(); |
|
101 | - } |
|
91 | + /** |
|
92 | + * Get all options for payment method. |
|
93 | + * |
|
94 | + * @param EE_Payment_Method $paypal_pm |
|
95 | + * @return array|bool |
|
96 | + */ |
|
97 | + public static function getAllData(EE_Payment_Method $paypal_pm) |
|
98 | + { |
|
99 | + $pp_meta_data = self::extraMeta($paypal_pm); |
|
100 | + return $pp_meta_data->getMetaData(); |
|
101 | + } |
|
102 | 102 | |
103 | 103 | |
104 | - /** |
|
105 | - * Delete all options for this payment method. |
|
106 | - * |
|
107 | - * @param EE_Payment_Method $paypal_pm |
|
108 | - * @return bool |
|
109 | - */ |
|
110 | - public static function deleteAllData(EE_Payment_Method $paypal_pm): bool |
|
111 | - { |
|
112 | - $pp_meta_data = self::extraMeta($paypal_pm); |
|
113 | - return $pp_meta_data->deleteMetaData(); |
|
114 | - } |
|
104 | + /** |
|
105 | + * Delete all options for this payment method. |
|
106 | + * |
|
107 | + * @param EE_Payment_Method $paypal_pm |
|
108 | + * @return bool |
|
109 | + */ |
|
110 | + public static function deleteAllData(EE_Payment_Method $paypal_pm): bool |
|
111 | + { |
|
112 | + $pp_meta_data = self::extraMeta($paypal_pm); |
|
113 | + return $pp_meta_data->deleteMetaData(); |
|
114 | + } |
|
115 | 115 | |
116 | 116 | |
117 | - /** |
|
118 | - * Save the debug mode option if it changed. |
|
119 | - * |
|
120 | - * @param EE_Payment_Method $paypal_pm |
|
121 | - * @param array $request_data |
|
122 | - * @return bool Updated or not. |
|
123 | - */ |
|
124 | - public static function updateDebugMode(EE_Payment_Method $paypal_pm, array $request_data): bool |
|
125 | - { |
|
126 | - if ( |
|
127 | - isset($request_data['sandbox_mode']) |
|
128 | - && in_array($request_data['sandbox_mode'], ['0', '1'], true) |
|
129 | - && $paypal_pm->debug_mode() !== (bool) $request_data['sandbox_mode'] |
|
130 | - ) { |
|
131 | - try { |
|
132 | - $paypal_pm->save(['PMD_debug_mode' => $request_data['sandbox_mode']]); |
|
133 | - } catch (EE_Error $e) { |
|
134 | - $err_msg = sprintf( |
|
135 | - esc_html__('Note, debug mode not saved ! %1$s', 'event_espresso'), |
|
136 | - $e->getMessage() |
|
137 | - ); |
|
138 | - PayPalLogger::errorLog($err_msg, $request_data, $paypal_pm); |
|
139 | - return false; |
|
140 | - } |
|
141 | - return true; |
|
142 | - } |
|
143 | - return false; |
|
144 | - } |
|
117 | + /** |
|
118 | + * Save the debug mode option if it changed. |
|
119 | + * |
|
120 | + * @param EE_Payment_Method $paypal_pm |
|
121 | + * @param array $request_data |
|
122 | + * @return bool Updated or not. |
|
123 | + */ |
|
124 | + public static function updateDebugMode(EE_Payment_Method $paypal_pm, array $request_data): bool |
|
125 | + { |
|
126 | + if ( |
|
127 | + isset($request_data['sandbox_mode']) |
|
128 | + && in_array($request_data['sandbox_mode'], ['0', '1'], true) |
|
129 | + && $paypal_pm->debug_mode() !== (bool) $request_data['sandbox_mode'] |
|
130 | + ) { |
|
131 | + try { |
|
132 | + $paypal_pm->save(['PMD_debug_mode' => $request_data['sandbox_mode']]); |
|
133 | + } catch (EE_Error $e) { |
|
134 | + $err_msg = sprintf( |
|
135 | + esc_html__('Note, debug mode not saved ! %1$s', 'event_espresso'), |
|
136 | + $e->getMessage() |
|
137 | + ); |
|
138 | + PayPalLogger::errorLog($err_msg, $request_data, $paypal_pm); |
|
139 | + return false; |
|
140 | + } |
|
141 | + return true; |
|
142 | + } |
|
143 | + return false; |
|
144 | + } |
|
145 | 145 | |
146 | 146 | |
147 | - /** |
|
148 | - * Save partner access token and parameters. |
|
149 | - * |
|
150 | - * @param EE_Payment_Method $paypal_pm |
|
151 | - * @param array $response |
|
152 | - * @return bool |
|
153 | - */ |
|
154 | - public static function savePartnerAccessToken(EE_Payment_Method $paypal_pm, array $response): bool |
|
155 | - { |
|
156 | - $paypal_data = []; |
|
157 | - $expected_parameters = [ |
|
158 | - Domain::META_KEY_ACCESS_TOKEN, |
|
159 | - Domain::META_KEY_EXPIRES_IN, |
|
160 | - Domain::META_KEY_APP_ID, |
|
161 | - Domain::META_KEY_PARTNER_CLIENT_ID, |
|
162 | - Domain::META_KEY_PARTNER_MERCHANT_ID, |
|
163 | - Domain::META_KEY_BN_CODE, |
|
164 | - ]; |
|
165 | - foreach ($expected_parameters as $api_key) { |
|
166 | - if (! isset($response[ $api_key ])) { |
|
167 | - // Don't want to try saving data that doesn't exist. |
|
168 | - continue; |
|
169 | - } |
|
170 | - try { |
|
171 | - switch ($api_key) { |
|
172 | - case Domain::META_KEY_ACCESS_TOKEN: |
|
173 | - case Domain::META_KEY_PARTNER_MERCHANT_ID: |
|
174 | - $paypal_data[ $api_key ] = self::encryptString($response[ $api_key ], $paypal_pm); |
|
175 | - break; |
|
176 | - case Domain::META_KEY_EXPIRES_IN: |
|
177 | - $paypal_data[ $api_key ] = time() + (int) sanitize_key($response[ $api_key ]); |
|
178 | - break; |
|
179 | - default: |
|
180 | - $paypal_data[ $api_key ] = sanitize_text_field($response[ $api_key ]); |
|
181 | - } |
|
182 | - } catch (Exception $exception) { |
|
183 | - PayPalLogger::errorLog($exception->getMessage(), $response, $paypal_pm); |
|
184 | - return false; |
|
185 | - } |
|
186 | - } |
|
187 | - return self::savePmOptions($paypal_pm, $paypal_data); |
|
188 | - } |
|
147 | + /** |
|
148 | + * Save partner access token and parameters. |
|
149 | + * |
|
150 | + * @param EE_Payment_Method $paypal_pm |
|
151 | + * @param array $response |
|
152 | + * @return bool |
|
153 | + */ |
|
154 | + public static function savePartnerAccessToken(EE_Payment_Method $paypal_pm, array $response): bool |
|
155 | + { |
|
156 | + $paypal_data = []; |
|
157 | + $expected_parameters = [ |
|
158 | + Domain::META_KEY_ACCESS_TOKEN, |
|
159 | + Domain::META_KEY_EXPIRES_IN, |
|
160 | + Domain::META_KEY_APP_ID, |
|
161 | + Domain::META_KEY_PARTNER_CLIENT_ID, |
|
162 | + Domain::META_KEY_PARTNER_MERCHANT_ID, |
|
163 | + Domain::META_KEY_BN_CODE, |
|
164 | + ]; |
|
165 | + foreach ($expected_parameters as $api_key) { |
|
166 | + if (! isset($response[ $api_key ])) { |
|
167 | + // Don't want to try saving data that doesn't exist. |
|
168 | + continue; |
|
169 | + } |
|
170 | + try { |
|
171 | + switch ($api_key) { |
|
172 | + case Domain::META_KEY_ACCESS_TOKEN: |
|
173 | + case Domain::META_KEY_PARTNER_MERCHANT_ID: |
|
174 | + $paypal_data[ $api_key ] = self::encryptString($response[ $api_key ], $paypal_pm); |
|
175 | + break; |
|
176 | + case Domain::META_KEY_EXPIRES_IN: |
|
177 | + $paypal_data[ $api_key ] = time() + (int) sanitize_key($response[ $api_key ]); |
|
178 | + break; |
|
179 | + default: |
|
180 | + $paypal_data[ $api_key ] = sanitize_text_field($response[ $api_key ]); |
|
181 | + } |
|
182 | + } catch (Exception $exception) { |
|
183 | + PayPalLogger::errorLog($exception->getMessage(), $response, $paypal_pm); |
|
184 | + return false; |
|
185 | + } |
|
186 | + } |
|
187 | + return self::savePmOptions($paypal_pm, $paypal_data); |
|
188 | + } |
|
189 | 189 | |
190 | 190 | |
191 | - /** |
|
192 | - * Save merchant/seller API credentials. |
|
193 | - * |
|
194 | - * @param EE_Payment_Method $paypal_pm |
|
195 | - * @param array $response |
|
196 | - * @return bool |
|
197 | - */ |
|
198 | - public static function saveSellerApiCredentials(EE_Payment_Method $paypal_pm, array $response): bool |
|
199 | - { |
|
200 | - $api_credentials = []; |
|
201 | - $expected_parameters = [ |
|
202 | - Domain::META_KEY_SELLER_MERCHANT_ID, |
|
203 | - ]; |
|
204 | - foreach ($expected_parameters as $api_key) { |
|
205 | - if (! isset($response[ $api_key ])) { |
|
206 | - // Don't want to try saving data that doesn't exist. |
|
207 | - continue; |
|
208 | - } |
|
209 | - try { |
|
210 | - $api_credentials[ $api_key ] = $response[ $api_key ]; |
|
211 | - } catch (Exception $exception) { |
|
212 | - PayPalLogger::errorLog($exception->getMessage(), $response, $paypal_pm); |
|
213 | - return false; |
|
214 | - } |
|
215 | - } |
|
216 | - return self::savePmOptions($paypal_pm, $api_credentials); |
|
217 | - } |
|
191 | + /** |
|
192 | + * Save merchant/seller API credentials. |
|
193 | + * |
|
194 | + * @param EE_Payment_Method $paypal_pm |
|
195 | + * @param array $response |
|
196 | + * @return bool |
|
197 | + */ |
|
198 | + public static function saveSellerApiCredentials(EE_Payment_Method $paypal_pm, array $response): bool |
|
199 | + { |
|
200 | + $api_credentials = []; |
|
201 | + $expected_parameters = [ |
|
202 | + Domain::META_KEY_SELLER_MERCHANT_ID, |
|
203 | + ]; |
|
204 | + foreach ($expected_parameters as $api_key) { |
|
205 | + if (! isset($response[ $api_key ])) { |
|
206 | + // Don't want to try saving data that doesn't exist. |
|
207 | + continue; |
|
208 | + } |
|
209 | + try { |
|
210 | + $api_credentials[ $api_key ] = $response[ $api_key ]; |
|
211 | + } catch (Exception $exception) { |
|
212 | + PayPalLogger::errorLog($exception->getMessage(), $response, $paypal_pm); |
|
213 | + return false; |
|
214 | + } |
|
215 | + } |
|
216 | + return self::savePmOptions($paypal_pm, $api_credentials); |
|
217 | + } |
|
218 | 218 | |
219 | 219 | |
220 | - /** |
|
221 | - * Save other payment method related settings from a data array. |
|
222 | - * |
|
223 | - * @param EE_Payment_Method $paypal_pm |
|
224 | - * @param array $data |
|
225 | - * @return bool |
|
226 | - * @throws EE_Error |
|
227 | - * @throws ReflectionException |
|
228 | - */ |
|
229 | - public static function parseAndSaveOptions(EE_Payment_Method $paypal_pm, array $data): bool |
|
230 | - { |
|
231 | - $allowed_checkout_type = 'express_checkout'; |
|
232 | - // Note, although PayPal shows that this should include PPCP_CUSTOM or EXPRESS_CHECKOUT only, |
|
233 | - // in reality, it will also include other products like MOBILE_PAYMENT_ACCEPTANCE etc. |
|
234 | - if (! empty($data['response']['products'][0]['name']) && is_array($data['response']['products'])) { |
|
235 | - foreach ($data['response']['products'] as $product) { |
|
236 | - if (str_contains($product['name'], 'PPCP')) { |
|
237 | - // This merchant has PPCP in the products list, so we can enable both (all) checkout types. |
|
238 | - $allowed_checkout_type = 'all'; |
|
239 | - break; |
|
240 | - } |
|
241 | - } |
|
242 | - } |
|
243 | - // Set the Checkout type (a PM option), just in case merchant doesn't save PM options manually. |
|
244 | - $checkout_type = $paypal_pm->get_extra_meta(Domain::META_KEY_CHECKOUT_TYPE, true, false); |
|
245 | - if (! $checkout_type) { |
|
246 | - $paypal_pm->update_extra_meta(Domain::META_KEY_CHECKOUT_TYPE, $allowed_checkout_type); |
|
247 | - } |
|
248 | - return PayPalExtraMetaManager::savePmOption( |
|
249 | - $paypal_pm, |
|
250 | - Domain::META_KEY_ALLOWED_CHECKOUT_TYPE, |
|
251 | - $allowed_checkout_type |
|
252 | - ); |
|
253 | - } |
|
220 | + /** |
|
221 | + * Save other payment method related settings from a data array. |
|
222 | + * |
|
223 | + * @param EE_Payment_Method $paypal_pm |
|
224 | + * @param array $data |
|
225 | + * @return bool |
|
226 | + * @throws EE_Error |
|
227 | + * @throws ReflectionException |
|
228 | + */ |
|
229 | + public static function parseAndSaveOptions(EE_Payment_Method $paypal_pm, array $data): bool |
|
230 | + { |
|
231 | + $allowed_checkout_type = 'express_checkout'; |
|
232 | + // Note, although PayPal shows that this should include PPCP_CUSTOM or EXPRESS_CHECKOUT only, |
|
233 | + // in reality, it will also include other products like MOBILE_PAYMENT_ACCEPTANCE etc. |
|
234 | + if (! empty($data['response']['products'][0]['name']) && is_array($data['response']['products'])) { |
|
235 | + foreach ($data['response']['products'] as $product) { |
|
236 | + if (str_contains($product['name'], 'PPCP')) { |
|
237 | + // This merchant has PPCP in the products list, so we can enable both (all) checkout types. |
|
238 | + $allowed_checkout_type = 'all'; |
|
239 | + break; |
|
240 | + } |
|
241 | + } |
|
242 | + } |
|
243 | + // Set the Checkout type (a PM option), just in case merchant doesn't save PM options manually. |
|
244 | + $checkout_type = $paypal_pm->get_extra_meta(Domain::META_KEY_CHECKOUT_TYPE, true, false); |
|
245 | + if (! $checkout_type) { |
|
246 | + $paypal_pm->update_extra_meta(Domain::META_KEY_CHECKOUT_TYPE, $allowed_checkout_type); |
|
247 | + } |
|
248 | + return PayPalExtraMetaManager::savePmOption( |
|
249 | + $paypal_pm, |
|
250 | + Domain::META_KEY_ALLOWED_CHECKOUT_TYPE, |
|
251 | + $allowed_checkout_type |
|
252 | + ); |
|
253 | + } |
|
254 | 254 | |
255 | 255 | |
256 | - /** |
|
257 | - * Get PayPal extra meta helper. |
|
258 | - * |
|
259 | - * @param EE_Payment_Method $paypal_pm |
|
260 | - * @return PayPalExtraMeta |
|
261 | - */ |
|
262 | - public static function extraMeta(EE_Payment_Method $paypal_pm): PayPalExtraMeta |
|
263 | - { |
|
264 | - return LoaderFactory::getLoader()->getShared(PayPalExtraMeta::class, [$paypal_pm]); |
|
265 | - } |
|
256 | + /** |
|
257 | + * Get PayPal extra meta helper. |
|
258 | + * |
|
259 | + * @param EE_Payment_Method $paypal_pm |
|
260 | + * @return PayPalExtraMeta |
|
261 | + */ |
|
262 | + public static function extraMeta(EE_Payment_Method $paypal_pm): PayPalExtraMeta |
|
263 | + { |
|
264 | + return LoaderFactory::getLoader()->getShared(PayPalExtraMeta::class, [$paypal_pm]); |
|
265 | + } |
|
266 | 266 | |
267 | 267 | |
268 | - /** |
|
269 | - * Encrypt a text field. |
|
270 | - * |
|
271 | - * @param string $text |
|
272 | - * @param EE_Payment_Method $paypal_pm |
|
273 | - * @return string|null |
|
274 | - */ |
|
275 | - public static function encryptString(string $text, EE_Payment_Method $paypal_pm): ?string |
|
276 | - { |
|
277 | - // We sure we are getting something ? |
|
278 | - if (! $text) { |
|
279 | - return $text; |
|
280 | - } |
|
281 | - // Do encrypt. |
|
282 | - $encryptor = LoaderFactory::getLoader()->getShared(OpenSSLEncryption::class, [new Base64Encoder()]); |
|
283 | - $sanitized_text = sanitize_text_field($text); |
|
284 | - $key_identifier = $paypal_pm->debug_mode() |
|
285 | - ? PPCommerceEncryptionKeyManager::SANDBOX_ENCRYPTION_KEY_ID |
|
286 | - : PPCommerceEncryptionKeyManager::PRODUCTION_ENCRYPTION_KEY_ID; |
|
287 | - return $encryptor->encrypt($sanitized_text, $key_identifier); |
|
288 | - } |
|
268 | + /** |
|
269 | + * Encrypt a text field. |
|
270 | + * |
|
271 | + * @param string $text |
|
272 | + * @param EE_Payment_Method $paypal_pm |
|
273 | + * @return string|null |
|
274 | + */ |
|
275 | + public static function encryptString(string $text, EE_Payment_Method $paypal_pm): ?string |
|
276 | + { |
|
277 | + // We sure we are getting something ? |
|
278 | + if (! $text) { |
|
279 | + return $text; |
|
280 | + } |
|
281 | + // Do encrypt. |
|
282 | + $encryptor = LoaderFactory::getLoader()->getShared(OpenSSLEncryption::class, [new Base64Encoder()]); |
|
283 | + $sanitized_text = sanitize_text_field($text); |
|
284 | + $key_identifier = $paypal_pm->debug_mode() |
|
285 | + ? PPCommerceEncryptionKeyManager::SANDBOX_ENCRYPTION_KEY_ID |
|
286 | + : PPCommerceEncryptionKeyManager::PRODUCTION_ENCRYPTION_KEY_ID; |
|
287 | + return $encryptor->encrypt($sanitized_text, $key_identifier); |
|
288 | + } |
|
289 | 289 | |
290 | 290 | |
291 | - /** |
|
292 | - * Decrypt a string. |
|
293 | - * |
|
294 | - * @param string $text |
|
295 | - * @param EE_Payment_Method $paypal_pm |
|
296 | - * @return string |
|
297 | - */ |
|
298 | - public static function decryptString(string $text, EE_Payment_Method $paypal_pm): string |
|
299 | - { |
|
300 | - // Are we even getting something ? |
|
301 | - if (! $text) { |
|
302 | - return $text; |
|
303 | - } |
|
304 | - // Try decrypting. |
|
305 | - try { |
|
306 | - $encryptor = LoaderFactory::getLoader()->getShared(OpenSSLEncryption::class, [new Base64Encoder()]); |
|
307 | - $key_identifier = $paypal_pm->debug_mode() |
|
308 | - ? PPCommerceEncryptionKeyManager::SANDBOX_ENCRYPTION_KEY_ID |
|
309 | - : PPCommerceEncryptionKeyManager::PRODUCTION_ENCRYPTION_KEY_ID; |
|
310 | - $decrypted = $encryptor->decrypt($text, $key_identifier); |
|
311 | - } catch (Exception $e) { |
|
312 | - return $text; |
|
313 | - } |
|
314 | - return $decrypted ?? $text; |
|
315 | - } |
|
291 | + /** |
|
292 | + * Decrypt a string. |
|
293 | + * |
|
294 | + * @param string $text |
|
295 | + * @param EE_Payment_Method $paypal_pm |
|
296 | + * @return string |
|
297 | + */ |
|
298 | + public static function decryptString(string $text, EE_Payment_Method $paypal_pm): string |
|
299 | + { |
|
300 | + // Are we even getting something ? |
|
301 | + if (! $text) { |
|
302 | + return $text; |
|
303 | + } |
|
304 | + // Try decrypting. |
|
305 | + try { |
|
306 | + $encryptor = LoaderFactory::getLoader()->getShared(OpenSSLEncryption::class, [new Base64Encoder()]); |
|
307 | + $key_identifier = $paypal_pm->debug_mode() |
|
308 | + ? PPCommerceEncryptionKeyManager::SANDBOX_ENCRYPTION_KEY_ID |
|
309 | + : PPCommerceEncryptionKeyManager::PRODUCTION_ENCRYPTION_KEY_ID; |
|
310 | + $decrypted = $encryptor->decrypt($text, $key_identifier); |
|
311 | + } catch (Exception $e) { |
|
312 | + return $text; |
|
313 | + } |
|
314 | + return $decrypted ?? $text; |
|
315 | + } |
|
316 | 316 | } |
@@ -163,7 +163,7 @@ discard block |
||
163 | 163 | Domain::META_KEY_BN_CODE, |
164 | 164 | ]; |
165 | 165 | foreach ($expected_parameters as $api_key) { |
166 | - if (! isset($response[ $api_key ])) { |
|
166 | + if ( ! isset($response[$api_key])) { |
|
167 | 167 | // Don't want to try saving data that doesn't exist. |
168 | 168 | continue; |
169 | 169 | } |
@@ -171,13 +171,13 @@ discard block |
||
171 | 171 | switch ($api_key) { |
172 | 172 | case Domain::META_KEY_ACCESS_TOKEN: |
173 | 173 | case Domain::META_KEY_PARTNER_MERCHANT_ID: |
174 | - $paypal_data[ $api_key ] = self::encryptString($response[ $api_key ], $paypal_pm); |
|
174 | + $paypal_data[$api_key] = self::encryptString($response[$api_key], $paypal_pm); |
|
175 | 175 | break; |
176 | 176 | case Domain::META_KEY_EXPIRES_IN: |
177 | - $paypal_data[ $api_key ] = time() + (int) sanitize_key($response[ $api_key ]); |
|
177 | + $paypal_data[$api_key] = time() + (int) sanitize_key($response[$api_key]); |
|
178 | 178 | break; |
179 | 179 | default: |
180 | - $paypal_data[ $api_key ] = sanitize_text_field($response[ $api_key ]); |
|
180 | + $paypal_data[$api_key] = sanitize_text_field($response[$api_key]); |
|
181 | 181 | } |
182 | 182 | } catch (Exception $exception) { |
183 | 183 | PayPalLogger::errorLog($exception->getMessage(), $response, $paypal_pm); |
@@ -202,12 +202,12 @@ discard block |
||
202 | 202 | Domain::META_KEY_SELLER_MERCHANT_ID, |
203 | 203 | ]; |
204 | 204 | foreach ($expected_parameters as $api_key) { |
205 | - if (! isset($response[ $api_key ])) { |
|
205 | + if ( ! isset($response[$api_key])) { |
|
206 | 206 | // Don't want to try saving data that doesn't exist. |
207 | 207 | continue; |
208 | 208 | } |
209 | 209 | try { |
210 | - $api_credentials[ $api_key ] = $response[ $api_key ]; |
|
210 | + $api_credentials[$api_key] = $response[$api_key]; |
|
211 | 211 | } catch (Exception $exception) { |
212 | 212 | PayPalLogger::errorLog($exception->getMessage(), $response, $paypal_pm); |
213 | 213 | return false; |
@@ -231,7 +231,7 @@ discard block |
||
231 | 231 | $allowed_checkout_type = 'express_checkout'; |
232 | 232 | // Note, although PayPal shows that this should include PPCP_CUSTOM or EXPRESS_CHECKOUT only, |
233 | 233 | // in reality, it will also include other products like MOBILE_PAYMENT_ACCEPTANCE etc. |
234 | - if (! empty($data['response']['products'][0]['name']) && is_array($data['response']['products'])) { |
|
234 | + if ( ! empty($data['response']['products'][0]['name']) && is_array($data['response']['products'])) { |
|
235 | 235 | foreach ($data['response']['products'] as $product) { |
236 | 236 | if (str_contains($product['name'], 'PPCP')) { |
237 | 237 | // This merchant has PPCP in the products list, so we can enable both (all) checkout types. |
@@ -242,7 +242,7 @@ discard block |
||
242 | 242 | } |
243 | 243 | // Set the Checkout type (a PM option), just in case merchant doesn't save PM options manually. |
244 | 244 | $checkout_type = $paypal_pm->get_extra_meta(Domain::META_KEY_CHECKOUT_TYPE, true, false); |
245 | - if (! $checkout_type) { |
|
245 | + if ( ! $checkout_type) { |
|
246 | 246 | $paypal_pm->update_extra_meta(Domain::META_KEY_CHECKOUT_TYPE, $allowed_checkout_type); |
247 | 247 | } |
248 | 248 | return PayPalExtraMetaManager::savePmOption( |
@@ -275,7 +275,7 @@ discard block |
||
275 | 275 | public static function encryptString(string $text, EE_Payment_Method $paypal_pm): ?string |
276 | 276 | { |
277 | 277 | // We sure we are getting something ? |
278 | - if (! $text) { |
|
278 | + if ( ! $text) { |
|
279 | 279 | return $text; |
280 | 280 | } |
281 | 281 | // Do encrypt. |
@@ -298,7 +298,7 @@ discard block |
||
298 | 298 | public static function decryptString(string $text, EE_Payment_Method $paypal_pm): string |
299 | 299 | { |
300 | 300 | // Are we even getting something ? |
301 | - if (! $text) { |
|
301 | + if ( ! $text) { |
|
302 | 302 | return $text; |
303 | 303 | } |
304 | 304 | // Try decrypting. |