| 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 |
|