GatewayPaymentController.php
105 lines
| 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\HandleGatewayPaymentCommand; |
| 9 | use Give\Framework\PaymentGateways\Commands\GatewayCommand; |
| 10 | use Give\Framework\PaymentGateways\Log\PaymentGatewayLog; |
| 11 | use Give\Framework\PaymentGateways\PaymentGateway; |
| 12 | use Give\Framework\PaymentGateways\Traits\HandleHttpResponses; |
| 13 | |
| 14 | /** |
| 15 | * @since 2.27.0 |
| 16 | */ |
| 17 | class GatewayPaymentController |
| 18 | { |
| 19 | use HandleHttpResponses; |
| 20 | |
| 21 | /** |
| 22 | * @var PaymentGateway |
| 23 | */ |
| 24 | protected $gateway; |
| 25 | |
| 26 | /** |
| 27 | * @since 2.27.0 |
| 28 | */ |
| 29 | public function __construct(PaymentGateway $gateway) |
| 30 | { |
| 31 | $this->gateway = $gateway; |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * @since 2.27.0 |
| 36 | */ |
| 37 | public function create(Donation $donation, array $gatewayData = []) |
| 38 | { |
| 39 | try { |
| 40 | $command = $this->gateway->createPayment($donation, $gatewayData); |
| 41 | $this->handleGatewayCommand($command, $donation); |
| 42 | } catch (\Exception $exception) { |
| 43 | PaymentGatewayLog::error( |
| 44 | $exception->getMessage(), |
| 45 | [ |
| 46 | 'Payment Gateway' => $this->gateway::id(), |
| 47 | 'Donation' => $donation->toArray(), |
| 48 | ] |
| 49 | ); |
| 50 | |
| 51 | $message = __( |
| 52 | 'An unexpected error occurred while processing the donation. Please try again or contact a site administrator.', |
| 53 | 'give' |
| 54 | ); |
| 55 | |
| 56 | $this->handleExceptionResponse($exception, $message); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * @since 2.29.0 |
| 62 | */ |
| 63 | public function refund(Donation $donation) |
| 64 | { |
| 65 | try { |
| 66 | $command = $this->gateway->refundDonation($donation); |
| 67 | |
| 68 | if ($command instanceof GatewayCommand) { |
| 69 | $this->handleGatewayCommand($command, $donation); |
| 70 | } |
| 71 | } catch (\Exception $exception) { |
| 72 | PaymentGatewayLog::error( |
| 73 | $exception->getMessage(), |
| 74 | [ |
| 75 | 'Payment Gateway' => $this->gateway::id(), |
| 76 | 'Donation' => $donation->toArray(), |
| 77 | ] |
| 78 | ); |
| 79 | |
| 80 | $message = __( |
| 81 | 'An unexpected error occurred while refunding the donation. Please try again or contact a site administrator.', |
| 82 | 'give' |
| 83 | ); |
| 84 | |
| 85 | $this->handleExceptionResponse($exception, $message); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Handle gateway command |
| 91 | * |
| 92 | * @since 2.27.0 move logic into action |
| 93 | * @since 2.18.0 |
| 94 | * |
| 95 | * @throws TypeNotSupported |
| 96 | * @throws Exception |
| 97 | */ |
| 98 | protected function handleGatewayCommand(GatewayCommand $command, Donation $donation) |
| 99 | { |
| 100 | $response = (new HandleGatewayPaymentCommand())($command, $donation); |
| 101 | |
| 102 | $this->handleResponse($response); |
| 103 | } |
| 104 | } |
| 105 |