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
CreateCheckoutSession.php
81 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\CheckoutSession; |
| 11 | use Give_Stripe_Customer; |
| 12 | |
| 13 | /** |
| 14 | * @since 2.19.0 |
| 15 | */ |
| 16 | class CreateCheckoutSession |
| 17 | { |
| 18 | /** |
| 19 | * @since 2.20.0 Update function to get input value to line_items[0][name] |
| 20 | * @since 2.19.0 |
| 21 | * |
| 22 | * @return CheckoutSession |
| 23 | * @throws InvalidPropertyName |
| 24 | * @throws Exception |
| 25 | */ |
| 26 | public function __invoke( |
| 27 | Donation $donation, |
| 28 | DonationSummary $donationSummary, |
| 29 | Give_Stripe_Customer $giveStripeCustomer |
| 30 | ) { |
| 31 | $session_args = [ |
| 32 | 'customer' => $giveStripeCustomer->get_id(), |
| 33 | 'client_reference_id' => $donation->purchaseKey, |
| 34 | 'payment_method_types' => ['card'], |
| 35 | 'billing_address_collection' => give_is_setting_enabled( |
| 36 | give_get_option('stripe_collect_billing') |
| 37 | ) ? 'required' : 'auto', |
| 38 | 'mode' => 'payment', |
| 39 | 'line_items' => [ |
| 40 | [ |
| 41 | 'name' => Donation::find($donation->id)->formTitle, |
| 42 | 'description' => $donationSummary->getSummaryWithDonor(), |
| 43 | 'amount' => $donation->amount->formatToMinorAmount(), |
| 44 | 'currency' => $donation->amount->getCurrency(), |
| 45 | 'quantity' => 1, |
| 46 | ], |
| 47 | ], |
| 48 | 'payment_intent_data' => [ |
| 49 | 'capture_method' => 'automatic', |
| 50 | 'description' => $donationSummary->getSummaryWithDonor(), |
| 51 | 'metadata' => give_stripe_prepare_metadata($donation->id), |
| 52 | 'statement_descriptor' => give_stripe_get_statement_descriptor(), |
| 53 | ], |
| 54 | 'submit_type' => 'donate', |
| 55 | 'success_url' => give_get_success_page_uri(), |
| 56 | 'cancel_url' => give_get_failed_transaction_uri(), |
| 57 | 'locale' => give_stripe_get_preferred_locale(), |
| 58 | ]; |
| 59 | |
| 60 | if ($formThumbnail = get_the_post_thumbnail($donation->formId)) { |
| 61 | $session_args['line_items'][0]['images'] = [$formThumbnail]; |
| 62 | } |
| 63 | |
| 64 | $session = give(CheckoutSession::class)->create($session_args); |
| 65 | |
| 66 | DonationNote::create([ |
| 67 | 'donationId' => $donation->id, |
| 68 | 'content' => sprintf( |
| 69 | /* translators: 1: Stripe checkout payment method session id */ |
| 70 | esc_html__( 'Stripe Checkout Session ID: %s', 'give'), |
| 71 | $session->id() |
| 72 | ) |
| 73 | ]); |
| 74 | |
| 75 | $donation->gatewayTransactionId = $session->id(); |
| 76 | $donation->save(); |
| 77 | |
| 78 | return $session; |
| 79 | } |
| 80 | } |
| 81 |