Traits
4 years ago
MerchantDetails.php
4 years ago
PayPalAuth.php
4 years ago
PayPalOrder.php
4 years ago
Settings.php
4 years ago
Webhooks.php
4 years ago
MerchantDetails.php
213 lines
| 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\PayPalClient; |
| 8 | use Give\PaymentGateways\PayPalCommerce\Repositories\Traits\HasMode; |
| 9 | |
| 10 | /** |
| 11 | * Class MerchantDetails |
| 12 | * |
| 13 | * @since 2.9.0 |
| 14 | */ |
| 15 | class MerchantDetails |
| 16 | { |
| 17 | use HasMode; |
| 18 | |
| 19 | /** |
| 20 | * Returns whether or not the account has been connected |
| 21 | * |
| 22 | * @since 2.9.0 |
| 23 | * @since 2.9.6 Check for PayPal merchant id to confirm whether or not account is connected. |
| 24 | * |
| 25 | * @return bool |
| 26 | */ |
| 27 | public function accountIsConnected() |
| 28 | { |
| 29 | /* @var $merchantDetails MerchantDetail */ |
| 30 | $merchantDetails = give(MerchantDetail::class); |
| 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.9.0 |
| 127 | * |
| 128 | * @return string |
| 129 | */ |
| 130 | public function getClientToken() |
| 131 | { |
| 132 | $optionName = $this->getClientTokenKey(); |
| 133 | |
| 134 | if ($optionValue = get_transient($optionName)) { |
| 135 | return $optionValue; |
| 136 | } |
| 137 | |
| 138 | /** @var MerchantDetail $merchant */ |
| 139 | $merchant = give(MerchantDetail::class); |
| 140 | |
| 141 | $response = wp_remote_retrieve_body( |
| 142 | wp_remote_post( |
| 143 | give(PayPalClient::class)->getApiUrl('v1/identity/generate-token'), |
| 144 | [ |
| 145 | 'headers' => [ |
| 146 | 'Accept' => 'application/json', |
| 147 | 'Accept-Language' => 'en_US', |
| 148 | 'Authorization' => sprintf( |
| 149 | 'Bearer %1$s', |
| 150 | $merchant->accessToken |
| 151 | ), |
| 152 | 'Content-Type' => 'application/json', |
| 153 | ], |
| 154 | ] |
| 155 | ) |
| 156 | ); |
| 157 | |
| 158 | if ( ! $response) { |
| 159 | return ''; |
| 160 | } |
| 161 | |
| 162 | $response = ArrayDataSet::camelCaseKeys(json_decode($response, true)); |
| 163 | |
| 164 | if ( ! array_key_exists('clientToken', $response)) { |
| 165 | return ''; |
| 166 | } |
| 167 | |
| 168 | set_transient( |
| 169 | $optionName, |
| 170 | $response['clientToken'], |
| 171 | $response['expiresIn'] - 60 // Expire token before one minute to prevent unnecessary race condition. |
| 172 | ); |
| 173 | |
| 174 | return $response['clientToken']; |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Returns the options key for the account in the give mode |
| 179 | * |
| 180 | * @since 2.9.0 |
| 181 | * |
| 182 | * @return string |
| 183 | */ |
| 184 | public function getAccountKey() |
| 185 | { |
| 186 | return "give_paypal_commerce_{$this->mode}_account"; |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Returns the options key for the account errors in the give mode |
| 191 | * |
| 192 | * @since 2.9.0 |
| 193 | * |
| 194 | * @return string |
| 195 | */ |
| 196 | private function getAccountErrorsKey() |
| 197 | { |
| 198 | return "give_paypal_commerce_{$this->mode}_account_errors"; |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Returns the options key for the client token in the give mode |
| 203 | * |
| 204 | * @since 2.9.0 |
| 205 | * |
| 206 | * @return string |
| 207 | */ |
| 208 | private function getClientTokenKey() |
| 209 | { |
| 210 | return "give_paypal_commerce_{$this->mode}_client_token"; |
| 211 | } |
| 212 | } |
| 213 |