perameter_3); $this->customerService = $customerService; } public function webhook() { // This is your Stripe CLI webhook secret for testing your endpoint locally. $endpoint_secret = 'whsec_xa29CzpHtGdhe6HHQdvJT8oH30zLsClW'; //$endpoint_secret = 'whsec_8713fc6dfb985a0a56eaa2dce9fa5e836b2480189f49865431a0fd529a6a7087'; $payload = @file_get_contents('php://input'); $sig_header = $_SERVER['HTTP_STRIPE_SIGNATURE']; $event = null; try { $event = \Stripe\Webhook::constructEvent( $payload, $sig_header, $endpoint_secret ); } catch(\UnexpectedValueException $e) { // Invalid payload http_response_code(400); exit(); } catch(\Stripe\Exception\SignatureVerificationException $e) { // Invalid signature http_response_code(400); exit(); } // Handle the checkout.session.completed event if ($event->type == 'checkout.session.completed') { $session = $event->data->object; if($session->mode == 'payment'){ $line_items = \Stripe\Checkout\Session::allLineItems($session->id, ['limit' => 100]); try { $user_credits = UserCredit::firstOrCreate(['user_id' => $session->client_reference_id]); $alacarte_order = AlacarteOrder::create([ 'order_number' => rand(11, 99).date('ymdhis'), 'user_id' => $session->client_reference_id, 'stripe_checkout_session_id' => $session->id, 'total' => $session->amount_total, 'status' => $session->payment_status ]); foreach ($line_items->data as $key => $item) { $alacarte = Alacarte::with('category')->where('stripe_product_id',$item->price->product)->first(); if($alacarte){ $alacarte_order->items()->create([ 'alacarte_id' => $alacarte->id, 'price' => $item->price->unit_amount / 100, 'quantity' => $item->quantity, 'discount' => $item->amount_discount, 'total' => $item->amount_total, ]); if($session->payment_status == 'paid'){ $before_credits = $this->customerService->credits($user_credits->user_id); $user_credits->alacarte_credits = $user_credits->alacarte_credits + ($alacarte->credits * $item->quantity); $user_credits->update(); $after_credits = $this->customerService->credits($user_credits->user_id); $user_credits_history = new UserCreditHistory; $user_credits_history->user_id = $user_credits->user_id; $user_credits_history->subscription_credits_from = $before_credits['total_available_credits']['subscription']; $user_credits_history->subscription_credits_to = $after_credits['total_available_credits']['subscription']; $user_credits_history->alacarte_credits_from = $before_credits['total_available_credits']['alacarte'];; $user_credits_history->alacarte_credits_to = $after_credits['total_available_credits']['alacarte']; $user_credits_history->details = 'Added '.$item->quantity.'X '.$alacarte->name; $user_credits_history->save(); } } } if($session->total_details->amount_discount > 0){ $checkout_session = \Stripe\Checkout\Session::retrieve([ 'id' => $session->id, 'expand' => ['total_details.breakdown'], ]); if($coupon_id = $checkout_session['total_details']['breakdown']['discounts'][0]['discount']['coupon']['id']){ $coupon = SubscriptionCoupon::where('stripe_coupon_id',$coupon_id)->first(); if($coupon){ $coupon_redemption = new SubscriptionCouponRedemption; $coupon_redemption->subscription_coupon_id = $coupon->id; $coupon_redemption->customer_id = $alacarte_order->user_id; $coupon_redemption->alacarte_order_id = $alacarte_order->id; $coupon_redemption->save(); } } } } catch(\Exception $e) { Log::error($e->getMessage()); http_response_code(400); exit(); } } } http_response_code(200); } }