Traits
5 years ago
MerchantDetails.php
5 years ago
PayPalAuth.php
5 years ago
PayPalOrder.php
5 years ago
Settings.php
5 years ago
Webhooks.php
5 years ago
MerchantDetails.php
197 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 | use HasMode; |
| 17 | |
| 18 | /** |
| 19 | * Returns whether or not the account has been connected |
| 20 | * |
| 21 | * @since 2.9.0 |
| 22 | * |
| 23 | * @return bool |
| 24 | */ |
| 25 | public function accountIsConnected() { |
| 26 | return (bool) get_option( $this->getAccountKey() ); |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Get merchant details. |
| 31 | * |
| 32 | * @since 2.9.0 |
| 33 | * |
| 34 | * @return MerchantDetail |
| 35 | */ |
| 36 | public function getDetails() { |
| 37 | return MerchantDetail::fromArray( get_option( $this->getAccountKey(), [] ) ); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Save merchant details. |
| 42 | * |
| 43 | * @since 2.9.0 |
| 44 | * |
| 45 | * @param MerchantDetail $merchantDetails |
| 46 | * |
| 47 | * @return bool |
| 48 | */ |
| 49 | public function save( MerchantDetail $merchantDetails ) { |
| 50 | return update_option( $this->getAccountKey(), $merchantDetails->toArray() ); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Delete merchant details. |
| 55 | * |
| 56 | * @since 2.9.0 |
| 57 | * |
| 58 | * @return bool |
| 59 | */ |
| 60 | public function delete() { |
| 61 | return delete_option( $this->getAccountKey() ); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Returns the account errors if there are any |
| 66 | * |
| 67 | * @since 2.9.0 |
| 68 | * |
| 69 | * @return string[]|null |
| 70 | */ |
| 71 | public function getAccountErrors() { |
| 72 | return get_option( $this->getAccountErrorsKey(), null ); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Saves the account error message |
| 77 | * |
| 78 | * @since 2.9.0 |
| 79 | * |
| 80 | * @param string[] $errorMessage |
| 81 | * |
| 82 | * @return bool |
| 83 | */ |
| 84 | public function saveAccountErrors( $errorMessage ) { |
| 85 | return update_option( $this->getAccountErrorsKey(), $errorMessage ); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Deletes the errors for the account |
| 90 | * |
| 91 | * @since 2.9.0 |
| 92 | * |
| 93 | * @return bool |
| 94 | */ |
| 95 | public function deleteAccountErrors() { |
| 96 | return delete_option( $this->getAccountErrorsKey() ); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Deletes the client token for the account |
| 101 | * |
| 102 | * @since 2.9.0 |
| 103 | * |
| 104 | * @return bool |
| 105 | */ |
| 106 | public function deleteClientToken() { |
| 107 | return delete_transient( $this->getClientTokenKey() ); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Get client token for hosted credit card fields. |
| 112 | * |
| 113 | * @since 2.9.0 |
| 114 | * |
| 115 | * @return string |
| 116 | */ |
| 117 | public function getClientToken() { |
| 118 | $optionName = $this->getClientTokenKey(); |
| 119 | |
| 120 | if ( $optionValue = get_transient( $optionName ) ) { |
| 121 | return $optionValue; |
| 122 | } |
| 123 | |
| 124 | /** @var MerchantDetail $merchant */ |
| 125 | $merchant = give( MerchantDetail::class ); |
| 126 | |
| 127 | $response = wp_remote_retrieve_body( |
| 128 | wp_remote_post( |
| 129 | give( PayPalClient::class )->getApiUrl( 'v1/identity/generate-token' ), |
| 130 | [ |
| 131 | 'headers' => [ |
| 132 | 'Accept' => 'application/json', |
| 133 | 'Accept-Language' => 'en_US', |
| 134 | 'Authorization' => sprintf( |
| 135 | 'Bearer %1$s', |
| 136 | $merchant->accessToken |
| 137 | ), |
| 138 | 'Content-Type' => 'application/json', |
| 139 | ], |
| 140 | ] |
| 141 | ) |
| 142 | ); |
| 143 | |
| 144 | if ( ! $response ) { |
| 145 | return ''; |
| 146 | } |
| 147 | |
| 148 | $response = ArrayDataSet::camelCaseKeys( json_decode( $response, true ) ); |
| 149 | |
| 150 | if ( ! array_key_exists( 'clientToken', $response ) ) { |
| 151 | return ''; |
| 152 | } |
| 153 | |
| 154 | set_transient( |
| 155 | $optionName, |
| 156 | $response['clientToken'], |
| 157 | $response['expiresIn'] - 60 // Expire token before one minute to prevent unnecessary race condition. |
| 158 | ); |
| 159 | |
| 160 | return $response['clientToken']; |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Returns the options key for the account in the give mode |
| 165 | * |
| 166 | * @since 2.9.0 |
| 167 | * |
| 168 | * @return string |
| 169 | */ |
| 170 | public function getAccountKey() { |
| 171 | return "give_paypal_commerce_{$this->mode}_account"; |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Returns the options key for the account errors in the give mode |
| 176 | * |
| 177 | * @since 2.9.0 |
| 178 | * |
| 179 | * @return string |
| 180 | */ |
| 181 | private function getAccountErrorsKey() { |
| 182 | return "give_paypal_commerce_{$this->mode}_account_errors"; |
| 183 | } |
| 184 | |
| 185 | |
| 186 | /** |
| 187 | * Returns the options key for the client token in the give mode |
| 188 | * |
| 189 | * @since 2.9.0 |
| 190 | * |
| 191 | * @return string |
| 192 | */ |
| 193 | private function getClientTokenKey() { |
| 194 | return "give_paypal_commerce_{$this->mode}_client_token"; |
| 195 | } |
| 196 | } |
| 197 |