PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 4.16.9
GiveWP – Donation Plugin and Fundraising Platform v4.16.9
4.16.9 4.16.8.1 4.16.8 4.16.7.2 4.16.7.1 4.16.7 4.16.6.1 4.16.6 4.16.5.1 4.16.5 4.16.4 4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 2.3.2 2.30.0 All 255 releases
give / src / Framework / PaymentGateways / Controllers / GatewayPaymentController.php

GatewayPaymentController.php in GiveWP – Donation Plugin and Fundraising Platform 4.16.9, at src/Framework/PaymentGateways/Controllers/GatewayPaymentController.php

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