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
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
99 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 | /** |
| 17 | * @since 2.9.0 |
| 18 | * |
| 19 | * @var MerchantDetails |
| 20 | */ |
| 21 | private $detailsRepository; |
| 22 | |
| 23 | /** |
| 24 | * @since 2.9.0 |
| 25 | * |
| 26 | * @var PayPalAuth |
| 27 | */ |
| 28 | private $payPalAuth; |
| 29 | |
| 30 | /** |
| 31 | * RefreshToken constructor. |
| 32 | * |
| 33 | * @since 2.9.0 |
| 34 | * |
| 35 | * @param MerchantDetails $detailsRepository |
| 36 | * @param PayPalAuth $payPalAuth |
| 37 | */ |
| 38 | public function __construct( MerchantDetails $detailsRepository, PayPalAuth $payPalAuth ) { |
| 39 | $this->detailsRepository = $detailsRepository; |
| 40 | $this->payPalAuth = $payPalAuth; |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Return cron json name which uses to refresh token. |
| 45 | * |
| 46 | * @since 2.9.0 |
| 47 | * |
| 48 | * @return string |
| 49 | */ |
| 50 | private function getCronJobHookName() { |
| 51 | return 'give_paypal_commerce_refresh_token'; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Register cron job to refresh access token. |
| 56 | * Note: only for internal use. |
| 57 | * |
| 58 | * @since 2.9.0 |
| 59 | * |
| 60 | * @param string $tokenExpires |
| 61 | * |
| 62 | */ |
| 63 | public function registerCronJobToRefreshToken( $tokenExpires ) { |
| 64 | wp_schedule_single_event( |
| 65 | time() + ( $tokenExpires - 1800 ), // Refresh token before half hours of expires date. |
| 66 | $this->getCronJobHookName() |
| 67 | ); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Delete cron job which refresh access token. |
| 72 | * Note: only for internal use. |
| 73 | * |
| 74 | * @since 2.9.0 |
| 75 | * |
| 76 | */ |
| 77 | public function deleteRefreshTokenCronJob() { |
| 78 | wp_clear_scheduled_hook( $this->getCronJobHookName() ); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Refresh token. |
| 83 | * Note: only for internal use |
| 84 | * |
| 85 | * @since 2.9.0 |
| 86 | */ |
| 87 | public function refreshToken() { |
| 88 | /* @var MerchantDetail $merchant */ |
| 89 | $merchant = give( MerchantDetail::class ); |
| 90 | |
| 91 | $tokenDetails = $this->payPalAuth->getTokenFromClientCredentials( $merchant->clientId, $merchant->clientSecret ); |
| 92 | |
| 93 | $merchant->setTokenDetails( $tokenDetails ); |
| 94 | $this->detailsRepository->save( $merchant ); |
| 95 | |
| 96 | $this->registerCronJobToRefreshToken( $tokenDetails['expiresIn'] ); |
| 97 | } |
| 98 | } |
| 99 |