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
101 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\PaymentGateways\Gateways\Stripe\Actions; |
| 4 | |
| 5 | use Give\Donations\Models\Donation; |
| 6 | use Give\Donations\Models\DonationNote; |
| 7 | use Give\Framework\Exceptions\Primitives\Exception; |
| 8 | use Give\Framework\PaymentGateways\DonationSummary; |
| 9 | use Give\PaymentGateways\Exceptions\InvalidPropertyName; |
| 10 | use Give\PaymentGateways\Gateways\Stripe\ValueObjects\PaymentIntent; |
| 11 | use Give\PaymentGateways\Gateways\Stripe\ValueObjects\PaymentMethod; |
| 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 | public function __construct(array $paymentIntentArgs = []) |
| 25 | { |
| 26 | $this->defaultIntentArgs = $paymentIntentArgs; |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * @since 2.19.0 |
| 31 | * |
| 32 | * @throws InvalidPropertyName |
| 33 | * @throws Exception |
| 34 | */ |
| 35 | public function __invoke( |
| 36 | Donation $donation, |
| 37 | DonationSummary $donationSummary, |
| 38 | \Give_Stripe_Customer $giveStripeCustomer, |
| 39 | PaymentMethod $paymentMethod |
| 40 | ): PaymentIntent { |
| 41 | /** |
| 42 | * This filter hook is used to update the payment intent arguments. |
| 43 | * |
| 44 | * @since 2.5.0 |
| 45 | */ |
| 46 | $intent_args = apply_filters( |
| 47 | 'give_stripe_create_intent_args', |
| 48 | array_merge([ |
| 49 | 'amount' => $donation->amount->formatToMinorAmount(), |
| 50 | 'currency' => $donation->amount->getCurrency(), |
| 51 | 'payment_method_types' => ['card'], |
| 52 | 'statement_descriptor' => give_stripe_get_statement_descriptor(), |
| 53 | 'description' => $donationSummary->getSummaryWithDonor(), |
| 54 | 'metadata' => give_stripe_prepare_metadata($donation->id), |
| 55 | 'customer' => $giveStripeCustomer->get_id(), |
| 56 | 'payment_method' => $paymentMethod->id(), |
| 57 | 'confirm' => true, |
| 58 | 'return_url' => give_get_success_page_uri(), |
| 59 | ], $this->defaultIntentArgs) |
| 60 | ); |
| 61 | |
| 62 | // Send Stripe Receipt emails when enabled. |
| 63 | if (give_is_setting_enabled(give_get_option('stripe_receipt_emails'))) { |
| 64 | $intent_args['receipt_email'] = $donation->email; |
| 65 | } |
| 66 | |
| 67 | $intent = give(PaymentIntent::class)->create($intent_args); |
| 68 | |
| 69 | DonationNote::create([ |
| 70 | 'donationId' => $donation->id, |
| 71 | 'content' => sprintf(__('Stripe Charge/Payment Intent ID: %s', 'give'), $intent->id()) |
| 72 | ]); |
| 73 | |
| 74 | DonationNote::create([ |
| 75 | 'donationId' => $donation->id, |
| 76 | 'content' => sprintf(__('Stripe Payment Intent Client Secret: %s', 'give'), $intent->clientSecret()) |
| 77 | ]); |
| 78 | |
| 79 | give_update_meta( |
| 80 | $donation->id, |
| 81 | '_give_stripe_payment_intent_client_secret', |
| 82 | $intent->clientSecret() |
| 83 | ); |
| 84 | |
| 85 | if ('requires_action' === $intent->status()) { |
| 86 | DonationNote::create([ |
| 87 | 'donationId' => $donation->id, |
| 88 | 'content' => __('Stripe requires additional action to be fulfilled. Check your Stripe account.', 'give') |
| 89 | ]); |
| 90 | |
| 91 | give_update_meta( |
| 92 | $donation->id, |
| 93 | '_give_stripe_payment_intent_require_action_url', |
| 94 | $intent->nextActionRedirectUrl() |
| 95 | ); |
| 96 | } |
| 97 | |
| 98 | return $intent; |
| 99 | } |
| 100 | } |
| 101 |