@@ -9,100 +9,100 @@ |
||
9 | 9 | */ |
10 | 10 | class EE_Term extends EE_Base_Class |
11 | 11 | { |
12 | - public array $post_type = []; |
|
12 | + public array $post_type = []; |
|
13 | 13 | |
14 | 14 | |
15 | - /** |
|
16 | - * Sets some dynamic defaults |
|
17 | - * |
|
18 | - * @param array $fieldValues |
|
19 | - * @param bool $bydb |
|
20 | - * @param string $timezone |
|
21 | - * @throws EE_Error |
|
22 | - * @throws ReflectionException |
|
23 | - */ |
|
24 | - protected function __construct($fieldValues = [], $bydb = false, $timezone = '') |
|
25 | - { |
|
26 | - if (! isset($fieldValues['slug'])) { |
|
27 | - $fieldValues['slug'] = $fieldValues['name']; |
|
28 | - } |
|
29 | - parent::__construct($fieldValues, $bydb, $timezone); |
|
30 | - } |
|
15 | + /** |
|
16 | + * Sets some dynamic defaults |
|
17 | + * |
|
18 | + * @param array $fieldValues |
|
19 | + * @param bool $bydb |
|
20 | + * @param string $timezone |
|
21 | + * @throws EE_Error |
|
22 | + * @throws ReflectionException |
|
23 | + */ |
|
24 | + protected function __construct($fieldValues = [], $bydb = false, $timezone = '') |
|
25 | + { |
|
26 | + if (! isset($fieldValues['slug'])) { |
|
27 | + $fieldValues['slug'] = $fieldValues['name']; |
|
28 | + } |
|
29 | + parent::__construct($fieldValues, $bydb, $timezone); |
|
30 | + } |
|
31 | 31 | |
32 | 32 | |
33 | - /** |
|
34 | - * @param array $props_n_values |
|
35 | - * @return EE_Term|null |
|
36 | - * @throws EE_Error |
|
37 | - * @throws ReflectionException |
|
38 | - */ |
|
39 | - public static function new_instance(array $props_n_values = []): ?EE_Term |
|
40 | - { |
|
41 | - $has_object = parent::_check_for_object($props_n_values, __CLASS__); |
|
42 | - return $has_object ?: new self($props_n_values); |
|
43 | - } |
|
33 | + /** |
|
34 | + * @param array $props_n_values |
|
35 | + * @return EE_Term|null |
|
36 | + * @throws EE_Error |
|
37 | + * @throws ReflectionException |
|
38 | + */ |
|
39 | + public static function new_instance(array $props_n_values = []): ?EE_Term |
|
40 | + { |
|
41 | + $has_object = parent::_check_for_object($props_n_values, __CLASS__); |
|
42 | + return $has_object ?: new self($props_n_values); |
|
43 | + } |
|
44 | 44 | |
45 | 45 | |
46 | - /** |
|
47 | - * @param array $props_n_values |
|
48 | - * @return EE_Term|null |
|
49 | - * @throws EE_Error |
|
50 | - * @throws ReflectionException |
|
51 | - */ |
|
52 | - public static function new_instance_from_db(array $props_n_values = []): ?EE_Term |
|
53 | - { |
|
54 | - return new self($props_n_values, true); |
|
55 | - } |
|
46 | + /** |
|
47 | + * @param array $props_n_values |
|
48 | + * @return EE_Term|null |
|
49 | + * @throws EE_Error |
|
50 | + * @throws ReflectionException |
|
51 | + */ |
|
52 | + public static function new_instance_from_db(array $props_n_values = []): ?EE_Term |
|
53 | + { |
|
54 | + return new self($props_n_values, true); |
|
55 | + } |
|
56 | 56 | |
57 | 57 | |
58 | - /** |
|
59 | - * Gets name |
|
60 | - * |
|
61 | - * @return string |
|
62 | - * @throws EE_Error |
|
63 | - * @throws ReflectionException |
|
64 | - */ |
|
65 | - public function name(): string |
|
66 | - { |
|
67 | - return (string) $this->get('name'); |
|
68 | - } |
|
58 | + /** |
|
59 | + * Gets name |
|
60 | + * |
|
61 | + * @return string |
|
62 | + * @throws EE_Error |
|
63 | + * @throws ReflectionException |
|
64 | + */ |
|
65 | + public function name(): string |
|
66 | + { |
|
67 | + return (string) $this->get('name'); |
|
68 | + } |
|
69 | 69 | |
70 | 70 | |
71 | - /** |
|
72 | - * Sets name |
|
73 | - * |
|
74 | - * @param string $name |
|
75 | - * @throws EE_Error |
|
76 | - * @throws ReflectionException |
|
77 | - */ |
|
78 | - public function set_name(string $name) |
|
79 | - { |
|
80 | - $this->set('name', $name); |
|
81 | - } |
|
71 | + /** |
|
72 | + * Sets name |
|
73 | + * |
|
74 | + * @param string $name |
|
75 | + * @throws EE_Error |
|
76 | + * @throws ReflectionException |
|
77 | + */ |
|
78 | + public function set_name(string $name) |
|
79 | + { |
|
80 | + $this->set('name', $name); |
|
81 | + } |
|
82 | 82 | |
83 | 83 | |
84 | - /** |
|
85 | - * Gets slug |
|
86 | - * |
|
87 | - * @return string |
|
88 | - * @throws EE_Error |
|
89 | - * @throws ReflectionException |
|
90 | - */ |
|
91 | - public function slug(): string |
|
92 | - { |
|
93 | - return (string) $this->get('slug'); |
|
94 | - } |
|
84 | + /** |
|
85 | + * Gets slug |
|
86 | + * |
|
87 | + * @return string |
|
88 | + * @throws EE_Error |
|
89 | + * @throws ReflectionException |
|
90 | + */ |
|
91 | + public function slug(): string |
|
92 | + { |
|
93 | + return (string) $this->get('slug'); |
|
94 | + } |
|
95 | 95 | |
96 | 96 | |
97 | - /** |
|
98 | - * Sets slug |
|
99 | - * |
|
100 | - * @param string $slug |
|
101 | - * @throws EE_Error |
|
102 | - * @throws ReflectionException |
|
103 | - */ |
|
104 | - public function set_slug(string $slug) |
|
105 | - { |
|
106 | - $this->set('slug', $slug); |
|
107 | - } |
|
97 | + /** |
|
98 | + * Sets slug |
|
99 | + * |
|
100 | + * @param string $slug |
|
101 | + * @throws EE_Error |
|
102 | + * @throws ReflectionException |
|
103 | + */ |
|
104 | + public function set_slug(string $slug) |
|
105 | + { |
|
106 | + $this->set('slug', $slug); |
|
107 | + } |
|
108 | 108 | } |
@@ -23,7 +23,7 @@ |
||
23 | 23 | */ |
24 | 24 | protected function __construct($fieldValues = [], $bydb = false, $timezone = '') |
25 | 25 | { |
26 | - if (! isset($fieldValues['slug'])) { |
|
26 | + if ( ! isset($fieldValues['slug'])) { |
|
27 | 27 | $fieldValues['slug'] = $fieldValues['name']; |
28 | 28 | } |
29 | 29 | parent::__construct($fieldValues, $bydb, $timezone); |
@@ -15,282 +15,282 @@ |
||
15 | 15 | */ |
16 | 16 | class EEG_PayPalCheckout extends EE_Onsite_Gateway |
17 | 17 | { |
18 | - /** |
|
19 | - * Currencies supported by this gateway. |
|
20 | - * |
|
21 | - * @var array |
|
22 | - */ |
|
23 | - protected $_currencies_supported = [ |
|
24 | - 'AUD', |
|
25 | - 'BRL', |
|
26 | - 'CAD', |
|
27 | - 'CNY', |
|
28 | - 'CZK', |
|
29 | - 'DKK', |
|
30 | - 'EUR', |
|
31 | - 'HKD', |
|
32 | - 'HUF', |
|
33 | - 'ILS', |
|
34 | - 'JPY', |
|
35 | - 'MYR', |
|
36 | - 'MXN', |
|
37 | - 'TWD', |
|
38 | - 'NZD', |
|
39 | - 'NOK', |
|
40 | - 'PHP', |
|
41 | - 'PLN', |
|
42 | - 'GBP', |
|
43 | - 'RUB', |
|
44 | - 'SGD', |
|
45 | - 'SEK', |
|
46 | - 'CHF', |
|
47 | - 'THB', |
|
48 | - 'USD', |
|
49 | - ]; |
|
18 | + /** |
|
19 | + * Currencies supported by this gateway. |
|
20 | + * |
|
21 | + * @var array |
|
22 | + */ |
|
23 | + protected $_currencies_supported = [ |
|
24 | + 'AUD', |
|
25 | + 'BRL', |
|
26 | + 'CAD', |
|
27 | + 'CNY', |
|
28 | + 'CZK', |
|
29 | + 'DKK', |
|
30 | + 'EUR', |
|
31 | + 'HKD', |
|
32 | + 'HUF', |
|
33 | + 'ILS', |
|
34 | + 'JPY', |
|
35 | + 'MYR', |
|
36 | + 'MXN', |
|
37 | + 'TWD', |
|
38 | + 'NZD', |
|
39 | + 'NOK', |
|
40 | + 'PHP', |
|
41 | + 'PLN', |
|
42 | + 'GBP', |
|
43 | + 'RUB', |
|
44 | + 'SGD', |
|
45 | + 'SEK', |
|
46 | + 'CHF', |
|
47 | + 'THB', |
|
48 | + 'USD', |
|
49 | + ]; |
|
50 | 50 | |
51 | 51 | |
52 | - /** |
|
53 | - * @param EE_Payment|null $payment |
|
54 | - * @param array|null $billing_info |
|
55 | - * @return EE_Payment |
|
56 | - * @throws EE_Error |
|
57 | - * @throws ReflectionException |
|
58 | - */ |
|
59 | - public function do_direct_payment($payment, $billing_info = null) |
|
60 | - { |
|
61 | - $failed_status = $this->_pay_model->failed_status(); |
|
62 | - $request = LoaderFactory::getLoader()->getShared(RequestInterface::class); |
|
63 | - // Check the payment. |
|
64 | - $payment_valid = $this->validateThisPayment($payment, $request); |
|
65 | - if ($payment_valid->details() === 'error' && $payment_valid->status() === $failed_status) { |
|
66 | - return $payment_valid; |
|
67 | - } |
|
68 | - $transaction = $payment->transaction(); |
|
69 | - $payment_method = $transaction->payment_method(); |
|
52 | + /** |
|
53 | + * @param EE_Payment|null $payment |
|
54 | + * @param array|null $billing_info |
|
55 | + * @return EE_Payment |
|
56 | + * @throws EE_Error |
|
57 | + * @throws ReflectionException |
|
58 | + */ |
|
59 | + public function do_direct_payment($payment, $billing_info = null) |
|
60 | + { |
|
61 | + $failed_status = $this->_pay_model->failed_status(); |
|
62 | + $request = LoaderFactory::getLoader()->getShared(RequestInterface::class); |
|
63 | + // Check the payment. |
|
64 | + $payment_valid = $this->validateThisPayment($payment, $request); |
|
65 | + if ($payment_valid->details() === 'error' && $payment_valid->status() === $failed_status) { |
|
66 | + return $payment_valid; |
|
67 | + } |
|
68 | + $transaction = $payment->transaction(); |
|
69 | + $payment_method = $transaction->payment_method(); |
|
70 | 70 | |
71 | - // Get saved order details. |
|
72 | - try { |
|
73 | - $order = PayPalExtraMetaManager::getPmOption($payment_method, Domain::META_KEY_LAST_ORDER); |
|
74 | - } catch (Exception $exception) { |
|
75 | - return $this->setPaymentFailure( |
|
76 | - $payment, |
|
77 | - $failed_status, |
|
78 | - $request->postParams(), |
|
79 | - $exception->getMessage() |
|
80 | - ); |
|
81 | - } |
|
82 | - $order_id = $request->getRequestParam('pp_order_id', '', DataType::STRING); |
|
83 | - $order_status = $this->isOrderCompleted($order_id, $order); |
|
84 | - if (! $order_status['completed']) { |
|
85 | - return $this->setPaymentFailure( |
|
86 | - $payment, |
|
87 | - $failed_status, |
|
88 | - [$order, $request->postParams()], |
|
89 | - $order_status['message'] |
|
90 | - ); |
|
91 | - } |
|
92 | - // Remove the saved order data. |
|
93 | - PayPalExtraMetaManager::deletePmOption($payment_method, Domain::META_KEY_LAST_ORDER); |
|
94 | - // Looks like all is good. Do a payment success. |
|
95 | - return $this->setPaymentSuccess($payment, $transaction, $order); |
|
96 | - } |
|
71 | + // Get saved order details. |
|
72 | + try { |
|
73 | + $order = PayPalExtraMetaManager::getPmOption($payment_method, Domain::META_KEY_LAST_ORDER); |
|
74 | + } catch (Exception $exception) { |
|
75 | + return $this->setPaymentFailure( |
|
76 | + $payment, |
|
77 | + $failed_status, |
|
78 | + $request->postParams(), |
|
79 | + $exception->getMessage() |
|
80 | + ); |
|
81 | + } |
|
82 | + $order_id = $request->getRequestParam('pp_order_id', '', DataType::STRING); |
|
83 | + $order_status = $this->isOrderCompleted($order_id, $order); |
|
84 | + if (! $order_status['completed']) { |
|
85 | + return $this->setPaymentFailure( |
|
86 | + $payment, |
|
87 | + $failed_status, |
|
88 | + [$order, $request->postParams()], |
|
89 | + $order_status['message'] |
|
90 | + ); |
|
91 | + } |
|
92 | + // Remove the saved order data. |
|
93 | + PayPalExtraMetaManager::deletePmOption($payment_method, Domain::META_KEY_LAST_ORDER); |
|
94 | + // Looks like all is good. Do a payment success. |
|
95 | + return $this->setPaymentSuccess($payment, $transaction, $order); |
|
96 | + } |
|
97 | 97 | |
98 | 98 | |
99 | - /** |
|
100 | - * Validate the payment. |
|
101 | - * |
|
102 | - * @param mixed $payment |
|
103 | - * @param RequestInterface $request |
|
104 | - * @return EE_Payment |
|
105 | - * @throws EE_Error |
|
106 | - * @throws ReflectionException |
|
107 | - */ |
|
108 | - public function validateThisPayment(?EE_Payment $payment, RequestInterface $request): EE_Payment |
|
109 | - { |
|
110 | - $failed_status = $this->_pay_model->failed_status(); |
|
111 | - // Check the payment. |
|
112 | - if (! $payment instanceof EE_Payment) { |
|
113 | - $payment = EE_Payment::new_instance(); |
|
114 | - $error_message = esc_html__('Error. No associated payment was found.', 'event_espresso'); |
|
115 | - return $this->setPaymentFailure($payment, $failed_status, $request->postParams(), $error_message); |
|
116 | - } |
|
117 | - // Check the transaction. |
|
118 | - $transaction = $payment->transaction(); |
|
119 | - if (! $transaction instanceof EE_Transaction) { |
|
120 | - $error_message = esc_html__( |
|
121 | - 'Could not process this payment because it has no associated transaction.', |
|
122 | - 'event_espresso' |
|
123 | - ); |
|
124 | - return $this->setPaymentFailure($payment, $failed_status, $request->postParams(), $error_message); |
|
125 | - } |
|
126 | - // Check for the payment nonce. |
|
127 | - // $order_nonce = $request->getRequestParam('pp_order_nonce'); |
|
128 | - // if (empty($order_nonce) || ! wp_verify_nonce($order_nonce, Domain::CAPTURE_ORDER_NONCE_NAME)) { |
|
129 | - // $error_message = esc_html__('No or incorrect order capture nonce provided !', 'event_espresso'); |
|
130 | - // return $this->setPaymentFailure($payment, $failed_status, $request->postParams(), $error_message); |
|
131 | - // } |
|
132 | - return $payment; |
|
133 | - } |
|
99 | + /** |
|
100 | + * Validate the payment. |
|
101 | + * |
|
102 | + * @param mixed $payment |
|
103 | + * @param RequestInterface $request |
|
104 | + * @return EE_Payment |
|
105 | + * @throws EE_Error |
|
106 | + * @throws ReflectionException |
|
107 | + */ |
|
108 | + public function validateThisPayment(?EE_Payment $payment, RequestInterface $request): EE_Payment |
|
109 | + { |
|
110 | + $failed_status = $this->_pay_model->failed_status(); |
|
111 | + // Check the payment. |
|
112 | + if (! $payment instanceof EE_Payment) { |
|
113 | + $payment = EE_Payment::new_instance(); |
|
114 | + $error_message = esc_html__('Error. No associated payment was found.', 'event_espresso'); |
|
115 | + return $this->setPaymentFailure($payment, $failed_status, $request->postParams(), $error_message); |
|
116 | + } |
|
117 | + // Check the transaction. |
|
118 | + $transaction = $payment->transaction(); |
|
119 | + if (! $transaction instanceof EE_Transaction) { |
|
120 | + $error_message = esc_html__( |
|
121 | + 'Could not process this payment because it has no associated transaction.', |
|
122 | + 'event_espresso' |
|
123 | + ); |
|
124 | + return $this->setPaymentFailure($payment, $failed_status, $request->postParams(), $error_message); |
|
125 | + } |
|
126 | + // Check for the payment nonce. |
|
127 | + // $order_nonce = $request->getRequestParam('pp_order_nonce'); |
|
128 | + // if (empty($order_nonce) || ! wp_verify_nonce($order_nonce, Domain::CAPTURE_ORDER_NONCE_NAME)) { |
|
129 | + // $error_message = esc_html__('No or incorrect order capture nonce provided !', 'event_espresso'); |
|
130 | + // return $this->setPaymentFailure($payment, $failed_status, $request->postParams(), $error_message); |
|
131 | + // } |
|
132 | + return $payment; |
|
133 | + } |
|
134 | 134 | |
135 | 135 | |
136 | - /** |
|
137 | - * Validate the Order. |
|
138 | - * |
|
139 | - * @param string $provided_order_id |
|
140 | - * @param $order |
|
141 | - * @return array ['completed' => {boolean}, 'message' => {string}] |
|
142 | - */ |
|
143 | - public function isOrderCompleted(string $provided_order_id, $order): array |
|
144 | - { |
|
145 | - $conclusion = [ |
|
146 | - 'completed' => false, |
|
147 | - 'message' => esc_html__('Could not validate this Order.', 'event_espresso'), |
|
148 | - ]; |
|
149 | - // Check the provided Order and order ID. |
|
150 | - if (! $provided_order_id) { |
|
151 | - $conclusion['message'] = esc_html__( |
|
152 | - 'Invalid Order ID provided! Not able to confirm the order', |
|
153 | - 'event_espresso' |
|
154 | - ); |
|
155 | - } elseif (! $order || ! is_array($order)) { |
|
156 | - $conclusion['message'] = esc_html__('Order data is in wrong format.', 'event_espresso'); |
|
157 | - } elseif ($order['id'] !== $provided_order_id) { |
|
158 | - $conclusion['message'] = esc_html__('Order ID mismatch.', 'event_espresso'); |
|
159 | - } elseif (empty($order['status'])) { |
|
160 | - $conclusion['message'] = esc_html__( |
|
161 | - 'There was an error with this payment. The status of the Order could not be determined.', |
|
162 | - 'event_espresso' |
|
163 | - ); |
|
164 | - } elseif ($order['status'] !== 'COMPLETED') { |
|
165 | - $conclusion['message'] = esc_html__( |
|
166 | - 'There was an error with this payment. Order was not approved.', |
|
167 | - 'event_espresso' |
|
168 | - ); |
|
169 | - } elseif (empty($order['purchase_units'][0]['payments']['captures'][0]['status'])) { |
|
170 | - $conclusion['message'] = esc_html__( |
|
171 | - 'There was an error with this payment. The status of the Payment could not be determined.', |
|
172 | - 'event_espresso' |
|
173 | - ); |
|
174 | - } elseif ($order['purchase_units'][0]['payments']['captures'][0]['status'] !== 'COMPLETED') { |
|
175 | - $conclusion['message'] = esc_html__( |
|
176 | - 'This payment was declined or failed validation. Please check the billing information you provided.', |
|
177 | - 'event_espresso' |
|
178 | - ); |
|
179 | - } else { |
|
180 | - // If we didn't fail on the above, the Order should be considered valid. |
|
181 | - $conclusion['completed'] = true; |
|
182 | - } |
|
183 | - return $conclusion; |
|
184 | - } |
|
136 | + /** |
|
137 | + * Validate the Order. |
|
138 | + * |
|
139 | + * @param string $provided_order_id |
|
140 | + * @param $order |
|
141 | + * @return array ['completed' => {boolean}, 'message' => {string}] |
|
142 | + */ |
|
143 | + public function isOrderCompleted(string $provided_order_id, $order): array |
|
144 | + { |
|
145 | + $conclusion = [ |
|
146 | + 'completed' => false, |
|
147 | + 'message' => esc_html__('Could not validate this Order.', 'event_espresso'), |
|
148 | + ]; |
|
149 | + // Check the provided Order and order ID. |
|
150 | + if (! $provided_order_id) { |
|
151 | + $conclusion['message'] = esc_html__( |
|
152 | + 'Invalid Order ID provided! Not able to confirm the order', |
|
153 | + 'event_espresso' |
|
154 | + ); |
|
155 | + } elseif (! $order || ! is_array($order)) { |
|
156 | + $conclusion['message'] = esc_html__('Order data is in wrong format.', 'event_espresso'); |
|
157 | + } elseif ($order['id'] !== $provided_order_id) { |
|
158 | + $conclusion['message'] = esc_html__('Order ID mismatch.', 'event_espresso'); |
|
159 | + } elseif (empty($order['status'])) { |
|
160 | + $conclusion['message'] = esc_html__( |
|
161 | + 'There was an error with this payment. The status of the Order could not be determined.', |
|
162 | + 'event_espresso' |
|
163 | + ); |
|
164 | + } elseif ($order['status'] !== 'COMPLETED') { |
|
165 | + $conclusion['message'] = esc_html__( |
|
166 | + 'There was an error with this payment. Order was not approved.', |
|
167 | + 'event_espresso' |
|
168 | + ); |
|
169 | + } elseif (empty($order['purchase_units'][0]['payments']['captures'][0]['status'])) { |
|
170 | + $conclusion['message'] = esc_html__( |
|
171 | + 'There was an error with this payment. The status of the Payment could not be determined.', |
|
172 | + 'event_espresso' |
|
173 | + ); |
|
174 | + } elseif ($order['purchase_units'][0]['payments']['captures'][0]['status'] !== 'COMPLETED') { |
|
175 | + $conclusion['message'] = esc_html__( |
|
176 | + 'This payment was declined or failed validation. Please check the billing information you provided.', |
|
177 | + 'event_espresso' |
|
178 | + ); |
|
179 | + } else { |
|
180 | + // If we didn't fail on the above, the Order should be considered valid. |
|
181 | + $conclusion['completed'] = true; |
|
182 | + } |
|
183 | + return $conclusion; |
|
184 | + } |
|
185 | 185 | |
186 | 186 | |
187 | - /** |
|
188 | - * Set a payment error and log the data. |
|
189 | - * |
|
190 | - * @param EE_Payment $payment |
|
191 | - * @param string $status |
|
192 | - * @param array|string $response_data |
|
193 | - * @param string $err_message |
|
194 | - * @return EE_Payment |
|
195 | - * @throws EE_Error|ReflectionException |
|
196 | - */ |
|
197 | - public function setPaymentFailure( |
|
198 | - EE_Payment $payment, |
|
199 | - string $status, |
|
200 | - $response_data, |
|
201 | - string $err_message = '' |
|
202 | - ): EE_Payment { |
|
203 | - $this->log(['Error request data:' => $response_data], $payment); |
|
204 | - $err_message = $err_message ?: sprintf( |
|
205 | - esc_html__( |
|
206 | - 'Your payment could not be processed successfully due to a technical issue.%sPlease try again or contact %s for assistance.', |
|
207 | - 'event_espresso' |
|
208 | - ), |
|
209 | - '<br/>', |
|
210 | - EE_Registry::instance()->CFG->organization->get_pretty('email') |
|
211 | - ); |
|
212 | - $payment->set_status($status); |
|
213 | - $payment->set_details($err_message); |
|
214 | - $payment->set_gateway_response($err_message); |
|
215 | - return $payment; |
|
216 | - } |
|
187 | + /** |
|
188 | + * Set a payment error and log the data. |
|
189 | + * |
|
190 | + * @param EE_Payment $payment |
|
191 | + * @param string $status |
|
192 | + * @param array|string $response_data |
|
193 | + * @param string $err_message |
|
194 | + * @return EE_Payment |
|
195 | + * @throws EE_Error|ReflectionException |
|
196 | + */ |
|
197 | + public function setPaymentFailure( |
|
198 | + EE_Payment $payment, |
|
199 | + string $status, |
|
200 | + $response_data, |
|
201 | + string $err_message = '' |
|
202 | + ): EE_Payment { |
|
203 | + $this->log(['Error request data:' => $response_data], $payment); |
|
204 | + $err_message = $err_message ?: sprintf( |
|
205 | + esc_html__( |
|
206 | + 'Your payment could not be processed successfully due to a technical issue.%sPlease try again or contact %s for assistance.', |
|
207 | + 'event_espresso' |
|
208 | + ), |
|
209 | + '<br/>', |
|
210 | + EE_Registry::instance()->CFG->organization->get_pretty('email') |
|
211 | + ); |
|
212 | + $payment->set_status($status); |
|
213 | + $payment->set_details($err_message); |
|
214 | + $payment->set_gateway_response($err_message); |
|
215 | + return $payment; |
|
216 | + } |
|
217 | 217 | |
218 | 218 | |
219 | - /** |
|
220 | - * Set the payment success. |
|
221 | - * |
|
222 | - * @param EE_Payment $payment |
|
223 | - * @param EE_Transaction $transaction |
|
224 | - * @param array $order |
|
225 | - * @return EE_Payment |
|
226 | - * @throws EE_Error|ReflectionException |
|
227 | - */ |
|
228 | - public function setPaymentSuccess(EE_Payment $payment, EE_Transaction $transaction, array $order): EE_Payment |
|
229 | - { |
|
230 | - $amount = $order['purchase_units'][0]['payments']['captures'][0]['amount']['value'] ?? 0; |
|
231 | - // Don't set the amount if there is no info on that with this order. |
|
232 | - if (! $amount) { |
|
233 | - $this->log(['Success order but amount is 0 !' => $order], $payment); |
|
234 | - } else { |
|
235 | - $payment->set_amount((float) $amount); |
|
236 | - } |
|
237 | - $payment->set_status(EEM_Payment::status_id_approved); |
|
238 | - $payment->set_txn_id_chq_nmbr($order['purchase_units'][0]['payments']['captures'][0]['id'] ?? $order['id']); |
|
239 | - $payment->set_gateway_response($order['status'] ?? 'success'); |
|
240 | - $this->saveBillingDetails($payment, $transaction, $order); |
|
241 | - return $payment; |
|
242 | - } |
|
219 | + /** |
|
220 | + * Set the payment success. |
|
221 | + * |
|
222 | + * @param EE_Payment $payment |
|
223 | + * @param EE_Transaction $transaction |
|
224 | + * @param array $order |
|
225 | + * @return EE_Payment |
|
226 | + * @throws EE_Error|ReflectionException |
|
227 | + */ |
|
228 | + public function setPaymentSuccess(EE_Payment $payment, EE_Transaction $transaction, array $order): EE_Payment |
|
229 | + { |
|
230 | + $amount = $order['purchase_units'][0]['payments']['captures'][0]['amount']['value'] ?? 0; |
|
231 | + // Don't set the amount if there is no info on that with this order. |
|
232 | + if (! $amount) { |
|
233 | + $this->log(['Success order but amount is 0 !' => $order], $payment); |
|
234 | + } else { |
|
235 | + $payment->set_amount((float) $amount); |
|
236 | + } |
|
237 | + $payment->set_status(EEM_Payment::status_id_approved); |
|
238 | + $payment->set_txn_id_chq_nmbr($order['purchase_units'][0]['payments']['captures'][0]['id'] ?? $order['id']); |
|
239 | + $payment->set_gateway_response($order['status'] ?? 'success'); |
|
240 | + $this->saveBillingDetails($payment, $transaction, $order); |
|
241 | + return $payment; |
|
242 | + } |
|
243 | 243 | |
244 | 244 | |
245 | - /** |
|
246 | - * Save some transaction details, like billing information. |
|
247 | - * |
|
248 | - * @param EE_Payment $payment |
|
249 | - * @param EE_Transaction $transaction |
|
250 | - * @param array $order |
|
251 | - * @return void |
|
252 | - * @throws EE_Error|ReflectionException |
|
253 | - */ |
|
254 | - public function saveBillingDetails(EE_Payment $payment, EE_Transaction $transaction, array $order): void |
|
255 | - { |
|
256 | - $input_values = []; |
|
257 | - $primary_reg = $transaction->primary_registration(); |
|
258 | - $attendee = $primary_reg instanceof EE_Registration ? $primary_reg->attendee() : null; |
|
259 | - $transaction = $payment->transaction(); |
|
260 | - $payment_method = $transaction->payment_method(); |
|
261 | - $postmeta_name = $payment_method->type_obj() instanceof EE_PMT_Base |
|
262 | - ? 'billing_info_' . $payment_method->type_obj()->system_name() |
|
263 | - : ''; |
|
264 | - if (empty($order['payment_source']) || ! $attendee instanceof EE_Attendee) { |
|
265 | - // I guess we are done here then. Just save what we have. |
|
266 | - $payment->set_details($order); |
|
267 | - return; |
|
268 | - } |
|
269 | - // Do we have order information from the express checkout (PayPal button) ? |
|
270 | - if (! empty($order['payment_source']['paypal'])) { |
|
271 | - $payer = $order['payment_source']['paypal']; |
|
272 | - $payer_name = $payer['name'] ?? ''; |
|
273 | - $input_values['first_name'] = $payer_name['given_name'] ?? $attendee->fname(); |
|
274 | - $input_values['last_name'] = $payer_name['surname'] ?? $attendee->lname(); |
|
275 | - $input_values['email'] = $payer_name['email_address'] ?? $attendee->email(); |
|
276 | - $input_values['address'] = $payer_name['address_line_1'] ?? $attendee->address(); |
|
277 | - $input_values['address2'] = $payer_name['address_line_2'] ?? $attendee->address2(); |
|
278 | - $input_values['city'] = $payer_name['admin_area_2'] ?? $attendee->city(); |
|
279 | - $input_values['country'] = $payer_name['country_code'] ?? $attendee->country(); |
|
280 | - $input_values['zip'] = $payer_name['postal_code'] ?? $attendee->zip(); |
|
281 | - } |
|
282 | - // Or card information from ACDC ? |
|
283 | - if (! empty($order['payment_source']['card'])) { |
|
284 | - $payer_card = $order['payment_source']['card']; |
|
285 | - if (! empty($payer_card['name'])) { |
|
286 | - $full_name = explode(' ', $payer_card['name']); |
|
287 | - // Don't need to save each field because others should be populated from the billing form. |
|
288 | - $input_values['credit_card'] = $payer_card['last_digits'] ?? ''; |
|
289 | - $input_values['first_name'] = $full_name[0] ?? $attendee->fname(); |
|
290 | - $input_values['last_name'] = $full_name[1] ?? $attendee->lname(); |
|
291 | - } |
|
292 | - } |
|
293 | - update_post_meta($attendee->ID(), $postmeta_name, $input_values); |
|
294 | - $attendee->save(); |
|
295 | - } |
|
245 | + /** |
|
246 | + * Save some transaction details, like billing information. |
|
247 | + * |
|
248 | + * @param EE_Payment $payment |
|
249 | + * @param EE_Transaction $transaction |
|
250 | + * @param array $order |
|
251 | + * @return void |
|
252 | + * @throws EE_Error|ReflectionException |
|
253 | + */ |
|
254 | + public function saveBillingDetails(EE_Payment $payment, EE_Transaction $transaction, array $order): void |
|
255 | + { |
|
256 | + $input_values = []; |
|
257 | + $primary_reg = $transaction->primary_registration(); |
|
258 | + $attendee = $primary_reg instanceof EE_Registration ? $primary_reg->attendee() : null; |
|
259 | + $transaction = $payment->transaction(); |
|
260 | + $payment_method = $transaction->payment_method(); |
|
261 | + $postmeta_name = $payment_method->type_obj() instanceof EE_PMT_Base |
|
262 | + ? 'billing_info_' . $payment_method->type_obj()->system_name() |
|
263 | + : ''; |
|
264 | + if (empty($order['payment_source']) || ! $attendee instanceof EE_Attendee) { |
|
265 | + // I guess we are done here then. Just save what we have. |
|
266 | + $payment->set_details($order); |
|
267 | + return; |
|
268 | + } |
|
269 | + // Do we have order information from the express checkout (PayPal button) ? |
|
270 | + if (! empty($order['payment_source']['paypal'])) { |
|
271 | + $payer = $order['payment_source']['paypal']; |
|
272 | + $payer_name = $payer['name'] ?? ''; |
|
273 | + $input_values['first_name'] = $payer_name['given_name'] ?? $attendee->fname(); |
|
274 | + $input_values['last_name'] = $payer_name['surname'] ?? $attendee->lname(); |
|
275 | + $input_values['email'] = $payer_name['email_address'] ?? $attendee->email(); |
|
276 | + $input_values['address'] = $payer_name['address_line_1'] ?? $attendee->address(); |
|
277 | + $input_values['address2'] = $payer_name['address_line_2'] ?? $attendee->address2(); |
|
278 | + $input_values['city'] = $payer_name['admin_area_2'] ?? $attendee->city(); |
|
279 | + $input_values['country'] = $payer_name['country_code'] ?? $attendee->country(); |
|
280 | + $input_values['zip'] = $payer_name['postal_code'] ?? $attendee->zip(); |
|
281 | + } |
|
282 | + // Or card information from ACDC ? |
|
283 | + if (! empty($order['payment_source']['card'])) { |
|
284 | + $payer_card = $order['payment_source']['card']; |
|
285 | + if (! empty($payer_card['name'])) { |
|
286 | + $full_name = explode(' ', $payer_card['name']); |
|
287 | + // Don't need to save each field because others should be populated from the billing form. |
|
288 | + $input_values['credit_card'] = $payer_card['last_digits'] ?? ''; |
|
289 | + $input_values['first_name'] = $full_name[0] ?? $attendee->fname(); |
|
290 | + $input_values['last_name'] = $full_name[1] ?? $attendee->lname(); |
|
291 | + } |
|
292 | + } |
|
293 | + update_post_meta($attendee->ID(), $postmeta_name, $input_values); |
|
294 | + $attendee->save(); |
|
295 | + } |
|
296 | 296 | } |
@@ -81,7 +81,7 @@ discard block |
||
81 | 81 | } |
82 | 82 | $order_id = $request->getRequestParam('pp_order_id', '', DataType::STRING); |
83 | 83 | $order_status = $this->isOrderCompleted($order_id, $order); |
84 | - if (! $order_status['completed']) { |
|
84 | + if ( ! $order_status['completed']) { |
|
85 | 85 | return $this->setPaymentFailure( |
86 | 86 | $payment, |
87 | 87 | $failed_status, |
@@ -109,14 +109,14 @@ discard block |
||
109 | 109 | { |
110 | 110 | $failed_status = $this->_pay_model->failed_status(); |
111 | 111 | // Check the payment. |
112 | - if (! $payment instanceof EE_Payment) { |
|
112 | + if ( ! $payment instanceof EE_Payment) { |
|
113 | 113 | $payment = EE_Payment::new_instance(); |
114 | 114 | $error_message = esc_html__('Error. No associated payment was found.', 'event_espresso'); |
115 | 115 | return $this->setPaymentFailure($payment, $failed_status, $request->postParams(), $error_message); |
116 | 116 | } |
117 | 117 | // Check the transaction. |
118 | 118 | $transaction = $payment->transaction(); |
119 | - if (! $transaction instanceof EE_Transaction) { |
|
119 | + if ( ! $transaction instanceof EE_Transaction) { |
|
120 | 120 | $error_message = esc_html__( |
121 | 121 | 'Could not process this payment because it has no associated transaction.', |
122 | 122 | 'event_espresso' |
@@ -147,12 +147,12 @@ discard block |
||
147 | 147 | 'message' => esc_html__('Could not validate this Order.', 'event_espresso'), |
148 | 148 | ]; |
149 | 149 | // Check the provided Order and order ID. |
150 | - if (! $provided_order_id) { |
|
150 | + if ( ! $provided_order_id) { |
|
151 | 151 | $conclusion['message'] = esc_html__( |
152 | 152 | 'Invalid Order ID provided! Not able to confirm the order', |
153 | 153 | 'event_espresso' |
154 | 154 | ); |
155 | - } elseif (! $order || ! is_array($order)) { |
|
155 | + } elseif ( ! $order || ! is_array($order)) { |
|
156 | 156 | $conclusion['message'] = esc_html__('Order data is in wrong format.', 'event_espresso'); |
157 | 157 | } elseif ($order['id'] !== $provided_order_id) { |
158 | 158 | $conclusion['message'] = esc_html__('Order ID mismatch.', 'event_espresso'); |
@@ -229,7 +229,7 @@ discard block |
||
229 | 229 | { |
230 | 230 | $amount = $order['purchase_units'][0]['payments']['captures'][0]['amount']['value'] ?? 0; |
231 | 231 | // Don't set the amount if there is no info on that with this order. |
232 | - if (! $amount) { |
|
232 | + if ( ! $amount) { |
|
233 | 233 | $this->log(['Success order but amount is 0 !' => $order], $payment); |
234 | 234 | } else { |
235 | 235 | $payment->set_amount((float) $amount); |
@@ -259,7 +259,7 @@ discard block |
||
259 | 259 | $transaction = $payment->transaction(); |
260 | 260 | $payment_method = $transaction->payment_method(); |
261 | 261 | $postmeta_name = $payment_method->type_obj() instanceof EE_PMT_Base |
262 | - ? 'billing_info_' . $payment_method->type_obj()->system_name() |
|
262 | + ? 'billing_info_'.$payment_method->type_obj()->system_name() |
|
263 | 263 | : ''; |
264 | 264 | if (empty($order['payment_source']) || ! $attendee instanceof EE_Attendee) { |
265 | 265 | // I guess we are done here then. Just save what we have. |
@@ -267,7 +267,7 @@ discard block |
||
267 | 267 | return; |
268 | 268 | } |
269 | 269 | // Do we have order information from the express checkout (PayPal button) ? |
270 | - if (! empty($order['payment_source']['paypal'])) { |
|
270 | + if ( ! empty($order['payment_source']['paypal'])) { |
|
271 | 271 | $payer = $order['payment_source']['paypal']; |
272 | 272 | $payer_name = $payer['name'] ?? ''; |
273 | 273 | $input_values['first_name'] = $payer_name['given_name'] ?? $attendee->fname(); |
@@ -280,9 +280,9 @@ discard block |
||
280 | 280 | $input_values['zip'] = $payer_name['postal_code'] ?? $attendee->zip(); |
281 | 281 | } |
282 | 282 | // Or card information from ACDC ? |
283 | - if (! empty($order['payment_source']['card'])) { |
|
283 | + if ( ! empty($order['payment_source']['card'])) { |
|
284 | 284 | $payer_card = $order['payment_source']['card']; |
285 | - if (! empty($payer_card['name'])) { |
|
285 | + if ( ! empty($payer_card['name'])) { |
|
286 | 286 | $full_name = explode(' ', $payer_card['name']); |
287 | 287 | // Don't need to save each field because others should be populated from the billing form. |
288 | 288 | $input_values['credit_card'] = $payer_card['last_digits'] ?? ''; |
@@ -15,169 +15,169 @@ |
||
15 | 15 | */ |
16 | 16 | class Domain |
17 | 17 | { |
18 | - /** |
|
19 | - * Name of the extra meta key that stores this PM options as one meta. |
|
20 | - */ |
|
21 | - public const META_KEY_PAYPAL_DATA = 'paypal_data'; |
|
22 | - /** |
|
23 | - * Name of the extra meta key that stores this PM options as one meta. |
|
24 | - */ |
|
25 | - public const META_KEY_PAYPAL_DATA_SANDBOX = 'paypal_data_sandbox'; |
|
26 | - |
|
27 | - /** |
|
28 | - * Name of the extra meta that stores whether the credentials were for the sandbox or live mode. |
|
29 | - */ |
|
30 | - public const META_KEY_SANDBOX_MODE = 'sandbox_mode'; |
|
31 | - |
|
32 | - /** |
|
33 | - * Name of the extra meta that stores the Access Token that is used to auth in API requests to PayPal. |
|
34 | - */ |
|
35 | - public const META_KEY_ACCESS_TOKEN = 'access_token'; |
|
36 | - |
|
37 | - /** |
|
38 | - * Name of the extra meta that stores the last request tracking ID. |
|
39 | - */ |
|
40 | - public const META_KEY_TRACKING_ID = 'tracking_id'; |
|
41 | - |
|
42 | - /** |
|
43 | - * Name of the extra meta that stores the Event Espresso PayPal Account's merchant id. |
|
44 | - */ |
|
45 | - public const META_KEY_APP_ID = 'app_id'; |
|
46 | - |
|
47 | - /** |
|
48 | - * Name of the extra meta that holds the seller client ID. |
|
49 | - */ |
|
50 | - public const META_KEY_CLIENT_ID = 'client_id'; |
|
51 | - |
|
52 | - /** |
|
53 | - * Name of the extra meta that holds the seller secret. |
|
54 | - */ |
|
55 | - public const META_KEY_CLIENT_SECRET = 'client_secret'; |
|
56 | - |
|
57 | - /** |
|
58 | - * Name of the extra meta that stores the expiration date of the Access Token. |
|
59 | - */ |
|
60 | - public const META_KEY_TOKEN_EXPIRES_IN = 'expires_in'; |
|
61 | - |
|
62 | - /** |
|
63 | - * Name of the extra meta that holds the partner client ID. |
|
64 | - */ |
|
65 | - public const META_KEY_PARTNER_CLIENT_ID = 'partner_client_id'; |
|
66 | - |
|
67 | - /** |
|
68 | - * Name of the extra meta that holds the partner merchant ID. |
|
69 | - */ |
|
70 | - public const META_KEY_PARTNER_MERCHANT_ID = 'partner_merchant_id'; |
|
71 | - |
|
72 | - /** |
|
73 | - * Name of the extra meta that holds the seller merchant ID. |
|
74 | - */ |
|
75 | - public const META_KEY_SELLER_MERCHANT_ID = 'merchantIdInPayPal'; |
|
76 | - |
|
77 | - /** |
|
78 | - * Name of the extra meta that holds the onboarding URL. |
|
79 | - */ |
|
80 | - public const META_KEY_ONBOARDING_URL = 'onboarding_url'; |
|
81 | - |
|
82 | - /** |
|
83 | - * Name of the extra meta that holds the BN / request tracking code. |
|
84 | - */ |
|
85 | - public const META_KEY_BN_CODE = 'bn_code'; |
|
86 | - |
|
87 | - /** |
|
88 | - * Name of the extra meta that holds the last order details. |
|
89 | - */ |
|
90 | - public const META_KEY_LAST_ORDER = 'last_order_details'; |
|
91 | - |
|
92 | - /** |
|
93 | - * Name of the extra meta that stores the allowed PP checkout type selected by merchant while onboarding. |
|
94 | - */ |
|
95 | - public const META_KEY_ALLOWED_CHECKOUT_TYPE = 'allowed_checkout_type'; |
|
96 | - |
|
97 | - /** |
|
98 | - * Name of the extra meta that stores the PP checkout type selected by merchant after onboarding. |
|
99 | - */ |
|
100 | - public const META_KEY_CHECKOUT_TYPE = 'checkout_type'; |
|
101 | - |
|
102 | - /** |
|
103 | - * Name of the PayPal API parameter that holds the auth code. |
|
104 | - */ |
|
105 | - public const API_KEY_AUTH_CODE = 'authCode'; |
|
106 | - |
|
107 | - /** |
|
108 | - * Name of the PayPal API parameter that holds the client token. |
|
109 | - */ |
|
110 | - public const API_KEY_CLIENT_TOKEN = 'client_token'; |
|
111 | - |
|
112 | - /** |
|
113 | - * Name of the extra meta that holds the seller payer ID. |
|
114 | - */ |
|
115 | - public const META_KEY_PAYER_ID = 'payer_id'; |
|
116 | - |
|
117 | - /** |
|
118 | - * Name of the PayPal API parameter that holds the client token expiration time. |
|
119 | - */ |
|
120 | - public const API_KEY_EXPIRES_IN = 'expires_in'; |
|
121 | - |
|
122 | - /** |
|
123 | - * Name of the PayPal API parameter that holds the list of oAuth integrations related to the merchant. |
|
124 | - */ |
|
125 | - public const API_PARAM_OAUTH_INTEGRATIONS = 'oauth_integrations'; |
|
126 | - |
|
127 | - /** |
|
128 | - * Name of the PayPal API parameter that holds the list of third party permissions that were granted. |
|
129 | - */ |
|
130 | - public const API_PARAM_PERMISSIONS_GRANTED = 'oauth_third_party'; |
|
131 | - |
|
132 | - /** |
|
133 | - * Name of the PayPal API parameter that holds the bool of if the primary email was confirmed. |
|
134 | - */ |
|
135 | - public const API_PARAM_PRIM_EMAIL_CONFIRMED = 'primary_email_confirmed'; |
|
136 | - |
|
137 | - /** |
|
138 | - * Name of the PayPal API parameter that holds the bool of if email was confirmed. |
|
139 | - */ |
|
140 | - public const API_PARAM_EMAIL_CONFIRMED = 'isEmailConfirmed'; |
|
141 | - |
|
142 | - /** |
|
143 | - * Name of the PayPal API parameter that holds the partner ID. |
|
144 | - */ |
|
145 | - public const API_PARAM_PARTNER_ID = 'merchantId'; |
|
146 | - |
|
147 | - /** |
|
148 | - * Name of the PayPal API parameter that holds the merchant ID in the Track seller onboarding status request. |
|
149 | - */ |
|
150 | - public const API_PARAM_TRACK_MERCHANT_ID = 'merchant_id'; |
|
151 | - |
|
152 | - /** |
|
153 | - * Name of the PayPal API parameter that holds the payments_receivable status. |
|
154 | - */ |
|
155 | - public const API_PARAM_PAYMENTS_RECEIVABLE = 'payments_receivable'; |
|
156 | - |
|
157 | - /** |
|
158 | - * Name of the nonce used in the capture order request. |
|
159 | - */ |
|
160 | - public const CAPTURE_ORDER_NONCE_NAME = 'eea_pp_commerce_capture_order_payment'; |
|
161 | - |
|
162 | - /** |
|
163 | - * Name of the nonce used in the onboarding process. |
|
164 | - */ |
|
165 | - public const NONCE_NAME_ONBOARDING_RETURN = 'eea_pp_commerce_onboarding_return'; |
|
166 | - |
|
167 | - /** |
|
168 | - * Holds this payment method slug. |
|
169 | - */ |
|
170 | - public const PM_SLUG = 'paypalcheckout'; |
|
171 | - |
|
172 | - |
|
173 | - /** |
|
174 | - * Returns the base PayPal API URL. |
|
175 | - * |
|
176 | - * @param EE_Payment_Method $payment_method |
|
177 | - * @return string |
|
178 | - */ |
|
179 | - public static function getPayPalApiUrl(EE_Payment_Method $payment_method): string |
|
180 | - { |
|
181 | - return $payment_method->debug_mode() ? 'https://api-m.sandbox.paypal.com' : 'https://api-m.paypal.com'; |
|
182 | - } |
|
18 | + /** |
|
19 | + * Name of the extra meta key that stores this PM options as one meta. |
|
20 | + */ |
|
21 | + public const META_KEY_PAYPAL_DATA = 'paypal_data'; |
|
22 | + /** |
|
23 | + * Name of the extra meta key that stores this PM options as one meta. |
|
24 | + */ |
|
25 | + public const META_KEY_PAYPAL_DATA_SANDBOX = 'paypal_data_sandbox'; |
|
26 | + |
|
27 | + /** |
|
28 | + * Name of the extra meta that stores whether the credentials were for the sandbox or live mode. |
|
29 | + */ |
|
30 | + public const META_KEY_SANDBOX_MODE = 'sandbox_mode'; |
|
31 | + |
|
32 | + /** |
|
33 | + * Name of the extra meta that stores the Access Token that is used to auth in API requests to PayPal. |
|
34 | + */ |
|
35 | + public const META_KEY_ACCESS_TOKEN = 'access_token'; |
|
36 | + |
|
37 | + /** |
|
38 | + * Name of the extra meta that stores the last request tracking ID. |
|
39 | + */ |
|
40 | + public const META_KEY_TRACKING_ID = 'tracking_id'; |
|
41 | + |
|
42 | + /** |
|
43 | + * Name of the extra meta that stores the Event Espresso PayPal Account's merchant id. |
|
44 | + */ |
|
45 | + public const META_KEY_APP_ID = 'app_id'; |
|
46 | + |
|
47 | + /** |
|
48 | + * Name of the extra meta that holds the seller client ID. |
|
49 | + */ |
|
50 | + public const META_KEY_CLIENT_ID = 'client_id'; |
|
51 | + |
|
52 | + /** |
|
53 | + * Name of the extra meta that holds the seller secret. |
|
54 | + */ |
|
55 | + public const META_KEY_CLIENT_SECRET = 'client_secret'; |
|
56 | + |
|
57 | + /** |
|
58 | + * Name of the extra meta that stores the expiration date of the Access Token. |
|
59 | + */ |
|
60 | + public const META_KEY_TOKEN_EXPIRES_IN = 'expires_in'; |
|
61 | + |
|
62 | + /** |
|
63 | + * Name of the extra meta that holds the partner client ID. |
|
64 | + */ |
|
65 | + public const META_KEY_PARTNER_CLIENT_ID = 'partner_client_id'; |
|
66 | + |
|
67 | + /** |
|
68 | + * Name of the extra meta that holds the partner merchant ID. |
|
69 | + */ |
|
70 | + public const META_KEY_PARTNER_MERCHANT_ID = 'partner_merchant_id'; |
|
71 | + |
|
72 | + /** |
|
73 | + * Name of the extra meta that holds the seller merchant ID. |
|
74 | + */ |
|
75 | + public const META_KEY_SELLER_MERCHANT_ID = 'merchantIdInPayPal'; |
|
76 | + |
|
77 | + /** |
|
78 | + * Name of the extra meta that holds the onboarding URL. |
|
79 | + */ |
|
80 | + public const META_KEY_ONBOARDING_URL = 'onboarding_url'; |
|
81 | + |
|
82 | + /** |
|
83 | + * Name of the extra meta that holds the BN / request tracking code. |
|
84 | + */ |
|
85 | + public const META_KEY_BN_CODE = 'bn_code'; |
|
86 | + |
|
87 | + /** |
|
88 | + * Name of the extra meta that holds the last order details. |
|
89 | + */ |
|
90 | + public const META_KEY_LAST_ORDER = 'last_order_details'; |
|
91 | + |
|
92 | + /** |
|
93 | + * Name of the extra meta that stores the allowed PP checkout type selected by merchant while onboarding. |
|
94 | + */ |
|
95 | + public const META_KEY_ALLOWED_CHECKOUT_TYPE = 'allowed_checkout_type'; |
|
96 | + |
|
97 | + /** |
|
98 | + * Name of the extra meta that stores the PP checkout type selected by merchant after onboarding. |
|
99 | + */ |
|
100 | + public const META_KEY_CHECKOUT_TYPE = 'checkout_type'; |
|
101 | + |
|
102 | + /** |
|
103 | + * Name of the PayPal API parameter that holds the auth code. |
|
104 | + */ |
|
105 | + public const API_KEY_AUTH_CODE = 'authCode'; |
|
106 | + |
|
107 | + /** |
|
108 | + * Name of the PayPal API parameter that holds the client token. |
|
109 | + */ |
|
110 | + public const API_KEY_CLIENT_TOKEN = 'client_token'; |
|
111 | + |
|
112 | + /** |
|
113 | + * Name of the extra meta that holds the seller payer ID. |
|
114 | + */ |
|
115 | + public const META_KEY_PAYER_ID = 'payer_id'; |
|
116 | + |
|
117 | + /** |
|
118 | + * Name of the PayPal API parameter that holds the client token expiration time. |
|
119 | + */ |
|
120 | + public const API_KEY_EXPIRES_IN = 'expires_in'; |
|
121 | + |
|
122 | + /** |
|
123 | + * Name of the PayPal API parameter that holds the list of oAuth integrations related to the merchant. |
|
124 | + */ |
|
125 | + public const API_PARAM_OAUTH_INTEGRATIONS = 'oauth_integrations'; |
|
126 | + |
|
127 | + /** |
|
128 | + * Name of the PayPal API parameter that holds the list of third party permissions that were granted. |
|
129 | + */ |
|
130 | + public const API_PARAM_PERMISSIONS_GRANTED = 'oauth_third_party'; |
|
131 | + |
|
132 | + /** |
|
133 | + * Name of the PayPal API parameter that holds the bool of if the primary email was confirmed. |
|
134 | + */ |
|
135 | + public const API_PARAM_PRIM_EMAIL_CONFIRMED = 'primary_email_confirmed'; |
|
136 | + |
|
137 | + /** |
|
138 | + * Name of the PayPal API parameter that holds the bool of if email was confirmed. |
|
139 | + */ |
|
140 | + public const API_PARAM_EMAIL_CONFIRMED = 'isEmailConfirmed'; |
|
141 | + |
|
142 | + /** |
|
143 | + * Name of the PayPal API parameter that holds the partner ID. |
|
144 | + */ |
|
145 | + public const API_PARAM_PARTNER_ID = 'merchantId'; |
|
146 | + |
|
147 | + /** |
|
148 | + * Name of the PayPal API parameter that holds the merchant ID in the Track seller onboarding status request. |
|
149 | + */ |
|
150 | + public const API_PARAM_TRACK_MERCHANT_ID = 'merchant_id'; |
|
151 | + |
|
152 | + /** |
|
153 | + * Name of the PayPal API parameter that holds the payments_receivable status. |
|
154 | + */ |
|
155 | + public const API_PARAM_PAYMENTS_RECEIVABLE = 'payments_receivable'; |
|
156 | + |
|
157 | + /** |
|
158 | + * Name of the nonce used in the capture order request. |
|
159 | + */ |
|
160 | + public const CAPTURE_ORDER_NONCE_NAME = 'eea_pp_commerce_capture_order_payment'; |
|
161 | + |
|
162 | + /** |
|
163 | + * Name of the nonce used in the onboarding process. |
|
164 | + */ |
|
165 | + public const NONCE_NAME_ONBOARDING_RETURN = 'eea_pp_commerce_onboarding_return'; |
|
166 | + |
|
167 | + /** |
|
168 | + * Holds this payment method slug. |
|
169 | + */ |
|
170 | + public const PM_SLUG = 'paypalcheckout'; |
|
171 | + |
|
172 | + |
|
173 | + /** |
|
174 | + * Returns the base PayPal API URL. |
|
175 | + * |
|
176 | + * @param EE_Payment_Method $payment_method |
|
177 | + * @return string |
|
178 | + */ |
|
179 | + public static function getPayPalApiUrl(EE_Payment_Method $payment_method): string |
|
180 | + { |
|
181 | + return $payment_method->debug_mode() ? 'https://api-m.sandbox.paypal.com' : 'https://api-m.paypal.com'; |
|
182 | + } |
|
183 | 183 | } |
@@ -23,271 +23,271 @@ |
||
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->remaining(), |
|
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->remaining(), |
|
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 |
|
178 | - && $line_item->OBJ_type() !== 'Promotion' |
|
179 | - && $line_item->quantity() > 0 |
|
180 | - ) { |
|
181 | - $item_money = $line_item->unit_price(); |
|
182 | - $li_description = $line_item->desc() ?? esc_html__('Event Ticket', 'event_espresso'); |
|
183 | - $line_items [] = [ |
|
184 | - 'name' => substr(wp_strip_all_tags($line_item->name()), 0, 126), |
|
185 | - 'quantity' => $line_item->quantity(), |
|
186 | - 'description' => substr(wp_strip_all_tags($li_description), 0, 125), |
|
187 | - 'unit_amount' => [ |
|
188 | - 'currency_code' => $this->currency_code, |
|
189 | - 'value' => $item_money, |
|
190 | - ], |
|
191 | - 'category' => 'DIGITAL_GOODS', |
|
192 | - ]; |
|
193 | - // Line item total. |
|
194 | - $this->items_total += $line_item->pretaxTotal(); |
|
195 | - } elseif ($line_item->OBJ_type() === 'Promotion' && $line_item->quantity() > 0) { |
|
196 | - // Promotions total. |
|
197 | - $this->promos_total += $line_item->total(); |
|
198 | - } |
|
199 | - } |
|
200 | - // Make sure we have an absolute number with only two decimal laces. |
|
201 | - $this->items_total = CurrencyManager::normalizeValue($this->items_total); |
|
202 | - $this->promos_total = CurrencyManager::normalizeValue($this->promos_total); |
|
203 | - // If this is a partial payment, apply the paid amount as a promo. |
|
204 | - if ($this->transaction->paid() > 0) { |
|
205 | - $this->promos_total += CurrencyManager::normalizeValue($this->transaction->paid()); |
|
206 | - } |
|
207 | - $this->countTaxTotal(); |
|
208 | - return $line_items; |
|
209 | - } |
|
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 |
|
178 | + && $line_item->OBJ_type() !== 'Promotion' |
|
179 | + && $line_item->quantity() > 0 |
|
180 | + ) { |
|
181 | + $item_money = $line_item->unit_price(); |
|
182 | + $li_description = $line_item->desc() ?? esc_html__('Event Ticket', 'event_espresso'); |
|
183 | + $line_items [] = [ |
|
184 | + 'name' => substr(wp_strip_all_tags($line_item->name()), 0, 126), |
|
185 | + 'quantity' => $line_item->quantity(), |
|
186 | + 'description' => substr(wp_strip_all_tags($li_description), 0, 125), |
|
187 | + 'unit_amount' => [ |
|
188 | + 'currency_code' => $this->currency_code, |
|
189 | + 'value' => $item_money, |
|
190 | + ], |
|
191 | + 'category' => 'DIGITAL_GOODS', |
|
192 | + ]; |
|
193 | + // Line item total. |
|
194 | + $this->items_total += $line_item->pretaxTotal(); |
|
195 | + } elseif ($line_item->OBJ_type() === 'Promotion' && $line_item->quantity() > 0) { |
|
196 | + // Promotions total. |
|
197 | + $this->promos_total += $line_item->total(); |
|
198 | + } |
|
199 | + } |
|
200 | + // Make sure we have an absolute number with only two decimal laces. |
|
201 | + $this->items_total = CurrencyManager::normalizeValue($this->items_total); |
|
202 | + $this->promos_total = CurrencyManager::normalizeValue($this->promos_total); |
|
203 | + // If this is a partial payment, apply the paid amount as a promo. |
|
204 | + if ($this->transaction->paid() > 0) { |
|
205 | + $this->promos_total += CurrencyManager::normalizeValue($this->transaction->paid()); |
|
206 | + } |
|
207 | + $this->countTaxTotal(); |
|
208 | + return $line_items; |
|
209 | + } |
|
210 | 210 | |
211 | 211 | |
212 | - /** |
|
213 | - * Count the tax total. |
|
214 | - * |
|
215 | - * @return void |
|
216 | - * @throws EE_Error|ReflectionException |
|
217 | - */ |
|
218 | - protected function countTaxTotal(): void |
|
219 | - { |
|
220 | - // List taxes. |
|
221 | - $this->tax_total = 0; |
|
222 | - $tax_items = $this->transaction->tax_items(); |
|
223 | - foreach ($tax_items as $tax_item) { |
|
224 | - $this->tax_total += $tax_item->total(); |
|
225 | - } |
|
226 | - $this->tax_total = CurrencyManager::normalizeValue($this->tax_total); |
|
227 | - } |
|
212 | + /** |
|
213 | + * Count the tax total. |
|
214 | + * |
|
215 | + * @return void |
|
216 | + * @throws EE_Error|ReflectionException |
|
217 | + */ |
|
218 | + protected function countTaxTotal(): void |
|
219 | + { |
|
220 | + // List taxes. |
|
221 | + $this->tax_total = 0; |
|
222 | + $tax_items = $this->transaction->tax_items(); |
|
223 | + foreach ($tax_items as $tax_item) { |
|
224 | + $this->tax_total += $tax_item->total(); |
|
225 | + } |
|
226 | + $this->tax_total = CurrencyManager::normalizeValue($this->tax_total); |
|
227 | + } |
|
228 | 228 | |
229 | 229 | |
230 | - /** |
|
231 | - * Itemize the payment the breakdown list. |
|
232 | - * |
|
233 | - * @return array |
|
234 | - */ |
|
235 | - protected function getBreakdown(): array |
|
236 | - { |
|
237 | - $breakdown['item_total'] = [ |
|
238 | - 'currency_code' => $this->currency_code, |
|
239 | - 'value' => $this->items_total, |
|
240 | - ]; |
|
241 | - $breakdown['tax_total'] = [ |
|
242 | - 'currency_code' => $this->currency_code, |
|
243 | - 'value' => $this->tax_total, |
|
244 | - ]; |
|
245 | - $breakdown['discount'] = [ |
|
246 | - 'currency_code' => $this->currency_code, |
|
247 | - 'value' => abs($this->promos_total), |
|
248 | - ]; |
|
249 | - return $breakdown; |
|
250 | - } |
|
230 | + /** |
|
231 | + * Itemize the payment the breakdown list. |
|
232 | + * |
|
233 | + * @return array |
|
234 | + */ |
|
235 | + protected function getBreakdown(): array |
|
236 | + { |
|
237 | + $breakdown['item_total'] = [ |
|
238 | + 'currency_code' => $this->currency_code, |
|
239 | + 'value' => $this->items_total, |
|
240 | + ]; |
|
241 | + $breakdown['tax_total'] = [ |
|
242 | + 'currency_code' => $this->currency_code, |
|
243 | + 'value' => $this->tax_total, |
|
244 | + ]; |
|
245 | + $breakdown['discount'] = [ |
|
246 | + 'currency_code' => $this->currency_code, |
|
247 | + 'value' => abs($this->promos_total), |
|
248 | + ]; |
|
249 | + return $breakdown; |
|
250 | + } |
|
251 | 251 | |
252 | 252 | |
253 | - /** |
|
254 | - * Makes sure that we have received an Order back from the API call. |
|
255 | - * |
|
256 | - * @param $response |
|
257 | - * @param $parameters |
|
258 | - * @return array |
|
259 | - * @throws EE_Error |
|
260 | - * @throws ReflectionException |
|
261 | - */ |
|
262 | - public function validateOrder($response, $parameters): array |
|
263 | - { |
|
264 | - $message = esc_html__('Validating Order Create:', 'event_espresso'); |
|
265 | - PayPalLogger::errorLog( |
|
266 | - $message, |
|
267 | - [$this->request_url, $response], |
|
268 | - $this->transaction->payment_method(), |
|
269 | - false, |
|
270 | - $this->transaction |
|
271 | - ); |
|
272 | - if (! empty($response['error'])) { |
|
273 | - return $response; |
|
274 | - } |
|
275 | - if (! isset($response['id'])) { |
|
276 | - $message = esc_html__('Unexpected response. Unable to find the order.', 'event_espresso'); |
|
277 | - try { |
|
278 | - PayPalLogger::errorLog( |
|
279 | - $message, |
|
280 | - [$this->request_url, $parameters, $response], |
|
281 | - $this->transaction->payment_method() |
|
282 | - ); |
|
283 | - } catch (EE_Error | ReflectionException $e) { |
|
284 | - // Just continue. |
|
285 | - } |
|
286 | - return [ |
|
287 | - 'error' => $response['error'] ?? 'missing_order', |
|
288 | - 'message' => $response['message'] ?? $message, |
|
289 | - ]; |
|
290 | - } |
|
291 | - return $response; |
|
292 | - } |
|
253 | + /** |
|
254 | + * Makes sure that we have received an Order back from the API call. |
|
255 | + * |
|
256 | + * @param $response |
|
257 | + * @param $parameters |
|
258 | + * @return array |
|
259 | + * @throws EE_Error |
|
260 | + * @throws ReflectionException |
|
261 | + */ |
|
262 | + public function validateOrder($response, $parameters): array |
|
263 | + { |
|
264 | + $message = esc_html__('Validating Order Create:', 'event_espresso'); |
|
265 | + PayPalLogger::errorLog( |
|
266 | + $message, |
|
267 | + [$this->request_url, $response], |
|
268 | + $this->transaction->payment_method(), |
|
269 | + false, |
|
270 | + $this->transaction |
|
271 | + ); |
|
272 | + if (! empty($response['error'])) { |
|
273 | + return $response; |
|
274 | + } |
|
275 | + if (! isset($response['id'])) { |
|
276 | + $message = esc_html__('Unexpected response. Unable to find the order.', 'event_espresso'); |
|
277 | + try { |
|
278 | + PayPalLogger::errorLog( |
|
279 | + $message, |
|
280 | + [$this->request_url, $parameters, $response], |
|
281 | + $this->transaction->payment_method() |
|
282 | + ); |
|
283 | + } catch (EE_Error | ReflectionException $e) { |
|
284 | + // Just continue. |
|
285 | + } |
|
286 | + return [ |
|
287 | + 'error' => $response['error'] ?? 'missing_order', |
|
288 | + 'message' => $response['message'] ?? $message, |
|
289 | + ]; |
|
290 | + } |
|
291 | + return $response; |
|
292 | + } |
|
293 | 293 | } |
@@ -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 | |
@@ -238,11 +238,11 @@ discard block |
||
238 | 238 | 'currency_code' => $this->currency_code, |
239 | 239 | 'value' => $this->items_total, |
240 | 240 | ]; |
241 | - $breakdown['tax_total'] = [ |
|
241 | + $breakdown['tax_total'] = [ |
|
242 | 242 | 'currency_code' => $this->currency_code, |
243 | 243 | 'value' => $this->tax_total, |
244 | 244 | ]; |
245 | - $breakdown['discount'] = [ |
|
245 | + $breakdown['discount'] = [ |
|
246 | 246 | 'currency_code' => $this->currency_code, |
247 | 247 | 'value' => abs($this->promos_total), |
248 | 248 | ]; |
@@ -269,10 +269,10 @@ discard block |
||
269 | 269 | false, |
270 | 270 | $this->transaction |
271 | 271 | ); |
272 | - if (! empty($response['error'])) { |
|
272 | + if ( ! empty($response['error'])) { |
|
273 | 273 | return $response; |
274 | 274 | } |
275 | - if (! isset($response['id'])) { |
|
275 | + if ( ! isset($response['id'])) { |
|
276 | 276 | $message = esc_html__('Unexpected response. Unable to find the order.', 'event_espresso'); |
277 | 277 | try { |
278 | 278 | PayPalLogger::errorLog( |
@@ -20,98 +20,98 @@ |
||
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->order_id = $order_id; |
|
50 | - $this->currency_code = CurrencyManager::currencyCode(); |
|
51 | - $this->request_url = $this->request_url . $order_id . '/capture'; |
|
52 | - } |
|
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->order_id = $order_id; |
|
50 | + $this->currency_code = CurrencyManager::currencyCode(); |
|
51 | + $this->request_url = $this->request_url . $order_id . '/capture'; |
|
52 | + } |
|
53 | 53 | |
54 | 54 | |
55 | - /** |
|
56 | - * Capture payment for PayPal Order. |
|
57 | - * |
|
58 | - * @return array |
|
59 | - */ |
|
60 | - public function capture(): array |
|
61 | - { |
|
62 | - // Create Order request. |
|
63 | - $capture_response = $this->api->sendRequest([], $this->request_url); |
|
64 | - return $this->validateOrder($capture_response); |
|
65 | - } |
|
55 | + /** |
|
56 | + * Capture payment for PayPal Order. |
|
57 | + * |
|
58 | + * @return array |
|
59 | + */ |
|
60 | + public function capture(): array |
|
61 | + { |
|
62 | + // Create Order request. |
|
63 | + $capture_response = $this->api->sendRequest([], $this->request_url); |
|
64 | + return $this->validateOrder($capture_response); |
|
65 | + } |
|
66 | 66 | |
67 | 67 | |
68 | - /** |
|
69 | - * Makes sure that we have received an Order back from the API call. |
|
70 | - * |
|
71 | - * @param $response |
|
72 | - * @return array |
|
73 | - * @throws EE_Error |
|
74 | - * @throws ReflectionException |
|
75 | - */ |
|
76 | - public function validateOrder($response): array |
|
77 | - { |
|
78 | - $message = esc_html__('Validating Order Capture:', 'event_espresso'); |
|
79 | - PayPalLogger::errorLog( |
|
80 | - $message, |
|
81 | - [$this->request_url, $response], |
|
82 | - $this->transaction->payment_method(), |
|
83 | - false, |
|
84 | - $this->transaction |
|
85 | - ); |
|
86 | - // We got a direct error response. Not valid. Return that error. |
|
87 | - if (! empty($response['error'])) { |
|
88 | - return $response; |
|
89 | - } |
|
90 | - // This also could be a retry capture, so consider this valid, if order already captured. |
|
91 | - if (! empty($response['message']['details']['issue']) |
|
92 | - && $response['message']['details']['issue'] === 'ORDER_ALREADY_CAPTURED' |
|
93 | - ) { |
|
94 | - // Need to make sure we pass on the order ID. |
|
95 | - if (empty($response['id'])) { |
|
96 | - $response['id'] = $this->order_id; |
|
97 | - } |
|
98 | - $response['status'] = 'ORDER_ALREADY_CAPTURED'; |
|
99 | - return $response; |
|
100 | - } |
|
101 | - // A success capture should return the order ID. |
|
102 | - if (! isset($response['id'])) { |
|
103 | - $message = esc_html__('Unexpected response. No order returned.', 'event_espresso'); |
|
104 | - try { |
|
105 | - PayPalLogger::errorLog($message, [$this->request_url, $response], $this->transaction->payment_method()); |
|
106 | - } catch (EE_Error | ReflectionException $e) { |
|
107 | - // Just continue. |
|
108 | - } |
|
109 | - return [ |
|
110 | - 'error' => $response['error'] ?? 'missing_order', |
|
111 | - 'message' => $response['message'] ?? $message, |
|
112 | - 'name' => $response['name'] ?? 'UNKNOWN_ERROR', |
|
113 | - ]; |
|
114 | - } |
|
115 | - return $response; |
|
116 | - } |
|
68 | + /** |
|
69 | + * Makes sure that we have received an Order back from the API call. |
|
70 | + * |
|
71 | + * @param $response |
|
72 | + * @return array |
|
73 | + * @throws EE_Error |
|
74 | + * @throws ReflectionException |
|
75 | + */ |
|
76 | + public function validateOrder($response): array |
|
77 | + { |
|
78 | + $message = esc_html__('Validating Order Capture:', 'event_espresso'); |
|
79 | + PayPalLogger::errorLog( |
|
80 | + $message, |
|
81 | + [$this->request_url, $response], |
|
82 | + $this->transaction->payment_method(), |
|
83 | + false, |
|
84 | + $this->transaction |
|
85 | + ); |
|
86 | + // We got a direct error response. Not valid. Return that error. |
|
87 | + if (! empty($response['error'])) { |
|
88 | + return $response; |
|
89 | + } |
|
90 | + // This also could be a retry capture, so consider this valid, if order already captured. |
|
91 | + if (! empty($response['message']['details']['issue']) |
|
92 | + && $response['message']['details']['issue'] === 'ORDER_ALREADY_CAPTURED' |
|
93 | + ) { |
|
94 | + // Need to make sure we pass on the order ID. |
|
95 | + if (empty($response['id'])) { |
|
96 | + $response['id'] = $this->order_id; |
|
97 | + } |
|
98 | + $response['status'] = 'ORDER_ALREADY_CAPTURED'; |
|
99 | + return $response; |
|
100 | + } |
|
101 | + // A success capture should return the order ID. |
|
102 | + if (! isset($response['id'])) { |
|
103 | + $message = esc_html__('Unexpected response. No order returned.', 'event_espresso'); |
|
104 | + try { |
|
105 | + PayPalLogger::errorLog($message, [$this->request_url, $response], $this->transaction->payment_method()); |
|
106 | + } catch (EE_Error | ReflectionException $e) { |
|
107 | + // Just continue. |
|
108 | + } |
|
109 | + return [ |
|
110 | + 'error' => $response['error'] ?? 'missing_order', |
|
111 | + 'message' => $response['message'] ?? $message, |
|
112 | + 'name' => $response['name'] ?? 'UNKNOWN_ERROR', |
|
113 | + ]; |
|
114 | + } |
|
115 | + return $response; |
|
116 | + } |
|
117 | 117 | } |
@@ -48,7 +48,7 @@ discard block |
||
48 | 48 | $this->transaction = $transaction; |
49 | 49 | $this->order_id = $order_id; |
50 | 50 | $this->currency_code = CurrencyManager::currencyCode(); |
51 | - $this->request_url = $this->request_url . $order_id . '/capture'; |
|
51 | + $this->request_url = $this->request_url.$order_id.'/capture'; |
|
52 | 52 | } |
53 | 53 | |
54 | 54 | |
@@ -78,17 +78,17 @@ discard block |
||
78 | 78 | $message = esc_html__('Validating Order Capture:', 'event_espresso'); |
79 | 79 | PayPalLogger::errorLog( |
80 | 80 | $message, |
81 | - [$this->request_url, $response], |
|
81 | + [$this->request_url, $response], |
|
82 | 82 | $this->transaction->payment_method(), |
83 | 83 | false, |
84 | 84 | $this->transaction |
85 | 85 | ); |
86 | 86 | // We got a direct error response. Not valid. Return that error. |
87 | - if (! empty($response['error'])) { |
|
87 | + if ( ! empty($response['error'])) { |
|
88 | 88 | return $response; |
89 | 89 | } |
90 | 90 | // This also could be a retry capture, so consider this valid, if order already captured. |
91 | - if (! empty($response['message']['details']['issue']) |
|
91 | + if ( ! empty($response['message']['details']['issue']) |
|
92 | 92 | && $response['message']['details']['issue'] === 'ORDER_ALREADY_CAPTURED' |
93 | 93 | ) { |
94 | 94 | // Need to make sure we pass on the order ID. |
@@ -99,7 +99,7 @@ discard block |
||
99 | 99 | return $response; |
100 | 100 | } |
101 | 101 | // A success capture should return the order ID. |
102 | - if (! isset($response['id'])) { |
|
102 | + if ( ! isset($response['id'])) { |
|
103 | 103 | $message = esc_html__('Unexpected response. No order returned.', 'event_espresso'); |
104 | 104 | try { |
105 | 105 | PayPalLogger::errorLog($message, [$this->request_url, $response], $this->transaction->payment_method()); |
@@ -18,162 +18,162 @@ |
||
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 | - * @throws EE_Error |
|
41 | - * @throws ReflectionException |
|
42 | - */ |
|
43 | - public function __construct(EE_Payment_Method $pm_instance) |
|
44 | - { |
|
45 | - $this->pm = $pm_instance; |
|
46 | - $this->metadata_key = $pm_instance->debug_mode() |
|
47 | - ? Domain::META_KEY_PAYPAL_DATA_SANDBOX |
|
48 | - : Domain::META_KEY_PAYPAL_DATA; |
|
49 | - } |
|
50 | - |
|
51 | - |
|
52 | - /** |
|
53 | - * Save/update the provided data to the PM extra meta. |
|
54 | - * |
|
55 | - * @param array $data |
|
56 | - * @return bool |
|
57 | - */ |
|
58 | - public function saveBatch(array $data): bool |
|
59 | - { |
|
60 | - // Update the PM data. |
|
61 | - try { |
|
62 | - $paypal_data = $this->pm->get_extra_meta($this->metadata_key, true, []); |
|
63 | - $data_to_save = array_replace_recursive($paypal_data, $data); |
|
64 | - $this->saveMetaData($data_to_save); |
|
65 | - } catch (Exception $e) { |
|
66 | - $err_msg = sprintf( |
|
67 | - esc_html__('Could not save merchant data. %1$s', 'event_espresso'), |
|
68 | - $e->getMessage() |
|
69 | - ); |
|
70 | - PayPalLogger::errorLog($err_msg, $data, $this->pm); |
|
71 | - return false; |
|
72 | - } |
|
73 | - return true; |
|
74 | - } |
|
75 | - |
|
76 | - |
|
77 | - /** |
|
78 | - * Get PM extra meta by meta name/option. |
|
79 | - * |
|
80 | - * @param string $option_name |
|
81 | - * @return mixed |
|
82 | - */ |
|
83 | - public function getOption(string $option_name) |
|
84 | - { |
|
85 | - $meta_data = $this->getMetaData(); |
|
86 | - return $meta_data[ $option_name ] ?? false; |
|
87 | - } |
|
88 | - |
|
89 | - |
|
90 | - /** |
|
91 | - * Get PM metadata. |
|
92 | - * Return the metadata array if all good. False otherwise. |
|
93 | - * |
|
94 | - * @return array |
|
95 | - */ |
|
96 | - public function getMetaData(): array |
|
97 | - { |
|
98 | - try { |
|
99 | - return $this->pm->get_extra_meta($this->metadata_key, true, []); |
|
100 | - } catch (EE_Error | ReflectionException $e) { |
|
101 | - $err_msg = sprintf( |
|
102 | - esc_html__('Error getting the PM meta data: %1$s', 'event_espresso'), |
|
103 | - $e->getMessage() |
|
104 | - ); |
|
105 | - PayPalLogger::errorLog($err_msg, [], $this->pm); |
|
106 | - return []; |
|
107 | - } |
|
108 | - } |
|
109 | - |
|
110 | - |
|
111 | - /** |
|
112 | - * Save/update the provided option to the PM extra meta. |
|
113 | - * |
|
114 | - * @param string $name |
|
115 | - * @param $value |
|
116 | - * @return bool |
|
117 | - */ |
|
118 | - public function saveOption(string $name, $value): bool |
|
119 | - { |
|
120 | - $meta_data = $this->getMetaData(); |
|
121 | - if (! $meta_data) { |
|
122 | - $meta_data = []; |
|
123 | - } |
|
124 | - $meta_data[ $name ] = $value; |
|
125 | - return $this->saveMetaData($meta_data); |
|
126 | - } |
|
127 | - |
|
128 | - |
|
129 | - /** |
|
130 | - * Save/update the PM extra meta. |
|
131 | - * |
|
132 | - * @param array $data |
|
133 | - * @return bool |
|
134 | - */ |
|
135 | - public function saveMetaData(array $data): bool |
|
136 | - { |
|
137 | - try { |
|
138 | - $this->pm->update_extra_meta($this->metadata_key, $data); |
|
139 | - } catch (Exception $e) { |
|
140 | - return false; |
|
141 | - } |
|
142 | - return true; |
|
143 | - } |
|
144 | - |
|
145 | - |
|
146 | - /** |
|
147 | - * Delete PM option. |
|
148 | - * |
|
149 | - * @param string $name |
|
150 | - * @return bool |
|
151 | - */ |
|
152 | - public function deleteOption(string $name): bool |
|
153 | - { |
|
154 | - $meta_data = $this->getMetaData(); |
|
155 | - if (! $meta_data) { |
|
156 | - return false; |
|
157 | - } |
|
158 | - unset($meta_data[ $name ]); |
|
159 | - return $this->saveMetaData($meta_data); |
|
160 | - } |
|
161 | - |
|
162 | - |
|
163 | - /** |
|
164 | - * Delete the PM extra meta. |
|
165 | - * |
|
166 | - * @return bool |
|
167 | - */ |
|
168 | - public function deleteMetaData(): bool |
|
169 | - { |
|
170 | - try { |
|
171 | - // Live and sandbox data. |
|
172 | - $this->pm->delete_extra_meta(Domain::META_KEY_PAYPAL_DATA); |
|
173 | - $this->pm->delete_extra_meta(Domain::META_KEY_PAYPAL_DATA_SANDBOX); |
|
174 | - } catch (Exception $e) { |
|
175 | - return false; |
|
176 | - } |
|
177 | - return true; |
|
178 | - } |
|
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 | + * @throws EE_Error |
|
41 | + * @throws ReflectionException |
|
42 | + */ |
|
43 | + public function __construct(EE_Payment_Method $pm_instance) |
|
44 | + { |
|
45 | + $this->pm = $pm_instance; |
|
46 | + $this->metadata_key = $pm_instance->debug_mode() |
|
47 | + ? Domain::META_KEY_PAYPAL_DATA_SANDBOX |
|
48 | + : Domain::META_KEY_PAYPAL_DATA; |
|
49 | + } |
|
50 | + |
|
51 | + |
|
52 | + /** |
|
53 | + * Save/update the provided data to the PM extra meta. |
|
54 | + * |
|
55 | + * @param array $data |
|
56 | + * @return bool |
|
57 | + */ |
|
58 | + public function saveBatch(array $data): bool |
|
59 | + { |
|
60 | + // Update the PM data. |
|
61 | + try { |
|
62 | + $paypal_data = $this->pm->get_extra_meta($this->metadata_key, true, []); |
|
63 | + $data_to_save = array_replace_recursive($paypal_data, $data); |
|
64 | + $this->saveMetaData($data_to_save); |
|
65 | + } catch (Exception $e) { |
|
66 | + $err_msg = sprintf( |
|
67 | + esc_html__('Could not save merchant data. %1$s', 'event_espresso'), |
|
68 | + $e->getMessage() |
|
69 | + ); |
|
70 | + PayPalLogger::errorLog($err_msg, $data, $this->pm); |
|
71 | + return false; |
|
72 | + } |
|
73 | + return true; |
|
74 | + } |
|
75 | + |
|
76 | + |
|
77 | + /** |
|
78 | + * Get PM extra meta by meta name/option. |
|
79 | + * |
|
80 | + * @param string $option_name |
|
81 | + * @return mixed |
|
82 | + */ |
|
83 | + public function getOption(string $option_name) |
|
84 | + { |
|
85 | + $meta_data = $this->getMetaData(); |
|
86 | + return $meta_data[ $option_name ] ?? false; |
|
87 | + } |
|
88 | + |
|
89 | + |
|
90 | + /** |
|
91 | + * Get PM metadata. |
|
92 | + * Return the metadata array if all good. False otherwise. |
|
93 | + * |
|
94 | + * @return array |
|
95 | + */ |
|
96 | + public function getMetaData(): array |
|
97 | + { |
|
98 | + try { |
|
99 | + return $this->pm->get_extra_meta($this->metadata_key, true, []); |
|
100 | + } catch (EE_Error | ReflectionException $e) { |
|
101 | + $err_msg = sprintf( |
|
102 | + esc_html__('Error getting the PM meta data: %1$s', 'event_espresso'), |
|
103 | + $e->getMessage() |
|
104 | + ); |
|
105 | + PayPalLogger::errorLog($err_msg, [], $this->pm); |
|
106 | + return []; |
|
107 | + } |
|
108 | + } |
|
109 | + |
|
110 | + |
|
111 | + /** |
|
112 | + * Save/update the provided option to the PM extra meta. |
|
113 | + * |
|
114 | + * @param string $name |
|
115 | + * @param $value |
|
116 | + * @return bool |
|
117 | + */ |
|
118 | + public function saveOption(string $name, $value): bool |
|
119 | + { |
|
120 | + $meta_data = $this->getMetaData(); |
|
121 | + if (! $meta_data) { |
|
122 | + $meta_data = []; |
|
123 | + } |
|
124 | + $meta_data[ $name ] = $value; |
|
125 | + return $this->saveMetaData($meta_data); |
|
126 | + } |
|
127 | + |
|
128 | + |
|
129 | + /** |
|
130 | + * Save/update the PM extra meta. |
|
131 | + * |
|
132 | + * @param array $data |
|
133 | + * @return bool |
|
134 | + */ |
|
135 | + public function saveMetaData(array $data): bool |
|
136 | + { |
|
137 | + try { |
|
138 | + $this->pm->update_extra_meta($this->metadata_key, $data); |
|
139 | + } catch (Exception $e) { |
|
140 | + return false; |
|
141 | + } |
|
142 | + return true; |
|
143 | + } |
|
144 | + |
|
145 | + |
|
146 | + /** |
|
147 | + * Delete PM option. |
|
148 | + * |
|
149 | + * @param string $name |
|
150 | + * @return bool |
|
151 | + */ |
|
152 | + public function deleteOption(string $name): bool |
|
153 | + { |
|
154 | + $meta_data = $this->getMetaData(); |
|
155 | + if (! $meta_data) { |
|
156 | + return false; |
|
157 | + } |
|
158 | + unset($meta_data[ $name ]); |
|
159 | + return $this->saveMetaData($meta_data); |
|
160 | + } |
|
161 | + |
|
162 | + |
|
163 | + /** |
|
164 | + * Delete the PM extra meta. |
|
165 | + * |
|
166 | + * @return bool |
|
167 | + */ |
|
168 | + public function deleteMetaData(): bool |
|
169 | + { |
|
170 | + try { |
|
171 | + // Live and sandbox data. |
|
172 | + $this->pm->delete_extra_meta(Domain::META_KEY_PAYPAL_DATA); |
|
173 | + $this->pm->delete_extra_meta(Domain::META_KEY_PAYPAL_DATA_SANDBOX); |
|
174 | + } catch (Exception $e) { |
|
175 | + return false; |
|
176 | + } |
|
177 | + return true; |
|
178 | + } |
|
179 | 179 | } |
@@ -22,294 +22,294 @@ |
||
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) |
|
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) |
|
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' => (bool) $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' => (bool) $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_TOKEN_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_TOKEN_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_TOKEN_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_TOKEN_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 | - $api_credentials[ $api_key ] = $response[ $api_key ]; |
|
210 | - } |
|
211 | - return self::savePmOptions($paypal_pm, $api_credentials); |
|
212 | - } |
|
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 | + $api_credentials[ $api_key ] = $response[ $api_key ]; |
|
210 | + } |
|
211 | + return self::savePmOptions($paypal_pm, $api_credentials); |
|
212 | + } |
|
213 | 213 | |
214 | 214 | |
215 | - /** |
|
216 | - * Save other payment method related settings from a data array. |
|
217 | - * |
|
218 | - * @param EE_Payment_Method $paypal_pm |
|
219 | - * @param array $data |
|
220 | - * @return bool |
|
221 | - * @throws EE_Error |
|
222 | - * @throws ReflectionException |
|
223 | - */ |
|
224 | - public static function parseAndSaveOptions(EE_Payment_Method $paypal_pm, array $data): bool |
|
225 | - { |
|
226 | - $allowed_checkout_type = 'express_checkout'; |
|
227 | - // Note, although PayPal shows that this should include PPCP_CUSTOM or EXPRESS_CHECKOUT only, |
|
228 | - // in reality, it will also include other products like MOBILE_PAYMENT_ACCEPTANCE etc. |
|
229 | - if (! empty($data['response']['products'][0]['name']) && is_array($data['response']['products'])) { |
|
230 | - foreach ($data['response']['products'] as $product) { |
|
231 | - if (str_contains($product['name'], 'PPCP')) { |
|
232 | - // This merchant has PPCP in the products list, so we can enable both (all) checkout types. |
|
233 | - $allowed_checkout_type = 'all'; |
|
234 | - break; |
|
235 | - } |
|
236 | - } |
|
237 | - } |
|
238 | - // Set the Checkout type (a PM option), just in case merchant doesn't save PM options manually. |
|
239 | - $checkout_type = $paypal_pm->get_extra_meta(Domain::META_KEY_CHECKOUT_TYPE, true, false); |
|
240 | - if (! $checkout_type) { |
|
241 | - $paypal_pm->update_extra_meta(Domain::META_KEY_CHECKOUT_TYPE, $allowed_checkout_type); |
|
242 | - } |
|
243 | - return PayPalExtraMetaManager::savePmOption( |
|
244 | - $paypal_pm, |
|
245 | - Domain::META_KEY_ALLOWED_CHECKOUT_TYPE, |
|
246 | - $allowed_checkout_type |
|
247 | - ); |
|
248 | - } |
|
215 | + /** |
|
216 | + * Save other payment method related settings from a data array. |
|
217 | + * |
|
218 | + * @param EE_Payment_Method $paypal_pm |
|
219 | + * @param array $data |
|
220 | + * @return bool |
|
221 | + * @throws EE_Error |
|
222 | + * @throws ReflectionException |
|
223 | + */ |
|
224 | + public static function parseAndSaveOptions(EE_Payment_Method $paypal_pm, array $data): bool |
|
225 | + { |
|
226 | + $allowed_checkout_type = 'express_checkout'; |
|
227 | + // Note, although PayPal shows that this should include PPCP_CUSTOM or EXPRESS_CHECKOUT only, |
|
228 | + // in reality, it will also include other products like MOBILE_PAYMENT_ACCEPTANCE etc. |
|
229 | + if (! empty($data['response']['products'][0]['name']) && is_array($data['response']['products'])) { |
|
230 | + foreach ($data['response']['products'] as $product) { |
|
231 | + if (str_contains($product['name'], 'PPCP')) { |
|
232 | + // This merchant has PPCP in the products list, so we can enable both (all) checkout types. |
|
233 | + $allowed_checkout_type = 'all'; |
|
234 | + break; |
|
235 | + } |
|
236 | + } |
|
237 | + } |
|
238 | + // Set the Checkout type (a PM option), just in case merchant doesn't save PM options manually. |
|
239 | + $checkout_type = $paypal_pm->get_extra_meta(Domain::META_KEY_CHECKOUT_TYPE, true, false); |
|
240 | + if (! $checkout_type) { |
|
241 | + $paypal_pm->update_extra_meta(Domain::META_KEY_CHECKOUT_TYPE, $allowed_checkout_type); |
|
242 | + } |
|
243 | + return PayPalExtraMetaManager::savePmOption( |
|
244 | + $paypal_pm, |
|
245 | + Domain::META_KEY_ALLOWED_CHECKOUT_TYPE, |
|
246 | + $allowed_checkout_type |
|
247 | + ); |
|
248 | + } |
|
249 | 249 | |
250 | 250 | |
251 | - /** |
|
252 | - * Get PayPal extra meta helper. |
|
253 | - * |
|
254 | - * @param EE_Payment_Method $paypal_pm |
|
255 | - * @return PayPalExtraMeta |
|
256 | - * @throws EE_Error |
|
257 | - * @throws ReflectionException |
|
258 | - */ |
|
259 | - public static function extraMeta(EE_Payment_Method $paypal_pm): PayPalExtraMeta |
|
260 | - { |
|
261 | - return new PayPalExtraMeta($paypal_pm); |
|
262 | - } |
|
251 | + /** |
|
252 | + * Get PayPal extra meta helper. |
|
253 | + * |
|
254 | + * @param EE_Payment_Method $paypal_pm |
|
255 | + * @return PayPalExtraMeta |
|
256 | + * @throws EE_Error |
|
257 | + * @throws ReflectionException |
|
258 | + */ |
|
259 | + public static function extraMeta(EE_Payment_Method $paypal_pm): PayPalExtraMeta |
|
260 | + { |
|
261 | + return new PayPalExtraMeta($paypal_pm); |
|
262 | + } |
|
263 | 263 | |
264 | 264 | |
265 | - /** |
|
266 | - * Encrypt a text field. |
|
267 | - * |
|
268 | - * @param string $text |
|
269 | - * @param EE_Payment_Method $paypal_pm |
|
270 | - * @return string|null |
|
271 | - * @throws EE_Error |
|
272 | - * @throws ReflectionException |
|
273 | - */ |
|
274 | - public static function encryptString(string $text, EE_Payment_Method $paypal_pm): ?string |
|
275 | - { |
|
276 | - // We sure we are getting something ? |
|
277 | - if (! $text) { |
|
278 | - return $text; |
|
279 | - } |
|
280 | - // Do encrypt. |
|
281 | - $encryptor = LoaderFactory::getLoader()->getShared(OpenSSLEncryption::class, [new Base64Encoder()]); |
|
282 | - $sanitized_text = sanitize_text_field($text); |
|
283 | - $key_identifier = $paypal_pm->debug_mode() |
|
284 | - ? PPCommerceEncryptionKeyManager::SANDBOX_ENCRYPTION_KEY_ID |
|
285 | - : PPCommerceEncryptionKeyManager::PRODUCTION_ENCRYPTION_KEY_ID; |
|
286 | - return $encryptor->encrypt($sanitized_text, $key_identifier); |
|
287 | - } |
|
265 | + /** |
|
266 | + * Encrypt a text field. |
|
267 | + * |
|
268 | + * @param string $text |
|
269 | + * @param EE_Payment_Method $paypal_pm |
|
270 | + * @return string|null |
|
271 | + * @throws EE_Error |
|
272 | + * @throws ReflectionException |
|
273 | + */ |
|
274 | + public static function encryptString(string $text, EE_Payment_Method $paypal_pm): ?string |
|
275 | + { |
|
276 | + // We sure we are getting something ? |
|
277 | + if (! $text) { |
|
278 | + return $text; |
|
279 | + } |
|
280 | + // Do encrypt. |
|
281 | + $encryptor = LoaderFactory::getLoader()->getShared(OpenSSLEncryption::class, [new Base64Encoder()]); |
|
282 | + $sanitized_text = sanitize_text_field($text); |
|
283 | + $key_identifier = $paypal_pm->debug_mode() |
|
284 | + ? PPCommerceEncryptionKeyManager::SANDBOX_ENCRYPTION_KEY_ID |
|
285 | + : PPCommerceEncryptionKeyManager::PRODUCTION_ENCRYPTION_KEY_ID; |
|
286 | + return $encryptor->encrypt($sanitized_text, $key_identifier); |
|
287 | + } |
|
288 | 288 | |
289 | 289 | |
290 | - /** |
|
291 | - * Decrypt a string. |
|
292 | - * |
|
293 | - * @param string $text |
|
294 | - * @param EE_Payment_Method $paypal_pm |
|
295 | - * @return string |
|
296 | - */ |
|
297 | - public static function decryptString(string $text, EE_Payment_Method $paypal_pm): string |
|
298 | - { |
|
299 | - // Are we even getting something ? |
|
300 | - if (! $text) { |
|
301 | - return $text; |
|
302 | - } |
|
303 | - // Try decrypting. |
|
304 | - try { |
|
305 | - $encryptor = LoaderFactory::getLoader()->getShared(OpenSSLEncryption::class, [new Base64Encoder()]); |
|
306 | - $key_identifier = $paypal_pm->debug_mode() |
|
307 | - ? PPCommerceEncryptionKeyManager::SANDBOX_ENCRYPTION_KEY_ID |
|
308 | - : PPCommerceEncryptionKeyManager::PRODUCTION_ENCRYPTION_KEY_ID; |
|
309 | - $decrypted = $encryptor->decrypt($text, $key_identifier); |
|
310 | - } catch (Exception $e) { |
|
311 | - return $text; |
|
312 | - } |
|
313 | - return $decrypted ?? $text; |
|
314 | - } |
|
290 | + /** |
|
291 | + * Decrypt a string. |
|
292 | + * |
|
293 | + * @param string $text |
|
294 | + * @param EE_Payment_Method $paypal_pm |
|
295 | + * @return string |
|
296 | + */ |
|
297 | + public static function decryptString(string $text, EE_Payment_Method $paypal_pm): string |
|
298 | + { |
|
299 | + // Are we even getting something ? |
|
300 | + if (! $text) { |
|
301 | + return $text; |
|
302 | + } |
|
303 | + // Try decrypting. |
|
304 | + try { |
|
305 | + $encryptor = LoaderFactory::getLoader()->getShared(OpenSSLEncryption::class, [new Base64Encoder()]); |
|
306 | + $key_identifier = $paypal_pm->debug_mode() |
|
307 | + ? PPCommerceEncryptionKeyManager::SANDBOX_ENCRYPTION_KEY_ID |
|
308 | + : PPCommerceEncryptionKeyManager::PRODUCTION_ENCRYPTION_KEY_ID; |
|
309 | + $decrypted = $encryptor->decrypt($text, $key_identifier); |
|
310 | + } catch (Exception $e) { |
|
311 | + return $text; |
|
312 | + } |
|
313 | + return $decrypted ?? $text; |
|
314 | + } |
|
315 | 315 | } |
@@ -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_TOKEN_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,11 +202,11 @@ 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 | - $api_credentials[ $api_key ] = $response[ $api_key ]; |
|
209 | + $api_credentials[$api_key] = $response[$api_key]; |
|
210 | 210 | } |
211 | 211 | return self::savePmOptions($paypal_pm, $api_credentials); |
212 | 212 | } |
@@ -226,7 +226,7 @@ discard block |
||
226 | 226 | $allowed_checkout_type = 'express_checkout'; |
227 | 227 | // Note, although PayPal shows that this should include PPCP_CUSTOM or EXPRESS_CHECKOUT only, |
228 | 228 | // in reality, it will also include other products like MOBILE_PAYMENT_ACCEPTANCE etc. |
229 | - if (! empty($data['response']['products'][0]['name']) && is_array($data['response']['products'])) { |
|
229 | + if ( ! empty($data['response']['products'][0]['name']) && is_array($data['response']['products'])) { |
|
230 | 230 | foreach ($data['response']['products'] as $product) { |
231 | 231 | if (str_contains($product['name'], 'PPCP')) { |
232 | 232 | // This merchant has PPCP in the products list, so we can enable both (all) checkout types. |
@@ -237,7 +237,7 @@ discard block |
||
237 | 237 | } |
238 | 238 | // Set the Checkout type (a PM option), just in case merchant doesn't save PM options manually. |
239 | 239 | $checkout_type = $paypal_pm->get_extra_meta(Domain::META_KEY_CHECKOUT_TYPE, true, false); |
240 | - if (! $checkout_type) { |
|
240 | + if ( ! $checkout_type) { |
|
241 | 241 | $paypal_pm->update_extra_meta(Domain::META_KEY_CHECKOUT_TYPE, $allowed_checkout_type); |
242 | 242 | } |
243 | 243 | return PayPalExtraMetaManager::savePmOption( |
@@ -274,7 +274,7 @@ discard block |
||
274 | 274 | public static function encryptString(string $text, EE_Payment_Method $paypal_pm): ?string |
275 | 275 | { |
276 | 276 | // We sure we are getting something ? |
277 | - if (! $text) { |
|
277 | + if ( ! $text) { |
|
278 | 278 | return $text; |
279 | 279 | } |
280 | 280 | // Do encrypt. |
@@ -297,7 +297,7 @@ discard block |
||
297 | 297 | public static function decryptString(string $text, EE_Payment_Method $paypal_pm): string |
298 | 298 | { |
299 | 299 | // Are we even getting something ? |
300 | - if (! $text) { |
|
300 | + if ( ! $text) { |
|
301 | 301 | return $text; |
302 | 302 | } |
303 | 303 | // Try decrypting. |
@@ -20,660 +20,660 @@ |
||
20 | 20 | */ |
21 | 21 | class EED_PayPalOnboard extends EED_Module |
22 | 22 | { |
23 | - /** |
|
24 | - * @return EED_Module |
|
25 | - * @throws EE_Error |
|
26 | - * @throws ReflectionException |
|
27 | - */ |
|
28 | - public static function instance(): EED_Module |
|
29 | - { |
|
30 | - return parent::get_instance(__CLASS__); |
|
31 | - } |
|
32 | - |
|
33 | - |
|
34 | - /** |
|
35 | - * Run - initial module setup. |
|
36 | - * |
|
37 | - * @param WP $WP |
|
38 | - * @return void |
|
39 | - */ |
|
40 | - public function run($WP) |
|
41 | - { |
|
42 | - } |
|
43 | - |
|
44 | - |
|
45 | - /** |
|
46 | - * For hooking into EE Admin Core and other modules. |
|
47 | - * |
|
48 | - * @return void |
|
49 | - */ |
|
50 | - public static function set_hooks_admin(): void |
|
51 | - { |
|
52 | - if (DbStatus::isOnline()) { |
|
53 | - // Get onboarding URL. |
|
54 | - add_action('wp_ajax_eeaPpGetOnboardingUrl', [__CLASS__, 'getOnboardingUrl']); |
|
55 | - // Catch the return/redirect from PayPal onboarding page. |
|
56 | - add_action('init', [__CLASS__, 'updateOnboardingStatus'], 10); |
|
57 | - // Return the connection/onboard status. |
|
58 | - add_action('wp_ajax_eeaPpGetOnboardStatus', [__CLASS__, 'getOnboardStatus']); |
|
59 | - // Revoke access. |
|
60 | - add_action('wp_ajax_eeaPpOffboard', [__CLASS__, 'offboard']); |
|
61 | - // Admin notice. |
|
62 | - add_action('admin_init', [__CLASS__, 'adminNotice']); |
|
63 | - } |
|
64 | - } |
|
65 | - |
|
66 | - |
|
67 | - /** |
|
68 | - * Get the onboarding URL. |
|
69 | - * (AJAX) |
|
70 | - * |
|
71 | - * @return void |
|
72 | - */ |
|
73 | - public static function getOnboardingUrl(): void |
|
74 | - { |
|
75 | - $signup_link = ''; |
|
76 | - try { |
|
77 | - $paypal_pm = EED_PayPalCommerce::getPaymentMethod(); |
|
78 | - if (! $paypal_pm instanceof EE_Payment_Method) { |
|
79 | - PayPalLogger::errorLogAndExit( |
|
80 | - esc_html__('No payment method.', 'event_espresso'), |
|
81 | - EED_Module::getRequest()->postParams(), |
|
82 | - $paypal_pm |
|
83 | - ); |
|
84 | - } |
|
85 | - PayPalExtraMetaManager::updateDebugMode($paypal_pm, EED_Module::getRequest()->postParams()); |
|
86 | - // $signup_link = PayPalExtraMetaManager::getPmOption($paypal_pm, Domain::META_KEY_ONBOARDING_URL); |
|
87 | - // $token_expired = EED_PayPalOnboard::partnerAccessTokenExpired($paypal_pm); |
|
88 | - // if (! $signup_link || $token_expired) { |
|
89 | - $signup_link = EED_PayPalOnboard::requestOnboardingUrl($paypal_pm); |
|
90 | - // } |
|
91 | - if (! $signup_link) { |
|
92 | - $err_msg = esc_html__('Error! Could not generate a sign-up link.', 'event_espresso'); |
|
93 | - PayPalLogger::errorLogAndExit($err_msg, ['signup_link' => $signup_link], $paypal_pm); |
|
94 | - } |
|
95 | - PayPalExtraMetaManager::savePmOption($paypal_pm, Domain::META_KEY_ONBOARDING_URL, $signup_link); |
|
96 | - } catch (Exception $exception) { |
|
97 | - PayPalLogger::errorLogAndExit($exception->getMessage()); |
|
98 | - } |
|
99 | - // Is it empty (can happen if we didn't get the URL through the API). |
|
100 | - $signup_link = $signup_link ? $signup_link . '?&displayMode=minibrowser' : '#'; |
|
101 | - wp_send_json( |
|
102 | - [ |
|
103 | - 'signup_link' => $signup_link, |
|
104 | - ] |
|
105 | - ); |
|
106 | - } |
|
107 | - |
|
108 | - |
|
109 | - /** |
|
110 | - * Request the sign-up link from PayPal. |
|
111 | - * |
|
112 | - * @param EE_Payment_Method $paypal_pm |
|
113 | - * @param bool $one_time_request |
|
114 | - * @return string |
|
115 | - * @throws EE_Error |
|
116 | - * @throws Exception |
|
117 | - */ |
|
118 | - public static function requestOnboardingUrl(EE_Payment_Method $paypal_pm, bool $one_time_request = false): string |
|
119 | - { |
|
120 | - $signup_link = ''; |
|
121 | - // Get the access token. |
|
122 | - $access_token = EED_PayPalOnboard::getPartnerAccessToken($paypal_pm); |
|
123 | - if (! $access_token) { |
|
124 | - $err_msg = esc_html__('Error! No access token.', 'event_espresso'); |
|
125 | - PayPalLogger::errorLog($err_msg, ['access_token' => $access_token], $paypal_pm); |
|
126 | - return ''; |
|
127 | - } |
|
128 | - // Request the access token. |
|
129 | - $body_params = EED_PayPalOnboard::signupLinkRequestBody($paypal_pm); |
|
130 | - $bn_code = PayPalExtraMetaManager::getPmOption($paypal_pm, Domain::META_KEY_BN_CODE); |
|
131 | - $post_params = [ |
|
132 | - 'method' => 'POST', |
|
133 | - 'headers' => [ |
|
134 | - 'User-Agent' => sanitize_text_field($_SERVER['HTTP_USER_AGENT']), |
|
135 | - 'Content-Type' => 'application/json', |
|
136 | - 'Authorization' => 'Bearer ' . $access_token, |
|
137 | - 'PayPal-Partner-Attribution-Id' => $bn_code, |
|
138 | - ], |
|
139 | - 'body' => $body_params, |
|
140 | - ]; |
|
141 | - $request_url = Domain::getPayPalApiUrl($paypal_pm) . '/v2/customer/partner-referrals'; |
|
142 | - $response = EED_PayPalOnboard::sendRequest($paypal_pm, $request_url, $post_params); |
|
143 | - // Check the data we received. |
|
144 | - if (isset($response['error']) || empty($response['links'])) { |
|
145 | - // Did the original access token get replaced by any chance ? |
|
146 | - if (! $one_time_request |
|
147 | - && ! empty($response['message']) |
|
148 | - && $response['message'] === 'Access Token not found in cache' |
|
149 | - ) { |
|
150 | - // Clear all PM metadata and try getting the access token One more time. |
|
151 | - PayPalExtraMetaManager::deleteAllData($paypal_pm); |
|
152 | - return EED_PayPalOnboard::requestOnboardingUrl($paypal_pm, true); |
|
153 | - } |
|
154 | - $err_msg = esc_html__('Incoming sign-up link parameter validation failed.', 'event_espresso'); |
|
155 | - PayPalLogger::errorLog($err_msg, $response, $paypal_pm); |
|
156 | - return ''; |
|
157 | - } |
|
158 | - // Now retrieve that sign-up link. |
|
159 | - foreach ($response['links'] as $link) { |
|
160 | - if ($link['rel'] === 'action_url') { |
|
161 | - $signup_link = $link['href'] ?? ''; |
|
162 | - } |
|
163 | - } |
|
164 | - return $signup_link; |
|
165 | - } |
|
166 | - |
|
167 | - |
|
168 | - /** |
|
169 | - * Get the return URL. |
|
170 | - * |
|
171 | - * @param EE_Payment_Method $paypal_pm |
|
172 | - * @return string |
|
173 | - * @throws Exception |
|
174 | - */ |
|
175 | - public static function signupLinkRequestBody(EE_Payment_Method $paypal_pm): string |
|
176 | - { |
|
177 | - $identifier_string = new OneTimeString($paypal_pm->debug_mode()); |
|
178 | - $tracking_id = $identifier_string->value(); |
|
179 | - $request = LoaderFactory::getLoader()->getShared(RequestInterface::class); |
|
180 | - $checkout_type = $request->getRequestParam('checkout_type', 'EXPRESS_CHECKOUT', DataType::STRING); |
|
181 | - // Save the identifier for future use. |
|
182 | - PayPalExtraMetaManager::savePmOption($paypal_pm, Domain::META_KEY_TRACKING_ID, $tracking_id); |
|
183 | - // Assemble the return URL. |
|
184 | - $return_url = EED_PayPalOnboard::getReturnUrl($paypal_pm); |
|
185 | - return json_encode([ |
|
186 | - 'tracking_id' => $tracking_id, |
|
187 | - 'operations' => [ |
|
188 | - [ |
|
189 | - 'operation' => 'API_INTEGRATION', |
|
190 | - 'api_integration_preference' => [ |
|
191 | - 'rest_api_integration' => [ |
|
192 | - 'integration_method' => 'PAYPAL', |
|
193 | - 'integration_type' => 'THIRD_PARTY', |
|
194 | - 'third_party_details' => [ |
|
195 | - 'features' => ['PAYMENT', 'REFUND'], |
|
196 | - ], |
|
197 | - ], |
|
198 | - ], |
|
199 | - ], |
|
200 | - ], |
|
201 | - 'products' => [$checkout_type], |
|
202 | - 'legal_consents' => [ |
|
203 | - [ |
|
204 | - 'type' => 'SHARE_DATA_CONSENT', |
|
205 | - 'granted' => true, |
|
206 | - ], |
|
207 | - ], |
|
208 | - 'partner_config_override' => [ |
|
209 | - 'return_url' => $return_url, |
|
210 | - ], |
|
211 | - ]); |
|
212 | - } |
|
213 | - |
|
214 | - |
|
215 | - /** |
|
216 | - * Get the return URL. |
|
217 | - * |
|
218 | - * @param EE_Payment_Method $paypal_pm |
|
219 | - * @return string |
|
220 | - * @throws EE_Error |
|
221 | - * @throws ReflectionException |
|
222 | - */ |
|
223 | - public static function getReturnUrl(EE_Payment_Method $paypal_pm): string |
|
224 | - { |
|
225 | - $wp_nonce = EED_Module::getRequest()->getRequestParam('wp_nonce'); |
|
226 | - $nonce = wp_create_nonce(Domain::NONCE_NAME_ONBOARDING_RETURN); |
|
227 | - return add_query_arg( |
|
228 | - [ |
|
229 | - 'page' => 'espresso_payment_settings', |
|
230 | - 'webhook_action' => 'eepPpcMerchantOnboard', |
|
231 | - 'payment_method' => $paypal_pm->slug(), |
|
232 | - '_wpnonce' => $wp_nonce, |
|
233 | - 'nonce' => $nonce, |
|
234 | - Domain::META_KEY_SANDBOX_MODE => $paypal_pm->debug_mode() ? '1' : '0', |
|
235 | - ], |
|
236 | - admin_url('admin.php') |
|
237 | - ); |
|
238 | - } |
|
239 | - |
|
240 | - |
|
241 | - /** |
|
242 | - * Redirect to the payment method (PP) settings home page. |
|
243 | - * |
|
244 | - * @return void |
|
245 | - */ |
|
246 | - public static function redirectToPmSettingsHome(): void |
|
247 | - { |
|
248 | - $get_params = EED_Module::getRequest()->getParams(); |
|
249 | - if (empty($get_params['payment_method'])) { |
|
250 | - // Simply do not redirect. |
|
251 | - return; |
|
252 | - } |
|
253 | - $args_to_add = [ |
|
254 | - 'page' => 'espresso_payment_settings', |
|
255 | - 'payment_method' => $get_params['payment_method'], |
|
256 | - ]; |
|
257 | - if (isset($get_params['sandbox_mode'])) { |
|
258 | - $args_to_add[ Domain::META_KEY_SANDBOX_MODE ] = $get_params['sandbox_mode']; |
|
259 | - } |
|
260 | - $home_url = add_query_arg($args_to_add, admin_url('admin.php')); |
|
261 | - wp_redirect($home_url); |
|
262 | - exit; |
|
263 | - } |
|
264 | - |
|
265 | - |
|
266 | - /** |
|
267 | - * Check user’s onboarding status. |
|
268 | - * This will handle the user return from the auth page and also check the status via the API. |
|
269 | - * |
|
270 | - * @return void |
|
271 | - * @throws EE_Error |
|
272 | - * @throws ReflectionException |
|
273 | - */ |
|
274 | - public static function updateOnboardingStatus(): void |
|
275 | - { |
|
276 | - // Check if this is the webhook from PayPal. |
|
277 | - if (! isset($_GET['webhook_action'], $_GET['nonce']) |
|
278 | - || $_GET['webhook_action'] !== 'eepPpcMerchantOnboard' |
|
279 | - ) { |
|
280 | - return; // Ignore. |
|
281 | - } |
|
282 | - $get_params = EED_Module::getRequest()->getParams(); |
|
283 | - // Get the payment method. |
|
284 | - $paypal_pm = EED_PayPalCommerce::getPaymentMethod(); |
|
285 | - // Check the response (GET) parameters. |
|
286 | - if (! EED_PayPalOnboard::onboardingStatusResponseValid($get_params, $paypal_pm)) { |
|
287 | - // Missing parameters. Can't proceed. |
|
288 | - PayPalLogger::errorLog( |
|
289 | - esc_html__('Missing required onboarding parameters.', 'event_espresso'), |
|
290 | - $get_params, |
|
291 | - $paypal_pm |
|
292 | - ); |
|
293 | - EED_PayPalOnboard::redirectToPmSettingsHome(); |
|
294 | - return; |
|
295 | - } |
|
296 | - // Check on the onboarding status (recommended by PP). |
|
297 | - $onboarding_status = EED_PayPalOnboard::trackSellerOnboarding( |
|
298 | - $paypal_pm, |
|
299 | - $get_params[ Domain::META_KEY_SELLER_MERCHANT_ID ] |
|
300 | - ); |
|
301 | - if (! isset($onboarding_status['valid']) || ! $onboarding_status['valid']) { |
|
302 | - PayPalLogger::errorLog( |
|
303 | - $onboarding_status['message'], |
|
304 | - array_merge($get_params, $onboarding_status), |
|
305 | - $paypal_pm |
|
306 | - ); |
|
307 | - EED_PayPalOnboard::redirectToPmSettingsHome(); |
|
308 | - return; |
|
309 | - } |
|
310 | - // Start saving the setup and info. |
|
311 | - PayPalExtraMetaManager::parseAndSaveOptions($paypal_pm, $onboarding_status); |
|
312 | - // Save the credentials. |
|
313 | - PayPalExtraMetaManager::saveSellerApiCredentials($paypal_pm, $get_params); |
|
314 | - // If onboarded successfully, remove the onboarding URL. |
|
315 | - PayPalExtraMetaManager::deletePmOption($paypal_pm, Domain::META_KEY_ONBOARDING_URL); |
|
316 | - // Also clen GET params by redirecting, because PP auto redirects to the return_url on closing the onboarding window. |
|
317 | - EED_PayPalOnboard::redirectToPmSettingsHome(); |
|
318 | - } |
|
319 | - |
|
320 | - |
|
321 | - /** |
|
322 | - * Check if all required parameters for the onboarding status check are present. |
|
323 | - * |
|
324 | - * @param array $data |
|
325 | - * @param mixed $paypal_pm |
|
326 | - * @return bool |
|
327 | - */ |
|
328 | - public static function onboardingStatusResponseValid(array $data, $paypal_pm): bool |
|
329 | - { |
|
330 | - // Check that we have all the required parameters and the nonce is ok. |
|
331 | - if ($paypal_pm instanceof EE_Payment_Method |
|
332 | - && wp_verify_nonce($data['nonce'], Domain::NONCE_NAME_ONBOARDING_RETURN) |
|
333 | - && ! empty($data[ Domain::API_PARAM_PARTNER_ID ]) |
|
334 | - && ! empty($data[ Domain::META_KEY_SELLER_MERCHANT_ID ]) |
|
335 | - && isset($data[ Domain::API_PARAM_EMAIL_CONFIRMED ]) |
|
336 | - ) { |
|
337 | - return true; |
|
338 | - } |
|
339 | - return false; |
|
340 | - } |
|
341 | - |
|
342 | - |
|
343 | - /** |
|
344 | - * Get partner access token. |
|
345 | - * |
|
346 | - * @param EE_Payment_Method $paypal_pm |
|
347 | - * @return string |
|
348 | - * @throws EE_Error |
|
349 | - * @throws ReflectionException |
|
350 | - */ |
|
351 | - public static function getPartnerAccessToken(EE_Payment_Method $paypal_pm): string |
|
352 | - { |
|
353 | - // Do we have it saved ? |
|
354 | - $access_token = PayPalExtraMetaManager::getPmOption($paypal_pm, Domain::META_KEY_ACCESS_TOKEN); |
|
355 | - $expired = EED_PayPalOnboard::partnerAccessTokenExpired($paypal_pm); |
|
356 | - // If we don't have it, request/update it. |
|
357 | - if (! $access_token || $expired) { |
|
358 | - return EED_PayPalOnboard::requestPartnerAccessToken($paypal_pm); |
|
359 | - } |
|
360 | - // Access token is saved as encrypted, so return decrypted. |
|
361 | - return $access_token; |
|
362 | - } |
|
363 | - |
|
364 | - |
|
365 | - /** |
|
366 | - * Get partner access token. |
|
367 | - * |
|
368 | - * @param EE_Payment_Method $paypal_pm |
|
369 | - * @return bool |
|
370 | - */ |
|
371 | - public static function partnerAccessTokenExpired(EE_Payment_Method $paypal_pm): bool |
|
372 | - { |
|
373 | - $expires_at = (int) PayPalExtraMetaManager::getPmOption($paypal_pm, Domain::META_KEY_TOKEN_EXPIRES_IN); |
|
374 | - if (! $expires_at) { |
|
375 | - return true; |
|
376 | - } |
|
377 | - // Validate the token expiration date. |
|
378 | - $now = time(); |
|
379 | - $minutes_left = round(($expires_at - $now) / 60); |
|
380 | - // Count as expired if less than 60 minutes till expiration left. |
|
381 | - if ($minutes_left <= 60) { |
|
382 | - return true; |
|
383 | - } |
|
384 | - return false; |
|
385 | - } |
|
386 | - |
|
387 | - |
|
388 | - /** |
|
389 | - * Request the partner access token from PayPal and save/update it. |
|
390 | - * |
|
391 | - * @param EE_Payment_Method $paypal_pm |
|
392 | - * @return string |
|
393 | - * @throws EE_Error |
|
394 | - * @throws ReflectionException |
|
395 | - */ |
|
396 | - public static function requestPartnerAccessToken(EE_Payment_Method $paypal_pm): string |
|
397 | - { |
|
398 | - $nonce = wp_create_nonce('eea_pp_commerce_get_access_token'); |
|
399 | - // Request the access token. |
|
400 | - $post_args = [ |
|
401 | - 'method' => 'POST', |
|
402 | - 'body' => [ |
|
403 | - 'nonce' => $nonce, |
|
404 | - 'api_version' => 'v1', |
|
405 | - Domain::META_KEY_SANDBOX_MODE => $paypal_pm->debug_mode() ? '1' : '0', |
|
406 | - ], |
|
407 | - ]; |
|
408 | - if (defined('LOCAL_MIDDLEMAN_SERVER')) { |
|
409 | - $post_args['sslverify'] = false; |
|
410 | - } |
|
411 | - $post_url = EED_PayPalOnboard::getMiddlemanBaseUrl($paypal_pm) . 'get_token'; |
|
412 | - $response = EED_PayPalOnboard::sendRequest($paypal_pm, $post_url, $post_args); |
|
413 | - if (isset($response['error'])) { |
|
414 | - return ''; |
|
415 | - } |
|
416 | - // Check the data we received. |
|
417 | - if (! EED_PayPalOnboard::partnerTokenResponseValid($response, $paypal_pm)) { |
|
418 | - return ''; |
|
419 | - } |
|
420 | - // If we are here all seems to be ok. Save the token and it's data. |
|
421 | - $saved = PayPalExtraMetaManager::savePartnerAccessToken($paypal_pm, $response); |
|
422 | - if (! $saved) { |
|
423 | - return ''; |
|
424 | - } |
|
425 | - return $response['access_token']; |
|
426 | - } |
|
427 | - |
|
428 | - |
|
429 | - /** |
|
430 | - * Request seller onboarding status from PayPal. |
|
431 | - * |
|
432 | - * @param EE_Payment_Method $paypal_pm |
|
433 | - * @param string $merchant_id |
|
434 | - * @return array |
|
435 | - */ |
|
436 | - public static function trackSellerOnboarding(EE_Payment_Method $paypal_pm, string $merchant_id): array |
|
437 | - { |
|
438 | - $track_onboarding = EED_PayPalOnboard::getTrackOnboardingApi($paypal_pm, $merchant_id); |
|
439 | - if (! $track_onboarding instanceof TrackSellerOnboarding) { |
|
440 | - $err_msg = esc_html__('Failed to track seller onboarding.', 'event_espresso'); |
|
441 | - return ['error' => 'TRACK_ONBOARDING_FAILED', 'message' => $err_msg]; |
|
442 | - } |
|
443 | - return $track_onboarding->isValid(); |
|
444 | - } |
|
445 | - |
|
446 | - |
|
447 | - /** |
|
448 | - * Returns the Track Seller Onboarding API. |
|
449 | - * |
|
450 | - * @param EE_Payment_Method $paypal_pm |
|
451 | - * @param string $merchant_id |
|
452 | - * @return TrackSellerOnboarding|null |
|
453 | - * @throws EE_Error |
|
454 | - * @throws ReflectionException |
|
455 | - */ |
|
456 | - public static function getTrackOnboardingApi( |
|
457 | - EE_Payment_Method $paypal_pm, |
|
458 | - string $merchant_id |
|
459 | - ): ?TrackSellerOnboarding { |
|
460 | - $partner_id = PayPalExtraMetaManager::getPmOption($paypal_pm, Domain::META_KEY_PARTNER_MERCHANT_ID); |
|
461 | - $paypal_api = EED_PayPalCommerce::getPayPalApi($paypal_pm); |
|
462 | - if (! $paypal_api instanceof PayPalApi || ! $partner_id) { |
|
463 | - return null; |
|
464 | - } |
|
465 | - return new TrackSellerOnboarding($paypal_api, $partner_id, $merchant_id, $paypal_pm->debug_mode()); |
|
466 | - } |
|
467 | - |
|
468 | - |
|
469 | - /** |
|
470 | - * Check the onboard status and return the result. |
|
471 | - * (AJAX) |
|
472 | - * |
|
473 | - * @return void |
|
474 | - */ |
|
475 | - public static function getOnboardStatus(): void |
|
476 | - { |
|
477 | - $paypal_pm = EED_PayPalCommerce::getPaymentMethod(); |
|
478 | - if (! $paypal_pm instanceof EE_Payment_Method) { |
|
479 | - $err_msg = esc_html__('Could not specify the payment method.', 'event_espresso'); |
|
480 | - PayPalLogger::errorLog($err_msg, EED_Module::getRequest()->postParams(), $paypal_pm); |
|
481 | - wp_send_json(['on_board' => false]); |
|
482 | - } |
|
483 | - try { |
|
484 | - $seller_id = PayPalExtraMetaManager::getPmOption($paypal_pm, Domain::META_KEY_SELLER_MERCHANT_ID) ?? '--'; |
|
485 | - } catch (Exception $e) { |
|
486 | - $seller_id = '--'; |
|
487 | - } |
|
488 | - wp_send_json( |
|
489 | - [ |
|
490 | - 'on_board' => EED_PayPalOnboard::isOnboard($paypal_pm), |
|
491 | - 'seller_id' => $seller_id, |
|
492 | - ] |
|
493 | - ); |
|
494 | - } |
|
495 | - |
|
496 | - |
|
497 | - /** |
|
498 | - * De-authorize the seller. Remove all API credentials. |
|
499 | - * (AJAX) |
|
500 | - * |
|
501 | - * @return void |
|
502 | - */ |
|
503 | - public static function offboard(): void |
|
504 | - { |
|
505 | - $paypal_pm = EED_PayPalCommerce::getPaymentMethod(); |
|
506 | - if (! $paypal_pm instanceof EE_Payment_Method) { |
|
507 | - wp_send_json([ |
|
508 | - 'error' => 'INVALID_PM', |
|
509 | - 'message' => esc_html__( |
|
510 | - 'Invalid payment method. Please refresh the page and try again.', |
|
511 | - 'event_espresso' |
|
512 | - ), |
|
513 | - ]); |
|
514 | - } |
|
515 | - PayPalExtraMetaManager::deleteAllData($paypal_pm); |
|
516 | - wp_send_json(['success' => true]); |
|
517 | - } |
|
518 | - |
|
519 | - |
|
520 | - /** |
|
521 | - * Checks if already onboard. |
|
522 | - * |
|
523 | - * @param EE_Payment_Method $payment_method |
|
524 | - * @return boolean |
|
525 | - */ |
|
526 | - public static function isOnboard(EE_Payment_Method $payment_method): bool |
|
527 | - { |
|
528 | - $pp_meta_data = PayPalExtraMetaManager::getAllData($payment_method); |
|
529 | - return |
|
530 | - // onborded with a third party integration ? |
|
531 | - (! empty($pp_meta_data[ Domain::META_KEY_SELLER_MERCHANT_ID ]) |
|
532 | - && ! empty($pp_meta_data[ Domain::META_KEY_ACCESS_TOKEN ]) |
|
533 | - ) |
|
534 | - // or with the first party integration ? |
|
535 | - || (! empty($pp_meta_data[ Domain::META_KEY_CLIENT_ID ]) |
|
536 | - && ! empty($pp_meta_data[ Domain::META_KEY_CLIENT_SECRET ]) |
|
537 | - && ! empty($pp_meta_data[ Domain::META_KEY_PAYER_ID ]) |
|
538 | - ); |
|
539 | - } |
|
540 | - |
|
541 | - |
|
542 | - /** |
|
543 | - * Send a request and return a decoded response body. |
|
544 | - * |
|
545 | - * @param EE_Payment_Method $paypal_pm |
|
546 | - * @param string $request_url |
|
547 | - * @param array $request_args |
|
548 | - * @return array |
|
549 | - */ |
|
550 | - public static function sendRequest(EE_Payment_Method $paypal_pm, string $request_url, array $request_args): array |
|
551 | - { |
|
552 | - $error_return = ['error' => true]; |
|
553 | - $response = wp_remote_request($request_url, $request_args); |
|
554 | - if (is_wp_error($response)) { |
|
555 | - $message = $response->get_error_message(); |
|
556 | - PayPalLogger::errorLog($message, [$request_url, $request_args, $response], $paypal_pm); |
|
557 | - $error_return['message'] = $message; |
|
558 | - return $error_return; |
|
559 | - } |
|
560 | - $response_body = (isset($response['body']) && $response['body']) ? json_decode($response['body'], true) : []; |
|
561 | - if (empty($response_body) || isset($response_body['error'])) { |
|
562 | - $message = $response_body['error_description'] |
|
563 | - ?? sprintf( |
|
564 | - esc_html__('Unknown response received while sending a request to: %1$s', 'event_espresso'), |
|
565 | - $request_url |
|
566 | - ); |
|
567 | - PayPalLogger::errorLog($message, [$request_url, $request_args, $response], $paypal_pm); |
|
568 | - $error_return['message'] = $message; |
|
569 | - return $error_return; |
|
570 | - } |
|
571 | - return $response_body; |
|
572 | - } |
|
573 | - |
|
574 | - |
|
575 | - /** |
|
576 | - * Check the response for a partner token request. |
|
577 | - * |
|
578 | - * @param $response |
|
579 | - * @param EE_Payment_Method $paypal_pm |
|
580 | - * @return bool |
|
581 | - */ |
|
582 | - public static function partnerTokenResponseValid($response, EE_Payment_Method $paypal_pm): bool |
|
583 | - { |
|
584 | - // Check the data we received. |
|
585 | - if ( |
|
586 | - empty($response['nonce']) |
|
587 | - || ! wp_verify_nonce($response['nonce'], 'eea_pp_commerce_get_access_token') |
|
588 | - || empty($response['access_token']) |
|
589 | - || empty($response['app_id']) |
|
590 | - || empty($response['expires_in']) |
|
591 | - || empty($response['partner_client_id']) |
|
592 | - || empty($response['partner_merchant_id']) |
|
593 | - ) { |
|
594 | - // This is an error. |
|
595 | - $err_msg = esc_html__('Incoming parameter validation failed.', 'event_espresso'); |
|
596 | - PayPalLogger::errorLog($err_msg, (array) $response, $paypal_pm); |
|
597 | - return false; |
|
598 | - } |
|
599 | - return true; |
|
600 | - } |
|
601 | - |
|
602 | - |
|
603 | - /** |
|
604 | - * Returns the base URL to the middleman server. |
|
605 | - * If LOCAL_MIDDLEMAN_SERVER is defined, requests will be sent to connect.eventespresso.test |
|
606 | - * |
|
607 | - * @param EE_Payment_Method $payment_method |
|
608 | - * @return string |
|
609 | - * @throws EE_Error |
|
610 | - * @throws ReflectionException |
|
611 | - */ |
|
612 | - public static function getMiddlemanBaseUrl(EE_Payment_Method $payment_method): string |
|
613 | - { |
|
614 | - $target = defined('LOCAL_MIDDLEMAN_SERVER') ? 'test' : 'com'; |
|
615 | - // If this PM is used under different provider accounts, you might need an account indicator. |
|
616 | - $account = defined('EE_PAYPAL_COMMERCE_ACCOUNT_INDICATOR') ? EE_PAYPAL_COMMERCE_ACCOUNT_INDICATOR : ''; |
|
617 | - $postfix = $payment_method->debug_mode() ? '_sandbox' : ''; |
|
618 | - $path = 'paypal_commerce' . $account . $postfix; |
|
619 | - return 'https://connect.eventespresso.' . $target . '/' . $path . '/'; |
|
620 | - } |
|
621 | - |
|
622 | - |
|
623 | - /** |
|
624 | - * This Payment Method admin notices. |
|
625 | - * |
|
626 | - * @return void |
|
627 | - * @throws EE_Error |
|
628 | - * @throws ReflectionException |
|
629 | - */ |
|
630 | - public static function adminNotice() |
|
631 | - { |
|
632 | - // Show the notice if PayPal Commerce PM is active but merchant is not onboard. |
|
633 | - $pp_commerce = EEM_Payment_Method::instance()->get_one_by_slug('paypalcheckout'); |
|
634 | - if ($pp_commerce instanceof EE_Payment_Method |
|
635 | - && $pp_commerce->active() |
|
636 | - && ! EED_PayPalOnboard::isOnboard($pp_commerce) |
|
637 | - ) { |
|
638 | - add_action('admin_notices', [__CLASS__, 'notOnboardNotice']); |
|
639 | - } |
|
640 | - } |
|
641 | - |
|
642 | - |
|
643 | - /** |
|
644 | - * Contents of the not onboard admin notice. |
|
645 | - * |
|
646 | - * @return void |
|
647 | - * @throws EE_Error |
|
648 | - * @throws ReflectionException |
|
649 | - */ |
|
650 | - public static function notOnboardNotice() |
|
651 | - { |
|
652 | - $open_anchor = $close_anchor = ''; |
|
653 | - $pp_commerce = EEM_Payment_Method::instance()->get_one_by_slug('paypalcheckout'); |
|
654 | - if ($pp_commerce instanceof EE_Payment_Method) { |
|
655 | - $pm_page = add_query_arg( |
|
656 | - [ |
|
657 | - 'page' => 'espresso_payment_settings', |
|
658 | - 'webhook_action' => 'eepPpcMerchantOnboard', |
|
659 | - 'payment_method' => $pp_commerce->slug(), |
|
660 | - ], |
|
661 | - admin_url('admin.php') |
|
662 | - ); |
|
663 | - $open_anchor = "<a href='$pm_page'>"; |
|
664 | - $close_anchor = "</a>"; |
|
665 | - } |
|
666 | - echo '<div class="error"><p>' |
|
667 | - . sprintf( |
|
668 | - esc_html__( |
|
669 | - '%1$sPayPal Commerce%2$s payment method was activated but is not connected to PayPal. Please %3$sfinish setting up%4$s this payment method.', |
|
670 | - 'event_espresso' |
|
671 | - ), |
|
672 | - '<strong>', |
|
673 | - '</strong>', |
|
674 | - $open_anchor, |
|
675 | - $close_anchor |
|
676 | - ) |
|
677 | - . '</p></div>'; |
|
678 | - } |
|
23 | + /** |
|
24 | + * @return EED_Module |
|
25 | + * @throws EE_Error |
|
26 | + * @throws ReflectionException |
|
27 | + */ |
|
28 | + public static function instance(): EED_Module |
|
29 | + { |
|
30 | + return parent::get_instance(__CLASS__); |
|
31 | + } |
|
32 | + |
|
33 | + |
|
34 | + /** |
|
35 | + * Run - initial module setup. |
|
36 | + * |
|
37 | + * @param WP $WP |
|
38 | + * @return void |
|
39 | + */ |
|
40 | + public function run($WP) |
|
41 | + { |
|
42 | + } |
|
43 | + |
|
44 | + |
|
45 | + /** |
|
46 | + * For hooking into EE Admin Core and other modules. |
|
47 | + * |
|
48 | + * @return void |
|
49 | + */ |
|
50 | + public static function set_hooks_admin(): void |
|
51 | + { |
|
52 | + if (DbStatus::isOnline()) { |
|
53 | + // Get onboarding URL. |
|
54 | + add_action('wp_ajax_eeaPpGetOnboardingUrl', [__CLASS__, 'getOnboardingUrl']); |
|
55 | + // Catch the return/redirect from PayPal onboarding page. |
|
56 | + add_action('init', [__CLASS__, 'updateOnboardingStatus'], 10); |
|
57 | + // Return the connection/onboard status. |
|
58 | + add_action('wp_ajax_eeaPpGetOnboardStatus', [__CLASS__, 'getOnboardStatus']); |
|
59 | + // Revoke access. |
|
60 | + add_action('wp_ajax_eeaPpOffboard', [__CLASS__, 'offboard']); |
|
61 | + // Admin notice. |
|
62 | + add_action('admin_init', [__CLASS__, 'adminNotice']); |
|
63 | + } |
|
64 | + } |
|
65 | + |
|
66 | + |
|
67 | + /** |
|
68 | + * Get the onboarding URL. |
|
69 | + * (AJAX) |
|
70 | + * |
|
71 | + * @return void |
|
72 | + */ |
|
73 | + public static function getOnboardingUrl(): void |
|
74 | + { |
|
75 | + $signup_link = ''; |
|
76 | + try { |
|
77 | + $paypal_pm = EED_PayPalCommerce::getPaymentMethod(); |
|
78 | + if (! $paypal_pm instanceof EE_Payment_Method) { |
|
79 | + PayPalLogger::errorLogAndExit( |
|
80 | + esc_html__('No payment method.', 'event_espresso'), |
|
81 | + EED_Module::getRequest()->postParams(), |
|
82 | + $paypal_pm |
|
83 | + ); |
|
84 | + } |
|
85 | + PayPalExtraMetaManager::updateDebugMode($paypal_pm, EED_Module::getRequest()->postParams()); |
|
86 | + // $signup_link = PayPalExtraMetaManager::getPmOption($paypal_pm, Domain::META_KEY_ONBOARDING_URL); |
|
87 | + // $token_expired = EED_PayPalOnboard::partnerAccessTokenExpired($paypal_pm); |
|
88 | + // if (! $signup_link || $token_expired) { |
|
89 | + $signup_link = EED_PayPalOnboard::requestOnboardingUrl($paypal_pm); |
|
90 | + // } |
|
91 | + if (! $signup_link) { |
|
92 | + $err_msg = esc_html__('Error! Could not generate a sign-up link.', 'event_espresso'); |
|
93 | + PayPalLogger::errorLogAndExit($err_msg, ['signup_link' => $signup_link], $paypal_pm); |
|
94 | + } |
|
95 | + PayPalExtraMetaManager::savePmOption($paypal_pm, Domain::META_KEY_ONBOARDING_URL, $signup_link); |
|
96 | + } catch (Exception $exception) { |
|
97 | + PayPalLogger::errorLogAndExit($exception->getMessage()); |
|
98 | + } |
|
99 | + // Is it empty (can happen if we didn't get the URL through the API). |
|
100 | + $signup_link = $signup_link ? $signup_link . '?&displayMode=minibrowser' : '#'; |
|
101 | + wp_send_json( |
|
102 | + [ |
|
103 | + 'signup_link' => $signup_link, |
|
104 | + ] |
|
105 | + ); |
|
106 | + } |
|
107 | + |
|
108 | + |
|
109 | + /** |
|
110 | + * Request the sign-up link from PayPal. |
|
111 | + * |
|
112 | + * @param EE_Payment_Method $paypal_pm |
|
113 | + * @param bool $one_time_request |
|
114 | + * @return string |
|
115 | + * @throws EE_Error |
|
116 | + * @throws Exception |
|
117 | + */ |
|
118 | + public static function requestOnboardingUrl(EE_Payment_Method $paypal_pm, bool $one_time_request = false): string |
|
119 | + { |
|
120 | + $signup_link = ''; |
|
121 | + // Get the access token. |
|
122 | + $access_token = EED_PayPalOnboard::getPartnerAccessToken($paypal_pm); |
|
123 | + if (! $access_token) { |
|
124 | + $err_msg = esc_html__('Error! No access token.', 'event_espresso'); |
|
125 | + PayPalLogger::errorLog($err_msg, ['access_token' => $access_token], $paypal_pm); |
|
126 | + return ''; |
|
127 | + } |
|
128 | + // Request the access token. |
|
129 | + $body_params = EED_PayPalOnboard::signupLinkRequestBody($paypal_pm); |
|
130 | + $bn_code = PayPalExtraMetaManager::getPmOption($paypal_pm, Domain::META_KEY_BN_CODE); |
|
131 | + $post_params = [ |
|
132 | + 'method' => 'POST', |
|
133 | + 'headers' => [ |
|
134 | + 'User-Agent' => sanitize_text_field($_SERVER['HTTP_USER_AGENT']), |
|
135 | + 'Content-Type' => 'application/json', |
|
136 | + 'Authorization' => 'Bearer ' . $access_token, |
|
137 | + 'PayPal-Partner-Attribution-Id' => $bn_code, |
|
138 | + ], |
|
139 | + 'body' => $body_params, |
|
140 | + ]; |
|
141 | + $request_url = Domain::getPayPalApiUrl($paypal_pm) . '/v2/customer/partner-referrals'; |
|
142 | + $response = EED_PayPalOnboard::sendRequest($paypal_pm, $request_url, $post_params); |
|
143 | + // Check the data we received. |
|
144 | + if (isset($response['error']) || empty($response['links'])) { |
|
145 | + // Did the original access token get replaced by any chance ? |
|
146 | + if (! $one_time_request |
|
147 | + && ! empty($response['message']) |
|
148 | + && $response['message'] === 'Access Token not found in cache' |
|
149 | + ) { |
|
150 | + // Clear all PM metadata and try getting the access token One more time. |
|
151 | + PayPalExtraMetaManager::deleteAllData($paypal_pm); |
|
152 | + return EED_PayPalOnboard::requestOnboardingUrl($paypal_pm, true); |
|
153 | + } |
|
154 | + $err_msg = esc_html__('Incoming sign-up link parameter validation failed.', 'event_espresso'); |
|
155 | + PayPalLogger::errorLog($err_msg, $response, $paypal_pm); |
|
156 | + return ''; |
|
157 | + } |
|
158 | + // Now retrieve that sign-up link. |
|
159 | + foreach ($response['links'] as $link) { |
|
160 | + if ($link['rel'] === 'action_url') { |
|
161 | + $signup_link = $link['href'] ?? ''; |
|
162 | + } |
|
163 | + } |
|
164 | + return $signup_link; |
|
165 | + } |
|
166 | + |
|
167 | + |
|
168 | + /** |
|
169 | + * Get the return URL. |
|
170 | + * |
|
171 | + * @param EE_Payment_Method $paypal_pm |
|
172 | + * @return string |
|
173 | + * @throws Exception |
|
174 | + */ |
|
175 | + public static function signupLinkRequestBody(EE_Payment_Method $paypal_pm): string |
|
176 | + { |
|
177 | + $identifier_string = new OneTimeString($paypal_pm->debug_mode()); |
|
178 | + $tracking_id = $identifier_string->value(); |
|
179 | + $request = LoaderFactory::getLoader()->getShared(RequestInterface::class); |
|
180 | + $checkout_type = $request->getRequestParam('checkout_type', 'EXPRESS_CHECKOUT', DataType::STRING); |
|
181 | + // Save the identifier for future use. |
|
182 | + PayPalExtraMetaManager::savePmOption($paypal_pm, Domain::META_KEY_TRACKING_ID, $tracking_id); |
|
183 | + // Assemble the return URL. |
|
184 | + $return_url = EED_PayPalOnboard::getReturnUrl($paypal_pm); |
|
185 | + return json_encode([ |
|
186 | + 'tracking_id' => $tracking_id, |
|
187 | + 'operations' => [ |
|
188 | + [ |
|
189 | + 'operation' => 'API_INTEGRATION', |
|
190 | + 'api_integration_preference' => [ |
|
191 | + 'rest_api_integration' => [ |
|
192 | + 'integration_method' => 'PAYPAL', |
|
193 | + 'integration_type' => 'THIRD_PARTY', |
|
194 | + 'third_party_details' => [ |
|
195 | + 'features' => ['PAYMENT', 'REFUND'], |
|
196 | + ], |
|
197 | + ], |
|
198 | + ], |
|
199 | + ], |
|
200 | + ], |
|
201 | + 'products' => [$checkout_type], |
|
202 | + 'legal_consents' => [ |
|
203 | + [ |
|
204 | + 'type' => 'SHARE_DATA_CONSENT', |
|
205 | + 'granted' => true, |
|
206 | + ], |
|
207 | + ], |
|
208 | + 'partner_config_override' => [ |
|
209 | + 'return_url' => $return_url, |
|
210 | + ], |
|
211 | + ]); |
|
212 | + } |
|
213 | + |
|
214 | + |
|
215 | + /** |
|
216 | + * Get the return URL. |
|
217 | + * |
|
218 | + * @param EE_Payment_Method $paypal_pm |
|
219 | + * @return string |
|
220 | + * @throws EE_Error |
|
221 | + * @throws ReflectionException |
|
222 | + */ |
|
223 | + public static function getReturnUrl(EE_Payment_Method $paypal_pm): string |
|
224 | + { |
|
225 | + $wp_nonce = EED_Module::getRequest()->getRequestParam('wp_nonce'); |
|
226 | + $nonce = wp_create_nonce(Domain::NONCE_NAME_ONBOARDING_RETURN); |
|
227 | + return add_query_arg( |
|
228 | + [ |
|
229 | + 'page' => 'espresso_payment_settings', |
|
230 | + 'webhook_action' => 'eepPpcMerchantOnboard', |
|
231 | + 'payment_method' => $paypal_pm->slug(), |
|
232 | + '_wpnonce' => $wp_nonce, |
|
233 | + 'nonce' => $nonce, |
|
234 | + Domain::META_KEY_SANDBOX_MODE => $paypal_pm->debug_mode() ? '1' : '0', |
|
235 | + ], |
|
236 | + admin_url('admin.php') |
|
237 | + ); |
|
238 | + } |
|
239 | + |
|
240 | + |
|
241 | + /** |
|
242 | + * Redirect to the payment method (PP) settings home page. |
|
243 | + * |
|
244 | + * @return void |
|
245 | + */ |
|
246 | + public static function redirectToPmSettingsHome(): void |
|
247 | + { |
|
248 | + $get_params = EED_Module::getRequest()->getParams(); |
|
249 | + if (empty($get_params['payment_method'])) { |
|
250 | + // Simply do not redirect. |
|
251 | + return; |
|
252 | + } |
|
253 | + $args_to_add = [ |
|
254 | + 'page' => 'espresso_payment_settings', |
|
255 | + 'payment_method' => $get_params['payment_method'], |
|
256 | + ]; |
|
257 | + if (isset($get_params['sandbox_mode'])) { |
|
258 | + $args_to_add[ Domain::META_KEY_SANDBOX_MODE ] = $get_params['sandbox_mode']; |
|
259 | + } |
|
260 | + $home_url = add_query_arg($args_to_add, admin_url('admin.php')); |
|
261 | + wp_redirect($home_url); |
|
262 | + exit; |
|
263 | + } |
|
264 | + |
|
265 | + |
|
266 | + /** |
|
267 | + * Check user’s onboarding status. |
|
268 | + * This will handle the user return from the auth page and also check the status via the API. |
|
269 | + * |
|
270 | + * @return void |
|
271 | + * @throws EE_Error |
|
272 | + * @throws ReflectionException |
|
273 | + */ |
|
274 | + public static function updateOnboardingStatus(): void |
|
275 | + { |
|
276 | + // Check if this is the webhook from PayPal. |
|
277 | + if (! isset($_GET['webhook_action'], $_GET['nonce']) |
|
278 | + || $_GET['webhook_action'] !== 'eepPpcMerchantOnboard' |
|
279 | + ) { |
|
280 | + return; // Ignore. |
|
281 | + } |
|
282 | + $get_params = EED_Module::getRequest()->getParams(); |
|
283 | + // Get the payment method. |
|
284 | + $paypal_pm = EED_PayPalCommerce::getPaymentMethod(); |
|
285 | + // Check the response (GET) parameters. |
|
286 | + if (! EED_PayPalOnboard::onboardingStatusResponseValid($get_params, $paypal_pm)) { |
|
287 | + // Missing parameters. Can't proceed. |
|
288 | + PayPalLogger::errorLog( |
|
289 | + esc_html__('Missing required onboarding parameters.', 'event_espresso'), |
|
290 | + $get_params, |
|
291 | + $paypal_pm |
|
292 | + ); |
|
293 | + EED_PayPalOnboard::redirectToPmSettingsHome(); |
|
294 | + return; |
|
295 | + } |
|
296 | + // Check on the onboarding status (recommended by PP). |
|
297 | + $onboarding_status = EED_PayPalOnboard::trackSellerOnboarding( |
|
298 | + $paypal_pm, |
|
299 | + $get_params[ Domain::META_KEY_SELLER_MERCHANT_ID ] |
|
300 | + ); |
|
301 | + if (! isset($onboarding_status['valid']) || ! $onboarding_status['valid']) { |
|
302 | + PayPalLogger::errorLog( |
|
303 | + $onboarding_status['message'], |
|
304 | + array_merge($get_params, $onboarding_status), |
|
305 | + $paypal_pm |
|
306 | + ); |
|
307 | + EED_PayPalOnboard::redirectToPmSettingsHome(); |
|
308 | + return; |
|
309 | + } |
|
310 | + // Start saving the setup and info. |
|
311 | + PayPalExtraMetaManager::parseAndSaveOptions($paypal_pm, $onboarding_status); |
|
312 | + // Save the credentials. |
|
313 | + PayPalExtraMetaManager::saveSellerApiCredentials($paypal_pm, $get_params); |
|
314 | + // If onboarded successfully, remove the onboarding URL. |
|
315 | + PayPalExtraMetaManager::deletePmOption($paypal_pm, Domain::META_KEY_ONBOARDING_URL); |
|
316 | + // Also clen GET params by redirecting, because PP auto redirects to the return_url on closing the onboarding window. |
|
317 | + EED_PayPalOnboard::redirectToPmSettingsHome(); |
|
318 | + } |
|
319 | + |
|
320 | + |
|
321 | + /** |
|
322 | + * Check if all required parameters for the onboarding status check are present. |
|
323 | + * |
|
324 | + * @param array $data |
|
325 | + * @param mixed $paypal_pm |
|
326 | + * @return bool |
|
327 | + */ |
|
328 | + public static function onboardingStatusResponseValid(array $data, $paypal_pm): bool |
|
329 | + { |
|
330 | + // Check that we have all the required parameters and the nonce is ok. |
|
331 | + if ($paypal_pm instanceof EE_Payment_Method |
|
332 | + && wp_verify_nonce($data['nonce'], Domain::NONCE_NAME_ONBOARDING_RETURN) |
|
333 | + && ! empty($data[ Domain::API_PARAM_PARTNER_ID ]) |
|
334 | + && ! empty($data[ Domain::META_KEY_SELLER_MERCHANT_ID ]) |
|
335 | + && isset($data[ Domain::API_PARAM_EMAIL_CONFIRMED ]) |
|
336 | + ) { |
|
337 | + return true; |
|
338 | + } |
|
339 | + return false; |
|
340 | + } |
|
341 | + |
|
342 | + |
|
343 | + /** |
|
344 | + * Get partner access token. |
|
345 | + * |
|
346 | + * @param EE_Payment_Method $paypal_pm |
|
347 | + * @return string |
|
348 | + * @throws EE_Error |
|
349 | + * @throws ReflectionException |
|
350 | + */ |
|
351 | + public static function getPartnerAccessToken(EE_Payment_Method $paypal_pm): string |
|
352 | + { |
|
353 | + // Do we have it saved ? |
|
354 | + $access_token = PayPalExtraMetaManager::getPmOption($paypal_pm, Domain::META_KEY_ACCESS_TOKEN); |
|
355 | + $expired = EED_PayPalOnboard::partnerAccessTokenExpired($paypal_pm); |
|
356 | + // If we don't have it, request/update it. |
|
357 | + if (! $access_token || $expired) { |
|
358 | + return EED_PayPalOnboard::requestPartnerAccessToken($paypal_pm); |
|
359 | + } |
|
360 | + // Access token is saved as encrypted, so return decrypted. |
|
361 | + return $access_token; |
|
362 | + } |
|
363 | + |
|
364 | + |
|
365 | + /** |
|
366 | + * Get partner access token. |
|
367 | + * |
|
368 | + * @param EE_Payment_Method $paypal_pm |
|
369 | + * @return bool |
|
370 | + */ |
|
371 | + public static function partnerAccessTokenExpired(EE_Payment_Method $paypal_pm): bool |
|
372 | + { |
|
373 | + $expires_at = (int) PayPalExtraMetaManager::getPmOption($paypal_pm, Domain::META_KEY_TOKEN_EXPIRES_IN); |
|
374 | + if (! $expires_at) { |
|
375 | + return true; |
|
376 | + } |
|
377 | + // Validate the token expiration date. |
|
378 | + $now = time(); |
|
379 | + $minutes_left = round(($expires_at - $now) / 60); |
|
380 | + // Count as expired if less than 60 minutes till expiration left. |
|
381 | + if ($minutes_left <= 60) { |
|
382 | + return true; |
|
383 | + } |
|
384 | + return false; |
|
385 | + } |
|
386 | + |
|
387 | + |
|
388 | + /** |
|
389 | + * Request the partner access token from PayPal and save/update it. |
|
390 | + * |
|
391 | + * @param EE_Payment_Method $paypal_pm |
|
392 | + * @return string |
|
393 | + * @throws EE_Error |
|
394 | + * @throws ReflectionException |
|
395 | + */ |
|
396 | + public static function requestPartnerAccessToken(EE_Payment_Method $paypal_pm): string |
|
397 | + { |
|
398 | + $nonce = wp_create_nonce('eea_pp_commerce_get_access_token'); |
|
399 | + // Request the access token. |
|
400 | + $post_args = [ |
|
401 | + 'method' => 'POST', |
|
402 | + 'body' => [ |
|
403 | + 'nonce' => $nonce, |
|
404 | + 'api_version' => 'v1', |
|
405 | + Domain::META_KEY_SANDBOX_MODE => $paypal_pm->debug_mode() ? '1' : '0', |
|
406 | + ], |
|
407 | + ]; |
|
408 | + if (defined('LOCAL_MIDDLEMAN_SERVER')) { |
|
409 | + $post_args['sslverify'] = false; |
|
410 | + } |
|
411 | + $post_url = EED_PayPalOnboard::getMiddlemanBaseUrl($paypal_pm) . 'get_token'; |
|
412 | + $response = EED_PayPalOnboard::sendRequest($paypal_pm, $post_url, $post_args); |
|
413 | + if (isset($response['error'])) { |
|
414 | + return ''; |
|
415 | + } |
|
416 | + // Check the data we received. |
|
417 | + if (! EED_PayPalOnboard::partnerTokenResponseValid($response, $paypal_pm)) { |
|
418 | + return ''; |
|
419 | + } |
|
420 | + // If we are here all seems to be ok. Save the token and it's data. |
|
421 | + $saved = PayPalExtraMetaManager::savePartnerAccessToken($paypal_pm, $response); |
|
422 | + if (! $saved) { |
|
423 | + return ''; |
|
424 | + } |
|
425 | + return $response['access_token']; |
|
426 | + } |
|
427 | + |
|
428 | + |
|
429 | + /** |
|
430 | + * Request seller onboarding status from PayPal. |
|
431 | + * |
|
432 | + * @param EE_Payment_Method $paypal_pm |
|
433 | + * @param string $merchant_id |
|
434 | + * @return array |
|
435 | + */ |
|
436 | + public static function trackSellerOnboarding(EE_Payment_Method $paypal_pm, string $merchant_id): array |
|
437 | + { |
|
438 | + $track_onboarding = EED_PayPalOnboard::getTrackOnboardingApi($paypal_pm, $merchant_id); |
|
439 | + if (! $track_onboarding instanceof TrackSellerOnboarding) { |
|
440 | + $err_msg = esc_html__('Failed to track seller onboarding.', 'event_espresso'); |
|
441 | + return ['error' => 'TRACK_ONBOARDING_FAILED', 'message' => $err_msg]; |
|
442 | + } |
|
443 | + return $track_onboarding->isValid(); |
|
444 | + } |
|
445 | + |
|
446 | + |
|
447 | + /** |
|
448 | + * Returns the Track Seller Onboarding API. |
|
449 | + * |
|
450 | + * @param EE_Payment_Method $paypal_pm |
|
451 | + * @param string $merchant_id |
|
452 | + * @return TrackSellerOnboarding|null |
|
453 | + * @throws EE_Error |
|
454 | + * @throws ReflectionException |
|
455 | + */ |
|
456 | + public static function getTrackOnboardingApi( |
|
457 | + EE_Payment_Method $paypal_pm, |
|
458 | + string $merchant_id |
|
459 | + ): ?TrackSellerOnboarding { |
|
460 | + $partner_id = PayPalExtraMetaManager::getPmOption($paypal_pm, Domain::META_KEY_PARTNER_MERCHANT_ID); |
|
461 | + $paypal_api = EED_PayPalCommerce::getPayPalApi($paypal_pm); |
|
462 | + if (! $paypal_api instanceof PayPalApi || ! $partner_id) { |
|
463 | + return null; |
|
464 | + } |
|
465 | + return new TrackSellerOnboarding($paypal_api, $partner_id, $merchant_id, $paypal_pm->debug_mode()); |
|
466 | + } |
|
467 | + |
|
468 | + |
|
469 | + /** |
|
470 | + * Check the onboard status and return the result. |
|
471 | + * (AJAX) |
|
472 | + * |
|
473 | + * @return void |
|
474 | + */ |
|
475 | + public static function getOnboardStatus(): void |
|
476 | + { |
|
477 | + $paypal_pm = EED_PayPalCommerce::getPaymentMethod(); |
|
478 | + if (! $paypal_pm instanceof EE_Payment_Method) { |
|
479 | + $err_msg = esc_html__('Could not specify the payment method.', 'event_espresso'); |
|
480 | + PayPalLogger::errorLog($err_msg, EED_Module::getRequest()->postParams(), $paypal_pm); |
|
481 | + wp_send_json(['on_board' => false]); |
|
482 | + } |
|
483 | + try { |
|
484 | + $seller_id = PayPalExtraMetaManager::getPmOption($paypal_pm, Domain::META_KEY_SELLER_MERCHANT_ID) ?? '--'; |
|
485 | + } catch (Exception $e) { |
|
486 | + $seller_id = '--'; |
|
487 | + } |
|
488 | + wp_send_json( |
|
489 | + [ |
|
490 | + 'on_board' => EED_PayPalOnboard::isOnboard($paypal_pm), |
|
491 | + 'seller_id' => $seller_id, |
|
492 | + ] |
|
493 | + ); |
|
494 | + } |
|
495 | + |
|
496 | + |
|
497 | + /** |
|
498 | + * De-authorize the seller. Remove all API credentials. |
|
499 | + * (AJAX) |
|
500 | + * |
|
501 | + * @return void |
|
502 | + */ |
|
503 | + public static function offboard(): void |
|
504 | + { |
|
505 | + $paypal_pm = EED_PayPalCommerce::getPaymentMethod(); |
|
506 | + if (! $paypal_pm instanceof EE_Payment_Method) { |
|
507 | + wp_send_json([ |
|
508 | + 'error' => 'INVALID_PM', |
|
509 | + 'message' => esc_html__( |
|
510 | + 'Invalid payment method. Please refresh the page and try again.', |
|
511 | + 'event_espresso' |
|
512 | + ), |
|
513 | + ]); |
|
514 | + } |
|
515 | + PayPalExtraMetaManager::deleteAllData($paypal_pm); |
|
516 | + wp_send_json(['success' => true]); |
|
517 | + } |
|
518 | + |
|
519 | + |
|
520 | + /** |
|
521 | + * Checks if already onboard. |
|
522 | + * |
|
523 | + * @param EE_Payment_Method $payment_method |
|
524 | + * @return boolean |
|
525 | + */ |
|
526 | + public static function isOnboard(EE_Payment_Method $payment_method): bool |
|
527 | + { |
|
528 | + $pp_meta_data = PayPalExtraMetaManager::getAllData($payment_method); |
|
529 | + return |
|
530 | + // onborded with a third party integration ? |
|
531 | + (! empty($pp_meta_data[ Domain::META_KEY_SELLER_MERCHANT_ID ]) |
|
532 | + && ! empty($pp_meta_data[ Domain::META_KEY_ACCESS_TOKEN ]) |
|
533 | + ) |
|
534 | + // or with the first party integration ? |
|
535 | + || (! empty($pp_meta_data[ Domain::META_KEY_CLIENT_ID ]) |
|
536 | + && ! empty($pp_meta_data[ Domain::META_KEY_CLIENT_SECRET ]) |
|
537 | + && ! empty($pp_meta_data[ Domain::META_KEY_PAYER_ID ]) |
|
538 | + ); |
|
539 | + } |
|
540 | + |
|
541 | + |
|
542 | + /** |
|
543 | + * Send a request and return a decoded response body. |
|
544 | + * |
|
545 | + * @param EE_Payment_Method $paypal_pm |
|
546 | + * @param string $request_url |
|
547 | + * @param array $request_args |
|
548 | + * @return array |
|
549 | + */ |
|
550 | + public static function sendRequest(EE_Payment_Method $paypal_pm, string $request_url, array $request_args): array |
|
551 | + { |
|
552 | + $error_return = ['error' => true]; |
|
553 | + $response = wp_remote_request($request_url, $request_args); |
|
554 | + if (is_wp_error($response)) { |
|
555 | + $message = $response->get_error_message(); |
|
556 | + PayPalLogger::errorLog($message, [$request_url, $request_args, $response], $paypal_pm); |
|
557 | + $error_return['message'] = $message; |
|
558 | + return $error_return; |
|
559 | + } |
|
560 | + $response_body = (isset($response['body']) && $response['body']) ? json_decode($response['body'], true) : []; |
|
561 | + if (empty($response_body) || isset($response_body['error'])) { |
|
562 | + $message = $response_body['error_description'] |
|
563 | + ?? sprintf( |
|
564 | + esc_html__('Unknown response received while sending a request to: %1$s', 'event_espresso'), |
|
565 | + $request_url |
|
566 | + ); |
|
567 | + PayPalLogger::errorLog($message, [$request_url, $request_args, $response], $paypal_pm); |
|
568 | + $error_return['message'] = $message; |
|
569 | + return $error_return; |
|
570 | + } |
|
571 | + return $response_body; |
|
572 | + } |
|
573 | + |
|
574 | + |
|
575 | + /** |
|
576 | + * Check the response for a partner token request. |
|
577 | + * |
|
578 | + * @param $response |
|
579 | + * @param EE_Payment_Method $paypal_pm |
|
580 | + * @return bool |
|
581 | + */ |
|
582 | + public static function partnerTokenResponseValid($response, EE_Payment_Method $paypal_pm): bool |
|
583 | + { |
|
584 | + // Check the data we received. |
|
585 | + if ( |
|
586 | + empty($response['nonce']) |
|
587 | + || ! wp_verify_nonce($response['nonce'], 'eea_pp_commerce_get_access_token') |
|
588 | + || empty($response['access_token']) |
|
589 | + || empty($response['app_id']) |
|
590 | + || empty($response['expires_in']) |
|
591 | + || empty($response['partner_client_id']) |
|
592 | + || empty($response['partner_merchant_id']) |
|
593 | + ) { |
|
594 | + // This is an error. |
|
595 | + $err_msg = esc_html__('Incoming parameter validation failed.', 'event_espresso'); |
|
596 | + PayPalLogger::errorLog($err_msg, (array) $response, $paypal_pm); |
|
597 | + return false; |
|
598 | + } |
|
599 | + return true; |
|
600 | + } |
|
601 | + |
|
602 | + |
|
603 | + /** |
|
604 | + * Returns the base URL to the middleman server. |
|
605 | + * If LOCAL_MIDDLEMAN_SERVER is defined, requests will be sent to connect.eventespresso.test |
|
606 | + * |
|
607 | + * @param EE_Payment_Method $payment_method |
|
608 | + * @return string |
|
609 | + * @throws EE_Error |
|
610 | + * @throws ReflectionException |
|
611 | + */ |
|
612 | + public static function getMiddlemanBaseUrl(EE_Payment_Method $payment_method): string |
|
613 | + { |
|
614 | + $target = defined('LOCAL_MIDDLEMAN_SERVER') ? 'test' : 'com'; |
|
615 | + // If this PM is used under different provider accounts, you might need an account indicator. |
|
616 | + $account = defined('EE_PAYPAL_COMMERCE_ACCOUNT_INDICATOR') ? EE_PAYPAL_COMMERCE_ACCOUNT_INDICATOR : ''; |
|
617 | + $postfix = $payment_method->debug_mode() ? '_sandbox' : ''; |
|
618 | + $path = 'paypal_commerce' . $account . $postfix; |
|
619 | + return 'https://connect.eventespresso.' . $target . '/' . $path . '/'; |
|
620 | + } |
|
621 | + |
|
622 | + |
|
623 | + /** |
|
624 | + * This Payment Method admin notices. |
|
625 | + * |
|
626 | + * @return void |
|
627 | + * @throws EE_Error |
|
628 | + * @throws ReflectionException |
|
629 | + */ |
|
630 | + public static function adminNotice() |
|
631 | + { |
|
632 | + // Show the notice if PayPal Commerce PM is active but merchant is not onboard. |
|
633 | + $pp_commerce = EEM_Payment_Method::instance()->get_one_by_slug('paypalcheckout'); |
|
634 | + if ($pp_commerce instanceof EE_Payment_Method |
|
635 | + && $pp_commerce->active() |
|
636 | + && ! EED_PayPalOnboard::isOnboard($pp_commerce) |
|
637 | + ) { |
|
638 | + add_action('admin_notices', [__CLASS__, 'notOnboardNotice']); |
|
639 | + } |
|
640 | + } |
|
641 | + |
|
642 | + |
|
643 | + /** |
|
644 | + * Contents of the not onboard admin notice. |
|
645 | + * |
|
646 | + * @return void |
|
647 | + * @throws EE_Error |
|
648 | + * @throws ReflectionException |
|
649 | + */ |
|
650 | + public static function notOnboardNotice() |
|
651 | + { |
|
652 | + $open_anchor = $close_anchor = ''; |
|
653 | + $pp_commerce = EEM_Payment_Method::instance()->get_one_by_slug('paypalcheckout'); |
|
654 | + if ($pp_commerce instanceof EE_Payment_Method) { |
|
655 | + $pm_page = add_query_arg( |
|
656 | + [ |
|
657 | + 'page' => 'espresso_payment_settings', |
|
658 | + 'webhook_action' => 'eepPpcMerchantOnboard', |
|
659 | + 'payment_method' => $pp_commerce->slug(), |
|
660 | + ], |
|
661 | + admin_url('admin.php') |
|
662 | + ); |
|
663 | + $open_anchor = "<a href='$pm_page'>"; |
|
664 | + $close_anchor = "</a>"; |
|
665 | + } |
|
666 | + echo '<div class="error"><p>' |
|
667 | + . sprintf( |
|
668 | + esc_html__( |
|
669 | + '%1$sPayPal Commerce%2$s payment method was activated but is not connected to PayPal. Please %3$sfinish setting up%4$s this payment method.', |
|
670 | + 'event_espresso' |
|
671 | + ), |
|
672 | + '<strong>', |
|
673 | + '</strong>', |
|
674 | + $open_anchor, |
|
675 | + $close_anchor |
|
676 | + ) |
|
677 | + . '</p></div>'; |
|
678 | + } |
|
679 | 679 | } |
@@ -75,7 +75,7 @@ discard block |
||
75 | 75 | $signup_link = ''; |
76 | 76 | try { |
77 | 77 | $paypal_pm = EED_PayPalCommerce::getPaymentMethod(); |
78 | - if (! $paypal_pm instanceof EE_Payment_Method) { |
|
78 | + if ( ! $paypal_pm instanceof EE_Payment_Method) { |
|
79 | 79 | PayPalLogger::errorLogAndExit( |
80 | 80 | esc_html__('No payment method.', 'event_espresso'), |
81 | 81 | EED_Module::getRequest()->postParams(), |
@@ -88,7 +88,7 @@ discard block |
||
88 | 88 | // if (! $signup_link || $token_expired) { |
89 | 89 | $signup_link = EED_PayPalOnboard::requestOnboardingUrl($paypal_pm); |
90 | 90 | // } |
91 | - if (! $signup_link) { |
|
91 | + if ( ! $signup_link) { |
|
92 | 92 | $err_msg = esc_html__('Error! Could not generate a sign-up link.', 'event_espresso'); |
93 | 93 | PayPalLogger::errorLogAndExit($err_msg, ['signup_link' => $signup_link], $paypal_pm); |
94 | 94 | } |
@@ -97,7 +97,7 @@ discard block |
||
97 | 97 | PayPalLogger::errorLogAndExit($exception->getMessage()); |
98 | 98 | } |
99 | 99 | // Is it empty (can happen if we didn't get the URL through the API). |
100 | - $signup_link = $signup_link ? $signup_link . '?&displayMode=minibrowser' : '#'; |
|
100 | + $signup_link = $signup_link ? $signup_link.'?&displayMode=minibrowser' : '#'; |
|
101 | 101 | wp_send_json( |
102 | 102 | [ |
103 | 103 | 'signup_link' => $signup_link, |
@@ -120,7 +120,7 @@ discard block |
||
120 | 120 | $signup_link = ''; |
121 | 121 | // Get the access token. |
122 | 122 | $access_token = EED_PayPalOnboard::getPartnerAccessToken($paypal_pm); |
123 | - if (! $access_token) { |
|
123 | + if ( ! $access_token) { |
|
124 | 124 | $err_msg = esc_html__('Error! No access token.', 'event_espresso'); |
125 | 125 | PayPalLogger::errorLog($err_msg, ['access_token' => $access_token], $paypal_pm); |
126 | 126 | return ''; |
@@ -133,17 +133,17 @@ discard block |
||
133 | 133 | 'headers' => [ |
134 | 134 | 'User-Agent' => sanitize_text_field($_SERVER['HTTP_USER_AGENT']), |
135 | 135 | 'Content-Type' => 'application/json', |
136 | - 'Authorization' => 'Bearer ' . $access_token, |
|
136 | + 'Authorization' => 'Bearer '.$access_token, |
|
137 | 137 | 'PayPal-Partner-Attribution-Id' => $bn_code, |
138 | 138 | ], |
139 | 139 | 'body' => $body_params, |
140 | 140 | ]; |
141 | - $request_url = Domain::getPayPalApiUrl($paypal_pm) . '/v2/customer/partner-referrals'; |
|
141 | + $request_url = Domain::getPayPalApiUrl($paypal_pm).'/v2/customer/partner-referrals'; |
|
142 | 142 | $response = EED_PayPalOnboard::sendRequest($paypal_pm, $request_url, $post_params); |
143 | 143 | // Check the data we received. |
144 | 144 | if (isset($response['error']) || empty($response['links'])) { |
145 | 145 | // Did the original access token get replaced by any chance ? |
146 | - if (! $one_time_request |
|
146 | + if ( ! $one_time_request |
|
147 | 147 | && ! empty($response['message']) |
148 | 148 | && $response['message'] === 'Access Token not found in cache' |
149 | 149 | ) { |
@@ -255,7 +255,7 @@ discard block |
||
255 | 255 | 'payment_method' => $get_params['payment_method'], |
256 | 256 | ]; |
257 | 257 | if (isset($get_params['sandbox_mode'])) { |
258 | - $args_to_add[ Domain::META_KEY_SANDBOX_MODE ] = $get_params['sandbox_mode']; |
|
258 | + $args_to_add[Domain::META_KEY_SANDBOX_MODE] = $get_params['sandbox_mode']; |
|
259 | 259 | } |
260 | 260 | $home_url = add_query_arg($args_to_add, admin_url('admin.php')); |
261 | 261 | wp_redirect($home_url); |
@@ -274,16 +274,16 @@ discard block |
||
274 | 274 | public static function updateOnboardingStatus(): void |
275 | 275 | { |
276 | 276 | // Check if this is the webhook from PayPal. |
277 | - if (! isset($_GET['webhook_action'], $_GET['nonce']) |
|
277 | + if ( ! isset($_GET['webhook_action'], $_GET['nonce']) |
|
278 | 278 | || $_GET['webhook_action'] !== 'eepPpcMerchantOnboard' |
279 | 279 | ) { |
280 | - return; // Ignore. |
|
280 | + return; // Ignore. |
|
281 | 281 | } |
282 | 282 | $get_params = EED_Module::getRequest()->getParams(); |
283 | 283 | // Get the payment method. |
284 | 284 | $paypal_pm = EED_PayPalCommerce::getPaymentMethod(); |
285 | 285 | // Check the response (GET) parameters. |
286 | - if (! EED_PayPalOnboard::onboardingStatusResponseValid($get_params, $paypal_pm)) { |
|
286 | + if ( ! EED_PayPalOnboard::onboardingStatusResponseValid($get_params, $paypal_pm)) { |
|
287 | 287 | // Missing parameters. Can't proceed. |
288 | 288 | PayPalLogger::errorLog( |
289 | 289 | esc_html__('Missing required onboarding parameters.', 'event_espresso'), |
@@ -296,9 +296,9 @@ discard block |
||
296 | 296 | // Check on the onboarding status (recommended by PP). |
297 | 297 | $onboarding_status = EED_PayPalOnboard::trackSellerOnboarding( |
298 | 298 | $paypal_pm, |
299 | - $get_params[ Domain::META_KEY_SELLER_MERCHANT_ID ] |
|
299 | + $get_params[Domain::META_KEY_SELLER_MERCHANT_ID] |
|
300 | 300 | ); |
301 | - if (! isset($onboarding_status['valid']) || ! $onboarding_status['valid']) { |
|
301 | + if ( ! isset($onboarding_status['valid']) || ! $onboarding_status['valid']) { |
|
302 | 302 | PayPalLogger::errorLog( |
303 | 303 | $onboarding_status['message'], |
304 | 304 | array_merge($get_params, $onboarding_status), |
@@ -330,9 +330,9 @@ discard block |
||
330 | 330 | // Check that we have all the required parameters and the nonce is ok. |
331 | 331 | if ($paypal_pm instanceof EE_Payment_Method |
332 | 332 | && wp_verify_nonce($data['nonce'], Domain::NONCE_NAME_ONBOARDING_RETURN) |
333 | - && ! empty($data[ Domain::API_PARAM_PARTNER_ID ]) |
|
334 | - && ! empty($data[ Domain::META_KEY_SELLER_MERCHANT_ID ]) |
|
335 | - && isset($data[ Domain::API_PARAM_EMAIL_CONFIRMED ]) |
|
333 | + && ! empty($data[Domain::API_PARAM_PARTNER_ID]) |
|
334 | + && ! empty($data[Domain::META_KEY_SELLER_MERCHANT_ID]) |
|
335 | + && isset($data[Domain::API_PARAM_EMAIL_CONFIRMED]) |
|
336 | 336 | ) { |
337 | 337 | return true; |
338 | 338 | } |
@@ -354,7 +354,7 @@ discard block |
||
354 | 354 | $access_token = PayPalExtraMetaManager::getPmOption($paypal_pm, Domain::META_KEY_ACCESS_TOKEN); |
355 | 355 | $expired = EED_PayPalOnboard::partnerAccessTokenExpired($paypal_pm); |
356 | 356 | // If we don't have it, request/update it. |
357 | - if (! $access_token || $expired) { |
|
357 | + if ( ! $access_token || $expired) { |
|
358 | 358 | return EED_PayPalOnboard::requestPartnerAccessToken($paypal_pm); |
359 | 359 | } |
360 | 360 | // Access token is saved as encrypted, so return decrypted. |
@@ -371,7 +371,7 @@ discard block |
||
371 | 371 | public static function partnerAccessTokenExpired(EE_Payment_Method $paypal_pm): bool |
372 | 372 | { |
373 | 373 | $expires_at = (int) PayPalExtraMetaManager::getPmOption($paypal_pm, Domain::META_KEY_TOKEN_EXPIRES_IN); |
374 | - if (! $expires_at) { |
|
374 | + if ( ! $expires_at) { |
|
375 | 375 | return true; |
376 | 376 | } |
377 | 377 | // Validate the token expiration date. |
@@ -408,18 +408,18 @@ discard block |
||
408 | 408 | if (defined('LOCAL_MIDDLEMAN_SERVER')) { |
409 | 409 | $post_args['sslverify'] = false; |
410 | 410 | } |
411 | - $post_url = EED_PayPalOnboard::getMiddlemanBaseUrl($paypal_pm) . 'get_token'; |
|
411 | + $post_url = EED_PayPalOnboard::getMiddlemanBaseUrl($paypal_pm).'get_token'; |
|
412 | 412 | $response = EED_PayPalOnboard::sendRequest($paypal_pm, $post_url, $post_args); |
413 | 413 | if (isset($response['error'])) { |
414 | 414 | return ''; |
415 | 415 | } |
416 | 416 | // Check the data we received. |
417 | - if (! EED_PayPalOnboard::partnerTokenResponseValid($response, $paypal_pm)) { |
|
417 | + if ( ! EED_PayPalOnboard::partnerTokenResponseValid($response, $paypal_pm)) { |
|
418 | 418 | return ''; |
419 | 419 | } |
420 | 420 | // If we are here all seems to be ok. Save the token and it's data. |
421 | 421 | $saved = PayPalExtraMetaManager::savePartnerAccessToken($paypal_pm, $response); |
422 | - if (! $saved) { |
|
422 | + if ( ! $saved) { |
|
423 | 423 | return ''; |
424 | 424 | } |
425 | 425 | return $response['access_token']; |
@@ -436,7 +436,7 @@ discard block |
||
436 | 436 | public static function trackSellerOnboarding(EE_Payment_Method $paypal_pm, string $merchant_id): array |
437 | 437 | { |
438 | 438 | $track_onboarding = EED_PayPalOnboard::getTrackOnboardingApi($paypal_pm, $merchant_id); |
439 | - if (! $track_onboarding instanceof TrackSellerOnboarding) { |
|
439 | + if ( ! $track_onboarding instanceof TrackSellerOnboarding) { |
|
440 | 440 | $err_msg = esc_html__('Failed to track seller onboarding.', 'event_espresso'); |
441 | 441 | return ['error' => 'TRACK_ONBOARDING_FAILED', 'message' => $err_msg]; |
442 | 442 | } |
@@ -459,7 +459,7 @@ discard block |
||
459 | 459 | ): ?TrackSellerOnboarding { |
460 | 460 | $partner_id = PayPalExtraMetaManager::getPmOption($paypal_pm, Domain::META_KEY_PARTNER_MERCHANT_ID); |
461 | 461 | $paypal_api = EED_PayPalCommerce::getPayPalApi($paypal_pm); |
462 | - if (! $paypal_api instanceof PayPalApi || ! $partner_id) { |
|
462 | + if ( ! $paypal_api instanceof PayPalApi || ! $partner_id) { |
|
463 | 463 | return null; |
464 | 464 | } |
465 | 465 | return new TrackSellerOnboarding($paypal_api, $partner_id, $merchant_id, $paypal_pm->debug_mode()); |
@@ -475,7 +475,7 @@ discard block |
||
475 | 475 | public static function getOnboardStatus(): void |
476 | 476 | { |
477 | 477 | $paypal_pm = EED_PayPalCommerce::getPaymentMethod(); |
478 | - if (! $paypal_pm instanceof EE_Payment_Method) { |
|
478 | + if ( ! $paypal_pm instanceof EE_Payment_Method) { |
|
479 | 479 | $err_msg = esc_html__('Could not specify the payment method.', 'event_espresso'); |
480 | 480 | PayPalLogger::errorLog($err_msg, EED_Module::getRequest()->postParams(), $paypal_pm); |
481 | 481 | wp_send_json(['on_board' => false]); |
@@ -503,7 +503,7 @@ discard block |
||
503 | 503 | public static function offboard(): void |
504 | 504 | { |
505 | 505 | $paypal_pm = EED_PayPalCommerce::getPaymentMethod(); |
506 | - if (! $paypal_pm instanceof EE_Payment_Method) { |
|
506 | + if ( ! $paypal_pm instanceof EE_Payment_Method) { |
|
507 | 507 | wp_send_json([ |
508 | 508 | 'error' => 'INVALID_PM', |
509 | 509 | 'message' => esc_html__( |
@@ -528,13 +528,13 @@ discard block |
||
528 | 528 | $pp_meta_data = PayPalExtraMetaManager::getAllData($payment_method); |
529 | 529 | return |
530 | 530 | // onborded with a third party integration ? |
531 | - (! empty($pp_meta_data[ Domain::META_KEY_SELLER_MERCHANT_ID ]) |
|
532 | - && ! empty($pp_meta_data[ Domain::META_KEY_ACCESS_TOKEN ]) |
|
531 | + ( ! empty($pp_meta_data[Domain::META_KEY_SELLER_MERCHANT_ID]) |
|
532 | + && ! empty($pp_meta_data[Domain::META_KEY_ACCESS_TOKEN]) |
|
533 | 533 | ) |
534 | 534 | // or with the first party integration ? |
535 | - || (! empty($pp_meta_data[ Domain::META_KEY_CLIENT_ID ]) |
|
536 | - && ! empty($pp_meta_data[ Domain::META_KEY_CLIENT_SECRET ]) |
|
537 | - && ! empty($pp_meta_data[ Domain::META_KEY_PAYER_ID ]) |
|
535 | + || ( ! empty($pp_meta_data[Domain::META_KEY_CLIENT_ID]) |
|
536 | + && ! empty($pp_meta_data[Domain::META_KEY_CLIENT_SECRET]) |
|
537 | + && ! empty($pp_meta_data[Domain::META_KEY_PAYER_ID]) |
|
538 | 538 | ); |
539 | 539 | } |
540 | 540 | |
@@ -615,8 +615,8 @@ discard block |
||
615 | 615 | // If this PM is used under different provider accounts, you might need an account indicator. |
616 | 616 | $account = defined('EE_PAYPAL_COMMERCE_ACCOUNT_INDICATOR') ? EE_PAYPAL_COMMERCE_ACCOUNT_INDICATOR : ''; |
617 | 617 | $postfix = $payment_method->debug_mode() ? '_sandbox' : ''; |
618 | - $path = 'paypal_commerce' . $account . $postfix; |
|
619 | - return 'https://connect.eventespresso.' . $target . '/' . $path . '/'; |
|
618 | + $path = 'paypal_commerce'.$account.$postfix; |
|
619 | + return 'https://connect.eventespresso.'.$target.'/'.$path.'/'; |
|
620 | 620 | } |
621 | 621 | |
622 | 622 |
@@ -15,168 +15,168 @@ discard block |
||
15 | 15 | */ |
16 | 16 | class Events_Admin_List_Table extends EE_Admin_List_Table |
17 | 17 | { |
18 | - private ?EE_Datetime $_dtt = null; |
|
19 | - |
|
20 | - |
|
21 | - /** |
|
22 | - * Initial setup of data properties for the list table. |
|
23 | - * |
|
24 | - * @throws Exception |
|
25 | - */ |
|
26 | - protected function _setup_data() |
|
27 | - { |
|
28 | - $this->_data = $this->_admin_page->get_events($this->_per_page, $this->_current_page); |
|
29 | - $this->_all_data_count = $this->_admin_page->get_events(0, 0, true); |
|
30 | - } |
|
31 | - |
|
32 | - |
|
33 | - /** |
|
34 | - * Set up of additional properties for the list table. |
|
35 | - * |
|
36 | - * @throws EE_Error |
|
37 | - * @throws ReflectionException |
|
38 | - */ |
|
39 | - protected function _set_properties() |
|
40 | - { |
|
41 | - $this->_wp_list_args = [ |
|
42 | - 'singular' => esc_html__('event', 'event_espresso'), |
|
43 | - 'plural' => esc_html__('events', 'event_espresso'), |
|
44 | - 'ajax' => true, // for now |
|
45 | - 'screen' => $this->_admin_page->get_current_screen()->id, |
|
46 | - ]; |
|
47 | - $approved_registrations = esc_html__('Approved Registrations', 'event_espresso'); |
|
48 | - $this->_columns = [ |
|
49 | - 'cb' => '<input type="checkbox" />', |
|
50 | - 'id' => esc_html__('ID', 'event_espresso'), |
|
51 | - 'name' => esc_html__('Name', 'event_espresso'), |
|
52 | - 'author' => esc_html__('Author', 'event_espresso'), |
|
53 | - 'venue' => esc_html__('Venue', 'event_espresso'), |
|
54 | - 'start_date_time' => esc_html__('Event Start', 'event_espresso'), |
|
55 | - 'reg_begins' => esc_html__('On Sale', 'event_espresso'), |
|
56 | - 'attendees' => ' |
|
18 | + private ?EE_Datetime $_dtt = null; |
|
19 | + |
|
20 | + |
|
21 | + /** |
|
22 | + * Initial setup of data properties for the list table. |
|
23 | + * |
|
24 | + * @throws Exception |
|
25 | + */ |
|
26 | + protected function _setup_data() |
|
27 | + { |
|
28 | + $this->_data = $this->_admin_page->get_events($this->_per_page, $this->_current_page); |
|
29 | + $this->_all_data_count = $this->_admin_page->get_events(0, 0, true); |
|
30 | + } |
|
31 | + |
|
32 | + |
|
33 | + /** |
|
34 | + * Set up of additional properties for the list table. |
|
35 | + * |
|
36 | + * @throws EE_Error |
|
37 | + * @throws ReflectionException |
|
38 | + */ |
|
39 | + protected function _set_properties() |
|
40 | + { |
|
41 | + $this->_wp_list_args = [ |
|
42 | + 'singular' => esc_html__('event', 'event_espresso'), |
|
43 | + 'plural' => esc_html__('events', 'event_espresso'), |
|
44 | + 'ajax' => true, // for now |
|
45 | + 'screen' => $this->_admin_page->get_current_screen()->id, |
|
46 | + ]; |
|
47 | + $approved_registrations = esc_html__('Approved Registrations', 'event_espresso'); |
|
48 | + $this->_columns = [ |
|
49 | + 'cb' => '<input type="checkbox" />', |
|
50 | + 'id' => esc_html__('ID', 'event_espresso'), |
|
51 | + 'name' => esc_html__('Name', 'event_espresso'), |
|
52 | + 'author' => esc_html__('Author', 'event_espresso'), |
|
53 | + 'venue' => esc_html__('Venue', 'event_espresso'), |
|
54 | + 'start_date_time' => esc_html__('Event Start', 'event_espresso'), |
|
55 | + 'reg_begins' => esc_html__('On Sale', 'event_espresso'), |
|
56 | + 'attendees' => ' |
|
57 | 57 | <span class="dashicons dashicons-groups ee-status-color--RAP ee-aria-tooltip" |
58 | 58 | aria-label="' . $approved_registrations . '"></span> |
59 | 59 | <span class="screen-reader-text">' . $approved_registrations . '</span>', |
60 | - 'actions' => $this->actionsColumnHeader(), |
|
61 | - ]; |
|
62 | - $this->addConditionalColumns(); |
|
63 | - $this->_sortable_columns = [ |
|
64 | - 'id' => ['EVT_ID' => true], |
|
65 | - 'name' => ['EVT_name' => false], |
|
66 | - 'author' => ['EVT_wp_user' => false], |
|
67 | - 'venue' => ['Venue.VNU_name' => false], |
|
68 | - 'start_date_time' => ['Datetime.DTT_EVT_start' => false], |
|
69 | - 'reg_begins' => ['Datetime.Ticket.TKT_start_date' => false], |
|
70 | - ]; |
|
71 | - |
|
72 | - $this->_primary_column = 'id'; |
|
73 | - $this->_hidden_columns = ['author', 'event_category']; |
|
74 | - } |
|
75 | - |
|
76 | - |
|
77 | - /** |
|
78 | - * @return array |
|
79 | - */ |
|
80 | - protected function _get_table_filters() |
|
81 | - { |
|
82 | - return []; // no filters with decaf |
|
83 | - } |
|
84 | - |
|
85 | - |
|
86 | - /** |
|
87 | - * Setup of views properties. |
|
88 | - * |
|
89 | - * @throws InvalidDataTypeException |
|
90 | - * @throws InvalidInterfaceException |
|
91 | - * @throws InvalidArgumentException |
|
92 | - * @throws EE_Error |
|
93 | - * @throws ReflectionException |
|
94 | - */ |
|
95 | - protected function _add_view_counts() |
|
96 | - { |
|
97 | - $this->_views['all']['count'] = $this->_admin_page->total_events(); |
|
98 | - $this->_views['draft']['count'] = $this->_admin_page->total_events_draft(); |
|
99 | - if ( |
|
100 | - EE_Registry::instance()->CAP->current_user_can( |
|
101 | - 'ee_delete_events', |
|
102 | - 'espresso_events_trash_events' |
|
103 | - ) |
|
104 | - ) { |
|
105 | - $this->_views['trash']['count'] = $this->_admin_page->total_trashed_events(); |
|
106 | - } |
|
107 | - } |
|
108 | - |
|
109 | - |
|
110 | - /** |
|
111 | - * @param EE_Event $item |
|
112 | - * @return string |
|
113 | - */ |
|
114 | - protected function _get_row_class($item): string |
|
115 | - { |
|
116 | - $class = parent::_get_row_class($item); |
|
117 | - if ($this->_has_checkbox_column) { |
|
118 | - $class .= ' has-checkbox-column'; |
|
119 | - } |
|
120 | - return $class; |
|
121 | - } |
|
122 | - |
|
123 | - |
|
124 | - /** |
|
125 | - * @param EE_Event $item |
|
126 | - * @return string |
|
127 | - * @throws EE_Error |
|
128 | - * @throws ReflectionException |
|
129 | - */ |
|
130 | - public function column_cb($item): string |
|
131 | - { |
|
132 | - if (! $item instanceof EE_Event) { |
|
133 | - return ''; |
|
134 | - } |
|
135 | - $this->_dtt = $item->primary_datetime(); // set this for use in other columns |
|
136 | - $content = sprintf( |
|
137 | - '<input type="checkbox" name="EVT_IDs[]" value="%s" />', |
|
138 | - $item->ID() |
|
139 | - ); |
|
140 | - return $this->columnContent('cb', $content, 'center'); |
|
141 | - } |
|
142 | - |
|
143 | - |
|
144 | - /** |
|
145 | - * @param EE_Event $event |
|
146 | - * @return string |
|
147 | - * @throws EE_Error |
|
148 | - * @throws ReflectionException |
|
149 | - */ |
|
150 | - public function column_id(EE_Event $event): string |
|
151 | - { |
|
152 | - $content = '<span class="ee-entity-id">' . $event->ID() . '</span>'; |
|
153 | - $content .= '<span class="show-on-mobile-view-only">'; |
|
154 | - $content .= $this->column_name($event, false); |
|
155 | - $content .= '</span>'; |
|
156 | - return $this->columnContent('id', $content, 'end'); |
|
157 | - } |
|
158 | - |
|
159 | - |
|
160 | - /** |
|
161 | - * @param EE_Event $event |
|
162 | - * @param bool $prep_content |
|
163 | - * @return string |
|
164 | - * @throws EE_Error |
|
165 | - * @throws ReflectionException |
|
166 | - */ |
|
167 | - public function column_name(EE_Event $event, bool $prep_content = true): string |
|
168 | - { |
|
169 | - $edit_query_args = [ |
|
170 | - 'action' => 'edit', |
|
171 | - 'post' => $event->ID(), |
|
172 | - ]; |
|
173 | - $edit_link = EE_Admin_Page::add_query_args_and_nonce($edit_query_args, EVENTS_ADMIN_URL); |
|
174 | - $actions = $this->_column_name_action_setup($event); |
|
175 | - $status = esc_attr($event->get_active_status()); |
|
176 | - $pretty_status = EEH_Template::pretty_status($status, false, 'sentence'); |
|
177 | - $status_dot = '<span class="ee-status-dot ee-status-bg--' . $status . '"></span>'; |
|
178 | - $visibility = $event->get_visibility_status(); |
|
179 | - $content = ' |
|
60 | + 'actions' => $this->actionsColumnHeader(), |
|
61 | + ]; |
|
62 | + $this->addConditionalColumns(); |
|
63 | + $this->_sortable_columns = [ |
|
64 | + 'id' => ['EVT_ID' => true], |
|
65 | + 'name' => ['EVT_name' => false], |
|
66 | + 'author' => ['EVT_wp_user' => false], |
|
67 | + 'venue' => ['Venue.VNU_name' => false], |
|
68 | + 'start_date_time' => ['Datetime.DTT_EVT_start' => false], |
|
69 | + 'reg_begins' => ['Datetime.Ticket.TKT_start_date' => false], |
|
70 | + ]; |
|
71 | + |
|
72 | + $this->_primary_column = 'id'; |
|
73 | + $this->_hidden_columns = ['author', 'event_category']; |
|
74 | + } |
|
75 | + |
|
76 | + |
|
77 | + /** |
|
78 | + * @return array |
|
79 | + */ |
|
80 | + protected function _get_table_filters() |
|
81 | + { |
|
82 | + return []; // no filters with decaf |
|
83 | + } |
|
84 | + |
|
85 | + |
|
86 | + /** |
|
87 | + * Setup of views properties. |
|
88 | + * |
|
89 | + * @throws InvalidDataTypeException |
|
90 | + * @throws InvalidInterfaceException |
|
91 | + * @throws InvalidArgumentException |
|
92 | + * @throws EE_Error |
|
93 | + * @throws ReflectionException |
|
94 | + */ |
|
95 | + protected function _add_view_counts() |
|
96 | + { |
|
97 | + $this->_views['all']['count'] = $this->_admin_page->total_events(); |
|
98 | + $this->_views['draft']['count'] = $this->_admin_page->total_events_draft(); |
|
99 | + if ( |
|
100 | + EE_Registry::instance()->CAP->current_user_can( |
|
101 | + 'ee_delete_events', |
|
102 | + 'espresso_events_trash_events' |
|
103 | + ) |
|
104 | + ) { |
|
105 | + $this->_views['trash']['count'] = $this->_admin_page->total_trashed_events(); |
|
106 | + } |
|
107 | + } |
|
108 | + |
|
109 | + |
|
110 | + /** |
|
111 | + * @param EE_Event $item |
|
112 | + * @return string |
|
113 | + */ |
|
114 | + protected function _get_row_class($item): string |
|
115 | + { |
|
116 | + $class = parent::_get_row_class($item); |
|
117 | + if ($this->_has_checkbox_column) { |
|
118 | + $class .= ' has-checkbox-column'; |
|
119 | + } |
|
120 | + return $class; |
|
121 | + } |
|
122 | + |
|
123 | + |
|
124 | + /** |
|
125 | + * @param EE_Event $item |
|
126 | + * @return string |
|
127 | + * @throws EE_Error |
|
128 | + * @throws ReflectionException |
|
129 | + */ |
|
130 | + public function column_cb($item): string |
|
131 | + { |
|
132 | + if (! $item instanceof EE_Event) { |
|
133 | + return ''; |
|
134 | + } |
|
135 | + $this->_dtt = $item->primary_datetime(); // set this for use in other columns |
|
136 | + $content = sprintf( |
|
137 | + '<input type="checkbox" name="EVT_IDs[]" value="%s" />', |
|
138 | + $item->ID() |
|
139 | + ); |
|
140 | + return $this->columnContent('cb', $content, 'center'); |
|
141 | + } |
|
142 | + |
|
143 | + |
|
144 | + /** |
|
145 | + * @param EE_Event $event |
|
146 | + * @return string |
|
147 | + * @throws EE_Error |
|
148 | + * @throws ReflectionException |
|
149 | + */ |
|
150 | + public function column_id(EE_Event $event): string |
|
151 | + { |
|
152 | + $content = '<span class="ee-entity-id">' . $event->ID() . '</span>'; |
|
153 | + $content .= '<span class="show-on-mobile-view-only">'; |
|
154 | + $content .= $this->column_name($event, false); |
|
155 | + $content .= '</span>'; |
|
156 | + return $this->columnContent('id', $content, 'end'); |
|
157 | + } |
|
158 | + |
|
159 | + |
|
160 | + /** |
|
161 | + * @param EE_Event $event |
|
162 | + * @param bool $prep_content |
|
163 | + * @return string |
|
164 | + * @throws EE_Error |
|
165 | + * @throws ReflectionException |
|
166 | + */ |
|
167 | + public function column_name(EE_Event $event, bool $prep_content = true): string |
|
168 | + { |
|
169 | + $edit_query_args = [ |
|
170 | + 'action' => 'edit', |
|
171 | + 'post' => $event->ID(), |
|
172 | + ]; |
|
173 | + $edit_link = EE_Admin_Page::add_query_args_and_nonce($edit_query_args, EVENTS_ADMIN_URL); |
|
174 | + $actions = $this->_column_name_action_setup($event); |
|
175 | + $status = esc_attr($event->get_active_status()); |
|
176 | + $pretty_status = EEH_Template::pretty_status($status, false, 'sentence'); |
|
177 | + $status_dot = '<span class="ee-status-dot ee-status-bg--' . $status . '"></span>'; |
|
178 | + $visibility = $event->get_visibility_status(); |
|
179 | + $content = ' |
|
180 | 180 | <div class="ee-layout-row ee-layout-row--fixed"> |
181 | 181 | <a class="row-title ee-status-color--' . $status . ' ee-aria-tooltip" |
182 | 182 | aria-label="' . $pretty_status . '" |
@@ -185,416 +185,416 @@ discard block |
||
185 | 185 | ' . $status_dot . $event->name() . ' |
186 | 186 | </a> |
187 | 187 | ' . ( |
188 | - $visibility |
|
189 | - ? '<span class="ee-event-visibility-status ee-status-text-small">(' . esc_html($visibility) . ')</span>' |
|
190 | - : '' |
|
191 | - ) . ' |
|
188 | + $visibility |
|
189 | + ? '<span class="ee-event-visibility-status ee-status-text-small">(' . esc_html($visibility) . ')</span>' |
|
190 | + : '' |
|
191 | + ) . ' |
|
192 | 192 | </div>'; |
193 | 193 | |
194 | - $content .= $this->row_actions($actions); |
|
195 | - |
|
196 | - return $prep_content ? $this->columnContent('name', $content) : $content; |
|
197 | - } |
|
198 | - |
|
199 | - |
|
200 | - /** |
|
201 | - * Just a method for setting up the actions for the name column |
|
202 | - * |
|
203 | - * @param EE_Event $event |
|
204 | - * @return array array of actions |
|
205 | - * @throws EE_Error |
|
206 | - * @throws InvalidArgumentException |
|
207 | - * @throws InvalidDataTypeException |
|
208 | - * @throws InvalidInterfaceException |
|
209 | - * @throws ReflectionException |
|
210 | - */ |
|
211 | - protected function _column_name_action_setup(EE_Event $event): array |
|
212 | - { |
|
213 | - // todo: remove when attendees is active |
|
214 | - if (! defined('REG_ADMIN_URL')) { |
|
215 | - define('REG_ADMIN_URL', EVENTS_ADMIN_URL); |
|
216 | - } |
|
217 | - $actions = []; |
|
218 | - $restore_event_link = ''; |
|
219 | - $delete_event_link = ''; |
|
220 | - $trash_event_link = ''; |
|
221 | - if ( |
|
222 | - EE_Registry::instance()->CAP->current_user_can( |
|
223 | - 'ee_edit_event', |
|
224 | - 'espresso_events_edit', |
|
225 | - $event->ID() |
|
226 | - ) |
|
227 | - ) { |
|
228 | - $edit_query_args = [ |
|
229 | - 'action' => 'edit', |
|
230 | - 'post' => $event->ID(), |
|
231 | - ]; |
|
232 | - $edit_link = EE_Admin_Page::add_query_args_and_nonce($edit_query_args, EVENTS_ADMIN_URL); |
|
233 | - $actions['edit'] = '<a href="' . $edit_link . '" class="ee-aria-tooltip" ' |
|
234 | - . ' aria-label="' . esc_attr__('Edit Event', 'event_espresso') . '">' |
|
235 | - . esc_html__('Edit', 'event_espresso') |
|
236 | - . '</a>'; |
|
237 | - } |
|
238 | - if ( |
|
239 | - EE_Registry::instance()->CAP->current_user_can( |
|
240 | - 'ee_read_registrations', |
|
241 | - 'espresso_registrations_view_registration' |
|
242 | - ) |
|
243 | - && EE_Registry::instance()->CAP->current_user_can( |
|
244 | - 'ee_read_event', |
|
245 | - 'espresso_registrations_view_registration', |
|
246 | - $event->ID() |
|
247 | - ) |
|
248 | - ) { |
|
249 | - $attendees_query_args = [ |
|
250 | - 'action' => 'default', |
|
251 | - 'event_id' => $event->ID(), |
|
252 | - ]; |
|
253 | - $attendees_link = EE_Admin_Page::add_query_args_and_nonce($attendees_query_args, REG_ADMIN_URL); |
|
254 | - $actions['attendees'] = '<a href="' . $attendees_link . '" class="ee-aria-tooltip"' |
|
255 | - . ' aria-label="' . esc_attr__('View Registrations', 'event_espresso') . '">' |
|
256 | - . esc_html__('Registrations', 'event_espresso') |
|
257 | - . '</a>'; |
|
258 | - } |
|
259 | - if ( |
|
260 | - EE_Registry::instance()->CAP->current_user_can( |
|
261 | - 'ee_delete_event', |
|
262 | - 'espresso_events_trash_event', |
|
263 | - $event->ID() |
|
264 | - ) |
|
265 | - ) { |
|
266 | - $trash_event_query_args = [ |
|
267 | - 'action' => 'trash_event', |
|
268 | - 'EVT_ID' => $event->ID(), |
|
269 | - ]; |
|
270 | - $trash_event_link = EE_Admin_Page::add_query_args_and_nonce( |
|
271 | - $trash_event_query_args, |
|
272 | - EVENTS_ADMIN_URL |
|
273 | - ); |
|
274 | - } |
|
275 | - if ( |
|
276 | - EE_Registry::instance()->CAP->current_user_can( |
|
277 | - 'ee_delete_event', |
|
278 | - 'espresso_events_restore_event', |
|
279 | - $event->ID() |
|
280 | - ) |
|
281 | - ) { |
|
282 | - $restore_event_query_args = [ |
|
283 | - 'action' => 'restore_event', |
|
284 | - 'EVT_ID' => $event->ID(), |
|
285 | - ]; |
|
286 | - $restore_event_link = EE_Admin_Page::add_query_args_and_nonce( |
|
287 | - $restore_event_query_args, |
|
288 | - EVENTS_ADMIN_URL |
|
289 | - ); |
|
290 | - } |
|
291 | - if ( |
|
292 | - EE_Registry::instance()->CAP->current_user_can( |
|
293 | - 'ee_delete_event', |
|
294 | - 'espresso_events_delete_event', |
|
295 | - $event->ID() |
|
296 | - ) |
|
297 | - ) { |
|
298 | - $delete_event_query_args = [ |
|
299 | - 'action' => 'delete_event', |
|
300 | - 'EVT_ID' => $event->ID(), |
|
301 | - ]; |
|
302 | - $delete_event_link = EE_Admin_Page::add_query_args_and_nonce( |
|
303 | - $delete_event_query_args, |
|
304 | - EVENTS_ADMIN_URL |
|
305 | - ); |
|
306 | - } |
|
307 | - $view_link = get_permalink($event->ID()); |
|
308 | - $actions['view'] = '<a href="' . $view_link . '" class="ee-aria-tooltip"' |
|
309 | - . ' aria-label="' . esc_attr__('View Event', 'event_espresso') . '">' |
|
310 | - . esc_html__('View', 'event_espresso') |
|
311 | - . '</a>'; |
|
312 | - if ($event->get('status') === 'trash') { |
|
313 | - if ( |
|
314 | - EE_Registry::instance()->CAP->current_user_can( |
|
315 | - 'ee_delete_event', |
|
316 | - 'espresso_events_restore_event', |
|
317 | - $event->ID() |
|
318 | - ) |
|
319 | - ) { |
|
320 | - $actions['restore_from_trash'] = '<a href="' . $restore_event_link . '" class="ee-aria-tooltip"' |
|
321 | - . ' aria-label="' . esc_attr__('Restore from Trash', 'event_espresso') |
|
322 | - . '">' |
|
323 | - . esc_html__('Restore from Trash', 'event_espresso') |
|
324 | - . '</a>'; |
|
325 | - } |
|
326 | - if ( |
|
327 | - EE_Registry::instance()->CAP->current_user_can( |
|
328 | - 'ee_delete_event', |
|
329 | - 'espresso_events_delete_event', |
|
330 | - $event->ID() |
|
331 | - ) |
|
332 | - ) { |
|
333 | - $actions['delete'] = '<a href="' . $delete_event_link . '" class="ee-aria-tooltip"' |
|
334 | - . ' aria-label="' . esc_attr__('Delete Permanently', 'event_espresso') . '">' |
|
335 | - . esc_html__('Delete Permanently', 'event_espresso') |
|
336 | - . '</a>'; |
|
337 | - } |
|
338 | - } else { |
|
339 | - if ( |
|
340 | - EE_Registry::instance()->CAP->current_user_can( |
|
341 | - 'ee_delete_event', |
|
342 | - 'espresso_events_trash_event', |
|
343 | - $event->ID() |
|
344 | - ) |
|
345 | - ) { |
|
346 | - $actions['move to trash'] = '<a href="' . $trash_event_link . '" class="ee-aria-tooltip"' |
|
347 | - . ' aria-label="' . esc_attr__('Trash Event', 'event_espresso') . '">' |
|
348 | - . esc_html__('Trash', 'event_espresso') |
|
349 | - . '</a>'; |
|
350 | - } |
|
351 | - } |
|
352 | - return $actions; |
|
353 | - } |
|
354 | - |
|
355 | - |
|
356 | - /** |
|
357 | - * @param EE_Event $event |
|
358 | - * @return string |
|
359 | - * @throws EE_Error |
|
360 | - * @throws ReflectionException |
|
361 | - */ |
|
362 | - public function column_author(EE_Event $event): string |
|
363 | - { |
|
364 | - // user author info |
|
365 | - $event_author = get_userdata($event->wp_user()); |
|
366 | - $gravatar = get_avatar($event->wp_user(), '24'); |
|
367 | - // filter link |
|
368 | - $query_args = [ |
|
369 | - 'action' => 'default', |
|
370 | - 'EVT_wp_user' => $event->wp_user(), |
|
371 | - ]; |
|
372 | - $filter_url = EE_Admin_Page::add_query_args_and_nonce($query_args, EVENTS_ADMIN_URL); |
|
373 | - $content = '<div class="ee-layout-row ee-layout-row--fixed">'; |
|
374 | - $content .= ' <a href="' . $filter_url . '" class="ee-event-author ee-aria-tooltip"' |
|
375 | - . ' aria-label="' . esc_attr__('Click to filter events by this author.', 'event_espresso') . '">' |
|
376 | - . $gravatar . $event_author->display_name |
|
377 | - . '</a>'; |
|
378 | - $content .= '</div>'; |
|
379 | - return $this->columnContent('author', $content); |
|
380 | - } |
|
381 | - |
|
382 | - |
|
383 | - /** |
|
384 | - * @param EE_Event $event |
|
385 | - * @return string |
|
386 | - * @throws EE_Error |
|
387 | - * @throws ReflectionException |
|
388 | - */ |
|
389 | - public function column_event_category(EE_Event $event): string |
|
390 | - { |
|
391 | - $event_categories = $event->get_all_event_categories(); |
|
392 | - $content = implode( |
|
393 | - ', ', |
|
394 | - array_map( |
|
395 | - function (EE_Term $category) { |
|
396 | - return $category->name(); |
|
397 | - }, |
|
398 | - $event_categories |
|
399 | - ) |
|
400 | - ); |
|
401 | - return $this->columnContent('event_category', $content); |
|
402 | - } |
|
403 | - |
|
404 | - |
|
405 | - /** |
|
406 | - * @param EE_Event $event |
|
407 | - * @return string |
|
408 | - * @throws EE_Error |
|
409 | - * @throws ReflectionException |
|
410 | - */ |
|
411 | - public function column_venue(EE_Event $event): string |
|
412 | - { |
|
413 | - $venue = $event->get_first_related('Venue'); |
|
414 | - $content = ! empty($venue) |
|
415 | - ? $venue->name() |
|
416 | - : ''; |
|
417 | - return $this->columnContent('venue', $content); |
|
418 | - } |
|
419 | - |
|
420 | - |
|
421 | - /** |
|
422 | - * @param EE_Event $event |
|
423 | - * @return string |
|
424 | - * @throws EE_Error |
|
425 | - * @throws ReflectionException |
|
426 | - */ |
|
427 | - public function column_start_date_time(EE_Event $event): string |
|
428 | - { |
|
429 | - $content = $this->_dtt instanceof EE_Datetime |
|
430 | - ? $this->_dtt->get_i18n_datetime('DTT_EVT_start') |
|
431 | - : esc_html__('No Date was saved for this Event', 'event_espresso'); |
|
432 | - return $this->columnContent('start_date_time', $content); |
|
433 | - } |
|
434 | - |
|
435 | - |
|
436 | - /** |
|
437 | - * @param EE_Event $event |
|
438 | - * @return string |
|
439 | - * @throws EE_Error |
|
440 | - * @throws ReflectionException |
|
441 | - */ |
|
442 | - public function column_reg_begins(EE_Event $event): string |
|
443 | - { |
|
444 | - $reg_start = $event->get_ticket_with_earliest_start_time(); |
|
445 | - $content = $reg_start instanceof EE_Ticket |
|
446 | - ? $reg_start->get_i18n_datetime('TKT_start_date') |
|
447 | - : esc_html__('No Tickets have been setup for this Event', 'event_espresso'); |
|
448 | - return $this->columnContent('reg_begins', $content); |
|
449 | - } |
|
450 | - |
|
451 | - |
|
452 | - /** |
|
453 | - * @param EE_Event $event |
|
454 | - * @return string |
|
455 | - * @throws EE_Error |
|
456 | - * @throws InvalidArgumentException |
|
457 | - * @throws InvalidDataTypeException |
|
458 | - * @throws InvalidInterfaceException |
|
459 | - * @throws ReflectionException |
|
460 | - */ |
|
461 | - public function column_attendees(EE_Event $event): string |
|
462 | - { |
|
463 | - $attendees_query_args = [ |
|
464 | - 'action' => 'default', |
|
465 | - 'event_id' => $event->ID(), |
|
466 | - '_reg_status' => EEM_Registration::status_id_approved, |
|
467 | - ]; |
|
468 | - $attendees_link = EE_Admin_Page::add_query_args_and_nonce($attendees_query_args, REG_ADMIN_URL); |
|
469 | - $registered_attendees = EEM_Registration::instance()->get_event_registration_count($event->ID()); |
|
470 | - |
|
471 | - $content = EE_Registry::instance()->CAP->current_user_can( |
|
472 | - 'ee_read_event', |
|
473 | - 'espresso_registrations_view_registration', |
|
474 | - $event->ID() |
|
475 | - ) && EE_Registry::instance()->CAP->current_user_can( |
|
476 | - 'ee_read_registrations', |
|
477 | - 'espresso_registrations_view_registration' |
|
478 | - ) |
|
479 | - ? '<a href="' . $attendees_link . '">' . $registered_attendees . '</a>' |
|
480 | - : $registered_attendees; |
|
481 | - return $this->columnContent('attendees', $content, 'center'); |
|
482 | - } |
|
483 | - |
|
484 | - |
|
485 | - /** |
|
486 | - * @param EE_Event $event |
|
487 | - * @return float |
|
488 | - * @throws EE_Error |
|
489 | - * @throws InvalidArgumentException |
|
490 | - * @throws InvalidDataTypeException |
|
491 | - * @throws InvalidInterfaceException |
|
492 | - * @throws ReflectionException |
|
493 | - */ |
|
494 | - public function column_tkts_sold(EE_Event $event): float |
|
495 | - { |
|
496 | - $content = EEM_Ticket::instance()->sum([['Datetime.EVT_ID' => $event->ID()]], 'TKT_sold'); |
|
497 | - return $this->columnContent('tkts_sold', $content); |
|
498 | - } |
|
499 | - |
|
500 | - |
|
501 | - /** |
|
502 | - * @param EE_Event $event |
|
503 | - * @return string |
|
504 | - * @throws EE_Error |
|
505 | - * @throws InvalidArgumentException |
|
506 | - * @throws InvalidDataTypeException |
|
507 | - * @throws InvalidInterfaceException |
|
508 | - * @throws ReflectionException |
|
509 | - */ |
|
510 | - public function column_actions(EE_Event $event): string |
|
511 | - { |
|
512 | - // todo: remove when attendees is active |
|
513 | - if (! defined('REG_ADMIN_URL')) { |
|
514 | - define('REG_ADMIN_URL', EVENTS_ADMIN_URL); |
|
515 | - } |
|
516 | - $action_links = []; |
|
517 | - $view_link = get_permalink($event->ID()); |
|
518 | - $action_links[] = '<a href="' . $view_link . '" class="ee-aria-tooltip button button--icon-only"' |
|
519 | - . ' aria-label="' . esc_attr__('View Event', 'event_espresso') . '" target="_blank"> |
|
194 | + $content .= $this->row_actions($actions); |
|
195 | + |
|
196 | + return $prep_content ? $this->columnContent('name', $content) : $content; |
|
197 | + } |
|
198 | + |
|
199 | + |
|
200 | + /** |
|
201 | + * Just a method for setting up the actions for the name column |
|
202 | + * |
|
203 | + * @param EE_Event $event |
|
204 | + * @return array array of actions |
|
205 | + * @throws EE_Error |
|
206 | + * @throws InvalidArgumentException |
|
207 | + * @throws InvalidDataTypeException |
|
208 | + * @throws InvalidInterfaceException |
|
209 | + * @throws ReflectionException |
|
210 | + */ |
|
211 | + protected function _column_name_action_setup(EE_Event $event): array |
|
212 | + { |
|
213 | + // todo: remove when attendees is active |
|
214 | + if (! defined('REG_ADMIN_URL')) { |
|
215 | + define('REG_ADMIN_URL', EVENTS_ADMIN_URL); |
|
216 | + } |
|
217 | + $actions = []; |
|
218 | + $restore_event_link = ''; |
|
219 | + $delete_event_link = ''; |
|
220 | + $trash_event_link = ''; |
|
221 | + if ( |
|
222 | + EE_Registry::instance()->CAP->current_user_can( |
|
223 | + 'ee_edit_event', |
|
224 | + 'espresso_events_edit', |
|
225 | + $event->ID() |
|
226 | + ) |
|
227 | + ) { |
|
228 | + $edit_query_args = [ |
|
229 | + 'action' => 'edit', |
|
230 | + 'post' => $event->ID(), |
|
231 | + ]; |
|
232 | + $edit_link = EE_Admin_Page::add_query_args_and_nonce($edit_query_args, EVENTS_ADMIN_URL); |
|
233 | + $actions['edit'] = '<a href="' . $edit_link . '" class="ee-aria-tooltip" ' |
|
234 | + . ' aria-label="' . esc_attr__('Edit Event', 'event_espresso') . '">' |
|
235 | + . esc_html__('Edit', 'event_espresso') |
|
236 | + . '</a>'; |
|
237 | + } |
|
238 | + if ( |
|
239 | + EE_Registry::instance()->CAP->current_user_can( |
|
240 | + 'ee_read_registrations', |
|
241 | + 'espresso_registrations_view_registration' |
|
242 | + ) |
|
243 | + && EE_Registry::instance()->CAP->current_user_can( |
|
244 | + 'ee_read_event', |
|
245 | + 'espresso_registrations_view_registration', |
|
246 | + $event->ID() |
|
247 | + ) |
|
248 | + ) { |
|
249 | + $attendees_query_args = [ |
|
250 | + 'action' => 'default', |
|
251 | + 'event_id' => $event->ID(), |
|
252 | + ]; |
|
253 | + $attendees_link = EE_Admin_Page::add_query_args_and_nonce($attendees_query_args, REG_ADMIN_URL); |
|
254 | + $actions['attendees'] = '<a href="' . $attendees_link . '" class="ee-aria-tooltip"' |
|
255 | + . ' aria-label="' . esc_attr__('View Registrations', 'event_espresso') . '">' |
|
256 | + . esc_html__('Registrations', 'event_espresso') |
|
257 | + . '</a>'; |
|
258 | + } |
|
259 | + if ( |
|
260 | + EE_Registry::instance()->CAP->current_user_can( |
|
261 | + 'ee_delete_event', |
|
262 | + 'espresso_events_trash_event', |
|
263 | + $event->ID() |
|
264 | + ) |
|
265 | + ) { |
|
266 | + $trash_event_query_args = [ |
|
267 | + 'action' => 'trash_event', |
|
268 | + 'EVT_ID' => $event->ID(), |
|
269 | + ]; |
|
270 | + $trash_event_link = EE_Admin_Page::add_query_args_and_nonce( |
|
271 | + $trash_event_query_args, |
|
272 | + EVENTS_ADMIN_URL |
|
273 | + ); |
|
274 | + } |
|
275 | + if ( |
|
276 | + EE_Registry::instance()->CAP->current_user_can( |
|
277 | + 'ee_delete_event', |
|
278 | + 'espresso_events_restore_event', |
|
279 | + $event->ID() |
|
280 | + ) |
|
281 | + ) { |
|
282 | + $restore_event_query_args = [ |
|
283 | + 'action' => 'restore_event', |
|
284 | + 'EVT_ID' => $event->ID(), |
|
285 | + ]; |
|
286 | + $restore_event_link = EE_Admin_Page::add_query_args_and_nonce( |
|
287 | + $restore_event_query_args, |
|
288 | + EVENTS_ADMIN_URL |
|
289 | + ); |
|
290 | + } |
|
291 | + if ( |
|
292 | + EE_Registry::instance()->CAP->current_user_can( |
|
293 | + 'ee_delete_event', |
|
294 | + 'espresso_events_delete_event', |
|
295 | + $event->ID() |
|
296 | + ) |
|
297 | + ) { |
|
298 | + $delete_event_query_args = [ |
|
299 | + 'action' => 'delete_event', |
|
300 | + 'EVT_ID' => $event->ID(), |
|
301 | + ]; |
|
302 | + $delete_event_link = EE_Admin_Page::add_query_args_and_nonce( |
|
303 | + $delete_event_query_args, |
|
304 | + EVENTS_ADMIN_URL |
|
305 | + ); |
|
306 | + } |
|
307 | + $view_link = get_permalink($event->ID()); |
|
308 | + $actions['view'] = '<a href="' . $view_link . '" class="ee-aria-tooltip"' |
|
309 | + . ' aria-label="' . esc_attr__('View Event', 'event_espresso') . '">' |
|
310 | + . esc_html__('View', 'event_espresso') |
|
311 | + . '</a>'; |
|
312 | + if ($event->get('status') === 'trash') { |
|
313 | + if ( |
|
314 | + EE_Registry::instance()->CAP->current_user_can( |
|
315 | + 'ee_delete_event', |
|
316 | + 'espresso_events_restore_event', |
|
317 | + $event->ID() |
|
318 | + ) |
|
319 | + ) { |
|
320 | + $actions['restore_from_trash'] = '<a href="' . $restore_event_link . '" class="ee-aria-tooltip"' |
|
321 | + . ' aria-label="' . esc_attr__('Restore from Trash', 'event_espresso') |
|
322 | + . '">' |
|
323 | + . esc_html__('Restore from Trash', 'event_espresso') |
|
324 | + . '</a>'; |
|
325 | + } |
|
326 | + if ( |
|
327 | + EE_Registry::instance()->CAP->current_user_can( |
|
328 | + 'ee_delete_event', |
|
329 | + 'espresso_events_delete_event', |
|
330 | + $event->ID() |
|
331 | + ) |
|
332 | + ) { |
|
333 | + $actions['delete'] = '<a href="' . $delete_event_link . '" class="ee-aria-tooltip"' |
|
334 | + . ' aria-label="' . esc_attr__('Delete Permanently', 'event_espresso') . '">' |
|
335 | + . esc_html__('Delete Permanently', 'event_espresso') |
|
336 | + . '</a>'; |
|
337 | + } |
|
338 | + } else { |
|
339 | + if ( |
|
340 | + EE_Registry::instance()->CAP->current_user_can( |
|
341 | + 'ee_delete_event', |
|
342 | + 'espresso_events_trash_event', |
|
343 | + $event->ID() |
|
344 | + ) |
|
345 | + ) { |
|
346 | + $actions['move to trash'] = '<a href="' . $trash_event_link . '" class="ee-aria-tooltip"' |
|
347 | + . ' aria-label="' . esc_attr__('Trash Event', 'event_espresso') . '">' |
|
348 | + . esc_html__('Trash', 'event_espresso') |
|
349 | + . '</a>'; |
|
350 | + } |
|
351 | + } |
|
352 | + return $actions; |
|
353 | + } |
|
354 | + |
|
355 | + |
|
356 | + /** |
|
357 | + * @param EE_Event $event |
|
358 | + * @return string |
|
359 | + * @throws EE_Error |
|
360 | + * @throws ReflectionException |
|
361 | + */ |
|
362 | + public function column_author(EE_Event $event): string |
|
363 | + { |
|
364 | + // user author info |
|
365 | + $event_author = get_userdata($event->wp_user()); |
|
366 | + $gravatar = get_avatar($event->wp_user(), '24'); |
|
367 | + // filter link |
|
368 | + $query_args = [ |
|
369 | + 'action' => 'default', |
|
370 | + 'EVT_wp_user' => $event->wp_user(), |
|
371 | + ]; |
|
372 | + $filter_url = EE_Admin_Page::add_query_args_and_nonce($query_args, EVENTS_ADMIN_URL); |
|
373 | + $content = '<div class="ee-layout-row ee-layout-row--fixed">'; |
|
374 | + $content .= ' <a href="' . $filter_url . '" class="ee-event-author ee-aria-tooltip"' |
|
375 | + . ' aria-label="' . esc_attr__('Click to filter events by this author.', 'event_espresso') . '">' |
|
376 | + . $gravatar . $event_author->display_name |
|
377 | + . '</a>'; |
|
378 | + $content .= '</div>'; |
|
379 | + return $this->columnContent('author', $content); |
|
380 | + } |
|
381 | + |
|
382 | + |
|
383 | + /** |
|
384 | + * @param EE_Event $event |
|
385 | + * @return string |
|
386 | + * @throws EE_Error |
|
387 | + * @throws ReflectionException |
|
388 | + */ |
|
389 | + public function column_event_category(EE_Event $event): string |
|
390 | + { |
|
391 | + $event_categories = $event->get_all_event_categories(); |
|
392 | + $content = implode( |
|
393 | + ', ', |
|
394 | + array_map( |
|
395 | + function (EE_Term $category) { |
|
396 | + return $category->name(); |
|
397 | + }, |
|
398 | + $event_categories |
|
399 | + ) |
|
400 | + ); |
|
401 | + return $this->columnContent('event_category', $content); |
|
402 | + } |
|
403 | + |
|
404 | + |
|
405 | + /** |
|
406 | + * @param EE_Event $event |
|
407 | + * @return string |
|
408 | + * @throws EE_Error |
|
409 | + * @throws ReflectionException |
|
410 | + */ |
|
411 | + public function column_venue(EE_Event $event): string |
|
412 | + { |
|
413 | + $venue = $event->get_first_related('Venue'); |
|
414 | + $content = ! empty($venue) |
|
415 | + ? $venue->name() |
|
416 | + : ''; |
|
417 | + return $this->columnContent('venue', $content); |
|
418 | + } |
|
419 | + |
|
420 | + |
|
421 | + /** |
|
422 | + * @param EE_Event $event |
|
423 | + * @return string |
|
424 | + * @throws EE_Error |
|
425 | + * @throws ReflectionException |
|
426 | + */ |
|
427 | + public function column_start_date_time(EE_Event $event): string |
|
428 | + { |
|
429 | + $content = $this->_dtt instanceof EE_Datetime |
|
430 | + ? $this->_dtt->get_i18n_datetime('DTT_EVT_start') |
|
431 | + : esc_html__('No Date was saved for this Event', 'event_espresso'); |
|
432 | + return $this->columnContent('start_date_time', $content); |
|
433 | + } |
|
434 | + |
|
435 | + |
|
436 | + /** |
|
437 | + * @param EE_Event $event |
|
438 | + * @return string |
|
439 | + * @throws EE_Error |
|
440 | + * @throws ReflectionException |
|
441 | + */ |
|
442 | + public function column_reg_begins(EE_Event $event): string |
|
443 | + { |
|
444 | + $reg_start = $event->get_ticket_with_earliest_start_time(); |
|
445 | + $content = $reg_start instanceof EE_Ticket |
|
446 | + ? $reg_start->get_i18n_datetime('TKT_start_date') |
|
447 | + : esc_html__('No Tickets have been setup for this Event', 'event_espresso'); |
|
448 | + return $this->columnContent('reg_begins', $content); |
|
449 | + } |
|
450 | + |
|
451 | + |
|
452 | + /** |
|
453 | + * @param EE_Event $event |
|
454 | + * @return string |
|
455 | + * @throws EE_Error |
|
456 | + * @throws InvalidArgumentException |
|
457 | + * @throws InvalidDataTypeException |
|
458 | + * @throws InvalidInterfaceException |
|
459 | + * @throws ReflectionException |
|
460 | + */ |
|
461 | + public function column_attendees(EE_Event $event): string |
|
462 | + { |
|
463 | + $attendees_query_args = [ |
|
464 | + 'action' => 'default', |
|
465 | + 'event_id' => $event->ID(), |
|
466 | + '_reg_status' => EEM_Registration::status_id_approved, |
|
467 | + ]; |
|
468 | + $attendees_link = EE_Admin_Page::add_query_args_and_nonce($attendees_query_args, REG_ADMIN_URL); |
|
469 | + $registered_attendees = EEM_Registration::instance()->get_event_registration_count($event->ID()); |
|
470 | + |
|
471 | + $content = EE_Registry::instance()->CAP->current_user_can( |
|
472 | + 'ee_read_event', |
|
473 | + 'espresso_registrations_view_registration', |
|
474 | + $event->ID() |
|
475 | + ) && EE_Registry::instance()->CAP->current_user_can( |
|
476 | + 'ee_read_registrations', |
|
477 | + 'espresso_registrations_view_registration' |
|
478 | + ) |
|
479 | + ? '<a href="' . $attendees_link . '">' . $registered_attendees . '</a>' |
|
480 | + : $registered_attendees; |
|
481 | + return $this->columnContent('attendees', $content, 'center'); |
|
482 | + } |
|
483 | + |
|
484 | + |
|
485 | + /** |
|
486 | + * @param EE_Event $event |
|
487 | + * @return float |
|
488 | + * @throws EE_Error |
|
489 | + * @throws InvalidArgumentException |
|
490 | + * @throws InvalidDataTypeException |
|
491 | + * @throws InvalidInterfaceException |
|
492 | + * @throws ReflectionException |
|
493 | + */ |
|
494 | + public function column_tkts_sold(EE_Event $event): float |
|
495 | + { |
|
496 | + $content = EEM_Ticket::instance()->sum([['Datetime.EVT_ID' => $event->ID()]], 'TKT_sold'); |
|
497 | + return $this->columnContent('tkts_sold', $content); |
|
498 | + } |
|
499 | + |
|
500 | + |
|
501 | + /** |
|
502 | + * @param EE_Event $event |
|
503 | + * @return string |
|
504 | + * @throws EE_Error |
|
505 | + * @throws InvalidArgumentException |
|
506 | + * @throws InvalidDataTypeException |
|
507 | + * @throws InvalidInterfaceException |
|
508 | + * @throws ReflectionException |
|
509 | + */ |
|
510 | + public function column_actions(EE_Event $event): string |
|
511 | + { |
|
512 | + // todo: remove when attendees is active |
|
513 | + if (! defined('REG_ADMIN_URL')) { |
|
514 | + define('REG_ADMIN_URL', EVENTS_ADMIN_URL); |
|
515 | + } |
|
516 | + $action_links = []; |
|
517 | + $view_link = get_permalink($event->ID()); |
|
518 | + $action_links[] = '<a href="' . $view_link . '" class="ee-aria-tooltip button button--icon-only"' |
|
519 | + . ' aria-label="' . esc_attr__('View Event', 'event_espresso') . '" target="_blank"> |
|
520 | 520 | <span class="dashicons dashicons-visibility"></span></a>'; |
521 | - if ( |
|
522 | - EE_Registry::instance()->CAP->current_user_can( |
|
523 | - 'ee_edit_event', |
|
524 | - 'espresso_events_edit', |
|
525 | - $event->ID() |
|
526 | - ) |
|
527 | - ) { |
|
528 | - $edit_query_args = [ |
|
529 | - 'action' => 'edit', |
|
530 | - 'post' => $event->ID(), |
|
531 | - ]; |
|
532 | - $edit_link = EE_Admin_Page::add_query_args_and_nonce($edit_query_args, EVENTS_ADMIN_URL); |
|
533 | - $action_links[] = '<a href="' . $edit_link . '" class="ee-aria-tooltip button button--icon-only"' |
|
534 | - . ' aria-label="' . esc_attr__('Edit Event', 'event_espresso') . '">' |
|
535 | - . '<span class="dashicons dashicons-calendar-alt"></span>' |
|
536 | - . '</a>'; |
|
537 | - } |
|
538 | - if ( |
|
539 | - EE_Registry::instance()->CAP->current_user_can( |
|
540 | - 'ee_read_registrations', |
|
541 | - 'espresso_registrations_view_registration' |
|
542 | - ) |
|
543 | - && EE_Registry::instance()->CAP->current_user_can( |
|
544 | - 'ee_read_event', |
|
545 | - 'espresso_registrations_view_registration', |
|
546 | - $event->ID() |
|
547 | - ) |
|
548 | - ) { |
|
549 | - $attendees_query_args = [ |
|
550 | - 'action' => 'default', |
|
551 | - 'event_id' => $event->ID(), |
|
552 | - ]; |
|
553 | - $attendees_link = EE_Admin_Page::add_query_args_and_nonce($attendees_query_args, REG_ADMIN_URL); |
|
554 | - $action_links[] = '<a href="' . $attendees_link . '" class="ee-aria-tooltip button button--icon-only"' |
|
555 | - . ' aria-label="' . esc_attr__('View Registrants', 'event_espresso') . '">' |
|
556 | - . '<span class="dashicons dashicons-groups"></span>' |
|
557 | - . '</a>'; |
|
558 | - } |
|
559 | - $action_links = apply_filters( |
|
560 | - 'FHEE__Events_Admin_List_Table__column_actions__action_links', |
|
561 | - $action_links, |
|
562 | - $event |
|
563 | - ); |
|
564 | - $content = $this->_action_string( |
|
565 | - implode("\n\t", $action_links), |
|
566 | - $event, |
|
567 | - 'div', |
|
568 | - 'event-overview-actions ee-list-table-actions' |
|
569 | - ); |
|
570 | - return $this->columnContent('actions', $this->actionsModalMenu($content)); |
|
571 | - } |
|
572 | - |
|
573 | - |
|
574 | - /** |
|
575 | - * Helper for adding columns conditionally |
|
576 | - * |
|
577 | - * @throws EE_Error |
|
578 | - * @throws InvalidArgumentException |
|
579 | - * @throws InvalidDataTypeException |
|
580 | - * @throws InvalidInterfaceException |
|
581 | - * @throws ReflectionException |
|
582 | - */ |
|
583 | - private function addConditionalColumns() |
|
584 | - { |
|
585 | - $event_category_count = EEM_Term::instance()->count( |
|
586 | - [['Term_Taxonomy.taxonomy' => EEM_CPT_Base::EVENT_CATEGORY_TAXONOMY]] |
|
587 | - ); |
|
588 | - if ($event_category_count === 0) { |
|
589 | - return; |
|
590 | - } |
|
591 | - $column_array = []; |
|
592 | - foreach ($this->_columns as $column => $column_label) { |
|
593 | - $column_array[ $column ] = $column_label; |
|
594 | - if ($column === 'venue') { |
|
595 | - $column_array['event_category'] = esc_html__('Event Category', 'event_espresso'); |
|
596 | - } |
|
597 | - } |
|
598 | - $this->_columns = $column_array; |
|
599 | - } |
|
521 | + if ( |
|
522 | + EE_Registry::instance()->CAP->current_user_can( |
|
523 | + 'ee_edit_event', |
|
524 | + 'espresso_events_edit', |
|
525 | + $event->ID() |
|
526 | + ) |
|
527 | + ) { |
|
528 | + $edit_query_args = [ |
|
529 | + 'action' => 'edit', |
|
530 | + 'post' => $event->ID(), |
|
531 | + ]; |
|
532 | + $edit_link = EE_Admin_Page::add_query_args_and_nonce($edit_query_args, EVENTS_ADMIN_URL); |
|
533 | + $action_links[] = '<a href="' . $edit_link . '" class="ee-aria-tooltip button button--icon-only"' |
|
534 | + . ' aria-label="' . esc_attr__('Edit Event', 'event_espresso') . '">' |
|
535 | + . '<span class="dashicons dashicons-calendar-alt"></span>' |
|
536 | + . '</a>'; |
|
537 | + } |
|
538 | + if ( |
|
539 | + EE_Registry::instance()->CAP->current_user_can( |
|
540 | + 'ee_read_registrations', |
|
541 | + 'espresso_registrations_view_registration' |
|
542 | + ) |
|
543 | + && EE_Registry::instance()->CAP->current_user_can( |
|
544 | + 'ee_read_event', |
|
545 | + 'espresso_registrations_view_registration', |
|
546 | + $event->ID() |
|
547 | + ) |
|
548 | + ) { |
|
549 | + $attendees_query_args = [ |
|
550 | + 'action' => 'default', |
|
551 | + 'event_id' => $event->ID(), |
|
552 | + ]; |
|
553 | + $attendees_link = EE_Admin_Page::add_query_args_and_nonce($attendees_query_args, REG_ADMIN_URL); |
|
554 | + $action_links[] = '<a href="' . $attendees_link . '" class="ee-aria-tooltip button button--icon-only"' |
|
555 | + . ' aria-label="' . esc_attr__('View Registrants', 'event_espresso') . '">' |
|
556 | + . '<span class="dashicons dashicons-groups"></span>' |
|
557 | + . '</a>'; |
|
558 | + } |
|
559 | + $action_links = apply_filters( |
|
560 | + 'FHEE__Events_Admin_List_Table__column_actions__action_links', |
|
561 | + $action_links, |
|
562 | + $event |
|
563 | + ); |
|
564 | + $content = $this->_action_string( |
|
565 | + implode("\n\t", $action_links), |
|
566 | + $event, |
|
567 | + 'div', |
|
568 | + 'event-overview-actions ee-list-table-actions' |
|
569 | + ); |
|
570 | + return $this->columnContent('actions', $this->actionsModalMenu($content)); |
|
571 | + } |
|
572 | + |
|
573 | + |
|
574 | + /** |
|
575 | + * Helper for adding columns conditionally |
|
576 | + * |
|
577 | + * @throws EE_Error |
|
578 | + * @throws InvalidArgumentException |
|
579 | + * @throws InvalidDataTypeException |
|
580 | + * @throws InvalidInterfaceException |
|
581 | + * @throws ReflectionException |
|
582 | + */ |
|
583 | + private function addConditionalColumns() |
|
584 | + { |
|
585 | + $event_category_count = EEM_Term::instance()->count( |
|
586 | + [['Term_Taxonomy.taxonomy' => EEM_CPT_Base::EVENT_CATEGORY_TAXONOMY]] |
|
587 | + ); |
|
588 | + if ($event_category_count === 0) { |
|
589 | + return; |
|
590 | + } |
|
591 | + $column_array = []; |
|
592 | + foreach ($this->_columns as $column => $column_label) { |
|
593 | + $column_array[ $column ] = $column_label; |
|
594 | + if ($column === 'venue') { |
|
595 | + $column_array['event_category'] = esc_html__('Event Category', 'event_espresso'); |
|
596 | + } |
|
597 | + } |
|
598 | + $this->_columns = $column_array; |
|
599 | + } |
|
600 | 600 | } |