app/Http/Controllers/CartController.php
/*
        Your Credit card was charged successfully.
        Date: 2018/12/05 20:26:35
        Invoice: 00026
        Charge ID: ch_1De6x4HMprYyJrNboyBNyQkm
        Card: Visa, 4242, 1/2019
        Total: 8.50

        (1) Points Reserve: For IccID: 8944503540145562672 8.50
    */
Your Credit card was charged successfully.


Your Credit card was charged successfully .
public function postShopPay(Request $request) {
        \Stripe\Stripe::setApiKey(config('services.stripe.secret'));
        $user = Auth::user();
        $stripe_id = $user->stripe_id;
        $currency = $user->currency;
        Cashier::useCurrency($currency);
        $order = new Order([
            'total_amount' => 0,
        ]);
        $order->user()->associate($user); 
        $order->save();
        $total_amount = 0;
        $rows = $request->rowId;
        foreach ($rows as $row_id) {
            $cart = CartItem::find($row_id);
            if ($cart) {
                $iccid = $cart->iccid;
                $quantity = $cart->quantity;
                $sku = PlanProductSku::find($cart->plan_product_sku_id);
                $month = $sku->month;
                $price = $sku->price;
                $sub_plan = $sku->sub_plan; 
                $product = PlanProduct::find($sku->plan_product_id);
                $points = $product->points;
                $data_plans = $product->data_plans;
                $item = $order->items()->make([
                    'quantity' => $quantity,
                    'price'  => $price,
                    'iccid' => $iccid,
                    'sub_plan' => $sub_plan,
                    'points' => $points,
                    'data_plans' => $data_plans,
                    'month' => $month,
                ]);
                $item->planProduct()->associate($sku->plan_product_id); 
                $item->planProductSku()->associate($sku); Sku
                $item->save();
                $txt_month = ($month > 1) ? 'Months' : 'Month';
                if (env('APP_USE_POINTS')) {
                    $description = $product->title.' '.$points.' Points per Month - for '.$month.' '.$txt_month;
                } else {
                    $description = $product->title.' '.$data_plans.' MB per Month - for '.$month.' '.$txt_month;
                }
                $amount = $quantity * $price;
                $total_amount += $amount;
                \Stripe\InvoiceItem::create([
                    "customer" => $stripe_id,
                    "amount" => $amount*100,
                    "currency" => $product->currency, 
                    "description" => $description, 
                    'metadata' => [
                        'order_no' => $order->no,
                        'iccid' => $iccid,
                        'plan' => $sub_plan,
                    ],
                ]);
                $cart->delete();
            }
        }
        $order->update(['total_amount' => $total_amount]);
        $invoice = \Stripe\Invoice::create([
            "customer" => $stripe_id,
            "auto_advance" => false, 
            'metadata' => [
                'order_no' => $order->no,
            ],
        ]);
        if ($invoice) {
            $ret = $invoice->pay();
            if ($ret->status == 'paid') {
                if ($ret->charge != null) {
                    $result = $this->updateOrder($order, $ret->charge);
                        $order = Order::where('no', $order->no)->first();
                        $order_items = OrderItem::where('order_id', $order->id)->get();
                        foreach ($order_items as $order_item) {
                            $subscription = $this->createSubscription($order, $order_item);
                        }
                } else {
                    session()->flash('danger', 'Invoice Charge Fail');
                }
            } else { 
                session()->flash('danger', 'Invoice Pay Fail');
            }
        } else {
            session()->flash('danger', 'Invoice Create Fail');
        }
return redirect()->route('account.profile');
 else {
    }
public function postShopPay ( Request $ request ) { \ Stripe \ Stripe :: setApiKey ( config ( 'services.stripe.secret' ) ) ; $ user = Auth :: user ( ) ; $ stripe_id = $ user -> stripe_id ; $ currency = $ user -> currency ; Cashier :: useCurrency ( $ currency ) ; $ order = new Order ( [ 'total_amount' => 0 , ] ) ; $ order -> user ( ) -> associate ( $ user ) ; $ order -> save ( ) ; $ total_amount = 0 ; $ rows = $ request -> rowId ; foreach ( $ rows as $ row_id ) { $ cart = CartItem :: find ( $ row_id ) ; if ( $ cart ) { $ iccid = $ cart -> iccid ; $ quantity = $ cart -> quantity ; $ sku = PlanProductSku :: find ( $ cart -> plan_product_sku_id ) ; $ month = $ sku -> month ; $ price = $ sku -> price ; $ sub_plan = $ sku -> sub_plan ; $ product = PlanProduct :: find ( $ sku -> plan_product_id ) ; $ points = $ product -> points ; $ data_plans = $ product -> data_plans ; $ item = $ order -> items ( ) -> make ( [ 'quantity' => $ quantity , 'price' => $ price , 'iccid' => $ iccid , 'sub_plan' => $ sub_plan , 'points' => $ points , 'data_plans' => $ data_plans , 'month' => $ month , ] ) ; $ item -> planProduct ( ) -> associate ( $ sku -> plan_product_id ) ; $ item -> planProductSku ( ) -> associate ( $ sku ) ; $ item -> save ( ) ; $ txt_month = ( $ month > 1 ) ? 'Months' : 'Month' ; if ( env ( 'APP_USE_POINTS' ) ) { $ description = $ product -> title . ' ' . $ points . ' Points per Month - for ' . $ month . ' ' . $ txt_month ; } else { $ description = $ product -> title . ' ' . $ data_plans . ' MB per Month - for ' . $ month . ' ' . $ txt_month ; } $ amount = $ quantity * $ price ; $ total_amount += $ amount ; \ Stripe \ InvoiceItem :: create ( [ " customer " => $ stripe_id , " amount " => $ amount * 100 , " currency " => $ product -> currency , " description " => $ description , 'metadata' => [ 'order_no' => $ order -> no , 'iccid' => $ iccid , 'plan' => $ sub_plan , ] , ] ) ; $ cart -> delete ( ) ; } } $ order -> update ( [ 'total_amount' => $ total_amount ] ) ; $ invoice = \ Stripe \ Invoice :: create ( [ " customer " => $ stripe_id , " auto_advance " => false , 'metadata' => [ 'order_no' => $ order -> no , ] , ] ) ; if ( $ invoice ) { $ ret = $ invoice -> pay ( ) ; if ( $ ret -> status == 'paid' ) { if ( $ ret -> charge != null ) { $ result = $ this -> updateOrder ( $ order , $ ret -> charge ) ; $ order = Order :: where ( 'no' , $ order -> no ) -> first ( ) ; $ order_items = OrderItem :: where ( 'order_id' , $ order -> id ) -> get ( ) ; foreach ( $ order_items as $ order_item ) { $ subscription = $ this -> createSubscription ( $ order , $ order_item ) ; } } else { session ( ) -> flash ( 'danger' , 'Invoice Charge Fail' ) ; } } else { session ( ) -> flash ( 'danger' , 'Invoice Pay Fail' ) ; } } else { session ( ) -> flash ( 'danger' , 'Invoice Create Fail' ) ; } return redirect ( ) -> route ( 'account.profile' ) ; }
Your Credit card was charged successfully.
Your Credit card was charged successfully .
// {"_token":"xxxx","rowId":["5","6"]} // Cashier::useCurrency('eur', '€'); /* Create Order */ // 'address'      => [ // 将地址信息放入订单中 //     'address'       => $address->full_address, //     'zip'           => $address->zip, //     'contact_name'  => $address->contact_name, //     'contact_phone' => $address->contact_phone, // ], // 'remark'       => $request->input('remark'), // 订单关联到当前用户 // $cart_id = $row_id; /* Plan Product SKU */ // $sku = $cart->planProductSku()->get()[0]; // 'au_5000_1m' /* Plan Product */ /* Create Order Item */ // function name must be planProduct // function name must be planProductSku /* create Invoice Item */ // SILVER 5000 Points per Month - for 1 Month // SILVER 5000 for 1 Month //$currency, //"SILVER 5000 Points per Month - for 1 Month", /* delete Cart Item */ /* Plan */ // $this->_paySubscriptionX(); /* create Invoice & Pay */ /* true: auto-finalize this draft after ~1 hour */ // echo $ret->charge.'<br/>';      // ch_1Di2cYG8UgnSL68U1YxHtDxP // echo $ret->paid.'<br/>';        // true // echo $ret->status.'<br/>';      // paid (draft, open, paid, uncollectible, or void) /*
"id": "in_1ENkGxG8UgnSL68U7SZxSnjB"
"billing": "charge_automatically"
"charge": null
"number": "38BFE108-0001"
"status": "paid"
*/ // return dd($ret); // for debug // if ($result == 'success') { // if (!$subscription) { send email } // } // ($ret->status == 'paid') //         try { //             $charge = \Stripe\Charge::create([ //                 'amount' => $total*100, //10000, //                 'currency' => $currency, //'usd', //                 'description' => 'Example charge', //                 'customer' => $stripe_id, //'cus_DvGvznoT2EBbyn', //             ]); //             // $charge = $user->charge($total*100); //         } catch (Exception $e) { // \Exception $e //             return ['status' => -1, 'msg' => $e->getMessage()]; //         } //         $event = $charge->jsonSerialize(); //         if ($event['status'] == 'succeeded' || $event['status'] == 'pending') { //             return ['status' => 1, 'msg' => 'OK']; //         } else { //             return ['status' => -1, 'msg' => $event['status']]; //         } // echo $charge->id.'<br/>'; // echo $charge->amount.'<br/>'; // echo $charge->status.'<br/>'; // return dd($charge); // Payment for invoice ED0214E-0001 – ch_1De8vyG8UgnSL68UhmxYH7h2 // $invoice = $user->invoiceFor('One Time Fee', $total*100); // $user->invoiceFor('One Time Fee', 500, [ //     'custom-option' => $value, // ]); // return dd($invoice);