DataTransferObjects
5 years ago
Models
5 years ago
Repositories
5 years ago
Webhooks
5 years ago
AccountAdminNotices.php
5 years ago
AdminSettingFields.php
5 years ago
AdvancedCardFields.php
5 years ago
AjaxRequestHandler.php
5 years ago
DonationDetailsPage.php
5 years ago
DonationFormPaymentMethod.php
5 years ago
DonationProcessor.php
5 years ago
PayPalClient.php
5 years ago
PayPalCommerce.php
5 years ago
RefreshToken.php
5 years ago
RefundPaymentHandler.php
5 years ago
ScriptLoader.php
5 years ago
Utils.php
5 years ago
onBoardingRedirectHandler.php
5 years ago
RefreshToken.php
108 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\PaymentGateways\PayPalCommerce; |
| 4 | |
| 5 | |
| 6 | use Give\PaymentGateways\PayPalCommerce\Models\MerchantDetail; |
| 7 | use Give\PaymentGateways\PayPalCommerce\Repositories\MerchantDetails; |
| 8 | use Give\PaymentGateways\PayPalCommerce\Repositories\PayPalAuth; |
| 9 | |
| 10 | /** |
| 11 | * Class RefreshToken |
| 12 | * |
| 13 | * @since 2.9.0 |
| 14 | */ |
| 15 | class RefreshToken { |
| 16 | /* @var MerchantDetail */ |
| 17 | private $merchantDetail; |
| 18 | |
| 19 | /** |
| 20 | * @since 2.9.0 |
| 21 | * |
| 22 | * @var MerchantDetails |
| 23 | */ |
| 24 | private $detailsRepository; |
| 25 | |
| 26 | /** |
| 27 | * @since 2.9.0 |
| 28 | * |
| 29 | * @var PayPalAuth |
| 30 | */ |
| 31 | private $payPalAuth; |
| 32 | |
| 33 | /** |
| 34 | * RefreshToken constructor. |
| 35 | * |
| 36 | * @since 2.9.0 |
| 37 | * @since 2.9.6 Add MerchantDetail constructor param. |
| 38 | * |
| 39 | * @param MerchantDetails $detailsRepository |
| 40 | * @param PayPalAuth $payPalAuth |
| 41 | * @param MerchantDetail $merchantDetail |
| 42 | */ |
| 43 | public function __construct( MerchantDetails $detailsRepository, PayPalAuth $payPalAuth, MerchantDetail $merchantDetail ) { |
| 44 | $this->detailsRepository = $detailsRepository; |
| 45 | $this->payPalAuth = $payPalAuth; |
| 46 | $this->merchantDetail = $merchantDetail; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Return cron json name which uses to refresh token. |
| 51 | * |
| 52 | * @since 2.9.0 |
| 53 | * |
| 54 | * @return string |
| 55 | */ |
| 56 | private function getCronJobHookName() { |
| 57 | return 'give_paypal_commerce_refresh_token'; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Register cron job to refresh access token. |
| 62 | * Note: only for internal use. |
| 63 | * |
| 64 | * @since 2.9.0 |
| 65 | * |
| 66 | * @param string $tokenExpires |
| 67 | * |
| 68 | */ |
| 69 | public function registerCronJobToRefreshToken( $tokenExpires ) { |
| 70 | wp_schedule_single_event( |
| 71 | time() + ( $tokenExpires - 1800 ), // Refresh token before half hours of expires date. |
| 72 | $this->getCronJobHookName() |
| 73 | ); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Delete cron job which refresh access token. |
| 78 | * Note: only for internal use. |
| 79 | * |
| 80 | * @since 2.9.0 |
| 81 | * |
| 82 | */ |
| 83 | public function deleteRefreshTokenCronJob() { |
| 84 | wp_clear_scheduled_hook( $this->getCronJobHookName() ); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Refresh token. |
| 89 | * Note: only for internal use |
| 90 | * |
| 91 | * @since 2.9.0 |
| 92 | * @since 2.9.6 Refresh token only if paypal merchant id exist. |
| 93 | */ |
| 94 | public function refreshToken() { |
| 95 | // Exit if account is not connected. |
| 96 | if ( ! $this->detailsRepository->accountIsConnected() ) { |
| 97 | return; |
| 98 | } |
| 99 | |
| 100 | $tokenDetails = $this->payPalAuth->getTokenFromClientCredentials( $this->merchantDetail->clientId, $this->merchantDetail->clientSecret ); |
| 101 | |
| 102 | $this->merchantDetail->setTokenDetails( $tokenDetails ); |
| 103 | $this->detailsRepository->save( $this->merchantDetail ); |
| 104 | |
| 105 | $this->registerCronJobToRefreshToken( $tokenDetails['expiresIn'] ); |
| 106 | } |
| 107 | } |
| 108 |