| 1 |
<?php |
| 2 |
|
| 3 |
namespace Give\PaymentGateways\PayPalCommerce\Repositories; |
| 4 |
|
| 5 |
use Give\Helpers\ArrayDataSet; |
| 6 |
use Give\PaymentGateways\PayPalCommerce\Models\MerchantDetail; |
| 7 |
use Give\PaymentGateways\PayPalCommerce\PayPalCheckoutSdk\Requests\GenerateClientToken; |
| 8 |
use Give\PaymentGateways\PayPalCommerce\PayPalClient; |
| 9 |
use Give\PaymentGateways\PayPalCommerce\Repositories\Traits\HasMode; |
| 10 |
|
| 11 |
/** |
| 12 |
* Class MerchantDetails |
| 13 |
* |
| 14 |
* @since 2.9.0 |
| 15 |
*/ |
| 16 |
class MerchantDetails |
| 17 |
{ |
| 18 |
use HasMode; |
| 19 |
|
| 20 |
/** |
| 21 |
* Returns whether or not the account has been connected |
| 22 |
* |
| 23 |
* @since 2.9.0 |
| 24 |
* @since 2.9.6 Check for PayPal merchant id to confirm whether or not account is connected. |
| 25 |
* |
| 26 |
* @return bool |
| 27 |
*/ |
| 28 |
public function accountIsConnected() |
| 29 |
{ |
| 30 |
$merchantDetails = $this->getDetails(); |
| 31 |
|
| 32 |
return (bool)$merchantDetails->merchantIdInPayPal; |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Get merchant details. |
| 37 |
* |
| 38 |
* @since 2.9.0 |
| 39 |
* |
| 40 |
* @return MerchantDetail |
| 41 |
*/ |
| 42 |
public function getDetails() |
| 43 |
{ |
| 44 |
return MerchantDetail::fromArray(get_option($this->getAccountKey(), [])); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Save merchant details. |
| 49 |
* |
| 50 |
* @since 2.9.0 |
| 51 |
* |
| 52 |
* @param MerchantDetail $merchantDetails |
| 53 |
* |
| 54 |
* @return bool |
| 55 |
*/ |
| 56 |
public function save(MerchantDetail $merchantDetails) |
| 57 |
{ |
| 58 |
return update_option($this->getAccountKey(), $merchantDetails->toArray()); |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Delete merchant details. |
| 63 |
* |
| 64 |
* @since 2.9.0 |
| 65 |
* |
| 66 |
* @return bool |
| 67 |
*/ |
| 68 |
public function delete() |
| 69 |
{ |
| 70 |
return delete_option($this->getAccountKey()); |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Returns the account errors if there are any |
| 75 |
* |
| 76 |
* @since 2.9.0 |
| 77 |
* |
| 78 |
* @return string[]|null |
| 79 |
*/ |
| 80 |
public function getAccountErrors() |
| 81 |
{ |
| 82 |
return get_option($this->getAccountErrorsKey(), null); |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Saves the account error message |
| 87 |
* |
| 88 |
* @since 2.9.0 |
| 89 |
* |
| 90 |
* @param string[] $errorMessage |
| 91 |
* |
| 92 |
* @return bool |
| 93 |
*/ |
| 94 |
public function saveAccountErrors($errorMessage) |
| 95 |
{ |
| 96 |
return update_option($this->getAccountErrorsKey(), $errorMessage); |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Deletes the errors for the account |
| 101 |
* |
| 102 |
* @since 2.9.0 |
| 103 |
* |
| 104 |
* @return bool |
| 105 |
*/ |
| 106 |
public function deleteAccountErrors() |
| 107 |
{ |
| 108 |
return delete_option($this->getAccountErrorsKey()); |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Deletes the client token for the account |
| 113 |
* |
| 114 |
* @since 2.9.0 |
| 115 |
* |
| 116 |
* @return bool |
| 117 |
*/ |
| 118 |
public function deleteClientToken() |
| 119 |
{ |
| 120 |
return delete_transient($this->getClientTokenKey()); |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Get client token for hosted credit card fields. |
| 125 |
* |
| 126 |
* @since 2.30.0 Use PayPal client to generate client token. |
| 127 |
* @since 2.9.0 |
| 128 |
* |
| 129 |
* @return string |
| 130 |
* @throws \Exception If there is an error generating the client token. |
| 131 |
*/ |
| 132 |
public function getClientToken(): string |
| 133 |
{ |
| 134 |
$optionName = $this->getClientTokenKey(); |
| 135 |
|
| 136 |
if ($optionValue = get_transient($optionName)) { |
| 137 |
return $optionValue; |
| 138 |
} |
| 139 |
|
| 140 |
try { |
| 141 |
$response = give(PayPalClient::class) |
| 142 |
->getHttpClient() |
| 143 |
->execute(new GenerateClientToken()); |
| 144 |
|
| 145 |
// If the response is empty or does not have the client token, return empty string. |
| 146 |
if ( |
| 147 |
$response->statusCode !== 200 |
| 148 |
|| ! property_exists($response->result, 'client_token') |
| 149 |
) { |
| 150 |
throw new \Exception(esc_html__('Unable to generate client token.', 'give')); |
| 151 |
} |
| 152 |
|
| 153 |
// Save the client token in the transient. |
| 154 |
set_transient( |
| 155 |
$optionName, |
| 156 |
$response->result->client_token, |
| 157 |
$response->result->expires_in - 60 // Expire token before one minute to prevent unnecessary race condition. |
| 158 |
); |
| 159 |
} catch (\Exception $e) { |
| 160 |
throw new \Exception($e->getMessage()); |
| 161 |
} |
| 162 |
|
| 163 |
return $response->result->client_token; |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Returns the options key for the account in the give mode |
| 168 |
* |
| 169 |
* @since 2.9.0 |
| 170 |
* |
| 171 |
* @return string |
| 172 |
*/ |
| 173 |
public function getAccountKey() |
| 174 |
{ |
| 175 |
return "give_paypal_commerce_{$this->mode}_account"; |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* Returns the options key for the account errors in the give mode |
| 180 |
* |
| 181 |
* @since 2.9.0 |
| 182 |
* |
| 183 |
* @return string |
| 184 |
*/ |
| 185 |
private function getAccountErrorsKey() |
| 186 |
{ |
| 187 |
return "give_paypal_commerce_{$this->mode}_account_errors"; |
| 188 |
} |
| 189 |
|
| 190 |
/** |
| 191 |
* Returns the options key for the client token in the give mode |
| 192 |
* |
| 193 |
* @since 2.9.0 |
| 194 |
* |
| 195 |
* @return string |
| 196 |
*/ |
| 197 |
private function getClientTokenKey() |
| 198 |
{ |
| 199 |
return "give_paypal_commerce_{$this->mode}_client_token"; |
| 200 |
} |
| 201 |
} |
| 202 |
|