Search code examples
laravelstripe-paymentsoctobercms

Capture stripe payment


I am using the stripe/stripe package to capture payment for single product. Everything is working as expected however when the user is returned to the order-confirmation page, I would like to get the stripe payment id. How would i achieve this?

Im not sure if i need a webhook and if i would, how would i use this?

public function onCharge() {

    $user = Auth::getUser();
    $course = CourseMeta::where('id', $this->param('id'))->first();


    $stripe = new \Stripe\StripeClient(env('STRIPE_SECRET'));
    $product = $stripe->products->create([
      'name' => $course->course->name . ' - ' . $course->date,
    ]);
    $price = $stripe->prices->create([
      'unit_amount' => $course->price * 120,
      'currency' => 'gbp',
      'product' => $product->id,
    ]);

    $validator = Validator::make(
        [
            'user_id' => $user->id,
            'coursemeta_id' => $course->id,
            'course_id' => $course->course->id,
            'stripe_id' => '1',
        ],
        [
            'user_id' => 'required',
            'coursemeta_id' => 'required',
            'course_id' => 'required',
            'stripe_id' => 'required'
        ]
    );

    if ($validator->fails()) {
        return Redirect::back()->withErrors($validator);
    } else {

            $order = new Order();

            $order->user_id = $user->id;
            $order->coursemeta_id = $course->id;
            $order->stripe_id = '1';
            $order->company = request()->get('company');
            $order->company_firstname = request()->get('company_firstname');
            $order->company_lastname = request()->get('company_lastname');
            $order->company_email = request()->get('company_email');
            $order->company_phone = request()->get('company_phone');
            $order->citb_levy = request()->get('citb_levy');
            $order->d_firstname = request()->get('d_firstname');
            $order->d_lastname = request()->get('d_lastname');
            $order->d_email = request()->get('d_email');
            $order->d_phone = request()->get('d_phone');
            $order->d_dob = request()->get('d_dob');
            $order->d_ninumber = request()->get('d_ninumber');


            $order->save();
        }


    $portal = $stripe->checkout->sessions->create([
      'success_url' => env('APP_URL') . '/order-confirmation'.'?' . 'user=' . $user->id . '&' . 'course=' . $course->id,
      'cancel_url' => env('APP_URL') . '/cancel',
      'line_items' => [
        [
          'price' => $price->id,
          'quantity' => 1
        ],
      ],
      'mode' => 'payment',
    ]);
          
    return redirect($portal->url);

}

Solution

  • You need to use the checkout session ID.

    when you create success_url and cancel_url you can also pass {CHECKOUT_SESSION_ID} param with curly braces. when payment gateway will redirect to one of those URLs it will automatically replace {CHECKOUT_SESSION_ID} with the actual session ID.

    In your case

    $portal = $stripe->checkout->sessions->create([
        // HERE ---------------------------------------------------\/----------\/
       'success_url' => env('APP_URL') . '/order-confirmation'.'?session_id={CHECKOUT_SESSION_ID}&' . 'user='. $user->id . '&' . 'course=' . $course->id,
       'cancel_url' => env('APP_URL') . '/cancel',
       'line_items' => [
         [
           'price' => $price->id,
           'quantity' => 1
         ],
       ],
      'mode' => 'payment',
    ]);
    

    Now when this page is called you can have session ID and you can retrieve all stuff from it.

    // on success or cancel page you can use this code to get infos
    $stripe = new \Stripe\StripeClient(env('STRIPE_SECRET'));
    
    
    $session = \Stripe\Checkout\Session::retrieve(get('session_id'));
    
    
    $customer = \Stripe\Customer::retrieve($session->customer);
    $paymentIntent = \Stripe\PaymentIntent::retrieve($session->payment_intent);
    
    // from this $session you can get customer/items/paymentIntent etc..
    
    

    ref : https://stripe.com/docs/payments/checkout/custom-success-page

    if any doubt please comment.