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
PayPalAuth.php
185 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\PaymentGateways\PayPalCommerce\Repositories; |
| 4 | |
| 5 | use Give\ConnectClient\ConnectClient; |
| 6 | use Give\Helpers\ArrayDataSet; |
| 7 | use Give\PaymentGateways\PayPalCommerce\PayPalClient; |
| 8 | |
| 9 | class PayPalAuth { |
| 10 | /** |
| 11 | * @since 2.9.0 |
| 12 | * |
| 13 | * @var PayPalClient |
| 14 | */ |
| 15 | private $payPalClient; |
| 16 | |
| 17 | /** |
| 18 | * @since 2.9.0 |
| 19 | * |
| 20 | * @var ConnectClient |
| 21 | */ |
| 22 | private $connectClient; |
| 23 | |
| 24 | /** |
| 25 | * PayPalAuth constructor. |
| 26 | * |
| 27 | * @since 2.9.0 |
| 28 | * |
| 29 | * @param PayPalClient $payPalClient |
| 30 | * @param ConnectClient $connectClient |
| 31 | */ |
| 32 | public function __construct( PayPalClient $payPalClient, ConnectClient $connectClient ) { |
| 33 | $this->payPalClient = $payPalClient; |
| 34 | $this->connectClient = $connectClient; |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Retrieves a token for the Client ID and Secret |
| 39 | * |
| 40 | * @since 2.9.0 |
| 41 | * |
| 42 | * @param string $client_id |
| 43 | * @param string $client_secret |
| 44 | * |
| 45 | * @return array |
| 46 | */ |
| 47 | public function getTokenFromClientCredentials( $client_id, $client_secret ) { |
| 48 | $auth = base64_encode( "$client_id:$client_secret" ); |
| 49 | |
| 50 | $request = wp_remote_post( |
| 51 | $this->payPalClient->getApiUrl( 'v1/oauth2/token' ), |
| 52 | [ |
| 53 | 'headers' => [ |
| 54 | 'Authorization' => "Basic $auth", |
| 55 | 'Content-Type' => 'application/x-www-form-urlencoded', |
| 56 | ], |
| 57 | 'body' => [ |
| 58 | 'grant_type' => 'client_credentials', |
| 59 | ], |
| 60 | ] |
| 61 | ); |
| 62 | |
| 63 | return ArrayDataSet::camelCaseKeys( json_decode( wp_remote_retrieve_body( $request ), true ) ); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Retrieves a token from the authorization code |
| 68 | * |
| 69 | * @since 2.9.0 |
| 70 | * |
| 71 | * @param string $authCode |
| 72 | * @param string $sharedId |
| 73 | * @param string $nonce |
| 74 | * |
| 75 | * @return array|null |
| 76 | */ |
| 77 | public function getTokenFromAuthorizationCode( $authCode, $sharedId, $nonce ) { |
| 78 | $response = wp_remote_retrieve_body( |
| 79 | wp_remote_post( |
| 80 | $this->payPalClient->getApiUrl( 'v1/oauth2/token' ), |
| 81 | [ |
| 82 | 'headers' => [ |
| 83 | 'Authorization' => sprintf( |
| 84 | 'Basic %1$s', |
| 85 | base64_encode( $sharedId ) |
| 86 | ), |
| 87 | 'Content-Type' => 'application/x-www-form-urlencoded', |
| 88 | ], |
| 89 | 'body' => [ |
| 90 | 'grant_type' => 'authorization_code', |
| 91 | 'code' => $authCode, |
| 92 | 'code_verifier' => $nonce, // Seller nonce. |
| 93 | ], |
| 94 | ] |
| 95 | ) |
| 96 | ); |
| 97 | |
| 98 | return empty( $response ) ? null : ArrayDataSet::camelCaseKeys( json_decode( $response, true ) ); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Retrieves a Partner Link for on-boarding |
| 103 | * |
| 104 | * @param $returnUrl |
| 105 | * @param $country |
| 106 | * |
| 107 | * @return array|null |
| 108 | */ |
| 109 | public function getSellerPartnerLink( $returnUrl, $country ) { |
| 110 | $response = wp_remote_retrieve_body( |
| 111 | wp_remote_post( |
| 112 | sprintf( |
| 113 | $this->connectClient->getApiUrl( 'paypal?mode=%1$s&request=partner-link' ), |
| 114 | $this->payPalClient->mode |
| 115 | ), |
| 116 | [ |
| 117 | 'body' => [ |
| 118 | 'return_url' => $returnUrl, |
| 119 | 'country_code' => $country, |
| 120 | ], |
| 121 | ] |
| 122 | ) |
| 123 | ); |
| 124 | |
| 125 | return empty( $response ) ? null : json_decode( $response, true ); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Get seller on-boarding details from seller. |
| 130 | * |
| 131 | * @since 2.9.0 |
| 132 | * |
| 133 | * @param string $accessToken |
| 134 | * |
| 135 | * @param string $merchantId |
| 136 | * |
| 137 | * @return array |
| 138 | */ |
| 139 | public function getSellerOnBoardingDetailsFromPayPal( $merchantId, $accessToken ) { |
| 140 | $request = wp_remote_post( |
| 141 | $this->connectClient->getApiUrl( |
| 142 | sprintf( |
| 143 | 'paypal?mode=%1$s&request=seller-status', |
| 144 | $this->payPalClient->mode |
| 145 | ) |
| 146 | ), |
| 147 | [ |
| 148 | 'body' => [ |
| 149 | 'merchant_id' => $merchantId, |
| 150 | 'token' => $accessToken, |
| 151 | ], |
| 152 | ] |
| 153 | ); |
| 154 | |
| 155 | return json_decode( wp_remote_retrieve_body( $request ), true ); |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Get seller rest API credentials |
| 160 | * |
| 161 | * @since 2.9.0 |
| 162 | * |
| 163 | * @param string $accessToken |
| 164 | * |
| 165 | * @return array |
| 166 | */ |
| 167 | public function getSellerRestAPICredentials( $accessToken ) { |
| 168 | $request = wp_remote_post( |
| 169 | $this->connectClient->getApiUrl( |
| 170 | sprintf( |
| 171 | 'paypal?mode=%1$s&request=seller-credentials', |
| 172 | $this->payPalClient->mode |
| 173 | ) |
| 174 | ), |
| 175 | [ |
| 176 | 'body' => [ |
| 177 | 'token' => $accessToken, |
| 178 | ], |
| 179 | ] |
| 180 | ); |
| 181 | |
| 182 | return json_decode( wp_remote_retrieve_body( $request ), true ); |
| 183 | } |
| 184 | } |
| 185 |