@@ -13,128 +13,128 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class ThirdPartyPayPalApi extends PayPalApi |
| 15 | 15 | { |
| 16 | - /** |
|
| 17 | - * Partner access token used to process payments. |
|
| 18 | - * |
|
| 19 | - * @var string |
|
| 20 | - */ |
|
| 21 | - protected string $access_token = ''; |
|
| 16 | + /** |
|
| 17 | + * Partner access token used to process payments. |
|
| 18 | + * |
|
| 19 | + * @var string |
|
| 20 | + */ |
|
| 21 | + protected string $access_token = ''; |
|
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * Partner Client ID. |
|
| 25 | - * |
|
| 26 | - * @var string |
|
| 27 | - */ |
|
| 28 | - protected string $partner_client_id = ''; |
|
| 23 | + /** |
|
| 24 | + * Partner Client ID. |
|
| 25 | + * |
|
| 26 | + * @var string |
|
| 27 | + */ |
|
| 28 | + protected string $partner_client_id = ''; |
|
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * Merchant (seller) merchant ID. |
|
| 32 | - * |
|
| 33 | - * @var string |
|
| 34 | - */ |
|
| 35 | - protected string $payer_id = ''; |
|
| 30 | + /** |
|
| 31 | + * Merchant (seller) merchant ID. |
|
| 32 | + * |
|
| 33 | + * @var string |
|
| 34 | + */ |
|
| 35 | + protected string $payer_id = ''; |
|
| 36 | 36 | |
| 37 | - /** |
|
| 38 | - * BN Code. Partner-Attribution-Id. |
|
| 39 | - * |
|
| 40 | - * @var string |
|
| 41 | - */ |
|
| 42 | - protected string $bn_code = ''; |
|
| 37 | + /** |
|
| 38 | + * BN Code. Partner-Attribution-Id. |
|
| 39 | + * |
|
| 40 | + * @var string |
|
| 41 | + */ |
|
| 42 | + protected string $bn_code = ''; |
|
| 43 | 43 | |
| 44 | 44 | |
| 45 | - /** |
|
| 46 | - * @param string $access_token |
|
| 47 | - * @param string $bn_code |
|
| 48 | - * @param string $partner_client_id |
|
| 49 | - * @param string $payer_id |
|
| 50 | - * @param bool $sandbox_mode |
|
| 51 | - */ |
|
| 52 | - public function __construct( |
|
| 53 | - string $access_token, |
|
| 54 | - string $bn_code, |
|
| 55 | - string $partner_client_id = '', |
|
| 56 | - string $payer_id = '', |
|
| 57 | - bool $sandbox_mode = true |
|
| 58 | - ) { |
|
| 59 | - parent::__construct($sandbox_mode); |
|
| 60 | - $this->access_token = $access_token; |
|
| 61 | - $this->partner_client_id = $partner_client_id; |
|
| 62 | - $this->payer_id = $payer_id; |
|
| 63 | - $this->bn_code = $bn_code; |
|
| 64 | - } |
|
| 45 | + /** |
|
| 46 | + * @param string $access_token |
|
| 47 | + * @param string $bn_code |
|
| 48 | + * @param string $partner_client_id |
|
| 49 | + * @param string $payer_id |
|
| 50 | + * @param bool $sandbox_mode |
|
| 51 | + */ |
|
| 52 | + public function __construct( |
|
| 53 | + string $access_token, |
|
| 54 | + string $bn_code, |
|
| 55 | + string $partner_client_id = '', |
|
| 56 | + string $payer_id = '', |
|
| 57 | + bool $sandbox_mode = true |
|
| 58 | + ) { |
|
| 59 | + parent::__construct($sandbox_mode); |
|
| 60 | + $this->access_token = $access_token; |
|
| 61 | + $this->partner_client_id = $partner_client_id; |
|
| 62 | + $this->payer_id = $payer_id; |
|
| 63 | + $this->bn_code = $bn_code; |
|
| 64 | + } |
|
| 65 | 65 | |
| 66 | 66 | |
| 67 | - /** |
|
| 68 | - * Send an API request. |
|
| 69 | - * |
|
| 70 | - * @param array $body_parameters |
|
| 71 | - * @param string $endpoint |
|
| 72 | - * @param string $method |
|
| 73 | - * @param array $headers |
|
| 74 | - * @return Object|array |
|
| 75 | - */ |
|
| 76 | - public function sendRequest(array $body_parameters, string $endpoint, string $method = 'POST', array $headers = []) |
|
| 77 | - { |
|
| 78 | - $request_parameters = $this->getRequestParameters($body_parameters, $method, $headers); |
|
| 79 | - return $this->request($endpoint, $request_parameters); |
|
| 80 | - } |
|
| 67 | + /** |
|
| 68 | + * Send an API request. |
|
| 69 | + * |
|
| 70 | + * @param array $body_parameters |
|
| 71 | + * @param string $endpoint |
|
| 72 | + * @param string $method |
|
| 73 | + * @param array $headers |
|
| 74 | + * @return Object|array |
|
| 75 | + */ |
|
| 76 | + public function sendRequest(array $body_parameters, string $endpoint, string $method = 'POST', array $headers = []) |
|
| 77 | + { |
|
| 78 | + $request_parameters = $this->getRequestParameters($body_parameters, $method, $headers); |
|
| 79 | + return $this->request($endpoint, $request_parameters); |
|
| 80 | + } |
|
| 81 | 81 | |
| 82 | 82 | |
| 83 | - /** |
|
| 84 | - * Build the request parameters. |
|
| 85 | - * |
|
| 86 | - * @param array $body_parameters |
|
| 87 | - * @param string $method |
|
| 88 | - * @param array $headers |
|
| 89 | - * @return array |
|
| 90 | - */ |
|
| 91 | - private function getRequestParameters(array $body_parameters, string $method, array $headers): array |
|
| 92 | - { |
|
| 93 | - $request_parameters = ['method' => $method]; |
|
| 94 | - $default_headers = [ |
|
| 95 | - 'User-Agent' => sanitize_text_field($_SERVER['HTTP_USER_AGENT']), |
|
| 96 | - 'PayPal-Partner-Attribution-Id' => $this->bnCode(), |
|
| 97 | - 'Content-Type' => 'application/json', |
|
| 98 | - 'Authorization' => 'Bearer ' . $this->accessToken(), |
|
| 99 | - ]; |
|
| 100 | - // If we have merchant credentials then we are onboard and can do requests on behalf of the seller. |
|
| 101 | - if ($this->partner_client_id && $this->payer_id) { |
|
| 102 | - $assertion1 = base64_encode(json_encode(['alg' => 'none'], JSON_HEX_APOS)); |
|
| 103 | - $assertion2 = base64_encode( |
|
| 104 | - json_encode( |
|
| 105 | - [ |
|
| 106 | - 'iss' => $this->partner_client_id, |
|
| 107 | - 'payer_id' => $this->payer_id, |
|
| 108 | - ], |
|
| 109 | - JSON_HEX_APOS |
|
| 110 | - ) |
|
| 111 | - ); |
|
| 112 | - // now concatenate the two assertions and add to the headers. |
|
| 113 | - $default_headers['PayPal-Auth-Assertion'] = "$assertion1.$assertion2."; |
|
| 114 | - } |
|
| 115 | - $request_parameters['headers'] = $headers + $default_headers; |
|
| 116 | - // Add body if this is a POST request. |
|
| 117 | - if ($body_parameters && ($method === 'POST' || $method === 'PUT')) { |
|
| 118 | - $request_parameters['body'] = json_encode($body_parameters); |
|
| 119 | - } |
|
| 120 | - return $request_parameters; |
|
| 121 | - } |
|
| 83 | + /** |
|
| 84 | + * Build the request parameters. |
|
| 85 | + * |
|
| 86 | + * @param array $body_parameters |
|
| 87 | + * @param string $method |
|
| 88 | + * @param array $headers |
|
| 89 | + * @return array |
|
| 90 | + */ |
|
| 91 | + private function getRequestParameters(array $body_parameters, string $method, array $headers): array |
|
| 92 | + { |
|
| 93 | + $request_parameters = ['method' => $method]; |
|
| 94 | + $default_headers = [ |
|
| 95 | + 'User-Agent' => sanitize_text_field($_SERVER['HTTP_USER_AGENT']), |
|
| 96 | + 'PayPal-Partner-Attribution-Id' => $this->bnCode(), |
|
| 97 | + 'Content-Type' => 'application/json', |
|
| 98 | + 'Authorization' => 'Bearer ' . $this->accessToken(), |
|
| 99 | + ]; |
|
| 100 | + // If we have merchant credentials then we are onboard and can do requests on behalf of the seller. |
|
| 101 | + if ($this->partner_client_id && $this->payer_id) { |
|
| 102 | + $assertion1 = base64_encode(json_encode(['alg' => 'none'], JSON_HEX_APOS)); |
|
| 103 | + $assertion2 = base64_encode( |
|
| 104 | + json_encode( |
|
| 105 | + [ |
|
| 106 | + 'iss' => $this->partner_client_id, |
|
| 107 | + 'payer_id' => $this->payer_id, |
|
| 108 | + ], |
|
| 109 | + JSON_HEX_APOS |
|
| 110 | + ) |
|
| 111 | + ); |
|
| 112 | + // now concatenate the two assertions and add to the headers. |
|
| 113 | + $default_headers['PayPal-Auth-Assertion'] = "$assertion1.$assertion2."; |
|
| 114 | + } |
|
| 115 | + $request_parameters['headers'] = $headers + $default_headers; |
|
| 116 | + // Add body if this is a POST request. |
|
| 117 | + if ($body_parameters && ($method === 'POST' || $method === 'PUT')) { |
|
| 118 | + $request_parameters['body'] = json_encode($body_parameters); |
|
| 119 | + } |
|
| 120 | + return $request_parameters; |
|
| 121 | + } |
|
| 122 | 122 | |
| 123 | 123 | |
| 124 | - /** |
|
| 125 | - * @return string |
|
| 126 | - */ |
|
| 127 | - public function accessToken(): string |
|
| 128 | - { |
|
| 129 | - return $this->access_token; |
|
| 130 | - } |
|
| 124 | + /** |
|
| 125 | + * @return string |
|
| 126 | + */ |
|
| 127 | + public function accessToken(): string |
|
| 128 | + { |
|
| 129 | + return $this->access_token; |
|
| 130 | + } |
|
| 131 | 131 | |
| 132 | 132 | |
| 133 | - /** |
|
| 134 | - * @return string |
|
| 135 | - */ |
|
| 136 | - public function bnCode(): string |
|
| 137 | - { |
|
| 138 | - return $this->bn_code; |
|
| 139 | - } |
|
| 133 | + /** |
|
| 134 | + * @return string |
|
| 135 | + */ |
|
| 136 | + public function bnCode(): string |
|
| 137 | + { |
|
| 138 | + return $this->bn_code; |
|
| 139 | + } |
|
| 140 | 140 | } |
@@ -95,7 +95,7 @@ |
||
| 95 | 95 | 'User-Agent' => sanitize_text_field($_SERVER['HTTP_USER_AGENT']), |
| 96 | 96 | 'PayPal-Partner-Attribution-Id' => $this->bnCode(), |
| 97 | 97 | 'Content-Type' => 'application/json', |
| 98 | - 'Authorization' => 'Bearer ' . $this->accessToken(), |
|
| 98 | + 'Authorization' => 'Bearer '.$this->accessToken(), |
|
| 99 | 99 | ]; |
| 100 | 100 | // If we have merchant credentials then we are onboard and can do requests on behalf of the seller. |
| 101 | 101 | if ($this->partner_client_id && $this->payer_id) { |
@@ -30,301 +30,301 @@ |
||
| 30 | 30 | */ |
| 31 | 31 | class CreateOrder extends OrdersApi |
| 32 | 32 | { |
| 33 | - /** |
|
| 34 | - * Line items total. |
|
| 35 | - * |
|
| 36 | - * @var float |
|
| 37 | - */ |
|
| 38 | - protected float $items_total = 0.0; |
|
| 33 | + /** |
|
| 34 | + * Line items total. |
|
| 35 | + * |
|
| 36 | + * @var float |
|
| 37 | + */ |
|
| 38 | + protected float $items_total = 0.0; |
|
| 39 | 39 | |
| 40 | - /** |
|
| 41 | - * Promotions total. |
|
| 42 | - * |
|
| 43 | - * @var float |
|
| 44 | - */ |
|
| 45 | - protected float $promos_total = 0.0; |
|
| 40 | + /** |
|
| 41 | + * Promotions total. |
|
| 42 | + * |
|
| 43 | + * @var float |
|
| 44 | + */ |
|
| 45 | + protected float $promos_total = 0.0; |
|
| 46 | 46 | |
| 47 | - /** |
|
| 48 | - * Tax total. |
|
| 49 | - * |
|
| 50 | - * @var float |
|
| 51 | - */ |
|
| 52 | - protected float $tax_total = 0.0; |
|
| 47 | + /** |
|
| 48 | + * Tax total. |
|
| 49 | + * |
|
| 50 | + * @var float |
|
| 51 | + */ |
|
| 52 | + protected float $tax_total = 0.0; |
|
| 53 | 53 | |
| 54 | - /** |
|
| 55 | - * Currency. |
|
| 56 | - * |
|
| 57 | - * @var string |
|
| 58 | - */ |
|
| 59 | - protected string $currency_code; |
|
| 54 | + /** |
|
| 55 | + * Currency. |
|
| 56 | + * |
|
| 57 | + * @var string |
|
| 58 | + */ |
|
| 59 | + protected string $currency_code; |
|
| 60 | 60 | |
| 61 | - /** |
|
| 62 | - * Billing info. |
|
| 63 | - * |
|
| 64 | - * @var array |
|
| 65 | - */ |
|
| 66 | - protected array $billing_info; |
|
| 61 | + /** |
|
| 62 | + * Billing info. |
|
| 63 | + * |
|
| 64 | + * @var array |
|
| 65 | + */ |
|
| 66 | + protected array $billing_info; |
|
| 67 | 67 | |
| 68 | - /** |
|
| 69 | - * Transaction this order is for. |
|
| 70 | - * |
|
| 71 | - * @var EE_Transaction |
|
| 72 | - */ |
|
| 73 | - protected EE_Transaction $transaction; |
|
| 68 | + /** |
|
| 69 | + * Transaction this order is for. |
|
| 70 | + * |
|
| 71 | + * @var EE_Transaction |
|
| 72 | + */ |
|
| 73 | + protected EE_Transaction $transaction; |
|
| 74 | 74 | |
| 75 | - private FeatureFlags $feature; |
|
| 75 | + private FeatureFlags $feature; |
|
| 76 | 76 | |
| 77 | 77 | |
| 78 | - /** |
|
| 79 | - * CreateOrder constructor. |
|
| 80 | - * |
|
| 81 | - * @param PayPalApi $api |
|
| 82 | - * @param EE_Transaction $transaction |
|
| 83 | - * @param array $billing_info |
|
| 84 | - * @param FeatureFlags $feature |
|
| 85 | - */ |
|
| 86 | - public function __construct(PayPalApi $api, EE_Transaction $transaction, array $billing_info, FeatureFlags $feature) |
|
| 87 | - { |
|
| 88 | - parent::__construct($api); |
|
| 89 | - $this->transaction = $transaction; |
|
| 90 | - $this->feature = $feature; |
|
| 91 | - $this->currency_code = CurrencyManager::currencyCode(); |
|
| 92 | - $this->sanitizeRequestParameters($billing_info); |
|
| 93 | - } |
|
| 78 | + /** |
|
| 79 | + * CreateOrder constructor. |
|
| 80 | + * |
|
| 81 | + * @param PayPalApi $api |
|
| 82 | + * @param EE_Transaction $transaction |
|
| 83 | + * @param array $billing_info |
|
| 84 | + * @param FeatureFlags $feature |
|
| 85 | + */ |
|
| 86 | + public function __construct(PayPalApi $api, EE_Transaction $transaction, array $billing_info, FeatureFlags $feature) |
|
| 87 | + { |
|
| 88 | + parent::__construct($api); |
|
| 89 | + $this->transaction = $transaction; |
|
| 90 | + $this->feature = $feature; |
|
| 91 | + $this->currency_code = CurrencyManager::currencyCode(); |
|
| 92 | + $this->sanitizeRequestParameters($billing_info); |
|
| 93 | + } |
|
| 94 | 94 | |
| 95 | 95 | |
| 96 | - /** |
|
| 97 | - * Sanitize the array of billing form data. |
|
| 98 | - * |
|
| 99 | - * @param array $billing_info |
|
| 100 | - * @return void |
|
| 101 | - */ |
|
| 102 | - public function sanitizeRequestParameters(array $billing_info): void |
|
| 103 | - { |
|
| 104 | - $sanitizer = new RequestSanitizer(new Basic()); |
|
| 105 | - foreach ($billing_info as $item => $value) { |
|
| 106 | - $this->billing_info[ $item ] = $sanitizer->clean($value); |
|
| 107 | - } |
|
| 108 | - } |
|
| 96 | + /** |
|
| 97 | + * Sanitize the array of billing form data. |
|
| 98 | + * |
|
| 99 | + * @param array $billing_info |
|
| 100 | + * @return void |
|
| 101 | + */ |
|
| 102 | + public function sanitizeRequestParameters(array $billing_info): void |
|
| 103 | + { |
|
| 104 | + $sanitizer = new RequestSanitizer(new Basic()); |
|
| 105 | + foreach ($billing_info as $item => $value) { |
|
| 106 | + $this->billing_info[ $item ] = $sanitizer->clean($value); |
|
| 107 | + } |
|
| 108 | + } |
|
| 109 | 109 | |
| 110 | 110 | |
| 111 | - /** |
|
| 112 | - * Create PayPal Order. |
|
| 113 | - * |
|
| 114 | - * @return array |
|
| 115 | - * @throws EE_Error |
|
| 116 | - * @throws ReflectionException |
|
| 117 | - */ |
|
| 118 | - public function create(): array |
|
| 119 | - { |
|
| 120 | - $order_parameters = $this->getParameters(); |
|
| 121 | - // Create Order request. |
|
| 122 | - $create_response = $this->api->sendRequest($order_parameters, $this->request_url); |
|
| 123 | - return $this->validateOrder($create_response, $order_parameters); |
|
| 124 | - } |
|
| 111 | + /** |
|
| 112 | + * Create PayPal Order. |
|
| 113 | + * |
|
| 114 | + * @return array |
|
| 115 | + * @throws EE_Error |
|
| 116 | + * @throws ReflectionException |
|
| 117 | + */ |
|
| 118 | + public function create(): array |
|
| 119 | + { |
|
| 120 | + $order_parameters = $this->getParameters(); |
|
| 121 | + // Create Order request. |
|
| 122 | + $create_response = $this->api->sendRequest($order_parameters, $this->request_url); |
|
| 123 | + return $this->validateOrder($create_response, $order_parameters); |
|
| 124 | + } |
|
| 125 | 125 | |
| 126 | 126 | |
| 127 | - /** |
|
| 128 | - * Form order parameters. |
|
| 129 | - * |
|
| 130 | - * @return array |
|
| 131 | - * @throws EE_Error |
|
| 132 | - * @throws ReflectionException |
|
| 133 | - * @throws Exception |
|
| 134 | - */ |
|
| 135 | - protected function getParameters(): array |
|
| 136 | - { |
|
| 137 | - $registrant = $this->transaction->primary_registration(); |
|
| 138 | - $attendee = $registrant->attendee(); |
|
| 139 | - $event = $registrant->event(); |
|
| 140 | - $description = $event->name() ?: sprintf( |
|
| 141 | - esc_html__('Tickets for an event at %1$s', 'event_espresso'), |
|
| 142 | - get_bloginfo('name') |
|
| 143 | - ); |
|
| 144 | - $parameters = [ |
|
| 145 | - 'intent' => 'CAPTURE', |
|
| 146 | - 'purchase_units' => [ |
|
| 147 | - [ |
|
| 148 | - 'custom_id' => $this->transaction->ID(), |
|
| 149 | - 'description' => substr(wp_strip_all_tags($description), 0, 125), |
|
| 150 | - 'items' => $this->getLineItems(), |
|
| 151 | - 'amount' => [ |
|
| 152 | - 'value' => $this->transaction->remaining(), |
|
| 153 | - 'currency_code' => $this->currency_code, |
|
| 154 | - 'breakdown' => $this->getBreakdown(), |
|
| 155 | - ], |
|
| 156 | - ], |
|
| 157 | - ], |
|
| 158 | - 'application_context' => [ |
|
| 159 | - 'shipping_preference' => 'NO_SHIPPING', |
|
| 160 | - 'user_action' => 'PAY_NOW', |
|
| 161 | - ], |
|
| 162 | - 'payer' => [ |
|
| 163 | - 'email_address' => $attendee->email(), |
|
| 164 | - 'name' => [ |
|
| 165 | - 'given_name' => $attendee->fname(), |
|
| 166 | - 'surname' => $attendee->lname(), |
|
| 127 | + /** |
|
| 128 | + * Form order parameters. |
|
| 129 | + * |
|
| 130 | + * @return array |
|
| 131 | + * @throws EE_Error |
|
| 132 | + * @throws ReflectionException |
|
| 133 | + * @throws Exception |
|
| 134 | + */ |
|
| 135 | + protected function getParameters(): array |
|
| 136 | + { |
|
| 137 | + $registrant = $this->transaction->primary_registration(); |
|
| 138 | + $attendee = $registrant->attendee(); |
|
| 139 | + $event = $registrant->event(); |
|
| 140 | + $description = $event->name() ?: sprintf( |
|
| 141 | + esc_html__('Tickets for an event at %1$s', 'event_espresso'), |
|
| 142 | + get_bloginfo('name') |
|
| 143 | + ); |
|
| 144 | + $parameters = [ |
|
| 145 | + 'intent' => 'CAPTURE', |
|
| 146 | + 'purchase_units' => [ |
|
| 147 | + [ |
|
| 148 | + 'custom_id' => $this->transaction->ID(), |
|
| 149 | + 'description' => substr(wp_strip_all_tags($description), 0, 125), |
|
| 150 | + 'items' => $this->getLineItems(), |
|
| 151 | + 'amount' => [ |
|
| 152 | + 'value' => $this->transaction->remaining(), |
|
| 153 | + 'currency_code' => $this->currency_code, |
|
| 154 | + 'breakdown' => $this->getBreakdown(), |
|
| 155 | + ], |
|
| 156 | + ], |
|
| 157 | + ], |
|
| 158 | + 'application_context' => [ |
|
| 159 | + 'shipping_preference' => 'NO_SHIPPING', |
|
| 160 | + 'user_action' => 'PAY_NOW', |
|
| 161 | + ], |
|
| 162 | + 'payer' => [ |
|
| 163 | + 'email_address' => $attendee->email(), |
|
| 164 | + 'name' => [ |
|
| 165 | + 'given_name' => $attendee->fname(), |
|
| 166 | + 'surname' => $attendee->lname(), |
|
| 167 | 167 | |
| 168 | - ], |
|
| 169 | - ], |
|
| 170 | - ]; |
|
| 171 | - // Do we have the permissions for the fees ? |
|
| 172 | - $scopes = PayPalExtraMetaManager::getPmOption( |
|
| 173 | - $this->transaction->payment_method(), |
|
| 174 | - Domain::META_KEY_AUTHORIZED_SCOPES |
|
| 175 | - ); |
|
| 176 | - if ( |
|
| 177 | - ( |
|
| 178 | - (defined('EE_PPC_USE_PAYMENT_FEES') && EE_PPC_USE_PAYMENT_FEES) |
|
| 179 | - || (! defined('EE_PPC_USE_PAYMENT_FEES') |
|
| 180 | - && $this->feature->allowed(FeatureFlag::USE_PAYMENT_PROCESSOR_FEES) |
|
| 181 | - ) |
|
| 182 | - ) |
|
| 183 | - && ! empty($scopes) && in_array('partnerfee', $scopes) |
|
| 184 | - ) { |
|
| 185 | - $payment_fees = LoaderFactory::getShared(PartnerPaymentFees::class); |
|
| 186 | - $parameters['purchase_units'][0]['payment_instruction'] = [ |
|
| 187 | - 'platform_fees' => [ |
|
| 188 | - [ |
|
| 189 | - 'amount' => [ |
|
| 190 | - 'value' => (string) $payment_fees->getPartnerFee($this->transaction), |
|
| 191 | - 'currency_code' => $this->currency_code, |
|
| 192 | - ], |
|
| 193 | - ], |
|
| 194 | - ] |
|
| 195 | - ]; |
|
| 196 | - } |
|
| 197 | - return $parameters; |
|
| 198 | - } |
|
| 168 | + ], |
|
| 169 | + ], |
|
| 170 | + ]; |
|
| 171 | + // Do we have the permissions for the fees ? |
|
| 172 | + $scopes = PayPalExtraMetaManager::getPmOption( |
|
| 173 | + $this->transaction->payment_method(), |
|
| 174 | + Domain::META_KEY_AUTHORIZED_SCOPES |
|
| 175 | + ); |
|
| 176 | + if ( |
|
| 177 | + ( |
|
| 178 | + (defined('EE_PPC_USE_PAYMENT_FEES') && EE_PPC_USE_PAYMENT_FEES) |
|
| 179 | + || (! defined('EE_PPC_USE_PAYMENT_FEES') |
|
| 180 | + && $this->feature->allowed(FeatureFlag::USE_PAYMENT_PROCESSOR_FEES) |
|
| 181 | + ) |
|
| 182 | + ) |
|
| 183 | + && ! empty($scopes) && in_array('partnerfee', $scopes) |
|
| 184 | + ) { |
|
| 185 | + $payment_fees = LoaderFactory::getShared(PartnerPaymentFees::class); |
|
| 186 | + $parameters['purchase_units'][0]['payment_instruction'] = [ |
|
| 187 | + 'platform_fees' => [ |
|
| 188 | + [ |
|
| 189 | + 'amount' => [ |
|
| 190 | + 'value' => (string) $payment_fees->getPartnerFee($this->transaction), |
|
| 191 | + 'currency_code' => $this->currency_code, |
|
| 192 | + ], |
|
| 193 | + ], |
|
| 194 | + ] |
|
| 195 | + ]; |
|
| 196 | + } |
|
| 197 | + return $parameters; |
|
| 198 | + } |
|
| 199 | 199 | |
| 200 | 200 | |
| 201 | - /** |
|
| 202 | - * Itemize the payment. List all the line items, discounts and taxes. |
|
| 203 | - * |
|
| 204 | - * @return array |
|
| 205 | - * @throws EE_Error|ReflectionException |
|
| 206 | - */ |
|
| 207 | - protected function getLineItems(): array |
|
| 208 | - { |
|
| 209 | - // Order line items. |
|
| 210 | - $line_items = []; |
|
| 211 | - $event_line_items = $this->transaction->items_purchased(); |
|
| 212 | - // List actual line items. |
|
| 213 | - foreach ($event_line_items as $line_item) { |
|
| 214 | - if ( |
|
| 215 | - $line_item instanceof EE_Line_Item |
|
| 216 | - && $line_item->OBJ_type() !== 'Promotion' |
|
| 217 | - && $line_item->quantity() > 0 |
|
| 218 | - ) { |
|
| 219 | - $item_money = $line_item->unit_price(); |
|
| 220 | - $li_description = $line_item->desc() ?? esc_html__('Event Ticket', 'event_espresso'); |
|
| 221 | - $line_items [] = [ |
|
| 222 | - 'name' => substr(wp_strip_all_tags($line_item->name()), 0, 126), |
|
| 223 | - 'quantity' => $line_item->quantity(), |
|
| 224 | - 'description' => substr(wp_strip_all_tags($li_description), 0, 125), |
|
| 225 | - 'unit_amount' => [ |
|
| 226 | - 'currency_code' => $this->currency_code, |
|
| 227 | - 'value' => $item_money, |
|
| 228 | - ], |
|
| 229 | - 'category' => 'DIGITAL_GOODS', |
|
| 230 | - ]; |
|
| 231 | - // Line item total. |
|
| 232 | - $this->items_total += $line_item->pretaxTotal(); |
|
| 233 | - } elseif ($line_item->OBJ_type() === 'Promotion' && $line_item->quantity() > 0) { |
|
| 234 | - // Promotions total. |
|
| 235 | - $this->promos_total += $line_item->total(); |
|
| 236 | - } |
|
| 237 | - } |
|
| 238 | - // Make sure we have an absolute number with only two decimal laces. |
|
| 239 | - $this->items_total = CurrencyManager::normalizeValue($this->items_total); |
|
| 240 | - $this->promos_total = CurrencyManager::normalizeValue($this->promos_total); |
|
| 241 | - // If this is a partial payment, apply the paid amount as a promo. |
|
| 242 | - if ($this->transaction->paid() > 0) { |
|
| 243 | - $this->promos_total += CurrencyManager::normalizeValue($this->transaction->paid()); |
|
| 244 | - } |
|
| 245 | - $this->countTaxTotal(); |
|
| 246 | - return $line_items; |
|
| 247 | - } |
|
| 201 | + /** |
|
| 202 | + * Itemize the payment. List all the line items, discounts and taxes. |
|
| 203 | + * |
|
| 204 | + * @return array |
|
| 205 | + * @throws EE_Error|ReflectionException |
|
| 206 | + */ |
|
| 207 | + protected function getLineItems(): array |
|
| 208 | + { |
|
| 209 | + // Order line items. |
|
| 210 | + $line_items = []; |
|
| 211 | + $event_line_items = $this->transaction->items_purchased(); |
|
| 212 | + // List actual line items. |
|
| 213 | + foreach ($event_line_items as $line_item) { |
|
| 214 | + if ( |
|
| 215 | + $line_item instanceof EE_Line_Item |
|
| 216 | + && $line_item->OBJ_type() !== 'Promotion' |
|
| 217 | + && $line_item->quantity() > 0 |
|
| 218 | + ) { |
|
| 219 | + $item_money = $line_item->unit_price(); |
|
| 220 | + $li_description = $line_item->desc() ?? esc_html__('Event Ticket', 'event_espresso'); |
|
| 221 | + $line_items [] = [ |
|
| 222 | + 'name' => substr(wp_strip_all_tags($line_item->name()), 0, 126), |
|
| 223 | + 'quantity' => $line_item->quantity(), |
|
| 224 | + 'description' => substr(wp_strip_all_tags($li_description), 0, 125), |
|
| 225 | + 'unit_amount' => [ |
|
| 226 | + 'currency_code' => $this->currency_code, |
|
| 227 | + 'value' => $item_money, |
|
| 228 | + ], |
|
| 229 | + 'category' => 'DIGITAL_GOODS', |
|
| 230 | + ]; |
|
| 231 | + // Line item total. |
|
| 232 | + $this->items_total += $line_item->pretaxTotal(); |
|
| 233 | + } elseif ($line_item->OBJ_type() === 'Promotion' && $line_item->quantity() > 0) { |
|
| 234 | + // Promotions total. |
|
| 235 | + $this->promos_total += $line_item->total(); |
|
| 236 | + } |
|
| 237 | + } |
|
| 238 | + // Make sure we have an absolute number with only two decimal laces. |
|
| 239 | + $this->items_total = CurrencyManager::normalizeValue($this->items_total); |
|
| 240 | + $this->promos_total = CurrencyManager::normalizeValue($this->promos_total); |
|
| 241 | + // If this is a partial payment, apply the paid amount as a promo. |
|
| 242 | + if ($this->transaction->paid() > 0) { |
|
| 243 | + $this->promos_total += CurrencyManager::normalizeValue($this->transaction->paid()); |
|
| 244 | + } |
|
| 245 | + $this->countTaxTotal(); |
|
| 246 | + return $line_items; |
|
| 247 | + } |
|
| 248 | 248 | |
| 249 | 249 | |
| 250 | - /** |
|
| 251 | - * Count the tax total. |
|
| 252 | - * |
|
| 253 | - * @return void |
|
| 254 | - * @throws EE_Error|ReflectionException |
|
| 255 | - */ |
|
| 256 | - protected function countTaxTotal(): void |
|
| 257 | - { |
|
| 258 | - // List taxes. |
|
| 259 | - $this->tax_total = 0.0; |
|
| 260 | - $tax_items = $this->transaction->tax_items(); |
|
| 261 | - foreach ($tax_items as $tax_item) { |
|
| 262 | - $this->tax_total += $tax_item->total(); |
|
| 263 | - } |
|
| 264 | - $this->tax_total = CurrencyManager::normalizeValue($this->tax_total); |
|
| 265 | - } |
|
| 250 | + /** |
|
| 251 | + * Count the tax total. |
|
| 252 | + * |
|
| 253 | + * @return void |
|
| 254 | + * @throws EE_Error|ReflectionException |
|
| 255 | + */ |
|
| 256 | + protected function countTaxTotal(): void |
|
| 257 | + { |
|
| 258 | + // List taxes. |
|
| 259 | + $this->tax_total = 0.0; |
|
| 260 | + $tax_items = $this->transaction->tax_items(); |
|
| 261 | + foreach ($tax_items as $tax_item) { |
|
| 262 | + $this->tax_total += $tax_item->total(); |
|
| 263 | + } |
|
| 264 | + $this->tax_total = CurrencyManager::normalizeValue($this->tax_total); |
|
| 265 | + } |
|
| 266 | 266 | |
| 267 | 267 | |
| 268 | - /** |
|
| 269 | - * Itemize the payment the breakdown list. |
|
| 270 | - * |
|
| 271 | - * @return array |
|
| 272 | - */ |
|
| 273 | - protected function getBreakdown(): array |
|
| 274 | - { |
|
| 275 | - $breakdown['item_total'] = [ |
|
| 276 | - 'currency_code' => $this->currency_code, |
|
| 277 | - 'value' => $this->items_total, |
|
| 278 | - ]; |
|
| 279 | - $breakdown['tax_total'] = [ |
|
| 280 | - 'currency_code' => $this->currency_code, |
|
| 281 | - 'value' => $this->tax_total, |
|
| 282 | - ]; |
|
| 283 | - $breakdown['discount'] = [ |
|
| 284 | - 'currency_code' => $this->currency_code, |
|
| 285 | - 'value' => abs($this->promos_total), |
|
| 286 | - ]; |
|
| 287 | - return $breakdown; |
|
| 288 | - } |
|
| 268 | + /** |
|
| 269 | + * Itemize the payment the breakdown list. |
|
| 270 | + * |
|
| 271 | + * @return array |
|
| 272 | + */ |
|
| 273 | + protected function getBreakdown(): array |
|
| 274 | + { |
|
| 275 | + $breakdown['item_total'] = [ |
|
| 276 | + 'currency_code' => $this->currency_code, |
|
| 277 | + 'value' => $this->items_total, |
|
| 278 | + ]; |
|
| 279 | + $breakdown['tax_total'] = [ |
|
| 280 | + 'currency_code' => $this->currency_code, |
|
| 281 | + 'value' => $this->tax_total, |
|
| 282 | + ]; |
|
| 283 | + $breakdown['discount'] = [ |
|
| 284 | + 'currency_code' => $this->currency_code, |
|
| 285 | + 'value' => abs($this->promos_total), |
|
| 286 | + ]; |
|
| 287 | + return $breakdown; |
|
| 288 | + } |
|
| 289 | 289 | |
| 290 | 290 | |
| 291 | - /** |
|
| 292 | - * Makes sure that we have received an Order back from the API call. |
|
| 293 | - * |
|
| 294 | - * @param $response |
|
| 295 | - * @param $parameters |
|
| 296 | - * @return array |
|
| 297 | - * @throws EE_Error |
|
| 298 | - * @throws ReflectionException |
|
| 299 | - */ |
|
| 300 | - public function validateOrder($response, $parameters): array |
|
| 301 | - { |
|
| 302 | - PayPalLogger::errorLog( |
|
| 303 | - esc_html__('Validating Order Create:', 'event_espresso'), |
|
| 304 | - [$this->request_url, $response], |
|
| 305 | - $this->transaction->payment_method(), |
|
| 306 | - false, |
|
| 307 | - $this->transaction |
|
| 308 | - ); |
|
| 309 | - if (! empty($response['error'])) { |
|
| 310 | - return $response; |
|
| 311 | - } |
|
| 312 | - if (! isset($response['id'])) { |
|
| 313 | - $message = esc_html__('Unexpected response. Unable to find the order.', 'event_espresso'); |
|
| 314 | - try { |
|
| 315 | - PayPalLogger::errorLog( |
|
| 316 | - $message, |
|
| 317 | - [$this->request_url, $parameters, $response], |
|
| 318 | - $this->transaction->payment_method() |
|
| 319 | - ); |
|
| 320 | - } catch (EE_Error | ReflectionException $e) { |
|
| 321 | - // Just continue. |
|
| 322 | - } |
|
| 323 | - return [ |
|
| 324 | - 'error' => $response['error'] ?? 'missing_order', |
|
| 325 | - 'message' => $response['message'] ?? $message, |
|
| 326 | - ]; |
|
| 327 | - } |
|
| 328 | - return $response; |
|
| 329 | - } |
|
| 291 | + /** |
|
| 292 | + * Makes sure that we have received an Order back from the API call. |
|
| 293 | + * |
|
| 294 | + * @param $response |
|
| 295 | + * @param $parameters |
|
| 296 | + * @return array |
|
| 297 | + * @throws EE_Error |
|
| 298 | + * @throws ReflectionException |
|
| 299 | + */ |
|
| 300 | + public function validateOrder($response, $parameters): array |
|
| 301 | + { |
|
| 302 | + PayPalLogger::errorLog( |
|
| 303 | + esc_html__('Validating Order Create:', 'event_espresso'), |
|
| 304 | + [$this->request_url, $response], |
|
| 305 | + $this->transaction->payment_method(), |
|
| 306 | + false, |
|
| 307 | + $this->transaction |
|
| 308 | + ); |
|
| 309 | + if (! empty($response['error'])) { |
|
| 310 | + return $response; |
|
| 311 | + } |
|
| 312 | + if (! isset($response['id'])) { |
|
| 313 | + $message = esc_html__('Unexpected response. Unable to find the order.', 'event_espresso'); |
|
| 314 | + try { |
|
| 315 | + PayPalLogger::errorLog( |
|
| 316 | + $message, |
|
| 317 | + [$this->request_url, $parameters, $response], |
|
| 318 | + $this->transaction->payment_method() |
|
| 319 | + ); |
|
| 320 | + } catch (EE_Error | ReflectionException $e) { |
|
| 321 | + // Just continue. |
|
| 322 | + } |
|
| 323 | + return [ |
|
| 324 | + 'error' => $response['error'] ?? 'missing_order', |
|
| 325 | + 'message' => $response['message'] ?? $message, |
|
| 326 | + ]; |
|
| 327 | + } |
|
| 328 | + return $response; |
|
| 329 | + } |
|
| 330 | 330 | } |
@@ -101,9 +101,9 @@ discard block |
||
| 101 | 101 | */ |
| 102 | 102 | public function sanitizeRequestParameters(array $billing_info): void |
| 103 | 103 | { |
| 104 | - $sanitizer = new RequestSanitizer(new Basic()); |
|
| 104 | + $sanitizer = new RequestSanitizer(new Basic()); |
|
| 105 | 105 | foreach ($billing_info as $item => $value) { |
| 106 | - $this->billing_info[ $item ] = $sanitizer->clean($value); |
|
| 106 | + $this->billing_info[$item] = $sanitizer->clean($value); |
|
| 107 | 107 | } |
| 108 | 108 | } |
| 109 | 109 | |
@@ -141,7 +141,7 @@ discard block |
||
| 141 | 141 | esc_html__('Tickets for an event at %1$s', 'event_espresso'), |
| 142 | 142 | get_bloginfo('name') |
| 143 | 143 | ); |
| 144 | - $parameters = [ |
|
| 144 | + $parameters = [ |
|
| 145 | 145 | 'intent' => 'CAPTURE', |
| 146 | 146 | 'purchase_units' => [ |
| 147 | 147 | [ |
@@ -176,7 +176,7 @@ discard block |
||
| 176 | 176 | if ( |
| 177 | 177 | ( |
| 178 | 178 | (defined('EE_PPC_USE_PAYMENT_FEES') && EE_PPC_USE_PAYMENT_FEES) |
| 179 | - || (! defined('EE_PPC_USE_PAYMENT_FEES') |
|
| 179 | + || ( ! defined('EE_PPC_USE_PAYMENT_FEES') |
|
| 180 | 180 | && $this->feature->allowed(FeatureFlag::USE_PAYMENT_PROCESSOR_FEES) |
| 181 | 181 | ) |
| 182 | 182 | ) |
@@ -276,11 +276,11 @@ discard block |
||
| 276 | 276 | 'currency_code' => $this->currency_code, |
| 277 | 277 | 'value' => $this->items_total, |
| 278 | 278 | ]; |
| 279 | - $breakdown['tax_total'] = [ |
|
| 279 | + $breakdown['tax_total'] = [ |
|
| 280 | 280 | 'currency_code' => $this->currency_code, |
| 281 | 281 | 'value' => $this->tax_total, |
| 282 | 282 | ]; |
| 283 | - $breakdown['discount'] = [ |
|
| 283 | + $breakdown['discount'] = [ |
|
| 284 | 284 | 'currency_code' => $this->currency_code, |
| 285 | 285 | 'value' => abs($this->promos_total), |
| 286 | 286 | ]; |
@@ -306,10 +306,10 @@ discard block |
||
| 306 | 306 | false, |
| 307 | 307 | $this->transaction |
| 308 | 308 | ); |
| 309 | - if (! empty($response['error'])) { |
|
| 309 | + if ( ! empty($response['error'])) { |
|
| 310 | 310 | return $response; |
| 311 | 311 | } |
| 312 | - if (! isset($response['id'])) { |
|
| 312 | + if ( ! isset($response['id'])) { |
|
| 313 | 313 | $message = esc_html__('Unexpected response. Unable to find the order.', 'event_espresso'); |
| 314 | 314 | try { |
| 315 | 315 | PayPalLogger::errorLog( |
@@ -20,100 +20,100 @@ |
||
| 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 | - * @throws EE_Error |
|
| 60 | - * @throws ReflectionException |
|
| 61 | - */ |
|
| 62 | - public function capture(): array |
|
| 63 | - { |
|
| 64 | - // Create Order request. |
|
| 65 | - $capture_response = $this->api->sendRequest([], $this->request_url); |
|
| 66 | - return $this->validateOrder($capture_response); |
|
| 67 | - } |
|
| 55 | + /** |
|
| 56 | + * Capture payment for PayPal Order. |
|
| 57 | + * |
|
| 58 | + * @return array |
|
| 59 | + * @throws EE_Error |
|
| 60 | + * @throws ReflectionException |
|
| 61 | + */ |
|
| 62 | + public function capture(): array |
|
| 63 | + { |
|
| 64 | + // Create Order request. |
|
| 65 | + $capture_response = $this->api->sendRequest([], $this->request_url); |
|
| 66 | + return $this->validateOrder($capture_response); |
|
| 67 | + } |
|
| 68 | 68 | |
| 69 | 69 | |
| 70 | - /** |
|
| 71 | - * Makes sure that we have received an Order back from the API call. |
|
| 72 | - * |
|
| 73 | - * @param $response |
|
| 74 | - * @return array |
|
| 75 | - * @throws EE_Error |
|
| 76 | - * @throws ReflectionException |
|
| 77 | - */ |
|
| 78 | - public function validateOrder($response): array |
|
| 79 | - { |
|
| 80 | - $message = esc_html__('Validating Order Capture:', 'event_espresso'); |
|
| 81 | - PayPalLogger::errorLog( |
|
| 82 | - $message, |
|
| 83 | - [$this->request_url, $response], |
|
| 84 | - $this->transaction->payment_method(), |
|
| 85 | - false, |
|
| 86 | - $this->transaction |
|
| 87 | - ); |
|
| 88 | - // We got a direct error response. Not valid. Return that error. |
|
| 89 | - if (! empty($response['error'])) { |
|
| 90 | - return $response; |
|
| 91 | - } |
|
| 92 | - // This also could be a retry capture, so consider this valid, if order already captured. |
|
| 93 | - if (! empty($response['message']['details']['issue']) |
|
| 94 | - && $response['message']['details']['issue'] === 'ORDER_ALREADY_CAPTURED' |
|
| 95 | - ) { |
|
| 96 | - // Need to make sure we pass on the order ID. |
|
| 97 | - if (empty($response['id'])) { |
|
| 98 | - $response['id'] = $this->order_id; |
|
| 99 | - } |
|
| 100 | - $response['status'] = 'ORDER_ALREADY_CAPTURED'; |
|
| 101 | - return $response; |
|
| 102 | - } |
|
| 103 | - // A success capture should return the order ID. |
|
| 104 | - if (! isset($response['id'])) { |
|
| 105 | - $message = esc_html__('Unexpected response. No order returned.', 'event_espresso'); |
|
| 106 | - try { |
|
| 107 | - PayPalLogger::errorLog($message, [$this->request_url, $response], $this->transaction->payment_method()); |
|
| 108 | - } catch (EE_Error | ReflectionException $e) { |
|
| 109 | - // Just continue. |
|
| 110 | - } |
|
| 111 | - return [ |
|
| 112 | - 'error' => $response['error'] ?? 'missing_order', |
|
| 113 | - 'message' => $response['message'] ?? $message, |
|
| 114 | - 'name' => $response['name'] ?? 'UNKNOWN_ERROR', |
|
| 115 | - ]; |
|
| 116 | - } |
|
| 117 | - return $response; |
|
| 118 | - } |
|
| 70 | + /** |
|
| 71 | + * Makes sure that we have received an Order back from the API call. |
|
| 72 | + * |
|
| 73 | + * @param $response |
|
| 74 | + * @return array |
|
| 75 | + * @throws EE_Error |
|
| 76 | + * @throws ReflectionException |
|
| 77 | + */ |
|
| 78 | + public function validateOrder($response): array |
|
| 79 | + { |
|
| 80 | + $message = esc_html__('Validating Order Capture:', 'event_espresso'); |
|
| 81 | + PayPalLogger::errorLog( |
|
| 82 | + $message, |
|
| 83 | + [$this->request_url, $response], |
|
| 84 | + $this->transaction->payment_method(), |
|
| 85 | + false, |
|
| 86 | + $this->transaction |
|
| 87 | + ); |
|
| 88 | + // We got a direct error response. Not valid. Return that error. |
|
| 89 | + if (! empty($response['error'])) { |
|
| 90 | + return $response; |
|
| 91 | + } |
|
| 92 | + // This also could be a retry capture, so consider this valid, if order already captured. |
|
| 93 | + if (! empty($response['message']['details']['issue']) |
|
| 94 | + && $response['message']['details']['issue'] === 'ORDER_ALREADY_CAPTURED' |
|
| 95 | + ) { |
|
| 96 | + // Need to make sure we pass on the order ID. |
|
| 97 | + if (empty($response['id'])) { |
|
| 98 | + $response['id'] = $this->order_id; |
|
| 99 | + } |
|
| 100 | + $response['status'] = 'ORDER_ALREADY_CAPTURED'; |
|
| 101 | + return $response; |
|
| 102 | + } |
|
| 103 | + // A success capture should return the order ID. |
|
| 104 | + if (! isset($response['id'])) { |
|
| 105 | + $message = esc_html__('Unexpected response. No order returned.', 'event_espresso'); |
|
| 106 | + try { |
|
| 107 | + PayPalLogger::errorLog($message, [$this->request_url, $response], $this->transaction->payment_method()); |
|
| 108 | + } catch (EE_Error | ReflectionException $e) { |
|
| 109 | + // Just continue. |
|
| 110 | + } |
|
| 111 | + return [ |
|
| 112 | + 'error' => $response['error'] ?? 'missing_order', |
|
| 113 | + 'message' => $response['message'] ?? $message, |
|
| 114 | + 'name' => $response['name'] ?? 'UNKNOWN_ERROR', |
|
| 115 | + ]; |
|
| 116 | + } |
|
| 117 | + return $response; |
|
| 118 | + } |
|
| 119 | 119 | } |
@@ -15,21 +15,21 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | abstract class OrdersApi |
| 17 | 17 | { |
| 18 | - protected PayPalApi $api; |
|
| 18 | + protected PayPalApi $api; |
|
| 19 | 19 | |
| 20 | - protected string $request_url; |
|
| 20 | + protected string $request_url; |
|
| 21 | 21 | |
| 22 | - protected string $order_id; |
|
| 22 | + protected string $order_id; |
|
| 23 | 23 | |
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * Orders API constructor. |
|
| 27 | - * |
|
| 28 | - * @param PayPalApi $api |
|
| 29 | - */ |
|
| 30 | - public function __construct(PayPalApi $api) |
|
| 31 | - { |
|
| 32 | - $this->api = $api; |
|
| 33 | - $this->request_url = $this->api->apiEndpoint() . 'checkout/orders/'; |
|
| 34 | - } |
|
| 25 | + /** |
|
| 26 | + * Orders API constructor. |
|
| 27 | + * |
|
| 28 | + * @param PayPalApi $api |
|
| 29 | + */ |
|
| 30 | + public function __construct(PayPalApi $api) |
|
| 31 | + { |
|
| 32 | + $this->api = $api; |
|
| 33 | + $this->request_url = $this->api->apiEndpoint() . 'checkout/orders/'; |
|
| 34 | + } |
|
| 35 | 35 | } |
@@ -15,73 +15,73 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | abstract class PayPalApi |
| 17 | 17 | { |
| 18 | - /** |
|
| 19 | - * @var bool Debug mode enabled ? |
|
| 20 | - */ |
|
| 21 | - protected bool $sandbox_mode; |
|
| 18 | + /** |
|
| 19 | + * @var bool Debug mode enabled ? |
|
| 20 | + */ |
|
| 21 | + protected bool $sandbox_mode; |
|
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * API request/response validation helper. |
|
| 25 | - * |
|
| 26 | - * @var ResponseInspector |
|
| 27 | - */ |
|
| 28 | - protected ResponseInspector $inspector; |
|
| 23 | + /** |
|
| 24 | + * API request/response validation helper. |
|
| 25 | + * |
|
| 26 | + * @var ResponseInspector |
|
| 27 | + */ |
|
| 28 | + protected ResponseInspector $inspector; |
|
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * @var string PayPal API endpoint. |
|
| 32 | - */ |
|
| 33 | - protected string $api_endpoint = ''; |
|
| 30 | + /** |
|
| 31 | + * @var string PayPal API endpoint. |
|
| 32 | + */ |
|
| 33 | + protected string $api_endpoint = ''; |
|
| 34 | 34 | |
| 35 | 35 | |
| 36 | - /** |
|
| 37 | - * @param bool $sandbox_mode |
|
| 38 | - */ |
|
| 39 | - public function __construct(bool $sandbox_mode = true) |
|
| 40 | - { |
|
| 41 | - $this->sandbox_mode = $sandbox_mode; |
|
| 42 | - // Is this a sandbox request. |
|
| 43 | - $this->api_endpoint = $this->sandbox_mode |
|
| 44 | - ? 'https://api-m.sandbox.paypal.com/v2/' |
|
| 45 | - : 'https://api-m.paypal.com/v2/'; |
|
| 46 | - $this->inspector = new ResponseInspector(); |
|
| 47 | - } |
|
| 36 | + /** |
|
| 37 | + * @param bool $sandbox_mode |
|
| 38 | + */ |
|
| 39 | + public function __construct(bool $sandbox_mode = true) |
|
| 40 | + { |
|
| 41 | + $this->sandbox_mode = $sandbox_mode; |
|
| 42 | + // Is this a sandbox request. |
|
| 43 | + $this->api_endpoint = $this->sandbox_mode |
|
| 44 | + ? 'https://api-m.sandbox.paypal.com/v2/' |
|
| 45 | + : 'https://api-m.paypal.com/v2/'; |
|
| 46 | + $this->inspector = new ResponseInspector(); |
|
| 47 | + } |
|
| 48 | 48 | |
| 49 | 49 | |
| 50 | - /** |
|
| 51 | - * Send an API request. |
|
| 52 | - * |
|
| 53 | - * @param string $endpoint |
|
| 54 | - * @param array $request_parameters |
|
| 55 | - * @return Object|array |
|
| 56 | - */ |
|
| 57 | - public function request(string $endpoint, array $request_parameters) |
|
| 58 | - { |
|
| 59 | - // Sent the API request. |
|
| 60 | - $response = wp_remote_request($endpoint, $request_parameters); |
|
| 61 | - // Validate the response. |
|
| 62 | - $this->inspector->validateResponse($response); |
|
| 63 | - if (! $this->inspector->isValid()) { |
|
| 64 | - PayPalLogger::errorLog($this->inspector->error()['message'], [$endpoint, $request_parameters, $response]); |
|
| 65 | - return $this->inspector->error(); |
|
| 66 | - } |
|
| 67 | - // Decode the parameters. |
|
| 68 | - $api_response = json_decode($response['body'], true); |
|
| 69 | - // Validate parameters. |
|
| 70 | - $this->inspector->validateParameters($api_response); |
|
| 71 | - if (! $this->inspector->isValid()) { |
|
| 72 | - PayPalLogger::errorLog($this->inspector->error()['message'], [$endpoint, $request_parameters, $response]); |
|
| 73 | - return $this->inspector->error(); |
|
| 74 | - } |
|
| 75 | - // All seem ok, return the response. |
|
| 76 | - return $api_response; |
|
| 77 | - } |
|
| 50 | + /** |
|
| 51 | + * Send an API request. |
|
| 52 | + * |
|
| 53 | + * @param string $endpoint |
|
| 54 | + * @param array $request_parameters |
|
| 55 | + * @return Object|array |
|
| 56 | + */ |
|
| 57 | + public function request(string $endpoint, array $request_parameters) |
|
| 58 | + { |
|
| 59 | + // Sent the API request. |
|
| 60 | + $response = wp_remote_request($endpoint, $request_parameters); |
|
| 61 | + // Validate the response. |
|
| 62 | + $this->inspector->validateResponse($response); |
|
| 63 | + if (! $this->inspector->isValid()) { |
|
| 64 | + PayPalLogger::errorLog($this->inspector->error()['message'], [$endpoint, $request_parameters, $response]); |
|
| 65 | + return $this->inspector->error(); |
|
| 66 | + } |
|
| 67 | + // Decode the parameters. |
|
| 68 | + $api_response = json_decode($response['body'], true); |
|
| 69 | + // Validate parameters. |
|
| 70 | + $this->inspector->validateParameters($api_response); |
|
| 71 | + if (! $this->inspector->isValid()) { |
|
| 72 | + PayPalLogger::errorLog($this->inspector->error()['message'], [$endpoint, $request_parameters, $response]); |
|
| 73 | + return $this->inspector->error(); |
|
| 74 | + } |
|
| 75 | + // All seem ok, return the response. |
|
| 76 | + return $api_response; |
|
| 77 | + } |
|
| 78 | 78 | |
| 79 | 79 | |
| 80 | - /** |
|
| 81 | - * @return string |
|
| 82 | - */ |
|
| 83 | - public function apiEndpoint(): string |
|
| 84 | - { |
|
| 85 | - return $this->api_endpoint; |
|
| 86 | - } |
|
| 80 | + /** |
|
| 81 | + * @return string |
|
| 82 | + */ |
|
| 83 | + public function apiEndpoint(): string |
|
| 84 | + { |
|
| 85 | + return $this->api_endpoint; |
|
| 86 | + } |
|
| 87 | 87 | } |
@@ -60,7 +60,7 @@ discard block |
||
| 60 | 60 | $response = wp_remote_request($endpoint, $request_parameters); |
| 61 | 61 | // Validate the response. |
| 62 | 62 | $this->inspector->validateResponse($response); |
| 63 | - if (! $this->inspector->isValid()) { |
|
| 63 | + if ( ! $this->inspector->isValid()) { |
|
| 64 | 64 | PayPalLogger::errorLog($this->inspector->error()['message'], [$endpoint, $request_parameters, $response]); |
| 65 | 65 | return $this->inspector->error(); |
| 66 | 66 | } |
@@ -68,7 +68,7 @@ discard block |
||
| 68 | 68 | $api_response = json_decode($response['body'], true); |
| 69 | 69 | // Validate parameters. |
| 70 | 70 | $this->inspector->validateParameters($api_response); |
| 71 | - if (! $this->inspector->isValid()) { |
|
| 71 | + if ( ! $this->inspector->isValid()) { |
|
| 72 | 72 | PayPalLogger::errorLog($this->inspector->error()['message'], [$endpoint, $request_parameters, $response]); |
| 73 | 73 | return $this->inspector->error(); |
| 74 | 74 | } |
@@ -14,51 +14,51 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | class OneTimeString |
| 16 | 16 | { |
| 17 | - private string $value; |
|
| 17 | + private string $value; |
|
| 18 | 18 | |
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * |
|
| 22 | - * @param bool $sandbox_mode |
|
| 23 | - * @throws Exception |
|
| 24 | - */ |
|
| 25 | - public function __construct(bool $sandbox_mode) |
|
| 26 | - { |
|
| 27 | - $key_prefix = $sandbox_mode ? 'sandbox' : 'live'; |
|
| 28 | - $random_bytes = $this->randomIdentifier(); |
|
| 29 | - $this->value = "$key_prefix-$random_bytes"; |
|
| 30 | - } |
|
| 20 | + /** |
|
| 21 | + * |
|
| 22 | + * @param bool $sandbox_mode |
|
| 23 | + * @throws Exception |
|
| 24 | + */ |
|
| 25 | + public function __construct(bool $sandbox_mode) |
|
| 26 | + { |
|
| 27 | + $key_prefix = $sandbox_mode ? 'sandbox' : 'live'; |
|
| 28 | + $random_bytes = $this->randomIdentifier(); |
|
| 29 | + $this->value = "$key_prefix-$random_bytes"; |
|
| 30 | + } |
|
| 31 | 31 | |
| 32 | 32 | |
| 33 | - /** |
|
| 34 | - * Create a random string. |
|
| 35 | - * |
|
| 36 | - * @param int $length |
|
| 37 | - * @return string |
|
| 38 | - * @throws Exception |
|
| 39 | - */ |
|
| 40 | - private function randomIdentifier(int $length = 50): string |
|
| 41 | - { |
|
| 42 | - return function_exists('random_bytes') |
|
| 43 | - ? bin2hex(random_bytes($length)) |
|
| 44 | - : bin2hex(openssl_random_pseudo_bytes($length)); |
|
| 45 | - } |
|
| 33 | + /** |
|
| 34 | + * Create a random string. |
|
| 35 | + * |
|
| 36 | + * @param int $length |
|
| 37 | + * @return string |
|
| 38 | + * @throws Exception |
|
| 39 | + */ |
|
| 40 | + private function randomIdentifier(int $length = 50): string |
|
| 41 | + { |
|
| 42 | + return function_exists('random_bytes') |
|
| 43 | + ? bin2hex(random_bytes($length)) |
|
| 44 | + : bin2hex(openssl_random_pseudo_bytes($length)); |
|
| 45 | + } |
|
| 46 | 46 | |
| 47 | 47 | |
| 48 | - /** |
|
| 49 | - * @return string |
|
| 50 | - */ |
|
| 51 | - public function value(): string |
|
| 52 | - { |
|
| 53 | - return $this->value; |
|
| 54 | - } |
|
| 48 | + /** |
|
| 49 | + * @return string |
|
| 50 | + */ |
|
| 51 | + public function value(): string |
|
| 52 | + { |
|
| 53 | + return $this->value; |
|
| 54 | + } |
|
| 55 | 55 | |
| 56 | 56 | |
| 57 | - /** |
|
| 58 | - * @return string |
|
| 59 | - */ |
|
| 60 | - public function __toString(): string |
|
| 61 | - { |
|
| 62 | - return $this->value; |
|
| 63 | - } |
|
| 57 | + /** |
|
| 58 | + * @return string |
|
| 59 | + */ |
|
| 60 | + public function __toString(): string |
|
| 61 | + { |
|
| 62 | + return $this->value; |
|
| 63 | + } |
|
| 64 | 64 | } |
@@ -13,113 +13,113 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class FirstPartyPayPalApi extends PayPalApi |
| 15 | 15 | { |
| 16 | - /** |
|
| 17 | - * Client ID. Used to process payments. |
|
| 18 | - * |
|
| 19 | - * @var string |
|
| 20 | - */ |
|
| 21 | - protected string $client_id = ''; |
|
| 22 | - |
|
| 23 | - /** |
|
| 24 | - * Client secret. Used to process payments. |
|
| 25 | - * |
|
| 26 | - * @var string |
|
| 27 | - */ |
|
| 28 | - protected string $client_secret = ''; |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * BN Code. Partner-Attribution-Id. |
|
| 32 | - * |
|
| 33 | - * @var string |
|
| 34 | - */ |
|
| 35 | - protected string $bn_code = ''; |
|
| 36 | - |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * @param string $client_id |
|
| 40 | - * @param string $client_secret |
|
| 41 | - * @param string $bn_code |
|
| 42 | - * @param bool $sandbox_mode |
|
| 43 | - */ |
|
| 44 | - public function __construct(string $client_id, string $client_secret, string $bn_code, bool $sandbox_mode = true) |
|
| 45 | - { |
|
| 46 | - parent::__construct($sandbox_mode); |
|
| 47 | - $this->client_id = $client_id; |
|
| 48 | - $this->client_secret = $client_secret; |
|
| 49 | - $this->bn_code = $bn_code; |
|
| 50 | - } |
|
| 51 | - |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * Send an API request. |
|
| 55 | - * |
|
| 56 | - * @param array $body_parameters |
|
| 57 | - * @param string $endpoint |
|
| 58 | - * @param string $method |
|
| 59 | - * @param array $headers |
|
| 60 | - * @return Object|array |
|
| 61 | - */ |
|
| 62 | - public function sendRequest(array $body_parameters, string $endpoint, string $method = 'POST', array $headers = []) |
|
| 63 | - { |
|
| 64 | - $request_parameters = $this->getRequestParameters($body_parameters, $method, $headers); |
|
| 65 | - return $this->request($endpoint, $request_parameters); |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * Build the request parameters. |
|
| 71 | - * |
|
| 72 | - * @param array $body_parameters |
|
| 73 | - * @param string $method |
|
| 74 | - * @param array $headers |
|
| 75 | - * @return array |
|
| 76 | - */ |
|
| 77 | - private function getRequestParameters(array $body_parameters, string $method, array $headers): array |
|
| 78 | - { |
|
| 79 | - $request_parameters = [ |
|
| 80 | - 'method' => $method, |
|
| 81 | - 'timeout' => 60, |
|
| 82 | - 'redirection' => 5, |
|
| 83 | - 'blocking' => true, |
|
| 84 | - ]; |
|
| 85 | - // add default values to incoming headers. |
|
| 86 | - $request_parameters['headers'] = $headers + [ |
|
| 87 | - 'User-Agent' => sanitize_text_field($_SERVER['HTTP_USER_AGENT']), |
|
| 88 | - 'PayPal-Partner-Attribution-Id' => $this->bn_code, |
|
| 89 | - 'Content-Type' => 'application/json', |
|
| 90 | - 'Authorization' => 'Basic ' . base64_encode("$this->client_id:$this->client_secret"), |
|
| 91 | - ]; |
|
| 92 | - // Add body if this is a POST request. |
|
| 93 | - if ($body_parameters && ($method === 'POST' || $method === 'PUT')) { |
|
| 94 | - $request_parameters['body'] = json_encode($body_parameters); |
|
| 95 | - } |
|
| 96 | - return $request_parameters; |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - |
|
| 100 | - /** |
|
| 101 | - * @return string |
|
| 102 | - */ |
|
| 103 | - public function clientId(): string |
|
| 104 | - { |
|
| 105 | - return $this->client_id; |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - |
|
| 109 | - /** |
|
| 110 | - * @return string |
|
| 111 | - */ |
|
| 112 | - public function clientSecret(): string |
|
| 113 | - { |
|
| 114 | - return $this->client_secret; |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - |
|
| 118 | - /** |
|
| 119 | - * @return string |
|
| 120 | - */ |
|
| 121 | - public function bnCode(): string |
|
| 122 | - { |
|
| 123 | - return $this->bn_code; |
|
| 124 | - } |
|
| 16 | + /** |
|
| 17 | + * Client ID. Used to process payments. |
|
| 18 | + * |
|
| 19 | + * @var string |
|
| 20 | + */ |
|
| 21 | + protected string $client_id = ''; |
|
| 22 | + |
|
| 23 | + /** |
|
| 24 | + * Client secret. Used to process payments. |
|
| 25 | + * |
|
| 26 | + * @var string |
|
| 27 | + */ |
|
| 28 | + protected string $client_secret = ''; |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * BN Code. Partner-Attribution-Id. |
|
| 32 | + * |
|
| 33 | + * @var string |
|
| 34 | + */ |
|
| 35 | + protected string $bn_code = ''; |
|
| 36 | + |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * @param string $client_id |
|
| 40 | + * @param string $client_secret |
|
| 41 | + * @param string $bn_code |
|
| 42 | + * @param bool $sandbox_mode |
|
| 43 | + */ |
|
| 44 | + public function __construct(string $client_id, string $client_secret, string $bn_code, bool $sandbox_mode = true) |
|
| 45 | + { |
|
| 46 | + parent::__construct($sandbox_mode); |
|
| 47 | + $this->client_id = $client_id; |
|
| 48 | + $this->client_secret = $client_secret; |
|
| 49 | + $this->bn_code = $bn_code; |
|
| 50 | + } |
|
| 51 | + |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * Send an API request. |
|
| 55 | + * |
|
| 56 | + * @param array $body_parameters |
|
| 57 | + * @param string $endpoint |
|
| 58 | + * @param string $method |
|
| 59 | + * @param array $headers |
|
| 60 | + * @return Object|array |
|
| 61 | + */ |
|
| 62 | + public function sendRequest(array $body_parameters, string $endpoint, string $method = 'POST', array $headers = []) |
|
| 63 | + { |
|
| 64 | + $request_parameters = $this->getRequestParameters($body_parameters, $method, $headers); |
|
| 65 | + return $this->request($endpoint, $request_parameters); |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * Build the request parameters. |
|
| 71 | + * |
|
| 72 | + * @param array $body_parameters |
|
| 73 | + * @param string $method |
|
| 74 | + * @param array $headers |
|
| 75 | + * @return array |
|
| 76 | + */ |
|
| 77 | + private function getRequestParameters(array $body_parameters, string $method, array $headers): array |
|
| 78 | + { |
|
| 79 | + $request_parameters = [ |
|
| 80 | + 'method' => $method, |
|
| 81 | + 'timeout' => 60, |
|
| 82 | + 'redirection' => 5, |
|
| 83 | + 'blocking' => true, |
|
| 84 | + ]; |
|
| 85 | + // add default values to incoming headers. |
|
| 86 | + $request_parameters['headers'] = $headers + [ |
|
| 87 | + 'User-Agent' => sanitize_text_field($_SERVER['HTTP_USER_AGENT']), |
|
| 88 | + 'PayPal-Partner-Attribution-Id' => $this->bn_code, |
|
| 89 | + 'Content-Type' => 'application/json', |
|
| 90 | + 'Authorization' => 'Basic ' . base64_encode("$this->client_id:$this->client_secret"), |
|
| 91 | + ]; |
|
| 92 | + // Add body if this is a POST request. |
|
| 93 | + if ($body_parameters && ($method === 'POST' || $method === 'PUT')) { |
|
| 94 | + $request_parameters['body'] = json_encode($body_parameters); |
|
| 95 | + } |
|
| 96 | + return $request_parameters; |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + |
|
| 100 | + /** |
|
| 101 | + * @return string |
|
| 102 | + */ |
|
| 103 | + public function clientId(): string |
|
| 104 | + { |
|
| 105 | + return $this->client_id; |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + |
|
| 109 | + /** |
|
| 110 | + * @return string |
|
| 111 | + */ |
|
| 112 | + public function clientSecret(): string |
|
| 113 | + { |
|
| 114 | + return $this->client_secret; |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + |
|
| 118 | + /** |
|
| 119 | + * @return string |
|
| 120 | + */ |
|
| 121 | + public function bnCode(): string |
|
| 122 | + { |
|
| 123 | + return $this->bn_code; |
|
| 124 | + } |
|
| 125 | 125 | } |
@@ -87,7 +87,7 @@ |
||
| 87 | 87 | 'User-Agent' => sanitize_text_field($_SERVER['HTTP_USER_AGENT']), |
| 88 | 88 | 'PayPal-Partner-Attribution-Id' => $this->bn_code, |
| 89 | 89 | 'Content-Type' => 'application/json', |
| 90 | - 'Authorization' => 'Basic ' . base64_encode("$this->client_id:$this->client_secret"), |
|
| 90 | + 'Authorization' => 'Basic '.base64_encode("$this->client_id:$this->client_secret"), |
|
| 91 | 91 | ]; |
| 92 | 92 | // Add body if this is a POST request. |
| 93 | 93 | if ($body_parameters && ($method === 'POST' || $method === 'PUT')) { |
@@ -18,190 +18,190 @@ |
||
| 18 | 18 | */ |
| 19 | 19 | class PayPalExtraMeta |
| 20 | 20 | { |
| 21 | - public EE_Payment_Method $pm; |
|
| 22 | - |
|
| 23 | - public string $metadata_key; |
|
| 24 | - |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * Class constructor. |
|
| 28 | - * |
|
| 29 | - * @param EE_Payment_Method $pm_instance |
|
| 30 | - * @throws EE_Error |
|
| 31 | - * @throws ReflectionException |
|
| 32 | - */ |
|
| 33 | - public function __construct(EE_Payment_Method $pm_instance) |
|
| 34 | - { |
|
| 35 | - $this->pm = $pm_instance; |
|
| 36 | - $this->metadata_key = $pm_instance->debug_mode() |
|
| 37 | - ? Domain::META_KEY_PAYPAL_DATA_SANDBOX |
|
| 38 | - : Domain::META_KEY_PAYPAL_DATA; |
|
| 39 | - } |
|
| 40 | - |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * Save/update the provided data to the PM extra meta. |
|
| 44 | - * |
|
| 45 | - * @param array $data |
|
| 46 | - * @return bool |
|
| 47 | - */ |
|
| 48 | - public function saveBatch(array $data): bool |
|
| 49 | - { |
|
| 50 | - // Update the PM data. |
|
| 51 | - try { |
|
| 52 | - $paypal_data = $this->getMetaData(); |
|
| 53 | - $this->saveMetaData(array_replace_recursive($paypal_data, $data)); |
|
| 54 | - } catch (Exception $e) { |
|
| 55 | - PayPalLogger::errorLog( |
|
| 56 | - sprintf( |
|
| 57 | - esc_html__('Could not save merchant data. %1$s', 'event_espresso'), |
|
| 58 | - $e->getMessage() |
|
| 59 | - ), |
|
| 60 | - ['data' => $data, 'trace' => $e->getTrace()], |
|
| 61 | - $this->pm |
|
| 62 | - ); |
|
| 63 | - return false; |
|
| 64 | - } |
|
| 65 | - return true; |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * Get PM extra meta by meta name/option. |
|
| 71 | - * |
|
| 72 | - * @param string $option_name |
|
| 73 | - * @return mixed |
|
| 74 | - */ |
|
| 75 | - public function getOption(string $option_name) |
|
| 76 | - { |
|
| 77 | - $meta_data = $this->getMetaData(); |
|
| 78 | - return $meta_data[ $option_name ] ?? false; |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - |
|
| 82 | - /** |
|
| 83 | - * Get PM metadata. |
|
| 84 | - * Return the metadata array if all good. False otherwise. |
|
| 85 | - * |
|
| 86 | - * @return array |
|
| 87 | - */ |
|
| 88 | - public function getMetaData(): array |
|
| 89 | - { |
|
| 90 | - try { |
|
| 91 | - return (array) $this->pm->get_extra_meta($this->metadata_key, true, []); |
|
| 92 | - } catch (EE_Error | ReflectionException $e) { |
|
| 93 | - PayPalLogger::errorLog( |
|
| 94 | - sprintf( |
|
| 95 | - esc_html__('Error getting the PM meta data: %1$s', 'event_espresso'), |
|
| 96 | - $e->getMessage() |
|
| 97 | - ), |
|
| 98 | - ['trace' => $e->getTrace()], |
|
| 99 | - $this->pm |
|
| 100 | - ); |
|
| 101 | - return []; |
|
| 102 | - } |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - |
|
| 106 | - /** |
|
| 107 | - * Save/update the provided option to the PM extra meta. |
|
| 108 | - * |
|
| 109 | - * @param string $name |
|
| 110 | - * @param $value |
|
| 111 | - * @return bool |
|
| 112 | - */ |
|
| 113 | - public function saveOption(string $name, $value): bool |
|
| 114 | - { |
|
| 115 | - $meta_data = $this->getMetaData(); |
|
| 116 | - if (! $meta_data) { |
|
| 117 | - $meta_data = []; |
|
| 118 | - } |
|
| 119 | - $meta_data[ $name ] = $value; |
|
| 120 | - return $this->saveMetaData($meta_data); |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - |
|
| 124 | - /** |
|
| 125 | - * Save/update the PM extra meta. |
|
| 126 | - * |
|
| 127 | - * @param array $data |
|
| 128 | - * @return bool |
|
| 129 | - */ |
|
| 130 | - public function saveMetaData(array $data): bool |
|
| 131 | - { |
|
| 132 | - try { |
|
| 133 | - $this->pm->update_extra_meta($this->metadata_key, $data); |
|
| 134 | - } catch (Exception $e) { |
|
| 135 | - PayPalLogger::errorLog( |
|
| 136 | - sprintf( |
|
| 137 | - esc_html__('Error saving the PM meta data: %1$s', 'event_espresso'), |
|
| 138 | - $e->getMessage() |
|
| 139 | - ), |
|
| 140 | - ['data' => $data, 'trace' => $e->getTrace()], |
|
| 141 | - $this->pm |
|
| 142 | - ); |
|
| 143 | - return false; |
|
| 144 | - } |
|
| 145 | - return true; |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - |
|
| 149 | - /** |
|
| 150 | - * Delete PM option. |
|
| 151 | - * |
|
| 152 | - * @param string $name |
|
| 153 | - * @return bool |
|
| 154 | - */ |
|
| 155 | - public function deleteOption(string $name): bool |
|
| 156 | - { |
|
| 157 | - $meta_data = $this->getMetaData(); |
|
| 158 | - if (! $meta_data) { |
|
| 159 | - return false; |
|
| 160 | - } |
|
| 161 | - // If the option does not exist, don't bother saving, but return true anyways. |
|
| 162 | - if (! isset($meta_data[ $name ])) { |
|
| 163 | - return true; |
|
| 164 | - } |
|
| 165 | - unset($meta_data[ $name ]); |
|
| 166 | - return $this->saveMetaData($meta_data); |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - |
|
| 170 | - /** |
|
| 171 | - * Delete all the PM extra meta. |
|
| 172 | - * |
|
| 173 | - * @return bool |
|
| 174 | - */ |
|
| 175 | - public function deleteAllMetaData(): bool |
|
| 176 | - { |
|
| 177 | - return $this->deleteMetaData(Domain::META_KEY_PAYPAL_DATA) |
|
| 178 | - && $this->deleteMetaData(Domain::META_KEY_PAYPAL_DATA_SANDBOX); |
|
| 179 | - } |
|
| 180 | - |
|
| 181 | - |
|
| 182 | - /** |
|
| 183 | - * Delete the PM extra meta. |
|
| 184 | - * |
|
| 185 | - * @param null $metadata_key |
|
| 186 | - * @return bool |
|
| 187 | - */ |
|
| 188 | - public function deleteMetaData($metadata_key = null): bool |
|
| 189 | - { |
|
| 190 | - try { |
|
| 191 | - // Related to PM and sandbox mode metadata (default), or by the provided metadata key? |
|
| 192 | - $metadata_key = $metadata_key ?? $this->metadata_key; |
|
| 193 | - $this->pm->delete_extra_meta($metadata_key); |
|
| 194 | - } catch (Exception $e) { |
|
| 195 | - PayPalLogger::errorLog( |
|
| 196 | - sprintf( |
|
| 197 | - esc_html__('Error deleting the PM meta data: %1$s', 'event_espresso'), |
|
| 198 | - $e->getMessage() |
|
| 199 | - ), |
|
| 200 | - ['trace' => $e->getTrace()], |
|
| 201 | - $this->pm |
|
| 202 | - ); |
|
| 203 | - return false; |
|
| 204 | - } |
|
| 205 | - return true; |
|
| 206 | - } |
|
| 21 | + public EE_Payment_Method $pm; |
|
| 22 | + |
|
| 23 | + public string $metadata_key; |
|
| 24 | + |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * Class constructor. |
|
| 28 | + * |
|
| 29 | + * @param EE_Payment_Method $pm_instance |
|
| 30 | + * @throws EE_Error |
|
| 31 | + * @throws ReflectionException |
|
| 32 | + */ |
|
| 33 | + public function __construct(EE_Payment_Method $pm_instance) |
|
| 34 | + { |
|
| 35 | + $this->pm = $pm_instance; |
|
| 36 | + $this->metadata_key = $pm_instance->debug_mode() |
|
| 37 | + ? Domain::META_KEY_PAYPAL_DATA_SANDBOX |
|
| 38 | + : Domain::META_KEY_PAYPAL_DATA; |
|
| 39 | + } |
|
| 40 | + |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * Save/update the provided data to the PM extra meta. |
|
| 44 | + * |
|
| 45 | + * @param array $data |
|
| 46 | + * @return bool |
|
| 47 | + */ |
|
| 48 | + public function saveBatch(array $data): bool |
|
| 49 | + { |
|
| 50 | + // Update the PM data. |
|
| 51 | + try { |
|
| 52 | + $paypal_data = $this->getMetaData(); |
|
| 53 | + $this->saveMetaData(array_replace_recursive($paypal_data, $data)); |
|
| 54 | + } catch (Exception $e) { |
|
| 55 | + PayPalLogger::errorLog( |
|
| 56 | + sprintf( |
|
| 57 | + esc_html__('Could not save merchant data. %1$s', 'event_espresso'), |
|
| 58 | + $e->getMessage() |
|
| 59 | + ), |
|
| 60 | + ['data' => $data, 'trace' => $e->getTrace()], |
|
| 61 | + $this->pm |
|
| 62 | + ); |
|
| 63 | + return false; |
|
| 64 | + } |
|
| 65 | + return true; |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * Get PM extra meta by meta name/option. |
|
| 71 | + * |
|
| 72 | + * @param string $option_name |
|
| 73 | + * @return mixed |
|
| 74 | + */ |
|
| 75 | + public function getOption(string $option_name) |
|
| 76 | + { |
|
| 77 | + $meta_data = $this->getMetaData(); |
|
| 78 | + return $meta_data[ $option_name ] ?? false; |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + |
|
| 82 | + /** |
|
| 83 | + * Get PM metadata. |
|
| 84 | + * Return the metadata array if all good. False otherwise. |
|
| 85 | + * |
|
| 86 | + * @return array |
|
| 87 | + */ |
|
| 88 | + public function getMetaData(): array |
|
| 89 | + { |
|
| 90 | + try { |
|
| 91 | + return (array) $this->pm->get_extra_meta($this->metadata_key, true, []); |
|
| 92 | + } catch (EE_Error | ReflectionException $e) { |
|
| 93 | + PayPalLogger::errorLog( |
|
| 94 | + sprintf( |
|
| 95 | + esc_html__('Error getting the PM meta data: %1$s', 'event_espresso'), |
|
| 96 | + $e->getMessage() |
|
| 97 | + ), |
|
| 98 | + ['trace' => $e->getTrace()], |
|
| 99 | + $this->pm |
|
| 100 | + ); |
|
| 101 | + return []; |
|
| 102 | + } |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + |
|
| 106 | + /** |
|
| 107 | + * Save/update the provided option to the PM extra meta. |
|
| 108 | + * |
|
| 109 | + * @param string $name |
|
| 110 | + * @param $value |
|
| 111 | + * @return bool |
|
| 112 | + */ |
|
| 113 | + public function saveOption(string $name, $value): bool |
|
| 114 | + { |
|
| 115 | + $meta_data = $this->getMetaData(); |
|
| 116 | + if (! $meta_data) { |
|
| 117 | + $meta_data = []; |
|
| 118 | + } |
|
| 119 | + $meta_data[ $name ] = $value; |
|
| 120 | + return $this->saveMetaData($meta_data); |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + |
|
| 124 | + /** |
|
| 125 | + * Save/update the PM extra meta. |
|
| 126 | + * |
|
| 127 | + * @param array $data |
|
| 128 | + * @return bool |
|
| 129 | + */ |
|
| 130 | + public function saveMetaData(array $data): bool |
|
| 131 | + { |
|
| 132 | + try { |
|
| 133 | + $this->pm->update_extra_meta($this->metadata_key, $data); |
|
| 134 | + } catch (Exception $e) { |
|
| 135 | + PayPalLogger::errorLog( |
|
| 136 | + sprintf( |
|
| 137 | + esc_html__('Error saving the PM meta data: %1$s', 'event_espresso'), |
|
| 138 | + $e->getMessage() |
|
| 139 | + ), |
|
| 140 | + ['data' => $data, 'trace' => $e->getTrace()], |
|
| 141 | + $this->pm |
|
| 142 | + ); |
|
| 143 | + return false; |
|
| 144 | + } |
|
| 145 | + return true; |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + |
|
| 149 | + /** |
|
| 150 | + * Delete PM option. |
|
| 151 | + * |
|
| 152 | + * @param string $name |
|
| 153 | + * @return bool |
|
| 154 | + */ |
|
| 155 | + public function deleteOption(string $name): bool |
|
| 156 | + { |
|
| 157 | + $meta_data = $this->getMetaData(); |
|
| 158 | + if (! $meta_data) { |
|
| 159 | + return false; |
|
| 160 | + } |
|
| 161 | + // If the option does not exist, don't bother saving, but return true anyways. |
|
| 162 | + if (! isset($meta_data[ $name ])) { |
|
| 163 | + return true; |
|
| 164 | + } |
|
| 165 | + unset($meta_data[ $name ]); |
|
| 166 | + return $this->saveMetaData($meta_data); |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + |
|
| 170 | + /** |
|
| 171 | + * Delete all the PM extra meta. |
|
| 172 | + * |
|
| 173 | + * @return bool |
|
| 174 | + */ |
|
| 175 | + public function deleteAllMetaData(): bool |
|
| 176 | + { |
|
| 177 | + return $this->deleteMetaData(Domain::META_KEY_PAYPAL_DATA) |
|
| 178 | + && $this->deleteMetaData(Domain::META_KEY_PAYPAL_DATA_SANDBOX); |
|
| 179 | + } |
|
| 180 | + |
|
| 181 | + |
|
| 182 | + /** |
|
| 183 | + * Delete the PM extra meta. |
|
| 184 | + * |
|
| 185 | + * @param null $metadata_key |
|
| 186 | + * @return bool |
|
| 187 | + */ |
|
| 188 | + public function deleteMetaData($metadata_key = null): bool |
|
| 189 | + { |
|
| 190 | + try { |
|
| 191 | + // Related to PM and sandbox mode metadata (default), or by the provided metadata key? |
|
| 192 | + $metadata_key = $metadata_key ?? $this->metadata_key; |
|
| 193 | + $this->pm->delete_extra_meta($metadata_key); |
|
| 194 | + } catch (Exception $e) { |
|
| 195 | + PayPalLogger::errorLog( |
|
| 196 | + sprintf( |
|
| 197 | + esc_html__('Error deleting the PM meta data: %1$s', 'event_espresso'), |
|
| 198 | + $e->getMessage() |
|
| 199 | + ), |
|
| 200 | + ['trace' => $e->getTrace()], |
|
| 201 | + $this->pm |
|
| 202 | + ); |
|
| 203 | + return false; |
|
| 204 | + } |
|
| 205 | + return true; |
|
| 206 | + } |
|
| 207 | 207 | } |
@@ -75,7 +75,7 @@ discard block |
||
| 75 | 75 | public function getOption(string $option_name) |
| 76 | 76 | { |
| 77 | 77 | $meta_data = $this->getMetaData(); |
| 78 | - return $meta_data[ $option_name ] ?? false; |
|
| 78 | + return $meta_data[$option_name] ?? false; |
|
| 79 | 79 | } |
| 80 | 80 | |
| 81 | 81 | |
@@ -113,10 +113,10 @@ discard block |
||
| 113 | 113 | public function saveOption(string $name, $value): bool |
| 114 | 114 | { |
| 115 | 115 | $meta_data = $this->getMetaData(); |
| 116 | - if (! $meta_data) { |
|
| 116 | + if ( ! $meta_data) { |
|
| 117 | 117 | $meta_data = []; |
| 118 | 118 | } |
| 119 | - $meta_data[ $name ] = $value; |
|
| 119 | + $meta_data[$name] = $value; |
|
| 120 | 120 | return $this->saveMetaData($meta_data); |
| 121 | 121 | } |
| 122 | 122 | |
@@ -155,14 +155,14 @@ discard block |
||
| 155 | 155 | public function deleteOption(string $name): bool |
| 156 | 156 | { |
| 157 | 157 | $meta_data = $this->getMetaData(); |
| 158 | - if (! $meta_data) { |
|
| 158 | + if ( ! $meta_data) { |
|
| 159 | 159 | return false; |
| 160 | 160 | } |
| 161 | 161 | // If the option does not exist, don't bother saving, but return true anyways. |
| 162 | - if (! isset($meta_data[ $name ])) { |
|
| 162 | + if ( ! isset($meta_data[$name])) { |
|
| 163 | 163 | return true; |
| 164 | 164 | } |
| 165 | - unset($meta_data[ $name ]); |
|
| 165 | + unset($meta_data[$name]); |
|
| 166 | 166 | return $this->saveMetaData($meta_data); |
| 167 | 167 | } |
| 168 | 168 | |
@@ -22,380 +22,380 @@ |
||
| 22 | 22 | */ |
| 23 | 23 | class PayPalExtraMetaManager |
| 24 | 24 | { |
| 25 | - private static ?OpenSSLEncryption $encryptor = null; |
|
| 26 | - |
|
| 27 | - private static ?PayPalExtraMeta $pay_pal_extra_meta = null; |
|
| 28 | - |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * Get PayPal extra meta helper. |
|
| 32 | - * |
|
| 33 | - * @param EE_Payment_Method $paypal_pm |
|
| 34 | - * @return PayPalExtraMeta |
|
| 35 | - * @throws EE_Error |
|
| 36 | - * @throws ReflectionException |
|
| 37 | - */ |
|
| 38 | - public static function extraMeta(EE_Payment_Method $paypal_pm): PayPalExtraMeta |
|
| 39 | - { |
|
| 40 | - if (! PayPalExtraMetaManager::$pay_pal_extra_meta instanceof PayPalExtraMeta) { |
|
| 41 | - PayPalExtraMetaManager::$pay_pal_extra_meta = new PayPalExtraMeta($paypal_pm); |
|
| 42 | - } |
|
| 43 | - return PayPalExtraMetaManager::$pay_pal_extra_meta; |
|
| 44 | - } |
|
| 45 | - |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * Get OpenSSLEncryption instance. |
|
| 49 | - * |
|
| 50 | - * @return OpenSSLEncryption |
|
| 51 | - */ |
|
| 52 | - public static function encryptor(): OpenSSLEncryption |
|
| 53 | - { |
|
| 54 | - if (! PayPalExtraMetaManager::$encryptor instanceof OpenSSLEncryption) { |
|
| 55 | - PayPalExtraMetaManager::$encryptor = LoaderFactory::getLoader()->getShared( |
|
| 56 | - OpenSSLEncryption::class, |
|
| 57 | - [new Base64Encoder()] |
|
| 58 | - ); |
|
| 59 | - } |
|
| 60 | - return PayPalExtraMetaManager::$encryptor; |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * Get payment method option/extra meta |
|
| 66 | - * |
|
| 67 | - * @param EE_Payment_Method $paypal_pm |
|
| 68 | - * @param string $option_name |
|
| 69 | - * @return mixed |
|
| 70 | - * @throws EE_Error |
|
| 71 | - * @throws ReflectionException |
|
| 72 | - */ |
|
| 73 | - public static function getPmOption(EE_Payment_Method $paypal_pm, string $option_name) |
|
| 74 | - { |
|
| 75 | - $option_value = PayPalExtraMetaManager::extraMeta($paypal_pm)->getOption($option_name); |
|
| 76 | - // Decrypt the encrypted options. |
|
| 77 | - if ( |
|
| 78 | - $option_name === Domain::META_KEY_ACCESS_TOKEN |
|
| 79 | - || $option_name === Domain::META_KEY_PARTNER_MERCHANT_ID |
|
| 80 | - || $option_name === Domain::META_KEY_CLIENT_SECRET |
|
| 81 | - ) { |
|
| 82 | - $option_value = PayPalExtraMetaManager::decryptString($option_value, $paypal_pm); |
|
| 83 | - } |
|
| 84 | - return $option_value; |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - |
|
| 88 | - /** |
|
| 89 | - * Save payment method option/extra meta |
|
| 90 | - * |
|
| 91 | - * @param EE_Payment_Method $paypal_pm |
|
| 92 | - * @param string $option_name |
|
| 93 | - * @param $option_value |
|
| 94 | - * @return bool |
|
| 95 | - * @throws EE_Error |
|
| 96 | - * @throws ReflectionException |
|
| 97 | - */ |
|
| 98 | - public static function savePmOption(EE_Payment_Method $paypal_pm, string $option_name, $option_value): bool |
|
| 99 | - { |
|
| 100 | - return PayPalExtraMetaManager::extraMeta($paypal_pm)->saveOption($option_name, $option_value); |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - |
|
| 104 | - /** |
|
| 105 | - * Save a list of payment method options/extra meta. |
|
| 106 | - * |
|
| 107 | - * @param EE_Payment_Method $paypal_pm |
|
| 108 | - * @param array $options_list |
|
| 109 | - * @return bool |
|
| 110 | - * @throws EE_Error |
|
| 111 | - * @throws ReflectionException |
|
| 112 | - */ |
|
| 113 | - public static function savePmOptions(EE_Payment_Method $paypal_pm, array $options_list): bool |
|
| 114 | - { |
|
| 115 | - return PayPalExtraMetaManager::extraMeta($paypal_pm)->saveBatch($options_list); |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - |
|
| 119 | - /** |
|
| 120 | - * Delete payment method option/extra meta |
|
| 121 | - * |
|
| 122 | - * @param EE_Payment_Method $paypal_pm |
|
| 123 | - * @param string $option_name |
|
| 124 | - * @return bool |
|
| 125 | - * @throws EE_Error |
|
| 126 | - * @throws ReflectionException |
|
| 127 | - */ |
|
| 128 | - public static function deletePmOption(EE_Payment_Method $paypal_pm, string $option_name): bool |
|
| 129 | - { |
|
| 130 | - return PayPalExtraMetaManager::extraMeta($paypal_pm)->deleteOption($option_name); |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - |
|
| 134 | - /** |
|
| 135 | - * Get all options for payment method. |
|
| 136 | - * |
|
| 137 | - * @param EE_Payment_Method $paypal_pm |
|
| 138 | - * @return array |
|
| 139 | - * @throws EE_Error |
|
| 140 | - * @throws ReflectionException |
|
| 141 | - */ |
|
| 142 | - public static function getAllData(EE_Payment_Method $paypal_pm): array |
|
| 143 | - { |
|
| 144 | - return PayPalExtraMetaManager::extraMeta($paypal_pm)->getMetaData(); |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - |
|
| 148 | - /** |
|
| 149 | - * Delete payment method metadata. |
|
| 150 | - * |
|
| 151 | - * @param EE_Payment_Method $paypal_pm |
|
| 152 | - * @return bool |
|
| 153 | - * @throws EE_Error |
|
| 154 | - * @throws ReflectionException |
|
| 155 | - */ |
|
| 156 | - public static function deleteData(EE_Payment_Method $paypal_pm): bool |
|
| 157 | - { |
|
| 158 | - return PayPalExtraMetaManager::extraMeta($paypal_pm)->deleteMetaData(); |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - |
|
| 162 | - /** |
|
| 163 | - * Delete all payment method metadata. |
|
| 164 | - * |
|
| 165 | - * @param EE_Payment_Method $paypal_pm |
|
| 166 | - * @return bool |
|
| 167 | - * @throws EE_Error |
|
| 168 | - * @throws ReflectionException |
|
| 169 | - */ |
|
| 170 | - public static function deleteAllData(EE_Payment_Method $paypal_pm): bool |
|
| 171 | - { |
|
| 172 | - return PayPalExtraMetaManager::extraMeta($paypal_pm)->deleteAllMetaData(); |
|
| 173 | - } |
|
| 174 | - |
|
| 175 | - /** |
|
| 176 | - * Save the debug mode option if it changed. |
|
| 177 | - * |
|
| 178 | - * @param EE_Payment_Method $paypal_pm |
|
| 179 | - * @param array $request_data |
|
| 180 | - * @return bool Updated or not. |
|
| 181 | - * @throws EE_Error |
|
| 182 | - * @throws ReflectionException |
|
| 183 | - */ |
|
| 184 | - public static function updateDebugMode(EE_Payment_Method $paypal_pm, array $request_data): bool |
|
| 185 | - { |
|
| 186 | - if ( |
|
| 187 | - isset($request_data['sandbox_mode']) |
|
| 188 | - && in_array($request_data['sandbox_mode'], ['0', '1'], true) |
|
| 189 | - && $paypal_pm->debug_mode() !== (bool) $request_data['sandbox_mode'] |
|
| 190 | - ) { |
|
| 191 | - try { |
|
| 192 | - $paypal_pm->save(['PMD_debug_mode' => (bool) $request_data['sandbox_mode']]); |
|
| 193 | - } catch (EE_Error $e) { |
|
| 194 | - PayPalLogger::errorLog( |
|
| 195 | - sprintf( |
|
| 196 | - esc_html__('Note, debug mode not saved ! %1$s', 'event_espresso'), |
|
| 197 | - $e->getMessage() |
|
| 198 | - ), |
|
| 199 | - ['request_data' => $request_data, 'trace' => $e->getTrace()], |
|
| 200 | - $paypal_pm |
|
| 201 | - ); |
|
| 202 | - return false; |
|
| 203 | - } |
|
| 204 | - return true; |
|
| 205 | - } |
|
| 206 | - return false; |
|
| 207 | - } |
|
| 208 | - |
|
| 209 | - |
|
| 210 | - /** |
|
| 211 | - * Save partner access token and parameters. |
|
| 212 | - * |
|
| 213 | - * @param EE_Payment_Method $paypal_pm |
|
| 214 | - * @param array $response |
|
| 215 | - * @return bool |
|
| 216 | - * @throws EE_Error |
|
| 217 | - * @throws ReflectionException |
|
| 218 | - */ |
|
| 219 | - public static function savePartnerAccessToken(EE_Payment_Method $paypal_pm, array $response): bool |
|
| 220 | - { |
|
| 221 | - $paypal_data = []; |
|
| 222 | - $expected_parameters = [ |
|
| 223 | - Domain::META_KEY_ACCESS_TOKEN, |
|
| 224 | - Domain::META_KEY_TOKEN_EXPIRES_IN, |
|
| 225 | - Domain::META_KEY_APP_ID, |
|
| 226 | - Domain::META_KEY_PARTNER_CLIENT_ID, |
|
| 227 | - Domain::META_KEY_PARTNER_MERCHANT_ID, |
|
| 228 | - Domain::META_KEY_BN_CODE, |
|
| 229 | - ]; |
|
| 230 | - foreach ($expected_parameters as $api_key) { |
|
| 231 | - if (! isset($response[ $api_key ])) { |
|
| 232 | - // Don't want to try saving data that doesn't exist. |
|
| 233 | - continue; |
|
| 234 | - } |
|
| 235 | - try { |
|
| 236 | - switch ($api_key) { |
|
| 237 | - case Domain::META_KEY_ACCESS_TOKEN: |
|
| 238 | - case Domain::META_KEY_PARTNER_MERCHANT_ID: |
|
| 239 | - $paypal_data[ $api_key ] = PayPalExtraMetaManager::encryptString( |
|
| 240 | - $response[ $api_key ], |
|
| 241 | - $paypal_pm |
|
| 242 | - ); |
|
| 243 | - break; |
|
| 244 | - case Domain::META_KEY_TOKEN_EXPIRES_IN: |
|
| 245 | - $paypal_data[ $api_key ] = time() + (int) sanitize_key($response[ $api_key ]); |
|
| 246 | - break; |
|
| 247 | - default: |
|
| 248 | - $paypal_data[ $api_key ] = sanitize_text_field($response[ $api_key ]); |
|
| 249 | - } |
|
| 250 | - } catch (Exception $e) { |
|
| 251 | - PayPalLogger::errorLog( |
|
| 252 | - $e->getMessage(), |
|
| 253 | - ['response' => $response, 'trace' => $e->getTrace()], |
|
| 254 | - $paypal_pm |
|
| 255 | - ); |
|
| 256 | - return false; |
|
| 257 | - } |
|
| 258 | - } |
|
| 259 | - return PayPalExtraMetaManager::savePmOptions($paypal_pm, $paypal_data); |
|
| 260 | - } |
|
| 261 | - |
|
| 262 | - |
|
| 263 | - /** |
|
| 264 | - * Save merchant/seller API credentials. |
|
| 265 | - * |
|
| 266 | - * @param EE_Payment_Method $paypal_pm |
|
| 267 | - * @param array $response |
|
| 268 | - * @return bool |
|
| 269 | - * @throws EE_Error |
|
| 270 | - * @throws ReflectionException |
|
| 271 | - */ |
|
| 272 | - public static function saveSellerApiCredentials(EE_Payment_Method $paypal_pm, array $response): bool |
|
| 273 | - { |
|
| 274 | - $api_credentials = []; |
|
| 275 | - $expected_parameters = [Domain::META_KEY_SELLER_MERCHANT_ID]; |
|
| 276 | - foreach ($expected_parameters as $api_key) { |
|
| 277 | - if (! isset($response[ $api_key ])) { |
|
| 278 | - // Don't want to try saving data that doesn't exist. |
|
| 279 | - continue; |
|
| 280 | - } |
|
| 281 | - $api_credentials[ $api_key ] = $response[ $api_key ]; |
|
| 282 | - } |
|
| 283 | - return PayPalExtraMetaManager::savePmOptions($paypal_pm, $api_credentials); |
|
| 284 | - } |
|
| 285 | - |
|
| 286 | - |
|
| 287 | - /** |
|
| 288 | - * Save other payment method related settings from a data array. |
|
| 289 | - * |
|
| 290 | - * @param EE_Payment_Method $paypal_pm |
|
| 291 | - * @param array $data |
|
| 292 | - * @return bool |
|
| 293 | - * @throws EE_Error |
|
| 294 | - * @throws ReflectionException |
|
| 295 | - */ |
|
| 296 | - public static function parseAndSaveOptions(EE_Payment_Method $paypal_pm, array $data): bool |
|
| 297 | - { |
|
| 298 | - $allowed_checkout_type = 'express_checkout'; |
|
| 299 | - // Note, although PayPal shows that this should include PPCP_CUSTOM or EXPRESS_CHECKOUT only, |
|
| 300 | - // in reality, it will also include other products like MOBILE_PAYMENT_ACCEPTANCE etc. |
|
| 301 | - if (! empty($data['response']['products'][0]['name'])) { |
|
| 302 | - foreach ($data['response']['products'] as $product) { |
|
| 303 | - if (str_contains($product['name'], 'PPCP')) { |
|
| 304 | - // This merchant has PPCP in the products list, so we can enable both (all) checkout types. |
|
| 305 | - $allowed_checkout_type = 'all'; |
|
| 306 | - break; |
|
| 307 | - } |
|
| 308 | - } |
|
| 309 | - } |
|
| 310 | - // Set the Checkout type (a PM option), just in case merchant doesn't save PM options manually. |
|
| 311 | - $checkout_type = $paypal_pm->get_extra_meta(Domain::META_KEY_CHECKOUT_TYPE, true, false); |
|
| 312 | - if (! $checkout_type) { |
|
| 313 | - $paypal_pm->update_extra_meta(Domain::META_KEY_CHECKOUT_TYPE, $allowed_checkout_type); |
|
| 314 | - } |
|
| 315 | - // Save the scopes that were authorized. |
|
| 316 | - if (! empty($data['response']['oauth_integrations'][0]['oauth_third_party'][0]['scopes'])) { |
|
| 317 | - $scopes = []; |
|
| 318 | - foreach ($data['response']['oauth_integrations'][0]['oauth_third_party'][0]['scopes'] as $scope) { |
|
| 319 | - // Scope will look like: 'https://uri.paypal.com/services/payments/partnerfee' |
|
| 320 | - $split = explode('/', $scope); |
|
| 321 | - $split_count = count($split); |
|
| 322 | - // Get the scope itself. |
|
| 323 | - $scopes[] = $split[ $split_count - 1 ]; |
|
| 324 | - } |
|
| 325 | - if (empty($scopes)) { |
|
| 326 | - // In case the there's a change in how scopes come in just save the list. |
|
| 327 | - $scopes = $data['response']['oauth_integrations'][0]['oauth_third_party'][0]['scopes']; |
|
| 328 | - } |
|
| 329 | - PayPalExtraMetaManager::savePmOption($paypal_pm, Domain::META_KEY_AUTHORIZED_SCOPES, $scopes); |
|
| 330 | - } |
|
| 331 | - return PayPalExtraMetaManager::savePmOption( |
|
| 332 | - $paypal_pm, |
|
| 333 | - Domain::META_KEY_ALLOWED_CHECKOUT_TYPE, |
|
| 334 | - $allowed_checkout_type |
|
| 335 | - ); |
|
| 336 | - } |
|
| 337 | - |
|
| 338 | - |
|
| 339 | - /** |
|
| 340 | - * Encrypt a text field. |
|
| 341 | - * |
|
| 342 | - * @param string $text |
|
| 343 | - * @param EE_Payment_Method $paypal_pm |
|
| 344 | - * @return string|null |
|
| 345 | - * @throws Exception |
|
| 346 | - */ |
|
| 347 | - public static function encryptString(string $text, EE_Payment_Method $paypal_pm): ?string |
|
| 348 | - { |
|
| 349 | - // We sure we are getting something ? |
|
| 350 | - if (! $text) { |
|
| 351 | - return $text; |
|
| 352 | - } |
|
| 353 | - |
|
| 354 | - try { |
|
| 355 | - // Do encrypt. |
|
| 356 | - $sanitized_text = sanitize_text_field($text); |
|
| 357 | - $key_identifier = $paypal_pm->debug_mode() |
|
| 358 | - ? PPCommerceEncryptionKeyManager::SANDBOX_ENCRYPTION_KEY_ID |
|
| 359 | - : PPCommerceEncryptionKeyManager::PRODUCTION_ENCRYPTION_KEY_ID; |
|
| 360 | - $encrypted = PayPalExtraMetaManager::encryptor()->encrypt($sanitized_text, $key_identifier); |
|
| 361 | - } catch (Exception $e) { |
|
| 362 | - PayPalLogger::errorLog( |
|
| 363 | - $e->getMessage(), |
|
| 364 | - ['trace' => $e->getTrace()], |
|
| 365 | - $paypal_pm |
|
| 366 | - ); |
|
| 367 | - } |
|
| 368 | - return $encrypted ?? null; |
|
| 369 | - } |
|
| 370 | - |
|
| 371 | - |
|
| 372 | - /** |
|
| 373 | - * Decrypt a string. |
|
| 374 | - * |
|
| 375 | - * @param string $text |
|
| 376 | - * @param EE_Payment_Method $paypal_pm |
|
| 377 | - * @return string |
|
| 378 | - */ |
|
| 379 | - public static function decryptString(string $text, EE_Payment_Method $paypal_pm): string |
|
| 380 | - { |
|
| 381 | - // Are we even getting something ? |
|
| 382 | - if (! $text) { |
|
| 383 | - return $text; |
|
| 384 | - } |
|
| 385 | - // Try decrypting. |
|
| 386 | - try { |
|
| 387 | - $key_identifier = $paypal_pm->debug_mode() |
|
| 388 | - ? PPCommerceEncryptionKeyManager::SANDBOX_ENCRYPTION_KEY_ID |
|
| 389 | - : PPCommerceEncryptionKeyManager::PRODUCTION_ENCRYPTION_KEY_ID; |
|
| 390 | - $decrypted = PayPalExtraMetaManager::encryptor()->decrypt($text, $key_identifier); |
|
| 391 | - } catch (Exception $e) { |
|
| 392 | - PayPalLogger::errorLog( |
|
| 393 | - $e->getMessage(), |
|
| 394 | - ['trace' => $e->getTrace()], |
|
| 395 | - $paypal_pm |
|
| 396 | - ); |
|
| 397 | - return $text; |
|
| 398 | - } |
|
| 399 | - return $decrypted ?? $text; |
|
| 400 | - } |
|
| 25 | + private static ?OpenSSLEncryption $encryptor = null; |
|
| 26 | + |
|
| 27 | + private static ?PayPalExtraMeta $pay_pal_extra_meta = null; |
|
| 28 | + |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * Get PayPal extra meta helper. |
|
| 32 | + * |
|
| 33 | + * @param EE_Payment_Method $paypal_pm |
|
| 34 | + * @return PayPalExtraMeta |
|
| 35 | + * @throws EE_Error |
|
| 36 | + * @throws ReflectionException |
|
| 37 | + */ |
|
| 38 | + public static function extraMeta(EE_Payment_Method $paypal_pm): PayPalExtraMeta |
|
| 39 | + { |
|
| 40 | + if (! PayPalExtraMetaManager::$pay_pal_extra_meta instanceof PayPalExtraMeta) { |
|
| 41 | + PayPalExtraMetaManager::$pay_pal_extra_meta = new PayPalExtraMeta($paypal_pm); |
|
| 42 | + } |
|
| 43 | + return PayPalExtraMetaManager::$pay_pal_extra_meta; |
|
| 44 | + } |
|
| 45 | + |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * Get OpenSSLEncryption instance. |
|
| 49 | + * |
|
| 50 | + * @return OpenSSLEncryption |
|
| 51 | + */ |
|
| 52 | + public static function encryptor(): OpenSSLEncryption |
|
| 53 | + { |
|
| 54 | + if (! PayPalExtraMetaManager::$encryptor instanceof OpenSSLEncryption) { |
|
| 55 | + PayPalExtraMetaManager::$encryptor = LoaderFactory::getLoader()->getShared( |
|
| 56 | + OpenSSLEncryption::class, |
|
| 57 | + [new Base64Encoder()] |
|
| 58 | + ); |
|
| 59 | + } |
|
| 60 | + return PayPalExtraMetaManager::$encryptor; |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * Get payment method option/extra meta |
|
| 66 | + * |
|
| 67 | + * @param EE_Payment_Method $paypal_pm |
|
| 68 | + * @param string $option_name |
|
| 69 | + * @return mixed |
|
| 70 | + * @throws EE_Error |
|
| 71 | + * @throws ReflectionException |
|
| 72 | + */ |
|
| 73 | + public static function getPmOption(EE_Payment_Method $paypal_pm, string $option_name) |
|
| 74 | + { |
|
| 75 | + $option_value = PayPalExtraMetaManager::extraMeta($paypal_pm)->getOption($option_name); |
|
| 76 | + // Decrypt the encrypted options. |
|
| 77 | + if ( |
|
| 78 | + $option_name === Domain::META_KEY_ACCESS_TOKEN |
|
| 79 | + || $option_name === Domain::META_KEY_PARTNER_MERCHANT_ID |
|
| 80 | + || $option_name === Domain::META_KEY_CLIENT_SECRET |
|
| 81 | + ) { |
|
| 82 | + $option_value = PayPalExtraMetaManager::decryptString($option_value, $paypal_pm); |
|
| 83 | + } |
|
| 84 | + return $option_value; |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + |
|
| 88 | + /** |
|
| 89 | + * Save payment method option/extra meta |
|
| 90 | + * |
|
| 91 | + * @param EE_Payment_Method $paypal_pm |
|
| 92 | + * @param string $option_name |
|
| 93 | + * @param $option_value |
|
| 94 | + * @return bool |
|
| 95 | + * @throws EE_Error |
|
| 96 | + * @throws ReflectionException |
|
| 97 | + */ |
|
| 98 | + public static function savePmOption(EE_Payment_Method $paypal_pm, string $option_name, $option_value): bool |
|
| 99 | + { |
|
| 100 | + return PayPalExtraMetaManager::extraMeta($paypal_pm)->saveOption($option_name, $option_value); |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + |
|
| 104 | + /** |
|
| 105 | + * Save a list of payment method options/extra meta. |
|
| 106 | + * |
|
| 107 | + * @param EE_Payment_Method $paypal_pm |
|
| 108 | + * @param array $options_list |
|
| 109 | + * @return bool |
|
| 110 | + * @throws EE_Error |
|
| 111 | + * @throws ReflectionException |
|
| 112 | + */ |
|
| 113 | + public static function savePmOptions(EE_Payment_Method $paypal_pm, array $options_list): bool |
|
| 114 | + { |
|
| 115 | + return PayPalExtraMetaManager::extraMeta($paypal_pm)->saveBatch($options_list); |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + |
|
| 119 | + /** |
|
| 120 | + * Delete payment method option/extra meta |
|
| 121 | + * |
|
| 122 | + * @param EE_Payment_Method $paypal_pm |
|
| 123 | + * @param string $option_name |
|
| 124 | + * @return bool |
|
| 125 | + * @throws EE_Error |
|
| 126 | + * @throws ReflectionException |
|
| 127 | + */ |
|
| 128 | + public static function deletePmOption(EE_Payment_Method $paypal_pm, string $option_name): bool |
|
| 129 | + { |
|
| 130 | + return PayPalExtraMetaManager::extraMeta($paypal_pm)->deleteOption($option_name); |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + |
|
| 134 | + /** |
|
| 135 | + * Get all options for payment method. |
|
| 136 | + * |
|
| 137 | + * @param EE_Payment_Method $paypal_pm |
|
| 138 | + * @return array |
|
| 139 | + * @throws EE_Error |
|
| 140 | + * @throws ReflectionException |
|
| 141 | + */ |
|
| 142 | + public static function getAllData(EE_Payment_Method $paypal_pm): array |
|
| 143 | + { |
|
| 144 | + return PayPalExtraMetaManager::extraMeta($paypal_pm)->getMetaData(); |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + |
|
| 148 | + /** |
|
| 149 | + * Delete payment method metadata. |
|
| 150 | + * |
|
| 151 | + * @param EE_Payment_Method $paypal_pm |
|
| 152 | + * @return bool |
|
| 153 | + * @throws EE_Error |
|
| 154 | + * @throws ReflectionException |
|
| 155 | + */ |
|
| 156 | + public static function deleteData(EE_Payment_Method $paypal_pm): bool |
|
| 157 | + { |
|
| 158 | + return PayPalExtraMetaManager::extraMeta($paypal_pm)->deleteMetaData(); |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + |
|
| 162 | + /** |
|
| 163 | + * Delete all payment method metadata. |
|
| 164 | + * |
|
| 165 | + * @param EE_Payment_Method $paypal_pm |
|
| 166 | + * @return bool |
|
| 167 | + * @throws EE_Error |
|
| 168 | + * @throws ReflectionException |
|
| 169 | + */ |
|
| 170 | + public static function deleteAllData(EE_Payment_Method $paypal_pm): bool |
|
| 171 | + { |
|
| 172 | + return PayPalExtraMetaManager::extraMeta($paypal_pm)->deleteAllMetaData(); |
|
| 173 | + } |
|
| 174 | + |
|
| 175 | + /** |
|
| 176 | + * Save the debug mode option if it changed. |
|
| 177 | + * |
|
| 178 | + * @param EE_Payment_Method $paypal_pm |
|
| 179 | + * @param array $request_data |
|
| 180 | + * @return bool Updated or not. |
|
| 181 | + * @throws EE_Error |
|
| 182 | + * @throws ReflectionException |
|
| 183 | + */ |
|
| 184 | + public static function updateDebugMode(EE_Payment_Method $paypal_pm, array $request_data): bool |
|
| 185 | + { |
|
| 186 | + if ( |
|
| 187 | + isset($request_data['sandbox_mode']) |
|
| 188 | + && in_array($request_data['sandbox_mode'], ['0', '1'], true) |
|
| 189 | + && $paypal_pm->debug_mode() !== (bool) $request_data['sandbox_mode'] |
|
| 190 | + ) { |
|
| 191 | + try { |
|
| 192 | + $paypal_pm->save(['PMD_debug_mode' => (bool) $request_data['sandbox_mode']]); |
|
| 193 | + } catch (EE_Error $e) { |
|
| 194 | + PayPalLogger::errorLog( |
|
| 195 | + sprintf( |
|
| 196 | + esc_html__('Note, debug mode not saved ! %1$s', 'event_espresso'), |
|
| 197 | + $e->getMessage() |
|
| 198 | + ), |
|
| 199 | + ['request_data' => $request_data, 'trace' => $e->getTrace()], |
|
| 200 | + $paypal_pm |
|
| 201 | + ); |
|
| 202 | + return false; |
|
| 203 | + } |
|
| 204 | + return true; |
|
| 205 | + } |
|
| 206 | + return false; |
|
| 207 | + } |
|
| 208 | + |
|
| 209 | + |
|
| 210 | + /** |
|
| 211 | + * Save partner access token and parameters. |
|
| 212 | + * |
|
| 213 | + * @param EE_Payment_Method $paypal_pm |
|
| 214 | + * @param array $response |
|
| 215 | + * @return bool |
|
| 216 | + * @throws EE_Error |
|
| 217 | + * @throws ReflectionException |
|
| 218 | + */ |
|
| 219 | + public static function savePartnerAccessToken(EE_Payment_Method $paypal_pm, array $response): bool |
|
| 220 | + { |
|
| 221 | + $paypal_data = []; |
|
| 222 | + $expected_parameters = [ |
|
| 223 | + Domain::META_KEY_ACCESS_TOKEN, |
|
| 224 | + Domain::META_KEY_TOKEN_EXPIRES_IN, |
|
| 225 | + Domain::META_KEY_APP_ID, |
|
| 226 | + Domain::META_KEY_PARTNER_CLIENT_ID, |
|
| 227 | + Domain::META_KEY_PARTNER_MERCHANT_ID, |
|
| 228 | + Domain::META_KEY_BN_CODE, |
|
| 229 | + ]; |
|
| 230 | + foreach ($expected_parameters as $api_key) { |
|
| 231 | + if (! isset($response[ $api_key ])) { |
|
| 232 | + // Don't want to try saving data that doesn't exist. |
|
| 233 | + continue; |
|
| 234 | + } |
|
| 235 | + try { |
|
| 236 | + switch ($api_key) { |
|
| 237 | + case Domain::META_KEY_ACCESS_TOKEN: |
|
| 238 | + case Domain::META_KEY_PARTNER_MERCHANT_ID: |
|
| 239 | + $paypal_data[ $api_key ] = PayPalExtraMetaManager::encryptString( |
|
| 240 | + $response[ $api_key ], |
|
| 241 | + $paypal_pm |
|
| 242 | + ); |
|
| 243 | + break; |
|
| 244 | + case Domain::META_KEY_TOKEN_EXPIRES_IN: |
|
| 245 | + $paypal_data[ $api_key ] = time() + (int) sanitize_key($response[ $api_key ]); |
|
| 246 | + break; |
|
| 247 | + default: |
|
| 248 | + $paypal_data[ $api_key ] = sanitize_text_field($response[ $api_key ]); |
|
| 249 | + } |
|
| 250 | + } catch (Exception $e) { |
|
| 251 | + PayPalLogger::errorLog( |
|
| 252 | + $e->getMessage(), |
|
| 253 | + ['response' => $response, 'trace' => $e->getTrace()], |
|
| 254 | + $paypal_pm |
|
| 255 | + ); |
|
| 256 | + return false; |
|
| 257 | + } |
|
| 258 | + } |
|
| 259 | + return PayPalExtraMetaManager::savePmOptions($paypal_pm, $paypal_data); |
|
| 260 | + } |
|
| 261 | + |
|
| 262 | + |
|
| 263 | + /** |
|
| 264 | + * Save merchant/seller API credentials. |
|
| 265 | + * |
|
| 266 | + * @param EE_Payment_Method $paypal_pm |
|
| 267 | + * @param array $response |
|
| 268 | + * @return bool |
|
| 269 | + * @throws EE_Error |
|
| 270 | + * @throws ReflectionException |
|
| 271 | + */ |
|
| 272 | + public static function saveSellerApiCredentials(EE_Payment_Method $paypal_pm, array $response): bool |
|
| 273 | + { |
|
| 274 | + $api_credentials = []; |
|
| 275 | + $expected_parameters = [Domain::META_KEY_SELLER_MERCHANT_ID]; |
|
| 276 | + foreach ($expected_parameters as $api_key) { |
|
| 277 | + if (! isset($response[ $api_key ])) { |
|
| 278 | + // Don't want to try saving data that doesn't exist. |
|
| 279 | + continue; |
|
| 280 | + } |
|
| 281 | + $api_credentials[ $api_key ] = $response[ $api_key ]; |
|
| 282 | + } |
|
| 283 | + return PayPalExtraMetaManager::savePmOptions($paypal_pm, $api_credentials); |
|
| 284 | + } |
|
| 285 | + |
|
| 286 | + |
|
| 287 | + /** |
|
| 288 | + * Save other payment method related settings from a data array. |
|
| 289 | + * |
|
| 290 | + * @param EE_Payment_Method $paypal_pm |
|
| 291 | + * @param array $data |
|
| 292 | + * @return bool |
|
| 293 | + * @throws EE_Error |
|
| 294 | + * @throws ReflectionException |
|
| 295 | + */ |
|
| 296 | + public static function parseAndSaveOptions(EE_Payment_Method $paypal_pm, array $data): bool |
|
| 297 | + { |
|
| 298 | + $allowed_checkout_type = 'express_checkout'; |
|
| 299 | + // Note, although PayPal shows that this should include PPCP_CUSTOM or EXPRESS_CHECKOUT only, |
|
| 300 | + // in reality, it will also include other products like MOBILE_PAYMENT_ACCEPTANCE etc. |
|
| 301 | + if (! empty($data['response']['products'][0]['name'])) { |
|
| 302 | + foreach ($data['response']['products'] as $product) { |
|
| 303 | + if (str_contains($product['name'], 'PPCP')) { |
|
| 304 | + // This merchant has PPCP in the products list, so we can enable both (all) checkout types. |
|
| 305 | + $allowed_checkout_type = 'all'; |
|
| 306 | + break; |
|
| 307 | + } |
|
| 308 | + } |
|
| 309 | + } |
|
| 310 | + // Set the Checkout type (a PM option), just in case merchant doesn't save PM options manually. |
|
| 311 | + $checkout_type = $paypal_pm->get_extra_meta(Domain::META_KEY_CHECKOUT_TYPE, true, false); |
|
| 312 | + if (! $checkout_type) { |
|
| 313 | + $paypal_pm->update_extra_meta(Domain::META_KEY_CHECKOUT_TYPE, $allowed_checkout_type); |
|
| 314 | + } |
|
| 315 | + // Save the scopes that were authorized. |
|
| 316 | + if (! empty($data['response']['oauth_integrations'][0]['oauth_third_party'][0]['scopes'])) { |
|
| 317 | + $scopes = []; |
|
| 318 | + foreach ($data['response']['oauth_integrations'][0]['oauth_third_party'][0]['scopes'] as $scope) { |
|
| 319 | + // Scope will look like: 'https://uri.paypal.com/services/payments/partnerfee' |
|
| 320 | + $split = explode('/', $scope); |
|
| 321 | + $split_count = count($split); |
|
| 322 | + // Get the scope itself. |
|
| 323 | + $scopes[] = $split[ $split_count - 1 ]; |
|
| 324 | + } |
|
| 325 | + if (empty($scopes)) { |
|
| 326 | + // In case the there's a change in how scopes come in just save the list. |
|
| 327 | + $scopes = $data['response']['oauth_integrations'][0]['oauth_third_party'][0]['scopes']; |
|
| 328 | + } |
|
| 329 | + PayPalExtraMetaManager::savePmOption($paypal_pm, Domain::META_KEY_AUTHORIZED_SCOPES, $scopes); |
|
| 330 | + } |
|
| 331 | + return PayPalExtraMetaManager::savePmOption( |
|
| 332 | + $paypal_pm, |
|
| 333 | + Domain::META_KEY_ALLOWED_CHECKOUT_TYPE, |
|
| 334 | + $allowed_checkout_type |
|
| 335 | + ); |
|
| 336 | + } |
|
| 337 | + |
|
| 338 | + |
|
| 339 | + /** |
|
| 340 | + * Encrypt a text field. |
|
| 341 | + * |
|
| 342 | + * @param string $text |
|
| 343 | + * @param EE_Payment_Method $paypal_pm |
|
| 344 | + * @return string|null |
|
| 345 | + * @throws Exception |
|
| 346 | + */ |
|
| 347 | + public static function encryptString(string $text, EE_Payment_Method $paypal_pm): ?string |
|
| 348 | + { |
|
| 349 | + // We sure we are getting something ? |
|
| 350 | + if (! $text) { |
|
| 351 | + return $text; |
|
| 352 | + } |
|
| 353 | + |
|
| 354 | + try { |
|
| 355 | + // Do encrypt. |
|
| 356 | + $sanitized_text = sanitize_text_field($text); |
|
| 357 | + $key_identifier = $paypal_pm->debug_mode() |
|
| 358 | + ? PPCommerceEncryptionKeyManager::SANDBOX_ENCRYPTION_KEY_ID |
|
| 359 | + : PPCommerceEncryptionKeyManager::PRODUCTION_ENCRYPTION_KEY_ID; |
|
| 360 | + $encrypted = PayPalExtraMetaManager::encryptor()->encrypt($sanitized_text, $key_identifier); |
|
| 361 | + } catch (Exception $e) { |
|
| 362 | + PayPalLogger::errorLog( |
|
| 363 | + $e->getMessage(), |
|
| 364 | + ['trace' => $e->getTrace()], |
|
| 365 | + $paypal_pm |
|
| 366 | + ); |
|
| 367 | + } |
|
| 368 | + return $encrypted ?? null; |
|
| 369 | + } |
|
| 370 | + |
|
| 371 | + |
|
| 372 | + /** |
|
| 373 | + * Decrypt a string. |
|
| 374 | + * |
|
| 375 | + * @param string $text |
|
| 376 | + * @param EE_Payment_Method $paypal_pm |
|
| 377 | + * @return string |
|
| 378 | + */ |
|
| 379 | + public static function decryptString(string $text, EE_Payment_Method $paypal_pm): string |
|
| 380 | + { |
|
| 381 | + // Are we even getting something ? |
|
| 382 | + if (! $text) { |
|
| 383 | + return $text; |
|
| 384 | + } |
|
| 385 | + // Try decrypting. |
|
| 386 | + try { |
|
| 387 | + $key_identifier = $paypal_pm->debug_mode() |
|
| 388 | + ? PPCommerceEncryptionKeyManager::SANDBOX_ENCRYPTION_KEY_ID |
|
| 389 | + : PPCommerceEncryptionKeyManager::PRODUCTION_ENCRYPTION_KEY_ID; |
|
| 390 | + $decrypted = PayPalExtraMetaManager::encryptor()->decrypt($text, $key_identifier); |
|
| 391 | + } catch (Exception $e) { |
|
| 392 | + PayPalLogger::errorLog( |
|
| 393 | + $e->getMessage(), |
|
| 394 | + ['trace' => $e->getTrace()], |
|
| 395 | + $paypal_pm |
|
| 396 | + ); |
|
| 397 | + return $text; |
|
| 398 | + } |
|
| 399 | + return $decrypted ?? $text; |
|
| 400 | + } |
|
| 401 | 401 | } |
@@ -37,7 +37,7 @@ discard block |
||
| 37 | 37 | */ |
| 38 | 38 | public static function extraMeta(EE_Payment_Method $paypal_pm): PayPalExtraMeta |
| 39 | 39 | { |
| 40 | - if (! PayPalExtraMetaManager::$pay_pal_extra_meta instanceof PayPalExtraMeta) { |
|
| 40 | + if ( ! PayPalExtraMetaManager::$pay_pal_extra_meta instanceof PayPalExtraMeta) { |
|
| 41 | 41 | PayPalExtraMetaManager::$pay_pal_extra_meta = new PayPalExtraMeta($paypal_pm); |
| 42 | 42 | } |
| 43 | 43 | return PayPalExtraMetaManager::$pay_pal_extra_meta; |
@@ -51,7 +51,7 @@ discard block |
||
| 51 | 51 | */ |
| 52 | 52 | public static function encryptor(): OpenSSLEncryption |
| 53 | 53 | { |
| 54 | - if (! PayPalExtraMetaManager::$encryptor instanceof OpenSSLEncryption) { |
|
| 54 | + if ( ! PayPalExtraMetaManager::$encryptor instanceof OpenSSLEncryption) { |
|
| 55 | 55 | PayPalExtraMetaManager::$encryptor = LoaderFactory::getLoader()->getShared( |
| 56 | 56 | OpenSSLEncryption::class, |
| 57 | 57 | [new Base64Encoder()] |
@@ -228,7 +228,7 @@ discard block |
||
| 228 | 228 | Domain::META_KEY_BN_CODE, |
| 229 | 229 | ]; |
| 230 | 230 | foreach ($expected_parameters as $api_key) { |
| 231 | - if (! isset($response[ $api_key ])) { |
|
| 231 | + if ( ! isset($response[$api_key])) { |
|
| 232 | 232 | // Don't want to try saving data that doesn't exist. |
| 233 | 233 | continue; |
| 234 | 234 | } |
@@ -236,16 +236,16 @@ discard block |
||
| 236 | 236 | switch ($api_key) { |
| 237 | 237 | case Domain::META_KEY_ACCESS_TOKEN: |
| 238 | 238 | case Domain::META_KEY_PARTNER_MERCHANT_ID: |
| 239 | - $paypal_data[ $api_key ] = PayPalExtraMetaManager::encryptString( |
|
| 240 | - $response[ $api_key ], |
|
| 239 | + $paypal_data[$api_key] = PayPalExtraMetaManager::encryptString( |
|
| 240 | + $response[$api_key], |
|
| 241 | 241 | $paypal_pm |
| 242 | 242 | ); |
| 243 | 243 | break; |
| 244 | 244 | case Domain::META_KEY_TOKEN_EXPIRES_IN: |
| 245 | - $paypal_data[ $api_key ] = time() + (int) sanitize_key($response[ $api_key ]); |
|
| 245 | + $paypal_data[$api_key] = time() + (int) sanitize_key($response[$api_key]); |
|
| 246 | 246 | break; |
| 247 | 247 | default: |
| 248 | - $paypal_data[ $api_key ] = sanitize_text_field($response[ $api_key ]); |
|
| 248 | + $paypal_data[$api_key] = sanitize_text_field($response[$api_key]); |
|
| 249 | 249 | } |
| 250 | 250 | } catch (Exception $e) { |
| 251 | 251 | PayPalLogger::errorLog( |
@@ -274,11 +274,11 @@ discard block |
||
| 274 | 274 | $api_credentials = []; |
| 275 | 275 | $expected_parameters = [Domain::META_KEY_SELLER_MERCHANT_ID]; |
| 276 | 276 | foreach ($expected_parameters as $api_key) { |
| 277 | - if (! isset($response[ $api_key ])) { |
|
| 277 | + if ( ! isset($response[$api_key])) { |
|
| 278 | 278 | // Don't want to try saving data that doesn't exist. |
| 279 | 279 | continue; |
| 280 | 280 | } |
| 281 | - $api_credentials[ $api_key ] = $response[ $api_key ]; |
|
| 281 | + $api_credentials[$api_key] = $response[$api_key]; |
|
| 282 | 282 | } |
| 283 | 283 | return PayPalExtraMetaManager::savePmOptions($paypal_pm, $api_credentials); |
| 284 | 284 | } |
@@ -298,7 +298,7 @@ discard block |
||
| 298 | 298 | $allowed_checkout_type = 'express_checkout'; |
| 299 | 299 | // Note, although PayPal shows that this should include PPCP_CUSTOM or EXPRESS_CHECKOUT only, |
| 300 | 300 | // in reality, it will also include other products like MOBILE_PAYMENT_ACCEPTANCE etc. |
| 301 | - if (! empty($data['response']['products'][0]['name'])) { |
|
| 301 | + if ( ! empty($data['response']['products'][0]['name'])) { |
|
| 302 | 302 | foreach ($data['response']['products'] as $product) { |
| 303 | 303 | if (str_contains($product['name'], 'PPCP')) { |
| 304 | 304 | // This merchant has PPCP in the products list, so we can enable both (all) checkout types. |
@@ -309,18 +309,18 @@ discard block |
||
| 309 | 309 | } |
| 310 | 310 | // Set the Checkout type (a PM option), just in case merchant doesn't save PM options manually. |
| 311 | 311 | $checkout_type = $paypal_pm->get_extra_meta(Domain::META_KEY_CHECKOUT_TYPE, true, false); |
| 312 | - if (! $checkout_type) { |
|
| 312 | + if ( ! $checkout_type) { |
|
| 313 | 313 | $paypal_pm->update_extra_meta(Domain::META_KEY_CHECKOUT_TYPE, $allowed_checkout_type); |
| 314 | 314 | } |
| 315 | 315 | // Save the scopes that were authorized. |
| 316 | - if (! empty($data['response']['oauth_integrations'][0]['oauth_third_party'][0]['scopes'])) { |
|
| 316 | + if ( ! empty($data['response']['oauth_integrations'][0]['oauth_third_party'][0]['scopes'])) { |
|
| 317 | 317 | $scopes = []; |
| 318 | 318 | foreach ($data['response']['oauth_integrations'][0]['oauth_third_party'][0]['scopes'] as $scope) { |
| 319 | 319 | // Scope will look like: 'https://uri.paypal.com/services/payments/partnerfee' |
| 320 | 320 | $split = explode('/', $scope); |
| 321 | 321 | $split_count = count($split); |
| 322 | 322 | // Get the scope itself. |
| 323 | - $scopes[] = $split[ $split_count - 1 ]; |
|
| 323 | + $scopes[] = $split[$split_count - 1]; |
|
| 324 | 324 | } |
| 325 | 325 | if (empty($scopes)) { |
| 326 | 326 | // In case the there's a change in how scopes come in just save the list. |
@@ -347,7 +347,7 @@ discard block |
||
| 347 | 347 | public static function encryptString(string $text, EE_Payment_Method $paypal_pm): ?string |
| 348 | 348 | { |
| 349 | 349 | // We sure we are getting something ? |
| 350 | - if (! $text) { |
|
| 350 | + if ( ! $text) { |
|
| 351 | 351 | return $text; |
| 352 | 352 | } |
| 353 | 353 | |
@@ -379,7 +379,7 @@ discard block |
||
| 379 | 379 | public static function decryptString(string $text, EE_Payment_Method $paypal_pm): string |
| 380 | 380 | { |
| 381 | 381 | // Are we even getting something ? |
| 382 | - if (! $text) { |
|
| 382 | + if ( ! $text) { |
|
| 383 | 383 | return $text; |
| 384 | 384 | } |
| 385 | 385 | // Try decrypting. |