Actions
4 years ago
DataTransferObjects
4 years ago
Exceptions
4 years ago
Migrations
4 years ago
Models
4 years ago
Repositories
4 years ago
Webhooks
4 years ago
AccountAdminNotices.php
4 years ago
AdminSettingFields.php
4 years ago
AdvancedCardFields.php
4 years ago
AjaxRequestHandler.php
4 years ago
DonationDetailsPage.php
4 years ago
DonationFormPaymentMethod.php
4 years ago
PayPalClient.php
4 years ago
PayPalCommerce.php
4 years ago
RefreshToken.php
4 years ago
RefundPaymentHandler.php
4 years ago
ScriptLoader.php
4 years ago
Utils.php
4 years ago
onBoardingRedirectHandler.php
4 years ago
RefreshToken.php
119 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\PaymentGateways\PayPalCommerce; |
| 4 | |
| 5 | use Give\PaymentGateways\PayPalCommerce\Models\MerchantDetail; |
| 6 | use Give\PaymentGateways\PayPalCommerce\Repositories\MerchantDetails; |
| 7 | use Give\PaymentGateways\PayPalCommerce\Repositories\PayPalAuth; |
| 8 | |
| 9 | /** |
| 10 | * Class RefreshToken |
| 11 | * |
| 12 | * @since 2.9.0 |
| 13 | */ |
| 14 | class RefreshToken |
| 15 | { |
| 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( |
| 44 | MerchantDetails $detailsRepository, |
| 45 | PayPalAuth $payPalAuth, |
| 46 | MerchantDetail $merchantDetail |
| 47 | ) { |
| 48 | $this->detailsRepository = $detailsRepository; |
| 49 | $this->payPalAuth = $payPalAuth; |
| 50 | $this->merchantDetail = $merchantDetail; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Return cron json name which uses to refresh token. |
| 55 | * |
| 56 | * @since 2.9.0 |
| 57 | * |
| 58 | * @return string |
| 59 | */ |
| 60 | private function getCronJobHookName() |
| 61 | { |
| 62 | return 'give_paypal_commerce_refresh_token'; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Register cron job to refresh access token. |
| 67 | * Note: only for internal use. |
| 68 | * |
| 69 | * @since 2.9.0 |
| 70 | * |
| 71 | * @param string $tokenExpires |
| 72 | * |
| 73 | */ |
| 74 | public function registerCronJobToRefreshToken($tokenExpires) |
| 75 | { |
| 76 | wp_schedule_single_event( |
| 77 | time() + ($tokenExpires - 1800), // Refresh token before half hours of expires date. |
| 78 | $this->getCronJobHookName() |
| 79 | ); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Delete cron job which refresh access token. |
| 84 | * Note: only for internal use. |
| 85 | * |
| 86 | * @since 2.9.0 |
| 87 | * |
| 88 | */ |
| 89 | public function deleteRefreshTokenCronJob() |
| 90 | { |
| 91 | wp_clear_scheduled_hook($this->getCronJobHookName()); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Refresh token. |
| 96 | * Note: only for internal use |
| 97 | * |
| 98 | * @since 2.9.0 |
| 99 | * @since 2.9.6 Refresh token only if paypal merchant id exist. |
| 100 | */ |
| 101 | public function refreshToken() |
| 102 | { |
| 103 | // Exit if account is not connected. |
| 104 | if ( ! $this->detailsRepository->accountIsConnected()) { |
| 105 | return; |
| 106 | } |
| 107 | |
| 108 | $tokenDetails = $this->payPalAuth->getTokenFromClientCredentials( |
| 109 | $this->merchantDetail->clientId, |
| 110 | $this->merchantDetail->clientSecret |
| 111 | ); |
| 112 | |
| 113 | $this->merchantDetail->setTokenDetails($tokenDetails); |
| 114 | $this->detailsRepository->save($this->merchantDetail); |
| 115 | |
| 116 | $this->registerCronJobToRefreshToken($tokenDetails['expiresIn']); |
| 117 | } |
| 118 | } |
| 119 |