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
77 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\PaymentGateways\Gateways\Stripe\Actions; |
| 4 | |
| 5 | use Give\Donations\Models\Donation; |
| 6 | use Give\Framework\PaymentGateways\DonationSummary; |
| 7 | use Give\PaymentGateways\DataTransferObjects\GatewayPaymentData; |
| 8 | use Give\PaymentGateways\Exceptions\InvalidPropertyName; |
| 9 | use Give\PaymentGateways\Gateways\Stripe\ValueObjects\CheckoutSession; |
| 10 | use Give\ValueObjects\Money; |
| 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 | * @param GatewayPaymentData $paymentData |
| 23 | * @param DonationSummary $donationSummary |
| 24 | * @param Give_Stripe_Customer $giveStripeCustomer |
| 25 | * |
| 26 | * @return CheckoutSession |
| 27 | * @throws InvalidPropertyName |
| 28 | */ |
| 29 | public function __invoke( |
| 30 | GatewayPaymentData $paymentData, |
| 31 | DonationSummary $donationSummary, |
| 32 | Give_Stripe_Customer $giveStripeCustomer |
| 33 | ) { |
| 34 | $session_args = [ |
| 35 | 'customer' => $giveStripeCustomer->get_id(), |
| 36 | 'client_reference_id' => $paymentData->purchaseKey, |
| 37 | 'payment_method_types' => ['card'], |
| 38 | 'billing_address_collection' => give_is_setting_enabled( |
| 39 | give_get_option('stripe_collect_billing') |
| 40 | ) ? 'required' : 'auto', |
| 41 | 'mode' => 'payment', |
| 42 | 'line_items' => [ |
| 43 | [ |
| 44 | 'name' => Donation::find($paymentData->donationId)->formTitle, |
| 45 | 'description' => $donationSummary->getSummaryWithDonor(), |
| 46 | 'amount' => Money::of($paymentData->price, $paymentData->currency)->getMinorAmount(), |
| 47 | 'currency' => $paymentData->currency, |
| 48 | 'quantity' => 1, |
| 49 | ], |
| 50 | ], |
| 51 | 'payment_intent_data' => [ |
| 52 | 'capture_method' => 'automatic', |
| 53 | 'description' => $donationSummary->getSummaryWithDonor(), |
| 54 | 'metadata' => give_stripe_prepare_metadata($paymentData->donationId), |
| 55 | 'statement_descriptor' => give_stripe_get_statement_descriptor(), |
| 56 | ], |
| 57 | 'submit_type' => 'donate', |
| 58 | 'success_url' => give_get_success_page_uri(), |
| 59 | 'cancel_url' => give_get_failed_transaction_uri(), |
| 60 | 'locale' => give_stripe_get_preferred_locale(), |
| 61 | ]; |
| 62 | |
| 63 | // If featured image exists, then add it to checkout session. |
| 64 | $formId = give_get_payment_form_id($paymentData->donationId); |
| 65 | if (!empty(get_the_post_thumbnail($formId))) { |
| 66 | $session_args['line_items'][0]['images'] = [get_the_post_thumbnail_url($formId)]; |
| 67 | } |
| 68 | |
| 69 | $session = give(CheckoutSession::class)->create($session_args); |
| 70 | |
| 71 | give_insert_payment_note($paymentData->donationId, 'Stripe Checkout Session ID: ' . $session->id()); |
| 72 | give_set_payment_transaction_id($paymentData->donationId, $session->id()); |
| 73 | |
| 74 | return $session; |
| 75 | } |
| 76 | } |
| 77 |