CreateCheckoutSession.php
4 years ago
CreatePaymentIntent.php
4 years ago
GetOrCreateStripeCustomer.php
4 years ago
GetPaymentMethodFromRequest.php
4 years ago
SaveDonationSummary.php
4 years ago
UpdateStripeAccountStatementDescriptor.php
4 years ago
ValidatePaymentMethod.php
4 years ago
CreatePaymentIntent.php
108 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\PaymentGateways\Gateways\Stripe\Actions; |
| 4 | |
| 5 | use Give\LegacyPaymentGateways\DataTransferObjects\LegacyDonationData; |
| 6 | use Give\PaymentGateways\DataTransferObjects\GatewayPaymentData; |
| 7 | use Give\Framework\PaymentGateways\DonationSummary; |
| 8 | use Give\PaymentGateways\Exceptions\InvalidPropertyName; |
| 9 | use Give\PaymentGateways\Gateways\Stripe\ValueObjects\PaymentIntent; |
| 10 | use Give\PaymentGateways\Gateways\Stripe\ValueObjects\PaymentMethod; |
| 11 | use Give\ValueObjects\Money; |
| 12 | |
| 13 | /** |
| 14 | * @since 2.19.0 |
| 15 | */ |
| 16 | class CreatePaymentIntent |
| 17 | { |
| 18 | /** @var array */ |
| 19 | protected $defaultIntentArgs; |
| 20 | |
| 21 | /** |
| 22 | * @since 2.19.0 |
| 23 | * |
| 24 | * @param array $paymentIntentArgs |
| 25 | */ |
| 26 | public function __construct($paymentIntentArgs = []) |
| 27 | { |
| 28 | $this->defaultIntentArgs = $paymentIntentArgs; |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * @since 2.19.0 |
| 33 | * |
| 34 | * @param GatewayPaymentData $paymentData |
| 35 | * @param DonationSummary $donationSummary |
| 36 | * @param \Give_Stripe_Customer $giveStripeCustomer |
| 37 | * @param PaymentMethod $paymentMethod |
| 38 | * |
| 39 | * @return PaymentIntent |
| 40 | * @throws InvalidPropertyName |
| 41 | */ |
| 42 | public function __invoke( |
| 43 | GatewayPaymentData $paymentData, |
| 44 | DonationSummary $donationSummary, |
| 45 | \Give_Stripe_Customer $giveStripeCustomer, |
| 46 | PaymentMethod $paymentMethod |
| 47 | ) { |
| 48 | /** |
| 49 | * This filter hook is used to update the payment intent arguments. |
| 50 | * |
| 51 | * @since 2.5.0 |
| 52 | */ |
| 53 | $intent_args = apply_filters( |
| 54 | 'give_stripe_create_intent_args', |
| 55 | array_merge([ |
| 56 | 'amount' => Money::of($paymentData->price, $paymentData->currency)->getMinorAmount(), |
| 57 | 'currency' => $paymentData->currency, |
| 58 | 'payment_method_types' => ['card'], |
| 59 | 'statement_descriptor' => give_stripe_get_statement_descriptor(), |
| 60 | 'description' => $donationSummary->getSummaryWithDonor(), |
| 61 | 'metadata' => give_stripe_prepare_metadata( |
| 62 | $paymentData->donationId, |
| 63 | new LegacyDonationData($paymentData, $paymentMethod->id()) |
| 64 | ), |
| 65 | 'customer' => $giveStripeCustomer->get_id(), |
| 66 | 'payment_method' => $paymentMethod->id(), |
| 67 | 'confirm' => true, |
| 68 | 'return_url' => $paymentData->redirectUrl, |
| 69 | ], $this->defaultIntentArgs) |
| 70 | ); |
| 71 | |
| 72 | // Send Stripe Receipt emails when enabled. |
| 73 | if (give_is_setting_enabled(give_get_option('stripe_receipt_emails'))) { |
| 74 | $intent_args['receipt_email'] = $paymentData->donorInfo->email; |
| 75 | } |
| 76 | |
| 77 | $intent = give(PaymentIntent::class)->create($intent_args); |
| 78 | |
| 79 | give_insert_payment_note( |
| 80 | $paymentData->donationId, |
| 81 | sprintf(__('Stripe Charge/Payment Intent ID: %s', 'give'), $intent->id()) |
| 82 | ); |
| 83 | give_insert_payment_note( |
| 84 | $paymentData->donationId, |
| 85 | sprintf(__('Stripe Payment Intent Client Secret: %s', 'give'), $intent->clientSecret()) |
| 86 | ); |
| 87 | give_update_meta( |
| 88 | $paymentData->donationId, |
| 89 | '_give_stripe_payment_intent_client_secret', |
| 90 | $intent->clientSecret() |
| 91 | ); |
| 92 | |
| 93 | if ('requires_action' == $intent->status()) { |
| 94 | give_insert_payment_note( |
| 95 | $paymentData->donationId, |
| 96 | __('Stripe requires additional action to be fulfilled. Check your Stripe account.', 'give') |
| 97 | ); |
| 98 | give_update_meta( |
| 99 | $paymentData->donationId, |
| 100 | '_give_stripe_payment_intent_require_action_url', |
| 101 | $intent->nextActionRedirectUrl() |
| 102 | ); |
| 103 | } |
| 104 | |
| 105 | return $intent; |
| 106 | } |
| 107 | } |
| 108 |