| 1 |
<?php |
| 2 |
|
| 3 |
namespace Give\PaymentGateways\Gateways\Stripe; |
| 4 |
|
| 5 |
use Give\Donations\Models\Donation; |
| 6 |
use Give\Framework\Exceptions\Primitives\Exception; |
| 7 |
use Give\Framework\PaymentGateways\Commands\GatewayCommand; |
| 8 |
use Give\Framework\PaymentGateways\Commands\RedirectOffsite; |
| 9 |
use Give\Framework\PaymentGateways\Exceptions\PaymentGatewayException; |
| 10 |
use Give\Framework\PaymentGateways\PaymentGateway; |
| 11 |
use Give\Framework\PaymentGateways\PaymentGatewayRegister; |
| 12 |
use Give\Helpers\Call; |
| 13 |
use Give\Helpers\Gateways\Stripe; |
| 14 |
use Give\PaymentGateways\Gateways\Stripe\Exceptions\CheckoutException; |
| 15 |
use Give\PaymentGateways\Gateways\Stripe\Exceptions\CheckoutTypeException; |
| 16 |
use Give\PaymentGateways\Gateways\Stripe\ValueObjects\PaymentMethod; |
| 17 |
|
| 18 |
/** |
| 19 |
* @since 2.19.0 |
| 20 |
*/ |
| 21 |
class CheckoutGateway extends PaymentGateway |
| 22 |
{ |
| 23 |
use Traits\CheckoutInstructions; |
| 24 |
use Traits\CheckoutModal; |
| 25 |
use Traits\CheckoutRedirect; |
| 26 |
use Traits\HandlePaymentIntentStatus; |
| 27 |
|
| 28 |
/** |
| 29 |
* @inheritDoc |
| 30 |
* @since 2.19.0 |
| 31 |
* |
| 32 |
* @param array{stripePaymentMethod: PaymentMethod} $gatewayData |
| 33 |
* |
| 34 |
* @throws PaymentGatewayException |
| 35 |
*/ |
| 36 |
public function createPayment(Donation $donation, $gatewayData): GatewayCommand |
| 37 |
{ |
| 38 |
switch ($this->getCheckoutType()) { |
| 39 |
case 'modal': |
| 40 |
return give(PaymentGatewayRegister::class) |
| 41 |
->getPaymentGateway(CreditCardGateway::id()) |
| 42 |
->createPayment($donation, $gatewayData); |
| 43 |
case 'redirect': |
| 44 |
return $this->createPaymentRedirect($donation); |
| 45 |
default: |
| 46 |
throw new CheckoutException('Invalid Checkout Error'); |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* @since 2.21.2 |
| 52 |
*/ |
| 53 |
public function getCheckoutType(): string |
| 54 |
{ |
| 55 |
return give_stripe_get_checkout_type(); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* @since 2.19.7 fix argument order of CreateCheckoutSession |
| 60 |
* @since 2.19.0 |
| 61 |
*/ |
| 62 |
protected function createPaymentRedirect(Donation $donation): RedirectOffsite |
| 63 |
{ |
| 64 |
$donationSummary = Call::invoke(Actions\SaveDonationSummary::class, $donation); |
| 65 |
$stripeCustomer = Call::invoke(Actions\GetOrCreateStripeCustomer::class, $donation); |
| 66 |
$session = Call::invoke(Actions\CreateCheckoutSession::class, $donation, $donationSummary, $stripeCustomer); |
| 67 |
|
| 68 |
return new RedirectOffsite( |
| 69 |
$this->getRedirectUrl($session->id(), give_get_payment_form_id($donation->id)) |
| 70 |
); |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* @inheritDoc |
| 75 |
*/ |
| 76 |
public static function id(): string |
| 77 |
{ |
| 78 |
return 'stripe_checkout'; |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* @inheritDoc |
| 83 |
*/ |
| 84 |
public function getId(): string |
| 85 |
{ |
| 86 |
return self::id(); |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* @inheritDoc |
| 91 |
*/ |
| 92 |
public function getName(): string |
| 93 |
{ |
| 94 |
return __('Stripe - Checkout', 'give'); |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* @inheritDoc |
| 99 |
*/ |
| 100 |
public function getPaymentMethodLabel(): string |
| 101 |
{ |
| 102 |
return __('Stripe - Checkout', 'give'); |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* @inheritDoc |
| 107 |
* |
| 108 |
* @since 2.23.1 |
| 109 |
* |
| 110 |
* @return string|void |
| 111 |
*/ |
| 112 |
public function getLegacyFormFieldMarkup(int $formId, array $args): string |
| 113 |
{ |
| 114 |
Stripe::canShowBillingAddress($formId, $args); |
| 115 |
|
| 116 |
switch ($checkoutType = give_stripe_get_checkout_type()) { |
| 117 |
case 'redirect': |
| 118 |
return $this->getCheckoutInstructions(); |
| 119 |
case 'modal': |
| 120 |
return $this->getCheckoutInstructions() |
| 121 |
. $this->getCheckoutModalHTML($formId, $args); |
| 122 |
default: |
| 123 |
throw new CheckoutTypeException($checkoutType); |
| 124 |
} |
| 125 |
} |
| 126 |
} |
| 127 |
|