<?php

namespace App\Http\Controllers\Api;

use App\Mail\MailCheckout;
use App\Models\Country;
use App\Models\Product;
use App\Models\ShippingCountry;
use App\Models\ShoppingOrder;
use App\Models\ShoppingOrderItem;
use App\Models\ShoppingUser;
use App\Services\CustomerPriority;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Carbon\Carbon;
use Illuminate\Support\Facades\Mail;
use PHPUnit\Framework\Constraint\Count;
use Yard;


class ShoppingUserController extends Controller
{

    //protected static API_MAIL = 'api.thomas.krummel@gmail.com';
    //protected static API_PASS = 'UF(Q<9knap!ev3vH?5~!b8DP';


    protected $successStatus = 200;
    protected $member_id = 3; //thomas.krummel@gmail.com


    /**
     * @param Request $request
     * wp_order_numbers[1234, 1234]
     * @return \Illuminate\Http\JsonResponse
     */
    public function status(Request $request)
    {

        $request->validate([
            'wp_order_numbers' => 'required',
        ]);

        if(!is_array($request->wp_order_numbers)){
            $wp_order_numbers = json_decode($request->wp_order_numbers);

        }else{
            $wp_order_numbers = $request->wp_order_numbers;
        }


        if(!$wp_order_numbers || !is_array($wp_order_numbers)){
            return response()->json([
                'success' => false,
                'message' => 'wp_order_numbers need as json [1234, 1234] ',
                'time' => Carbon::now()->toDateTimeString()
            ], 400);
        }

        $status = [];
        foreach ($wp_order_numbers as $wp_order_number){
            $shopping_user = ShoppingUser::where('wp_order_number', '=', $wp_order_number)->first();
            $status[] = [
                'wp_order_number' => $wp_order_number,
                'user' => $shopping_user ? true : false,
                'order' => ($shopping_user && $shopping_user->shopping_order) ? true : false,
                'status' => $shopping_user ? $shopping_user->getAPIShippedType() : false,
            ];
        }

        return response()->json([
            'success' => true,
            'data' => $status,
            'time' => Carbon::now()->toDateTimeString()
        ], 200);

    }

    /**
     * @param Request $request
     * wp_order_number [1234]
     * @return \Illuminate\Http\JsonResponse
     */
    public function cancel(Request $request)
    {
        $request->validate([
            'wp_order_number' => 'required|int',
        ]);
        $shopping_user = ShoppingUser::where('wp_order_number', '=', $request->wp_order_number)->first();
        if (!$shopping_user) {
            return response()->json([
                'success' => false,
                'message' => 'Entry with wp_order_number ' . $request->wp_order_number . ' not found',
                'order' => false,
                'status' => false,
                'time' => Carbon::now()->toDateTimeString()
            ], 400);
        }
        if(!$shopping_user->shopping_order){
            return response()->json([
                'success' => false,
                'message' => 'Entry with wp_order_number ' . $request->wp_order_number . ' has no order',
                'order' => false,
                'status' => $shopping_user->getAPIShippedType(),
                'time' => Carbon::now()->toDateTimeString()
            ], 400);
        }
        if($shopping_user->shopping_order->shipped > 0){
            return response()->json([
                'success' => false,
                'message' => 'Entry with wp_order_number ' . $request->wp_order_number . ' can not cancel',
                'order' => true,
                'status' => $shopping_user->getAPIShippedType(),
                'time' => Carbon::now()->toDateTimeString()
            ], 400);
        }

        $shopping_user->shopping_order->shipped = 10;
        $shopping_user->shopping_order->save();

        return response()->json([
            'success' => true,
            'message' => 'Entry with wp_order_number ' . $request->wp_order_number . ' is cancel',
            'order' => true,
            'status' => $shopping_user->getAPIShippedType(),
            'time' => Carbon::now()->toDateTimeString()
        ], 200);

    }


    /**
     * @param Request $request
     * wp_order_number [1234]
     * @return \Illuminate\Http\JsonResponse
     */
    public function open(Request $request)
    {
        $request->validate([
            'wp_order_number' => 'required|int',
        ]);
        $shopping_user = ShoppingUser::where('wp_order_number', '=', $request->wp_order_number)->first();
        if (!$shopping_user) {
            return response()->json([
                'success' => false,
                'message' => 'Entry with wp_order_number ' . $request->wp_order_number . ' not found',
                'order' => false,
                'status' => false,
                'time' => Carbon::now()->toDateTimeString()
            ], 400);
        }
        if(!$shopping_user->shopping_order){
            return response()->json([
                'success' => false,
                'message' => 'Entry with wp_order_number ' . $request->wp_order_number . ' has no order',
                'order' => false,
                'status' => $shopping_user->getAPIShippedType(),
                'time' => Carbon::now()->toDateTimeString()
            ], 400);
        }
        if($shopping_user->shopping_order->shipped !== 10){
            return response()->json([
                'success' => false,
                'message' => 'Entry with wp_order_number ' . $request->wp_order_number . ' can not open',
                'order' => true,
                'status' => $shopping_user->getAPIShippedType(),
                'time' => Carbon::now()->toDateTimeString()
            ], 400);
        }

        $shopping_user->shopping_order->shipped = 0;
        $shopping_user->shopping_order->save();

        return response()->json([
            'success' => true,
            'message' => 'Entry with wp_order_number ' . $request->wp_order_number . ' is open',
            'order' => true,
            'status' => $shopping_user->getAPIShippedType(),
            'time' => Carbon::now()->toDateTimeString()
        ], 200);

    }


    /**
     * @param Request $request
     * wp_order_numbers [1234, 1234]
     * @return \Illuminate\Http\JsonResponse
     */
    public function show(Request $request)
    {
        //$this->member_id = auth()->user()->m_sponsor;
        $request->validate([
            'wp_order_numbers' => 'required',
        ]);

        if(!is_array($request->wp_order_numbers)){
            $wp_order_numbers = json_decode($request->wp_order_numbers);

        }else{
            $wp_order_numbers = $request->wp_order_numbers;
        }

        if(!$wp_order_numbers || !is_array($wp_order_numbers)){
            return response()->json([
                'success' => false,
                'message' => 'wp_order_numbers need as json [1234, 1234] ',
                'time' => Carbon::now()->toDateTimeString()
            ], 400);
        }
        $data = [];

        foreach ($wp_order_numbers as $wp_order_number){
            $shopping_user = ShoppingUser::where('wp_order_number', '=', $wp_order_number)->first();
            $user = false;
            $order = false;
            if ($shopping_user) {
                $user = $this->prepareForShow($shopping_user);
                $order = $this->prepareForShowOrder($shopping_user->shopping_order);
            }
            $data[] = [
                'wp_order_number' => $wp_order_number,
                'user' => $user,
                'order' =>  $order,
                'customer_number' => $shopping_user ? $shopping_user->number : false,
                'member_email' => ($shopping_user && $shopping_user->member) ? $shopping_user->member->email : false,
                'status' => $shopping_user ? $shopping_user->getAPIShippedType() : false,            ];
        }
        return response()->json([
            'success' => true,
            'data' => $data,
            'time' => Carbon::now()->toDateTimeString()
        ], 200);

    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function store(Request $request)
    {
        $request->validate([
            'billing_email' => 'required|string|email',
            'billing_firstname' => 'required|string',
            'billing_lastname' => 'required|string',
            'billing_address' => 'required|string',
            'billing_zipcode' => 'required|string',
            'billing_city' => 'required|string',
            'billing_country_code' => 'required|string',
            'wp_order_number' => 'required|int|unique:shopping_users,wp_order_number',
            'wp_order_date' => 'required|date',
        ]);

        $this->member_id = auth()->user()->m_sponsor;

        $data = $this->prepareForStore($request->all());
        $data['member_id'] = $this->member_id ;
        $data['number'] = ShoppingUser::max('number') + 1;
        $data['mode'] = $request->mode ? $request->mode : 'live';
        $data['is_from'] = 'extern';
        $data['is_for'] = 'ot';

        $shopping_user = ShoppingUser::create($data);

        //Kundenhoheit prüfen
        $priority = CustomerPriority::checkOne($shopping_user, true, false);
        \App\Services\Shop::newUserOrder($shopping_user->number);
        //exists //like //update
        $user = $this->prepareForShow($shopping_user);
        return response()->json([
            'success' => true,
            'data' => [
                'wp_order_number' => $shopping_user->wp_order_number,
                'user' => $user,
                'customer_priority' => $priority,
                'customer_number' => $shopping_user->number,
                'member_email' =>  $shopping_user->member->email
            ],
            'time' => Carbon::now()->toDateTimeString()
        ], 200);

    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function update(Request $request)
    {
        $request->validate([
            'wp_order_number' => 'required|int',
        ]);
        $shopping_user = ShoppingUser::where('wp_order_number', '=', $request->wp_order_number)->first();
        if (!$shopping_user) {
            return response()->json([
                'success' => false,
                'message' => 'Entry with wp_order_number ' . $request->wp_order_number . ' not found',
                'time' => Carbon::now()->toDateTimeString()
            ], 400);
        }

        $data = $this->prepareForUpdate($request->all());
        //Kundenhoheit prüfen
        $priority = CustomerPriority::checkChangeOne($shopping_user, $data, true);
        $updated = $shopping_user->fill($data)->save();
        \App\Services\Shop::newUserOrder($shopping_user->number);

        if ($updated){
            $user = $this->prepareForShow($shopping_user);
            $order = $this->prepareForShowOrder($shopping_user->shopping_order);
            return response()->json([
                'success' => true,
                'data' => [
                    'wp_order_number' => $shopping_user->wp_order_number,
                    'user' => $user,
                    'order' =>  $order,
                    'customer_priority' => $priority,
                    'customer_number' => $shopping_user ? $shopping_user->number : false,
                    'member_email' => ($shopping_user && $shopping_user->member) ? $shopping_user->member->email : false,
                    'status' => $shopping_user ? $shopping_user->getAPIShippedType() : false,
                ],
                'time' => Carbon::now()->toDateTimeString()
            ], 200);
        }
        return response()->json([
            'success' => false,
            'message' => 'Entry could not be updated'
        ], 500);
    }

    public function order(Request $request)
    {
        $request->validate([
            'wp_order_number' => 'required|int',
            'wp_order' => 'required',
        ]);


        $shopping_user = ShoppingUser::where('wp_order_number', '=', $request->wp_order_number)->first();
        if (!$shopping_user) {
            return response()->json([
                'success' => false,
                'message' => 'Entry with wp_order_number ' . $request->wp_order_number . ' not found',
                'time' => Carbon::now()->toDateTimeString()
            ], 400);
        }

        if($shopping_user->shopping_order){
            return response()->json([
                'success' => false,
                'message' => 'Order with wp_order_number ' . $request->wp_order_number . ' exists',
                'time' => Carbon::now()->toDateTimeString()
            ], 400);
        }
        if(!is_array($request->wp_order)){
            $wp_order = json_decode($request->wp_order);

        }else{
            $wp_order = $request->wp_order;
        }

        $wp_invoice_path = isset($request->wp_invoice_path) ? $request->wp_invoice_path : null;
        $wp_advertising = isset($request->wp_advertising) ? $request->wp_advertising : '';
        $wp_incentives = isset($request->wp_incentives) ? $request->wp_incentives : '';

        $wp_notice = [
            'wp_advertising' => $wp_advertising,
            'wp_incentives' => $wp_incentives,
        ];

        $wp_order = $this->prepareOrder($wp_order, $shopping_user, $wp_invoice_path, $wp_notice);

        if ($wp_order){
            $user = $this->prepareForShow($shopping_user);
            $order = $this->prepareForShowOrder($shopping_user->shopping_order);
            return response()->json([
                'success' => true,
                'data' => [
                    'wp_order_number' => $shopping_user->wp_order_number,
                    'wp_invoice_path' => $wp_invoice_path,
                    'wp_advertising' => $wp_advertising,
                    'wp_incentives' => $wp_incentives,
                    'wp_order' => $wp_order,
                    'user' => $user,
                    'order' => $order,
                    'customer_number' => $shopping_user->number,
                    'member_email' =>  $shopping_user->member->email,
                    'status' => $shopping_user->getAPIShippedType(),
                ],
                'time' => Carbon::now()->toDateTimeString()
            ], 200);
        }
        return response()->json([
            'success' => false,
            'message' => 'Order could not be stored'
        ], 500);
    }

    public function delete(Request $request)
    {
        $request->validate([
            'wp_order_number' => 'required|int',
        ]);
        $shopping_user = ShoppingUser::where('wp_order_number', '=', $request->wp_order_number)->where('mode', '=', 'dev')->first();
        if (!$shopping_user) {
            return response()->json([
                'success' => false,
                'message' => 'Entry with wp_order_number ' . $request->wp_order_number . ' not found or mode != dev',
                'time' => Carbon::now()->toDateTimeString()
            ], 400);
        }
        $shopping_order = $shopping_user->shopping_order;
        if($shopping_order){
            foreach ($shopping_order->shopping_order_items as $shopping_order_item){
                $shopping_order_item->delete();
            }
            $shopping_order->delete();
        }
        $shopping_user->wp_order_number = time();
        $shopping_user->save();
        if ($shopping_user->delete()) {
            return response()->json([
                'success' => true
            ]);
        }
        return response()->json([
            'success' => false,
            'message' => 'Entry could not be deleted'
        ], 500);
    }

    private function prepareForShow($shopping_user){

        if(!$shopping_user){
            return false;
        }

        $shopping_user_data = $shopping_user->toArray();
        $needs = ['wp_order_number', 'wp_order_date', 'billing_company', 'billing_firstname', 'billing_lastname', 'billing_address', 'billing_address_2', 'billing_zipcode', 'billing_city', 'billing_phone', 'billing_email',
            'same_as_billing', 'shipping_company', 'shipping_firstname', 'shipping_lastname', 'shipping_address', 'shipping_address_2', 'shipping_zipcode', 'shipping_city', 'shipping_phone',
            'created_at', 'updated_at', 'user_deleted_at']; //'has_buyed', 'subscribed',

        //$salutation = array('mr' => 1, 'ms' => 2);
        $ret = [];
        foreach ($shopping_user_data as $key=>$value){

            if($key === 'billing_country_id'){
                $ret['billing_country_code'] = $shopping_user->billing_country_id ? $shopping_user->billing_country->code : null;
            }
            if($key === 'shipping_country_id'){
                $ret['shipping_country_code'] = $shopping_user->shipping_country_id ? $shopping_user->shipping_country->code : null;
            }
            if($key === 'billing_salutation'){
                $ret['billing_salutation'] = $shopping_user->billing_salutation === 'ms' ? 2 : 1;
            }
            if($key === 'shipping_salutation'){
                $ret['shipping_salutation'] = $shopping_user->shipping_salutation === 'ms' ? 2 : 1;
            }

            if(in_array($key, $needs)){
                $ret[$key] = $value;
            }
        }
        return $ret;
    }

    private function prepareForShowOrder($shopping_order){

        if(!$shopping_order){
            return false;
        }
        $ret = [
            'country' => isset($shopping_order->shipping_country->country->code) ? $shopping_order->shipping_country->country->code : '',
            'wp_invoice_path' => $shopping_order->wp_invoice_path,
            'total' => ($shopping_order->total*100),
            'shipping' => ($shopping_order->shipping*100),
            'total_net' => ($shopping_order->subtotal*100),
            'tax_rate' => ($shopping_order->tax_rate*100),
            'tax'  => ($shopping_order->tax*100),
            'total_with_shipping' => ($shopping_order->total_shipping*100),
            'weight' => $shopping_order->weight,
        ];
        $ret['items'] = [];
        foreach ($shopping_order->shopping_order_items as $item){
            $ret['items'][] = [
                'article' => $item->product->wp_number,
                'name' => $item->product->name,
                'qty' => $item->qty,
                'price' => ($item->price * 100),
            ];
        }
        return $ret;
    }

    private function prepareForUpdate($data){

        //$salutation = array(1 => 'mr', 2 => 'ms', 3=>null);

        if(isset($data['billing_salutation'])){
            $data['billing_salutation'] =  (int) $data['billing_salutation'];
            $data['billing_salutation'] = $data['billing_salutation'] == 2 ? 'ms' : 'mr';
        }
        if(isset($data['shipping_salutation'])){
            $data['shipping_salutation'] =  (int) $data['shipping_salutation'];
            $data['shipping_salutation']  = $data['shipping_salutation'] == 2 ? 'ms' : 'mr';
        }

        $ret = [];
        $needs = [ 'billing_salutation', 'billing_company', 'billing_firstname', 'billing_lastname', 'billing_address', 'billing_address_2', 'billing_zipcode', 'billing_city', 'billing_phone', 'billing_email', 'same_as_billing',
            'shipping_salutation', 'shipping_company', 'shipping_firstname', 'shipping_lastname', 'shipping_address', 'shipping_address_2', 'shipping_zipcode', 'shipping_city', 'shipping_phone'];

        foreach ($data as $key=>$value){
            if($key === 'billing_country_code' && isset($data['billing_country_code'])) {
                $ret['billing_country_id'] =  Country::getCountryIdByCodeOrOne($data['billing_country_code']);
            }
            if($key === 'shipping_country_code' && isset($data['shipping_country_code']) ) {
                $ret['shipping_country_id'] = Country::getCountryIdByCodeOrOne($data['shipping_country_code']);
            }
            if($key === 'billing_phone') {
                $ret['billing_phone'] = strlen($data['billing_phone']) <= 3 ? '' : $data['billing_phone'];
            }
            if($key === 'shipping_phone') {
                $ret['shipping_phone'] = strlen($data['shipping_phone']) <= 3 ? '' : $data['shipping_phone'];
            }
            if(in_array($key, $needs)){
                $ret[$key] = $value;
            }
        }
        return $ret;
    }

    private function prepareForStore($data){

        //$salutation = array(1 => 'mr', 2 => 'ms', 3=>null);
        if(isset($data['billing_salutation'])){
            $data['billing_salutation'] =  (int) $data['billing_salutation'];
            $data['billing_salutation'] = $data['billing_salutation'] == 2 ? 'ms' : 'mr';
        }
        if(isset($data['shipping_salutation'])){
            $data['shipping_salutation'] =  (int) $data['shipping_salutation'];
            $data['shipping_salutation']  = $data['shipping_salutation'] == 2 ? 'ms' : 'mr';
        }
        $ret = [];
        $needs = [ 'billing_salutation', 'billing_company', 'billing_firstname', 'billing_lastname', 'billing_address', 'billing_address_2', 'billing_zipcode', 'billing_city', 'billing_country_id', 'billing_phone', 'billing_email',
            'shipping_salutation', 'shipping_company', 'shipping_firstname', 'shipping_lastname', 'shipping_address', 'shipping_address_2', 'shipping_zipcode', 'shipping_city', 'shipping_country_id', 'shipping_phone',
            'same_as_billing',  //'has_buyed', 'subscribed',
            'wp_order_number', 'wp_order_date'];

        foreach ($needs as $need){

            $ret[$need] = isset($data[$need]) ? $data[$need] : null;
            if ($need === 'billing_country_id') {
                $ret['billing_country_id'] = isset($data['billing_country_code']) ? Country::getCountryIdByCodeOrOne($data['billing_country_code']) : 1;
            }
            if ($need === 'shipping_country_id') {
                $ret['shipping_country_id'] = isset($data['shipping_country_code']) ? Country::getCountryIdByCodeOrOne($data['shipping_country_code']) : $ret['billing_country_id'];
            }
            if ($need === 'billing_phone' && $ret[$need] !== null) {
                $ret['billing_phone'] = strlen($data['billing_phone']) <= 3 ? '' : $data['billing_phone'];
            }
            if ($need === 'shipping_phone' && $ret[$need] !== null) {
                $ret['shipping_phone'] = strlen($data['shipping_phone']) <= 3 ? '' : $data['shipping_phone'];
            }
            if ($need === 'wp_order_date') {
                $ret['wp_order_date'] = Carbon::parse($ret['wp_order_date'])->toDateTimeString();
            }
            if ($need === 'same_as_billing') {
                $ret['same_as_billing'] =  isset($data['same_as_billing']) ? $data['same_as_billing'] : true;
            }
        }
        $ret['has_buyed'] = true;
        $ret['subscribed'] = false;
        return $ret;
    }

    private function prepareOrder($wp_shopping_order, $shopping_user, $wp_invoice_path, $wp_notice){
        Yard::instance('shopping')->destroy();
        $ret = [];

        if(is_array($wp_shopping_order)){
            foreach ($wp_shopping_order as $order) {
                //$object = json_decode(json_encode($order), FALSE);
                $order = (object) $order;
                $error = [];
                if (!isset($order->article) || !isset($order->qty) || !isset($order->price)) {
                    $error[] = "article parameter is missing";
                } else {

                    $product = Product::whereWpNumber($order->article)->first();
                    if (!$product) {
                        $error[] = "article not found";
                    } else {
                        if ($order->price != ($product->price * 100)) {
                            $error[] = "different price: " . ($product->price * 100);
                        }
                        Yard::instance('shopping')->add($product->id, $product->name, (int) $order->qty, $product->price, ['image' => [], 'slug' => $product->slug, 'weight' => $product->weight]);
                    }
                }
                $order->message = $error;
                $ret[] = $order;
            }

            $ShippingCountry = ShippingCountry::whereCountryId($shopping_user->shipping_country_id)->first();
            if($ShippingCountry){
                Yard::instance('shopping')->setShippingCountryWithPrice($ShippingCountry->id);
            }
            $shopping_order = $this->makeShoppingOrder($shopping_user, $wp_invoice_path, $wp_notice);
            $this->orderStatusSendMail($shopping_order);

            $shopping_user->shopping_order = $shopping_order;
            Yard::instance('shopping')->destroy();
        }
        return $ret;
    }

    private function makeShoppingOrder($shopping_user, $wp_invoice_path, $wp_notice){

        $data = [
            'shopping_user_id' => $shopping_user->id,
            'auth_user_id' => $shopping_user->auth_user_id,
            'country_id' => Yard::instance('shopping')->getShippingCountryId(),
            'user_shop_id' => auth()->user()->user_sponsor->shop->id,
            'member_id' => $shopping_user->member_id,
            'total' => Yard::instance('shopping')->total(2, '.', ''),
            'subtotal' => Yard::instance('shopping')->subtotal(2, '.', ''),
            'shipping' => Yard::instance('shopping')->shipping(2, '.', ','),
            'shipping_net' => Yard::instance('shopping')->shippingNet(2, '.', ''),
            'subtotal_shipping' => Yard::instance('shopping')->subtotalWithShipping(2, '.', ''),
            'tax' => Yard::instance('shopping')->taxWithShipping(2, '.', ''),
            'total_shipping' => Yard::instance('shopping')->totalWithShipping(2, '.', ''),
            'points' => Yard::instance('shopping')->points(),
            'weight' => Yard::instance('shopping')->weight(),
            'paid' => true,
            'txaction' => 'extern',
            'wp_invoice_path' => $wp_invoice_path,
            'wp_notice' => $wp_notice,
            'mode' => $shopping_user->mode,
        ];
        $shopping_order = $shopping_user->shopping_order;
        if($shopping_order){
            $shopping_order->fill($data);
            $shopping_order->save();
        }else{
            $shopping_order= ShoppingOrder::create($data);
        }
        $items = Yard::instance('shopping')->content();

        $shopping_order->shopping_order_items()->each(function($model) use ($items, $shopping_order) {
            foreach ($items as $item) {
                if ($model->row_id === $item->rowId) {
                    $model->fill([
                        'shopping_order_id' => $shopping_order->id,
                        'row_id' =>  $item->rowId,
                        'product_id' => $item->id,
                        'qty' => $item->qty,
                        'price' => $item->price,
                        'price_net' => Yard::instance('shopping')->rowPriceNet($item, 3, '.', ''),
                        'tax_rate' => $item->taxRate,
                        'slug' => $item->options->slug,
                    ])->save();
                    return false;
                }
            }
            return $model->delete();
        });


        foreach ($items as $item) {
            if (!ShoppingOrderItem::where('shopping_order_id', $shopping_order->id)->where('row_id', $item->rowId)->count())
                ShoppingOrderItem::create([
                    'shopping_order_id' => $shopping_order->id,
                    'row_id' =>  $item->rowId,
                    'product_id' => $item->id,
                    'qty' => $item->qty,
                    'price' => $item->price,
                    'price_net' => Yard::instance('shopping')->rowPriceNet($item, 3, '.', ''),
                    'tax_rate' => $item->taxRate,
                    'slug' => $item->options->slug
                ]);
        }

        return $shopping_order;
    }


    public function orderStatusSendMail(ShoppingOrder $shopping_order){

        $bcc = [];
        $user_mail = $shopping_order->shopping_user->member->email;
        if($shopping_order->mode === 'dev'){
            $bcc[] = config('app.checkout_test_mail');
        }else{
            $bcc[] = config('app.checkout_mail');
        }

        Mail::to($user_mail)->bcc($bcc)->send(new MailCheckout($shopping_order->txaction, $shopping_order, null, false, $shopping_order->mode));
    }

}