| 1 |
<?php |
| 2 |
|
| 3 |
namespace Give\Framework\PaymentGateways\Controllers; |
| 4 |
|
| 5 |
use Give\Donations\Models\Donation; |
| 6 |
use Give\Framework\Exceptions\Primitives\Exception; |
| 7 |
use Give\Framework\FieldsAPI\Exceptions\TypeNotSupported; |
| 8 |
use Give\Framework\PaymentGateways\Actions\HandleGatewaySubscriptionCommand; |
| 9 |
use Give\Framework\PaymentGateways\Commands\GatewayCommand; |
| 10 |
use Give\Framework\PaymentGateways\Exceptions\PaymentGatewayException; |
| 11 |
use Give\Framework\PaymentGateways\Log\PaymentGatewayLog; |
| 12 |
use Give\Framework\PaymentGateways\PaymentGateway; |
| 13 |
use Give\Framework\PaymentGateways\Traits\HandleHttpResponses; |
| 14 |
use Give\Subscriptions\Models\Subscription; |
| 15 |
|
| 16 |
/** |
| 17 |
* @since 2.27.0 |
| 18 |
*/ |
| 19 |
class GatewaySubscriptionController |
| 20 |
{ |
| 21 |
use HandleHttpResponses; |
| 22 |
|
| 23 |
/** |
| 24 |
* @var PaymentGateway |
| 25 |
*/ |
| 26 |
protected $gateway; |
| 27 |
|
| 28 |
/** |
| 29 |
* @since 2.27.0 |
| 30 |
*/ |
| 31 |
public function __construct(PaymentGateway $gateway) |
| 32 |
{ |
| 33 |
$this->gateway = $gateway; |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* @since 3.0.0 Catch and handle errors from the gateway upstream |
| 38 |
* @since 2.27.0 |
| 39 |
* |
| 40 |
* @throws Exception|TypeNotSupported|PaymentGatewayException |
| 41 |
*/ |
| 42 |
public function create(Donation $donation, Subscription $subscription, array $gatewayData = []) |
| 43 |
{ |
| 44 |
$command = $this->gateway->createSubscription($donation, $subscription, $gatewayData); |
| 45 |
|
| 46 |
$this->handleGatewayCommand($command, $donation, $subscription); |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* @since 2.33.0 |
| 51 |
*/ |
| 52 |
public function synchronizeSubscription(Subscription $subscription) |
| 53 |
{ |
| 54 |
try { |
| 55 |
$command = $this->gateway->synchronizeSubscription($subscription); |
| 56 |
$this->handleGatewayCommand($command, $subscription->initialDonation(), $subscription); |
| 57 |
} catch (\Exception $exception) { |
| 58 |
PaymentGatewayLog::error( |
| 59 |
$exception->getMessage(), |
| 60 |
[ |
| 61 |
'Payment Gateway' => $this->gateway::id(), |
| 62 |
'Donation' => $subscription->initialDonation()->toArray(), |
| 63 |
'Subscription' => $subscription->toArray(), |
| 64 |
] |
| 65 |
); |
| 66 |
|
| 67 |
$message = __( |
| 68 |
'An unexpected error occurred while synchronizing the subscription. Please try again or contact the site administrator.', |
| 69 |
'give' |
| 70 |
); |
| 71 |
|
| 72 |
$this->handleExceptionResponse($exception, $message); |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Handle gateway subscription command |
| 78 |
* |
| 79 |
* @since 2.27.0 move logic into action |
| 80 |
* @since 2.26.0 add RespondToBrowser command |
| 81 |
* @since 2.21.0 Handle RedirectOffsite response. |
| 82 |
* @since 2.18.0 |
| 83 |
* |
| 84 |
* @throws TypeNotSupported |
| 85 |
* @throws Exception |
| 86 |
*/ |
| 87 |
public function handleGatewayCommand( |
| 88 |
GatewayCommand $command, |
| 89 |
Donation $donation, |
| 90 |
Subscription $subscription |
| 91 |
) { |
| 92 |
$response = (new HandleGatewaySubscriptionCommand())($command, $donation, $subscription); |
| 93 |
|
| 94 |
$this->handleResponse($response); |
| 95 |
} |
| 96 |
} |
| 97 |
|