HandleGatewayPaymentCommand.php
| 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\PaymentCompleteHandler; |
| 11 | use Give\Framework\PaymentGateways\CommandHandlers\PaymentPendingHandler; |
| 12 | use Give\Framework\PaymentGateways\CommandHandlers\PaymentProcessingHandler; |
| 13 | use Give\Framework\PaymentGateways\CommandHandlers\PaymentRefundedHandler; |
| 14 | use Give\Framework\PaymentGateways\CommandHandlers\RedirectOffsiteHandler; |
| 15 | use Give\Framework\PaymentGateways\CommandHandlers\RespondToBrowserHandler; |
| 16 | use Give\Framework\PaymentGateways\Commands\GatewayCommand; |
| 17 | use Give\Framework\PaymentGateways\Commands\PaymentComplete; |
| 18 | use Give\Framework\PaymentGateways\Commands\PaymentPending; |
| 19 | use Give\Framework\PaymentGateways\Commands\PaymentProcessing; |
| 20 | use Give\Framework\PaymentGateways\Commands\PaymentRefunded; |
| 21 | use Give\Framework\PaymentGateways\Commands\RedirectOffsite; |
| 22 | use Give\Framework\PaymentGateways\Commands\RespondToBrowser; |
| 23 | |
| 24 | |
| 25 | /** |
| 26 | * @since 2.27.0 |
| 27 | */ |
| 28 | class HandleGatewayPaymentCommand |
| 29 | { |
| 30 | /** |
| 31 | * Handle gateway command |
| 32 | * |
| 33 | * @since 3.0.0 Handle PaymentPending command |
| 34 | * @since 2.29.0 Handle PaymentRefunded command |
| 35 | * @since 2.27.0 return responses |
| 36 | * @since 2.18.0 |
| 37 | * |
| 38 | * @return JsonResponse|RedirectResponse |
| 39 | * @throws TypeNotSupported|Exception |
| 40 | */ |
| 41 | public function __invoke(GatewayCommand $command, Donation $donation) |
| 42 | { |
| 43 | if ($command instanceof PaymentComplete) { |
| 44 | $handler = new PaymentCompleteHandler($command); |
| 45 | |
| 46 | $handler->handle($donation); |
| 47 | |
| 48 | return new RedirectResponse(give_get_success_page_uri()); |
| 49 | } |
| 50 | |
| 51 | if ($command instanceof PaymentProcessing) { |
| 52 | $handler = new PaymentProcessingHandler($command); |
| 53 | |
| 54 | $handler->handle($donation); |
| 55 | |
| 56 | return new RedirectResponse(give_get_success_page_uri()); |
| 57 | } |
| 58 | |
| 59 | if ($command instanceof PaymentPending) { |
| 60 | $handler = new PaymentPendingHandler($command); |
| 61 | |
| 62 | $handler->handle($donation); |
| 63 | |
| 64 | return new RedirectResponse(give_get_success_page_uri()); |
| 65 | } |
| 66 | |
| 67 | if ($command instanceof PaymentRefunded) { |
| 68 | $handler = new PaymentRefundedHandler($command); |
| 69 | $handler->handle($donation); |
| 70 | $url = isset($_REQUEST['_wp_http_referer']) ? home_url($_REQUEST['_wp_http_referer']) : home_url('/'); |
| 71 | |
| 72 | return new RedirectResponse($url); |
| 73 | } |
| 74 | |
| 75 | if ($command instanceof RedirectOffsite) { |
| 76 | return (new RedirectOffsiteHandler())($command); |
| 77 | } |
| 78 | |
| 79 | if ($command instanceof RespondToBrowser) { |
| 80 | return (new RespondToBrowserHandler())($command); |
| 81 | } |
| 82 | |
| 83 | throw new TypeNotSupported( |
| 84 | sprintf( |
| 85 | "Return type must be an instance of %s", |
| 86 | GatewayCommand::class |
| 87 | ) |
| 88 | ); |
| 89 | } |
| 90 | } |
| 91 |