| 1 |
<?php |
| 2 |
|
| 3 |
namespace Give\Framework\PaymentGateways\Actions; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use Give\Donations\Models\Donation; |
| 7 |
use Give\Framework\FieldsAPI\Exceptions\TypeNotSupported; |
| 8 |
use Give\Framework\Http\Response\Types\JsonResponse; |
| 9 |
use Give\Framework\Http\Response\Types\RedirectResponse; |
| 10 |
use Give\Framework\PaymentGateways\CommandHandlers\RedirectOffsiteHandler; |
| 11 |
use Give\Framework\PaymentGateways\CommandHandlers\RespondToBrowserHandler; |
| 12 |
use Give\Framework\PaymentGateways\CommandHandlers\SubscriptionCompleteHandler; |
| 13 |
use Give\Framework\PaymentGateways\CommandHandlers\SubscriptionProcessingHandler; |
| 14 |
use Give\Framework\PaymentGateways\CommandHandlers\SubscriptionSyncedHandler; |
| 15 |
use Give\Framework\PaymentGateways\Commands\GatewayCommand; |
| 16 |
use Give\Framework\PaymentGateways\Commands\RedirectOffsite; |
| 17 |
use Give\Framework\PaymentGateways\Commands\RespondToBrowser; |
| 18 |
use Give\Framework\PaymentGateways\Commands\SubscriptionComplete; |
| 19 |
use Give\Framework\PaymentGateways\Commands\SubscriptionProcessing; |
| 20 |
use Give\Framework\PaymentGateways\Commands\SubscriptionSynced; |
| 21 |
use Give\Subscriptions\Models\Subscription; |
| 22 |
|
| 23 |
|
| 24 |
/** |
| 25 |
* @since 2.27.0 |
| 26 |
*/ |
| 27 |
class HandleGatewaySubscriptionCommand |
| 28 |
{ |
| 29 |
/** |
| 30 |
* |
| 31 |
* Handle gateway subscription command |
| 32 |
* |
| 33 |
* @since 2.27.0 return responses |
| 34 |
* @since 2.26.0 add RespondToBrowser command |
| 35 |
* @since 2.21.0 Handle RedirectOffsite response. |
| 36 |
* @since 2.18.0 |
| 37 |
* |
| 38 |
* @return JsonResponse|RedirectResponse |
| 39 |
* @throws TypeNotSupported |
| 40 |
* @throws Exception |
| 41 |
*/ |
| 42 |
public function __invoke( |
| 43 |
GatewayCommand $command, |
| 44 |
Donation $donation, |
| 45 |
Subscription $subscription |
| 46 |
) { |
| 47 |
if ($command instanceof SubscriptionComplete) { |
| 48 |
(new SubscriptionCompleteHandler())($command, $subscription, $donation); |
| 49 |
|
| 50 |
return new RedirectResponse(give_get_success_page_uri()); |
| 51 |
} |
| 52 |
|
| 53 |
if ($command instanceof SubscriptionProcessing) { |
| 54 |
(new SubscriptionProcessingHandler($command, $subscription, $donation))(); |
| 55 |
|
| 56 |
return new RedirectResponse(give_get_success_page_uri()); |
| 57 |
} |
| 58 |
|
| 59 |
if ($command instanceof RedirectOffsite) { |
| 60 |
return (new RedirectOffsiteHandler())($command); |
| 61 |
} |
| 62 |
|
| 63 |
if ($command instanceof RespondToBrowser) { |
| 64 |
return (new RespondToBrowserHandler())($command); |
| 65 |
} |
| 66 |
|
| 67 |
if ($command instanceof SubscriptionSynced) { |
| 68 |
return (new SubscriptionSyncedHandler())($command); |
| 69 |
} |
| 70 |
|
| 71 |
throw new TypeNotSupported( |
| 72 |
sprintf( |
| 73 |
"Return type must be an instance of %s", |
| 74 |
GatewayCommand::class |
| 75 |
) |
| 76 |
); |
| 77 |
} |
| 78 |
} |
| 79 |
|