From 781ad61ace191fa082cab6bbd41bfe22f91d2975 Mon Sep 17 00:00:00 2001 From: Steve <34465153+xxl4@users.noreply.github.com> Date: Fri, 8 Nov 2024 13:36:31 +0800 Subject: [PATCH] fix --- src/Helpers/Utils.php | 184 ++ src/Http/Controllers/Api/ApiController.php | 1278 +++++++++++++ .../Controllers/Web/ProductController.php | 1614 +++++++++++++++++ .../Controllers/Web/ProductV2Controller.php | 366 ++++ .../Controllers/Web/ProductV3Controller.php | 250 +++ .../Controllers/Web/ProductV4Controller.php | 395 ++++ .../Controllers/Web/ProductV5Controller.php | 500 +++++ 7 files changed, 4587 insertions(+) create mode 100644 src/Helpers/Utils.php create mode 100644 src/Http/Controllers/Api/ApiController.php create mode 100644 src/Http/Controllers/Web/ProductController.php create mode 100644 src/Http/Controllers/Web/ProductV2Controller.php create mode 100644 src/Http/Controllers/Web/ProductV3Controller.php create mode 100644 src/Http/Controllers/Web/ProductV4Controller.php create mode 100644 src/Http/Controllers/Web/ProductV5Controller.php diff --git a/src/Helpers/Utils.php b/src/Helpers/Utils.php new file mode 100644 index 0000000..860128c --- /dev/null +++ b/src/Helpers/Utils.php @@ -0,0 +1,184 @@ +getCurrentCurrencyCode(); + $cache_key = "product_ext_".$product->id."_".count($nums)."_".$currency; + $package_products = Cache::get($cache_key); + + $shipping_price_key = "shipping_price"."_".$currency; + $shipping_price = Cache::get($shipping_price_key); + //var_dump($shipping_price); + if(empty($shipping_price)) { + $shipping_price = core()->getConfigData('sales.carriers.flatrate.default_rate'); + $shipping_price = core()->convertPrice($shipping_price); + Cache::put($shipping_price_key, $shipping_price); + } + if(empty($package_products)) { + //if(true) { + $package_products = []; + $productBaseImage = product_image()->getProductBaseImage($product); + + //source price + $productAttributeValueRepository = new ProductAttributeValue(); + + // original price + $productBgAttribute_price = $productAttributeValueRepository->where([ + 'product_id' => $product->id, + 'attribute_id' => 31, + ])->first(); + $source_price = 0; + if(!is_null($productBgAttribute_price)) $source_price = $productBgAttribute_price->float_value; + $source_price = core()->convertPrice($source_price); + if(empty($source_price)) { + return abort(404); + } + + foreach($nums as $key=>$i) { + + $package_product = []; + $package_product['id'] = $i; + $package_product['name'] = $i."x " . $product->name; + $package_product['image'] = $productBaseImage['medium_image_url']; + $package_product['amount'] = $i; + //$package_product['old_price'] = $productPrice['regular']['price'] * $i; + $price = self::getCartProductPrice($product,$product->id, $i); + $package_product['old_price'] = round($source_price * $i, 2); + //$old_price_format = core()->convertPrice($package_product['old_price']); + $package_product['old_price_format'] = core()->currency(round($package_product['old_price'], 2)); + //$package_product['new_price'] = "3.23" * $i; + $package_product['currency_symbol'] = core()->currencySymbol(core()->getCurrentCurrencyCode()); + if ($i==2) $discount = 0.8; + if ($i==3) $discount = 0.7; + if ($i==4) $discount = 0.6; + if ($i==1) $discount = 1; + $package_product['new_price'] = $price * $discount; + //$new_price_format = core()->currency($currency).round($package_product['new_price'], 2); + $package_product['new_price_format'] = core()->currency(round($package_product['new_price'], 2)); + $tip1_price = (1 - round(($package_product['new_price'] / $package_product['old_price']), 2)) * 100; + $package_product['tip1'] = $tip1_price."% "; + $tip2_price = round($package_product['new_price'] / $i, 2); + $package_product['tip2'] = core()->currency($tip2_price); + $package_product['shipping_fee'] = core()->convertPrice($shipping_price); // shipping price + $package_product['return_fee'] = core()->convertPrice(2.99); // return price + $popup_info['name'] = null; + $popup_info['old_price'] = null; + $popup_info['new_price'] = null; + $popup_info['img'] = null; + $package_product['popup_info'] = $popup_info; + $package_products[] = $package_product; + } + + Cache::put($cache_key, json_encode($package_products)); + //var_dump("hello"); + return $package_products; + } + + return json_decode($package_products, JSON_OBJECT_AS_ARRAY); + } + + /** + * + * + * 计算商品在具体的数量的时候的价格,主要是考虑到会有购物车折扣的情况下 + * + * @param int $product_id + * @param int $qty + * + * @return float price + * + */ + private static function getCartProductPrice($product, $product_id, $qty) { + //清空购车动作 + Cart::deActivateCart(); + //添加对应的商品到购物车中 + + $productType = $product->type; + + $AddcartProduct = []; + + $AddcartProduct['quantity'] = $qty; + + $productViewHelper = new \Webkul\Product\Helpers\ConfigurableOption(); + + $attributes = $productViewHelper->getConfigurationConfig($product); + + if($product_id) + + if($productType=="simple") { + + } + + if($product_id==3692) { + //Log::info($product_id."--". json_encode($attributes)); + //exit; + } + + if($productType=='configurable') { + $product_variant_id = 0; + $super_attribute = []; + //var_dump($attributes);exit; + foreach($attributes['attributes'] as $key=>$attribute) { + Log::info($product_id."--". json_encode($attribute)); + if(!isset($attribute['options'][0]['id'])) continue; + $super_attribute[$attribute['id']] = isset($attribute['options'][0]['id']) ? $attribute['options'][0]['id'] : 0 ; + $product_variant_id = isset($attribute['options'][0]['products'][0]) ? $attribute['options'][0]['products'][0] : 0 ; + } + + $AddcartProduct['selected_configurable_option'] = $product_variant_id; + $AddcartProduct['super_attribute'] = $super_attribute; + } + //var_dump($attributes);exit; + //var_dump($product['product_id']);exit; + if($product_id==3692) { + //Log::info($product_id."--". json_encode($AddcartProduct)); + //exit; + } + $cart = Cart::addProduct($product['product_id'], $AddcartProduct); + $cart = Cart::getCart(); + //清空购车动作 + Cart::deActivateCart(); + + return $cart->grand_total; + + } + + public static function getCurrencyByCountry($country) { + + $channel = core()->getCurrentChannel(); + + Log::info("channel: ".json_encode($channel)); + + $country = strtoupper($country); + + // currency from currenices table + $currencies = core()->getAllCurrencies(); + + Log::info("currencies: ".json_encode($currencies)); + + $currencies = core()->getChannelBaseCurrency(); + + Log::info("channel currencies: ".json_encode($currencies)); + + return "USD"; + } + +} \ No newline at end of file diff --git a/src/Http/Controllers/Api/ApiController.php b/src/Http/Controllers/Api/ApiController.php new file mode 100644 index 0000000..a1540fd --- /dev/null +++ b/src/Http/Controllers/Api/ApiController.php @@ -0,0 +1,1278 @@ +server('HTTP_CF_IPCOUNTRY'); + + // currency by ip + $currency = \Nicelizhi\OneBuy\Helpers\Utils::getCurrencyByCountry($ip_country); + + $currency_get = $request->input('currency'); + if(!empty($currency_get)) { + core()->setCurrentCurrency($currency_get); + } + + + + //var_dump($currency);exit; + + $data = Cache::get($this->checkout_v2_cache_key.$slug.$currency); + $env = config("app.env"); + // when the env is pord use cache + + $paypal_id_token = $this->smartButton->getIDAccessToken(); + $paypal_access_token = $paypal_id_token->result->access_token; + $paypal_id_token = $paypal_id_token->result->id_token; + + if(empty($data)) { + //if(true) { + $product = $this->productRepository->findBySlug($slug); + if(is_null($product)) { + return response()->json(['error' => 'Product not found','code'=>'201'], 400); + } + $data = []; + $productViewHelper = new \Webkul\Product\Helpers\ConfigurableOption(); + $attributes = $productViewHelper->getConfigurationConfig($product); + + + $redis = Redis::connection('default'); + + $product_attr_sort_cache_key = "product_attr_sort_23_".$product->id; + $product_attr_sort = $redis->hgetall($product_attr_sort_cache_key); // get sku sort + + foreach($attributes['attributes'] as $key=>$attribute) { + + $product_attr_sort_cache_key = "product_attr_sort_".$attribute['id']."_".$product->id; + $product_attr_sort = $redis->hgetall($product_attr_sort_cache_key); // get sku sort + $attributes['attributes'][$key]['attr_sort'] = $product_attr_sort; + } + + foreach($attributes['index'] as $key=>$index) { + + $sku_products = $this->productRepository->where("id", $key)->select(['sku'])->first(); + $attributes['index'][$key]['sku'] = $sku_products->sku; + $index2 = ""; + $total = count($index); + $i = 0; + foreach($index as $key2=>$ind) { + $i++; + if(empty($index2)) { + $index2=$key2."_".$ind; + } else { + $index2=$index2.",".$key2."_".$ind; + } + if($i==$total) $attributes['index2'][$index2] = [$key,$sku_products->sku]; + } + //var_dump($index); + + } + + $package_products = []; + $package_products = \Nicelizhi\OneBuy\Helpers\Utils::makeProducts($product, [2,1,3,4]); + $product = new ProductResource($product); + $data['product'] = $product; + $data['package_products'] = $package_products; + $data['sku'] = $product->sku; + $data['attr'] = $attributes; + + $countries = config("countries"); + + $default_country = config('onebuy.default_country'); + + $airwallex_method = config('onebuy.airwallex.method'); + + $payments = config('onebuy.payments'); // config the payments status + + $payments_default = config('onebuy.payments_default'); + $brand = config('onebuy.brand'); + + $gtag = config('onebuy.gtag'); + + $fb_ids = config('onebuy.fb_ids'); + $ob_adv_id = config('onebuy.ob_adv_id'); + + $crm_channel = config('onebuy.crm_channel'); + + $quora_adv_id = config('onebuy.quora_adv_id'); + + $paypal_client_id = core()->getConfigData('sales.payment_methods.paypal_smart_button.client_id'); + + $data['countries'] = $countries; + $data['default_country'] = $default_country; + $data['airwallex_method'] = $airwallex_method; + $data['payments'] = $payments; + $data['payments_default'] = $payments_default; + $data['brand'] = $brand; + $data['gtag'] = $gtag; + $data['fb_ids'] = $fb_ids; + $data['ob_adv_id'] = $ob_adv_id; + $data['crm_channel'] = $crm_channel; + $data['quora_adv_id'] = $quora_adv_id; + $data['paypal_client_id'] = $paypal_client_id; + $data['env'] = config("app.env"); + $data['sellPoints'] = $redis->hgetall("sell_points_".$slug); + $data['sell-points'] = $redis->hgetall("sell_points_".$slug); + + $ads = []; // add ads + + $productBgAttribute = $this->productAttributeValueRepository->findOneWhere([ + 'product_id' => $product->id, + 'attribute_id' => 29, + ]); + + + $productBgAttribute_mobile = $this->productAttributeValueRepository->findOneWhere([ + 'product_id' => $product->id, + 'attribute_id' => 30, + ]); + + $productSizeImage = $this->productAttributeValueRepository->findOneWhere([ + 'product_id' => $product->id, + 'attribute_id' => 32, + ]); + + $ads['pc']['img'] = isset($productBgAttribute->text_value) ? config("app.url").'/storage/'.$productBgAttribute->text_value : config("app.url")."/checkout/onebuy/banners/".$default_country."_pc.jpg"; + $ads['mobile']['img'] = isset($productBgAttribute_mobile->text_value) ? config("app.url").'/storage/'.$productBgAttribute_mobile->text_value : config("app.url")."/checkout/onebuy/banners/".$default_country."_mobile.jpg"; + $ads['size']['img'] = isset($productSizeImage->text_value) ? config("app.url").'/storage/'.$productSizeImage->text_value : ""; + + $data['ads'] = $ads; + + // countdown + //$countdown = $redis->hgetall("countdown_".$slug); + $countdown = 5; // when 0 is not show + $data['countdown'] = $countdown; + + // ad_message + $data['ad_message']['text'] = ""; + // $data['ad_message']['color'] = "#FF0000"; + // $data['ad_message']['bg_color'] = "#FFFF00"; + // $data['ad_message']['href'] = "https://www.google.com"; + + $data['ip_country'] = $ip_country; + + $data['currency'] = $currency; + + $data['customer_config'] = []; + + $checkoutItems = \Nicelizhi\Shopify\Helpers\Utils::getAllCheckoutVersion(); + $customer_config = []; + foreach($checkoutItems as $key=>$item) { + $cachek_key = "checkout_".$item."_".$slug; + $cacheData = $redis->get($cachek_key); + $customer_config[$item] = json_decode($cacheData); + } + + $data['customer_config'] = $customer_config; + + + Cache::put($this->checkout_v2_cache_key.$slug, json_encode($data)); + + $data['paypal_id_token'] = $paypal_id_token; + $data['paypal_access_token'] = $paypal_access_token; + + + + + return response()->json($data); + + } + + + //json decode to array + + $data = json_decode($data); + $data = (array)$data; + $data['paypal_id_token'] = $paypal_id_token; + $data['paypal_access_token'] = $paypal_access_token; + + + return response()->json($data); + } + + /** + * Create a order from cart + * + * @return void + */ + public function OrderAddSync(Request $request) { + + $payment_method = $request->input('payment_method'); + $payment_method_input = $request->input('payment_method'); + $input = $request->all(); + $refer = isset($input['refer']) ? trim($input['refer']) : ""; + + $products = $request->input("products"); + // + Cart::deActivateCart(); + foreach($products as $key=>$product) { + //var_dump($product); + $product['quantity'] = $product['amount']; + $product['selected_configurable_option'] = $product['variant_id']; + if(!empty($product['attr_id'])) { + $attr_ids = explode(',', $product['attr_id']); + foreach($attr_ids as $key=>$attr_id) { + $attr = explode('_', $attr_id); + $super_attribute[$attr[0]] = $attr[1]; + } + + $product['super_attribute'] = $super_attribute; + } + //Log::info("add product into cart ". json_encode($product)); + $cart = Cart::addProduct($product['product_id'], $product); + + if ( + is_array($cart) + && isset($cart['warning']) + ) { + return new JsonResource([ + 'message' => $cart['warning'], + ]); + } + + } + + $this->returnInsurance($input, $cart); + + + // + $addressData = []; + + + $addressData['billing'] = []; + $address1 = []; + array_push($address1, $input['address']); + $addressData['billing']['city'] = $input['city']; + $addressData['billing']['country'] = $input['country']; + $addressData['billing']['email'] = $input['email']; + $addressData['billing']['first_name'] = $input['first_name']; + $addressData['billing']['last_name'] = $input['second_name']; + $input['phone_full'] = str_replace('undefined+','', $input['phone_full']); + $addressData['billing']['phone'] = $input['phone_full']; + $addressData['billing']['postcode'] = $input['code']; + $addressData['billing']['state'] = $input['province']; + $addressData['billing']['use_for_shipping'] = true; + $addressData['billing']['address1'] = $address1; + + $addressData['billing']['address1'] = implode(PHP_EOL, $addressData['billing']['address1']); + + $shipping = []; + $address1 = []; + array_push($address1, $input['address']); + $shipping['city'] = $input['city']; + $shipping['country'] = $input['country']; + $shipping['email'] = $input['email']; + $shipping['first_name'] = $input['first_name']; + $shipping['last_name'] = $input['second_name']; + //undefined+ + $input['phone_full'] = str_replace('undefined+','', $input['phone_full']); + $shipping['phone'] = $input['phone_full']; + $shipping['postcode'] = $input['code']; + $shipping['state'] = $input['province']; + $shipping['use_for_shipping'] = true; + $shipping['address1'] = $address1; + $shipping['address1'] = implode(PHP_EOL, $shipping['address1']); + + + $addressData['shipping'] = $shipping; + $addressData['shipping']['isSaved'] = false; + $address1 = []; + array_push($address1, $input['address']); + $addressData['shipping']['address1'] = $address1; + $addressData['shipping']['address1'] = implode(PHP_EOL, $addressData['shipping']['address1']); + + // customer bill address info + if(@$input['shipping_address']=="other") { + $address1 = []; + array_push($address1, $input['bill_address']); + $billing = []; + $billing['city'] = $input['bill_city']; + $billing['country'] = $input['bill_country']; + $billing['email'] = $input['email']; + $billing['first_name'] = $input['bill_first_name']; + $billing['last_name'] = $input['bill_second_name']; + //undefined+ + $input['phone_full'] = str_replace('undefined+','', $input['phone_full']); + $billing['phone'] = $input['phone_full']; + $billing['postcode'] = $input['bill_code']; + $billing['state'] = $input['bill_province']; + //$billing['use_for_shipping'] = true; + $billing['address1'] = $address1; + $billing['address1'] = implode(PHP_EOL, $billing['address1']); + + // $billing['address1'] = implode(PHP_EOL, $billing['address1']); + + $addressData['billing'] = $billing; + } + + + Log::info("address" . json_encode($addressData)); + + if ( + Cart::hasError() + || ! Cart::saveCustomerAddress($addressData) + ) { + return new JsonResource([ + 'redirect' => false, + 'data' => Cart::getCart(), + ]); + } + + + + // + $shippingMethod = "free_free"; // free shipping + $shippingMethod = "flatrate_flatrate"; + + if ( + Cart::hasError() + || ! $shippingMethod + || ! Cart::saveShippingMethod($shippingMethod) + ) { + return response()->json([ + 'redirect_url' => route('shop.checkout.cart.index'), + ], Response::HTTP_FORBIDDEN); + } + + Cart::collectTotals(); + + + if($payment_method_input=="airwallex_klarna") $payment_method = "airwallex"; + if($payment_method_input=="airwallex_dropin") $payment_method = "airwallex"; + if($payment_method_input=="airwallex_google") $payment_method = "airwallex"; + if($payment_method_input=="airwallex_apple") $payment_method = "airwallex"; + if($payment_method_input=="airwallex") $payment_method = "airwallex"; + + $couponCode = $input['coupon_code']; + try { + if (strlen($couponCode)) { + $coupon = $this->cartRuleCouponRepository->findOneByField('code', $couponCode); + + if (! $coupon) { + return (new JsonResource([ + 'data' => new CartResource(Cart::getCart()), + 'message' => trans('Coupon not found.'), + ]))->response()->setStatusCode(Response::HTTP_UNPROCESSABLE_ENTITY); + } + + if ($coupon->cart_rule->status) { + if (Cart::getCart()->coupon_code == $couponCode) { + return (new JsonResource([ + 'data' => new CartResource(Cart::getCart()), + 'message' => trans('shop::app.checkout.cart.coupon-already-applied'), + ]))->response()->setStatusCode(Response::HTTP_UNPROCESSABLE_ENTITY); + } + + Cart::setCouponCode($couponCode); + //$this->validateOrder(); + Cart::collectTotals(); + } + } + } catch (\Exception $e) { + return (new JsonResource([ + 'data' => new CartResource(Cart::getCart()), + 'message' => trans('shop::app.checkout.cart.coupon.error'), + ]))->response()->setStatusCode(Response::HTTP_INTERNAL_SERVER_ERROR); + } + + // when enable the upselling and can config the upselling rule for carts + if($payment_method=='airwallex') { + // + $payment = []; + $payment['description'] = $payment_method."-".$refer; + $payment['method'] = $payment_method; + $payment['method_title'] = $payment_method."-".$refer; + $payment['sort'] = "2"; + // Cart::savePaymentMethod($payment); + + if ( + Cart::hasError() + || ! $payment + || ! Cart::savePaymentMethod($payment) + ) { + return response()->json([ + 'redirect_url' => route('shop.checkout.cart.index'), + ], Response::HTTP_FORBIDDEN); + } + + + Cart::collectTotals(); + $this->validateOrder(); + $cart = Cart::getCart(); + + // when enable the upselling and can config the upselling rule for carts + if(config("Upselling.enable")) { + + $upselling = app('NexaMerchant\Upselling\Upselling'); + $upselling->applyUpselling($cart); + } + + $order = $this->orderRepository->create(Cart::prepareDataForOrder()); + // Cart::deActivateCart(); + // Cart::activateCartIfSessionHasDeactivatedCartId(); + $data['result'] = 200; + $data['order'] = $order; + if ($order) { + $orderId = $order->id; + + //customer id + $cus_id = isset($input['cus_id']) ? trim($input['cus_id']) : null; + + $airwallex_customer = []; + if(is_null($cus_id)) { + //Step 1: Create a Customer + try { + $airwallex_customer = $this->airwallex->createCustomer($cart, $order->id); + $cus_id = $airwallex_customer->id; + } catch (\Exception $e) { + return response()->json(['error' => $e->getMessage(),'code'=>'203'], 400); + } + // $airwallex_customer = $this->airwallex->createCustomer($cart, $order->id); + // $cus_id = $airwallex_customer->id; + }else{ + $airwallex_customer['id'] = $cus_id; + } + + //create a airwallex payment order + $transactionManager = $this->airwallex->createPaymentOrder($cart, $order->id, $cus_id); + //Step 2: Generate a client secret for the Customer + $customerClientSecret = $this->airwallex->createCustomerClientSecret($cus_id); + if(!isset($transactionManager->client_secret)) { + response()->json(['error' => $transactionManager->body->message,'code'=>'203'], 400); + } + + //$transactionManager = $this->airwallex->createPaymentOrder($cart, $order->id); + Log::info("airwallex-".$order->id."--".json_encode($transactionManager)); + $data['client_secret'] = $transactionManager->client_secret; + $data['payment_intent_id'] = $transactionManager->id; + $data['currency'] = $transactionManager->currency; + $data['transaction'] = $transactionManager; + $data['customer'] = $airwallex_customer; + $data['customer_client_secret'] = $customerClientSecret; + $data['country'] = $input['country']; + $data['billing'] = $addressData['billing']; + + // redis save the customer id from airwallex + Redis::set("airwallex_customer_".$order->id, $cus_id); + } + + return response()->json($data); + } + + } + + public function OrderAddrAfter(Request $request) { + $input = $request->all(); + + //$last_order_id = $request->session()->get('last_order_id'); // check the laster order id + $last_order_id = ""; + //$last_order_id = "ddddd"; + $force = $request->input("force"); + + Log::info("last order id " . $last_order_id); + + if(!empty($last_order_id) && $force !="1") { + return response()->json(['error' => 'You Have already placed order, if you want to place another order please confirm your order','code'=>'202'], 400); + } + + + $refer = isset($input['refer']) ? trim($input['refer']) : ""; + + $products = $request->input("products"); + Log::info("products". json_encode($products)); + Cart::deActivateCart(); + foreach($products as $key=>$product) { + //var_dump($product); + if(!isset($product['amount'])) continue; // when the amount eq 0 + $product['quantity'] = $product['amount']; + $product['selected_configurable_option'] = $product['variant_id']; + if(!empty($product['attr_id'])) { + $attr_ids = explode(',', $product['attr_id']); + foreach($attr_ids as $key=>$attr_id) { + $attr = explode('_', $attr_id); + $super_attribute[$attr[0]] = $attr[1]; + } + + $product['super_attribute'] = $super_attribute; + } + + //Log::info("add product into cart ". json_encode($product)); + $cart = Cart::addProduct($product['product_id'], $product); + if ( + is_array($cart) + && isset($cart['warning']) + ) { + return new JsonResource([ + 'message' => $cart['warning'], + ]); + } + } + + $this->returnInsurance($input, $cart); + + + $addressData = []; + $addressData['billing'] = []; + $address1 = []; + array_push($address1, $input['address']); + $addressData['billing']['city'] = $input['city']; + $addressData['billing']['country'] = $input['country']; + $addressData['billing']['email'] = $input['email']; + $addressData['billing']['first_name'] = $input['first_name']; + $addressData['billing']['last_name'] = $input['second_name']; + $input['phone_full'] = str_replace('undefined+','', $input['phone_full']); + $addressData['billing']['phone'] = $input['phone_full']; + $addressData['billing']['postcode'] = $input['code']; + $addressData['billing']['state'] = $input['province']; + $addressData['billing']['use_for_shipping'] = true; + $addressData['billing']['address1'] = $address1; + $addressData['shipping'] = []; + $addressData['shipping']['isSaved'] = false; + $address1 = []; + array_push($address1, ""); + $addressData['shipping']['address1'] = $address1; + + $addressData['billing']['address1'] = implode(PHP_EOL, $addressData['billing']['address1']); + + $addressData['shipping']['address1'] = implode(PHP_EOL, $addressData['shipping']['address1']); + + Log::info("paypal pay ".$refer.'--'.json_encode($addressData)); + + + if ( + Cart::hasError() + || ! Cart::saveCustomerAddress($addressData) + ) { + return new JsonResource([ + 'redirect' => true, + 'data' => route('shop.checkout.cart.index'), + ]); + } + + $shippingMethod = "free_free"; // 包邮 + $shippingMethod = "flatrate_flatrate"; + // Cart::saveShippingMethod($shippingMethod); + + if ( + Cart::hasError() + || ! $shippingMethod + || ! Cart::saveShippingMethod($shippingMethod) + ) { + return response()->json([ + 'redirect_url' => route('shop.checkout.cart.index'), + ], Response::HTTP_FORBIDDEN); + } + + Cart::collectTotals(); + + $payment = []; + $payment['description'] = "PayPal-".$refer; + $payment['method'] = "paypal_smart_button"; + $payment['method_title'] = "PayPal Smart Button-".$refer; + $payment['sort'] = "1"; + // Cart::savePaymentMethod($payment); + + if ( + Cart::hasError() + || ! $payment + || ! Cart::savePaymentMethod($payment) + ) { + return response()->json([ + 'redirect_url' => route('shop.checkout.cart.index'), + ], Response::HTTP_FORBIDDEN); + } + + + $couponCode = $input['coupon_code']; + try { + if (strlen($couponCode)) { + $coupon = $this->cartRuleCouponRepository->findOneByField('code', $couponCode); + + if (! $coupon) { + return (new JsonResource([ + 'data' => new CartResource(Cart::getCart()), + 'message' => trans('Coupon not found.'), + ]))->response()->setStatusCode(Response::HTTP_UNPROCESSABLE_ENTITY); + } + + if ($coupon->cart_rule->status) { + if (Cart::getCart()->coupon_code == $couponCode) { + return (new JsonResource([ + 'data' => new CartResource(Cart::getCart()), + 'message' => trans('shop::app.checkout.cart.coupon-already-applied'), + ]))->response()->setStatusCode(Response::HTTP_UNPROCESSABLE_ENTITY); + } + + Cart::setCouponCode($couponCode); + Cart::collectTotals(); + + } + } + } catch (\Exception $e) { + return (new JsonResource([ + 'data' => new CartResource(Cart::getCart()), + 'message' => trans('shop::app.checkout.cart.coupon.error'), + ]))->response()->setStatusCode(Response::HTTP_INTERNAL_SERVER_ERROR); + } + + // when enable the upselling and can config the upselling rule for carts + if(config("Upselling.enable")) { + + $upselling = app('NexaMerchant\Upselling\Upselling'); + $upselling->applyUpselling($cart); + } + + + try { + $order = $this->smartButton->createOrder($this->buildRequestBody($input)); + $data = []; + $data['order'] = $order; + $data['code'] = 200; + $data['result'] = 200; + $data['cart'] = Cart::getCart(); + return response()->json($data); + //return response()->json($order); + } catch (\Exception $e) { + return response()->json(json_decode($e->getMessage()), 400); + } + } + + /** + * Order confirm + * + * @return \Illuminate\Http\Response + */ + public function OrderConfirm(Request $request) { + $payment_intent_id = $request->input("payment_intent_id"); + $order_id = $request->input("order_id"); + + $order = $this->orderRepository->find($order_id); + + $transactionManager = $this->airwallex->confirmPayment($payment_intent_id, $order); + + $data = []; + $data['payment'] = $transactionManager; + $data['code'] = 200; + $data['result'] = 200; + $data['order_id'] = $order_id; + $data['order_id'] = $order_id; + return response()->json($data); + } + + public function OrderLog(Request $request) { + Log::info("request ". json_encode($request->all())); + $session_data = $request->session()->all(); + Log::info("session ". json_encode($session_data)); + $refer = $request->cookie('refer'); + Log::info("cookie refer ". $refer); + } + + public function OrderQuery(Request $request) { + $order_id = $request->input("id"); + + $order = $this->orderRepository->find($order_id); + if(is_null($order)) { + return new JsonResource([ + 'order_id' => 0, + 'info' => [], + ]); + } + + return new JsonResource([ + 'order_id' => $order_id, + 'info' => $order, + ]); + } + + public function OrderStatus(Request $request) { + try { + $order = $this->smartButton->getOrder(request()->input('orderData.orderID')); + + $cartId = $request->input('orderData.cartId'); + if(empty($cartId)) { + $cartId = $request->input('data.cart.id'); + } + + if(!empty($cartId)) { + + $cart = $this->cartRepository->find($cartId); + Cart::setCart($cart); + + } + + $refer = $request->input("refer"); + + + $params = request()->input("params"); + if(!empty($params)) { + + $addressData = []; + $addressData['billing'] = []; + $address1 = []; + array_push($address1, $params['address']); + + $addressData['billing']['city'] = $params['city']; + $addressData['billing']['email'] = $params['email']; + $addressData['billing']['country'] = $params['country']; + $addressData['billing']['first_name'] = $params['first_name']; + $addressData['billing']['last_name'] = $params['second_name']; + $addressData['billing']['phone'] = $params['phone_full']; + $addressData['billing']['phone'] = $params['phone_full']; + $addressData['billing']['address1'] = $address1; + + $addressData['billing']['state'] = $params['province']; + $addressData['billing']['postcode'] = $params['code']; + + //$addressData['shipping'] = []; + $addressData['shipping']['isSaved'] = false; + //$address1 = []; + //array_push($address1, ""); + $addressData['shipping']['address1'] = $address1; + + $addressData['billing']['address1'] = implode(PHP_EOL, $addressData['billing']['address1']); + + $addressData['shipping']['address1'] = implode(PHP_EOL, $addressData['shipping']['address1']); + if(!isset($addressData['shipping']['email'])) { + $addressData['shipping'] = $addressData['billing']; + } + + + Log::error("paypal pay address ".$refer.'--'.json_encode($addressData)); + + if ( + Cart::hasError() + || ! Cart::saveCustomerAddress($addressData) + ) { + return new JsonResource([ + 'redirect' => true, + 'data' => route('shop.checkout.cart.index'), + ]); + } + + // if the status not eq completed, then capture the order + + if($order->result->status != "COMPLETED") { + $this->smartButton->captureOrder(request()->input('orderData.orderID')); + } + //$this->smartButton->captureOrder(request()->input('orderData.orderID')); + + //$this->smartButton->AuthorizeOrder(request()->input('orderData.orderID')); + + //$request->session()->put('last_order_id', request()->input('orderData.orderID')); + + }else{ + + $order = (array)$order; + + //var_dump($order); + + $purchase_units = (array)$order['result']->purchase_units; + $input = (array)$purchase_units[0]->shipping; + $payer = (array)$order['result']->payer; + $payment_source = (array)$order['result']->payment_source; + $payment_source_paypal = (array)$payment_source['paypal']; + + //Log::info("paypal source".json_encode($payment_source)); + //Log::info("paypal source paypal".json_encode($payment_source_paypal)); + + // 添加地址内容 + $addressData = []; + $addressData['billing'] = []; + $address1 = []; + $address_line_2 = isset($input['address']->address_line_2) ? $input['address']->address_line_2 : ""; + array_push($address1, $input['address']->address_line_1. $address_line_2); + $addressData['billing']['city'] = isset($input['address']->admin_area_2) ? $input['address']->admin_area_2 : ""; + $addressData['billing']['country'] = $input['address']->country_code; + $addressData['billing']['email'] = $payer['email_address']; + $addressData['billing']['first_name'] = $payer['name']->given_name; + $addressData['billing']['last_name'] = $payer['name']->surname; + $national_number = isset($payment_source_paypal['phone_number']) ? $payment_source_paypal['phone_number']->national_number : ""; + $addressData['billing']['phone'] = $national_number; + $addressData['billing']['postcode'] = isset($input['address']->postal_code) ? $input['address']->postal_code : ""; + $addressData['billing']['state'] = isset($input['address']->admin_area_1) ? $input['address']->admin_area_1 : ""; + $addressData['billing']['use_for_shipping'] = true; + $addressData['billing']['address1'] = $address1; + $addressData['shipping'] = []; + $addressData['shipping']['isSaved'] = false; + $address1 = []; + array_push($address1, ""); + $addressData['shipping']['address1'] = $address1; + + $addressData['billing']['address1'] = implode(PHP_EOL, $addressData['billing']['address1']); + + $addressData['shipping']['address1'] = implode(PHP_EOL, $addressData['shipping']['address1']); + + if ( + Cart::hasError() + || ! Cart::saveCustomerAddress($addressData) + ) { + return new JsonResource([ + 'redirect' => true, + 'data' => route('shop.checkout.cart.index'), + ]); + } + + if($order['result']->status != "COMPLETED") { + $this->smartButton->captureOrder(request()->input('orderData.orderID')); + } + + + } + + $orderRes = $this->saveOrder(); + + // get order transaction info + $order = $this->orderRepository->find($orderRes->id); + + $data = []; + $data['order'] = $order; + $data['transaction'] = $order->transactions; + $data['code'] = 200; + $data['result'] = 200; + $data['order_id'] = $orderRes->id; + + return response()->json($data); + + } catch (\Exception $e) { + Log::info("paypal pay exception". json_encode($e->getMessage())); + return response()->json($e->getMessage()); + return response()->json(json_decode($e->getMessage()), 400); + } + } + + + private function returnInsurance($input, $cart) { + // when return insurance eq 1 and auto add the insurance product into cart + $input['return_insurance'] = isset($input['return_insurance']) ? $input['return_insurance'] : 0; + if($input['return_insurance']==1) { + + Cart::addProduct(config('onebuy.return_shipping_insurance.product_id'), [ + 'quantity' =>1 , + 'product_sku' => config('onebuy.return_shipping_insurance.product_sku'), + 'selected_configurable_option' => '', + 'product_id' => config('onebuy.return_shipping_insurance.product_id'), + 'variant_id' => '' + ]); + + + } + + } + + /** + * + * check the coupon info + * @param string code + * + */ + public function CheckCoupon(Request $request) { + $couponCode = $request->input("code"); + + try { + if (strlen($couponCode)) { + $coupon = $this->cartRuleCouponRepository->findOneByField('code', $couponCode); + + if (! $coupon) { + return (new JsonResource([ + 'data' => new CartResource(Cart::getCart()), + 'message' => trans('Coupon not found.'), + ]))->response()->setStatusCode(Response::HTTP_UNPROCESSABLE_ENTITY); + } + + $couponConfig = []; + + $couponConfig = $this->cartRuleRepository->findOneByField('id',$coupon->cart_rule_id); + + return (new JsonResource([ + 'data' => [ + 'coupon' => $coupon, + 'couponConfig' => $couponConfig, + ], + 'message' => trans('Coupon applied successfully.'), + ]))->response()->setStatusCode(Response::HTTP_OK); + } + } catch (\Exception $e) { + return (new JsonResource([ + 'data' => new CartResource(Cart::getCart()), + 'message' => trans('shop::app.checkout.cart.coupon.error'), + ]))->response()->setStatusCode(Response::HTTP_INTERNAL_SERVER_ERROR); + } + + } + + /** + * Saving order once captured and all formalities done. + * + * @return \Illuminate\Http\Response + */ + protected function saveOrder() + { + if (Cart::hasError()) { + return response()->json(['redirect_url' => route('shop.checkout.cart.index')], 403); + } + + try { + Cart::collectTotals(); + + $this->validateOrder(); + + $order = $this->orderRepository->create(Cart::prepareDataForOrder()); + + $this->orderRepository->update(['status' => 'processing'], $order->id); + + if ($order->canInvoice()) { + $this->invoiceRepository->create($this->prepareInvoiceData($order)); + } + + Cart::deActivateCart(); + + return $order; + + //session()->flash('order', $order); + + return response()->json([ + 'success' => true, + ]); + } catch (\Exception $e) { + + return response()->json(['error' => $e->getMessage()], 400); + } + } + + /** + * Prepares order's invoice data for creation. + * + * @param \Webkul\Sales\Models\Order $order + * @return array + */ + protected function prepareInvoiceData($order) + { + $invoiceData = ['order_id' => $order->id]; + + foreach ($order->items as $item) { + $invoiceData['invoice']['items'][$item->id] = $item->qty_to_invoice; + } + + return $invoiceData; + } + + /** + * + * @link https://developer.paypal.com/docs/multiparty/checkout/save-payment-methods/during-purchase/js-sdk/paypal/ + * + */ + protected function buildRequestBody($input) + { + $cart = Cart::getCart(); + + + $billingAddressLines = $this->getAddressLines($cart->billing_address->address1); + + $data = [ + 'intent' => 'CAPTURE', + 'application_context' => [ + //'shipping_preference' => 'NO_SHIPPING', + 'shipping_preference' => 'GET_FROM_FILE', // 用户选择自己的地址内容 + ], + + + 'purchase_units' => [ + [ + 'amount' => [ + 'value' => $this->smartButton->formatCurrencyValue((float) $cart->sub_total + $cart->tax_total + ($cart->selected_shipping_rate ? $cart->selected_shipping_rate->price : 0) - $cart->discount_amount), + 'currency_code' => $cart->cart_currency_code, + + 'breakdown' => [ + 'item_total' => [ + 'currency_code' => $cart->cart_currency_code, + 'value' => $this->smartButton->formatCurrencyValue((float) $cart->sub_total), + ], + + 'shipping' => [ + 'currency_code' => $cart->cart_currency_code, + 'value' => $this->smartButton->formatCurrencyValue((float) ($cart->selected_shipping_rate ? $cart->selected_shipping_rate->price : 0)), + ], + + 'tax_total' => [ + 'currency_code' => $cart->cart_currency_code, + 'value' => $this->smartButton->formatCurrencyValue((float) $cart->tax_total), + ], + + 'discount' => [ + 'currency_code' => $cart->cart_currency_code, + 'value' => $this->smartButton->formatCurrencyValue((float) $cart->discount_amount), + ], + ], + ], + + 'items' => $this->getLineItems($cart), + ], + ], + ]; + + if (! empty($cart->billing_address->phone)) { + $data['payer']['phone'] = [ + 'phone_type' => 'MOBILE', + + 'phone_number' => [ + 'national_number' => $this->smartButton->formatPhone($cart->billing_address->phone), + ], + ]; + } + + if ( + $cart->haveStockableItems() + && $cart->shipping_address + ) { + //$data['application_context']['shipping_preference'] = 'SET_PROVIDED_ADDRESS'; + + /* + $data['purchase_units'][0] = array_merge($data['purchase_units'][0], [ + 'shipping' => [ + 'address' => [ + 'address_line_1' => current($billingAddressLines), + 'address_line_2' => last($billingAddressLines), + 'admin_area_2' => $cart->shipping_address->city, + 'admin_area_1' => $cart->shipping_address->state, + 'postal_code' => $cart->shipping_address->postcode, + 'country_code' => $cart->shipping_address->country, + ], + ], + ]); + */ + } + + $input['payment_vault'] = isset($input['payment_vault']) ? $input['payment_vault'] : "0"; + $paypal_vault_id = isset($input['paypal_vault_id']) ? $input['paypal_vault_id'] : ""; + if($input['payment_vault']=='1') { + // for vault + if(empty($paypal_vault_id)) { + $data["payment_source"] = [ + "paypal" => [ + "attributes" => [ + "vault" => [ + "store_in_vault" => "ON_SUCCESS", + "usage_type" => "MERCHANT", + "customer_type" => "CONSUMER" + ] + ], + "experience_context" => [ + "return_url" => $input['payment_return_url'], + 'cancel_url' => $input['payment_cancel_url'], + ] + ] + ]; + }else{ + $data["payment_source"] = [ + "paypal" => [ + "vault_id" => $paypal_vault_id, + "experience_context" => [ + "return_url" => $input['payment_return_url'], + 'cancel_url' => $input['payment_cancel_url'], + ] + ] + ]; + } + } + + //var_dump($data);exit; + + return $data; + } + + /** + * Return cart items. + * + * @param string $cart + * @return array + */ + protected function getLineItems($cart) + { + $lineItems = []; + + foreach ($cart->items as $item) { + if(empty($item->name)) $item->name = "Product"; + $lineItems[] = [ + 'unit_amount' => [ + 'currency_code' => $cart->cart_currency_code, + 'value' => $this->smartButton->formatCurrencyValue((float) $item->price), + ], + 'quantity' => $item->quantity, + 'name' => $item->name, + 'sku' => $item->sku, + 'category' => $item->getTypeInstance()->isStockable() ? 'PHYSICAL_GOODS' : 'DIGITAL_GOODS', + ]; + } + + return $lineItems; + } + + /** + * Return convert multiple address lines into 2 address lines. + * + * @param string $address + * @return array + */ + protected function getAddressLines($address) + { + $address = explode(PHP_EOL, $address, 2); + + $addressLines = [current($address)]; + + if (isset($address[1])) { + $addressLines[] = str_replace(["\r\n", "\r", "\n"], ' ', last($address)); + } else { + $addressLines[] = ''; + } + + return $addressLines; + } + + /** + * Validate order before creation. + * + * @return void|\Exception + */ + public function validateOrder() + { + $cart = Cart::getCart(); + + $minimumOrderAmount = core()->getConfigData('sales.order_settings.minimum_order.minimum_order_amount') ?: 0; + + if ( + auth()->guard('customer')->check() + && auth()->guard('customer')->user()->is_suspended + ) { + throw new \Exception(trans('shop::app.checkout.cart.suspended-account-message')); + } + + if ( + auth()->guard('customer')->user() + && ! auth()->guard('customer')->user()->status + ) { + throw new \Exception(trans('shop::app.checkout.cart.inactive-account-message')); + } + + if (! $cart->checkMinimumOrder()) { + throw new \Exception(trans('shop::app.checkout.cart.minimum-order-message', ['amount' => core()->currency($minimumOrderAmount)])); + } + + if ($cart->haveStockableItems() && ! $cart->shipping_address) { + throw new \Exception(trans('shop::app.checkout.cart.check-shipping-address')); + } + + if (! $cart->billing_address) { + throw new \Exception(trans('shop::app.checkout.cart.check-billing-address')); + } + + if ( + $cart->haveStockableItems() + && ! $cart->selected_shipping_rate + ) { + throw new \Exception(trans('shop::app.checkout.cart.specify-shipping-method')); + } + + if (! $cart->payment) { + throw new \Exception(trans('shop::app.checkout.cart.specify-payment-method')); + } + } + + + /** + * + * + * faq interface + * + * + */ + + public function faq() { + + $redis = Redis::connection('default'); + $faqItems = $redis->hgetall($this->faq_cache_key); + ksort($faqItems); + + return response()->json([ + 'data' => (array)$faqItems, + 'code' => 200, + 'message' => 'success' + ], 200); + + } + + /** + * + * + */ + public function cms($slug, Request $request) { + $page = $this->cmsRepository->findByUrlKeyOrFail($slug); + + return response()->json([ + 'data' => $page, + 'code' => 200, + 'message' => 'success' + ], 200); + } + +} \ No newline at end of file diff --git a/src/Http/Controllers/Web/ProductController.php b/src/Http/Controllers/Web/ProductController.php new file mode 100644 index 0000000..cd2af0a --- /dev/null +++ b/src/Http/Controllers/Web/ProductController.php @@ -0,0 +1,1614 @@ +productRepository->findBySlug($slugOrPath); + Cache::put($cache_key, $product); + } + + if ( + ! $product + || ! $product->visible_individually + || ! $product->url_key + || ! $product->status + ) { + abort(404); + } + + //visitor()->visit($product); + + $refer = $request->input("refer"); + + if(!empty($refer)) { + $request->session()->put('refer', $refer); + $request->session()->put('refer_'.$slug, $refer); + }else{ + $refer = $request->session()->get('refer'); + } + + // 四个商品的价格情况 + $package_products = []; + $productBaseImage = product_image()->getProductBaseImage($product); + $package_products = \Nicelizhi\OneBuy\Helpers\Utils::makeProducts($product, [2,1,3,4]); + + // 获取 faq 数据 + $redis = Redis::connection('default'); + + // skus 数据 + $skus = []; + + $cache_key = "product_sku_".$product->id; + $size_cache_key = "product_sku_size_".$product->id; + $color_cache_key = "product_color_size_".$product->id; + $skus = Cache::get($cache_key); + $qty_items_size = Cache::get($size_cache_key); + $qty_items_color = Cache::get($color_cache_key); + if(empty($skus)) { + $sku_products = $this->productRepository->where("parent_id", $product->id)->get(); + + $attributeOptionRepository = app(AttributeOptionRepository::class); + + foreach($sku_products as $key=>$sku) { + $sku_id = $sku->id; + $sku_code = $sku->sku; + unset($sku); + /** + * + * + * {"name":"Women's thin no wire lace bra - Black \/ S","sku_code":"CJ02168-C#black-S#m","sku_id":44113194877163,"attribute_name":"S,Black","key":"S_Black"} + * + * + */ + $productAttribute = $this->productAttributeValueRepository->findOneWhere([ + 'product_id' => $sku_id, + 'attribute_id' => 2, + ]); + + //var_dump($productAttribute); + + $sku['name'] = $productAttribute['text_value']; + + + + $sku['sku_code'] = $sku_code; + $sku['sku_id'] = $sku_id; + + // banner pic big url (pc) + $colorAttribute = $this->productAttributeValueRepository->findOneWhere([ + 'product_id' => $sku_id, + 'attribute_id' => 23, + ]); + + // banner pic mobile url + $sizeAttribute = $this->productAttributeValueRepository->findOneWhere([ + 'product_id' => $sku_id, + 'attribute_id' => 24, + ]); + + $SizeattributeOptions = $attributeOptionRepository->findOneWhere(['id'=>$sizeAttribute['integer_value']]); + $ColorattributeOptions = $attributeOptionRepository->findOneWhere(['id'=>$colorAttribute['integer_value']]); + + $attribute_name = $SizeattributeOptions->admin_name.",".$ColorattributeOptions->admin_name; + + $sku['attribute_name'] = $attribute_name; + $sku['attr_id'] = "24_".$colorAttribute['integer_value'].",23_".$sizeAttribute['integer_value']; + + //$sku['key'] = $ColorattributeOptions->admin_name."_".$SizeattributeOptions->admin_name; // 这个数据需要留意他的位置,JS判断会需要使用 + $sku['key'] = $SizeattributeOptions->admin_name."_".$ColorattributeOptions->admin_name; // 这个数据需要留意他的位置,JS判断会需要使用 + + $qty_items_color[$ColorattributeOptions->admin_name][] = $SizeattributeOptions->admin_name; + $qty_items_size[$SizeattributeOptions->admin_name][] = $ColorattributeOptions->admin_name; + + $skus[] = $sku; + } + Cache::put($cache_key, json_encode($skus)); + Cache::put($size_cache_key, json_encode($qty_items_size)); + Cache::put($color_cache_key, json_encode($qty_items_color)); + }else { + $skus = json_decode($skus, JSON_OBJECT_AS_ARRAY); + } + + $product_attributes = []; + + $cache_key = "product_attributes_".$product->id; + $product_attributes = Cache::get($cache_key); + + + //$product_attributes = []; + if(empty($product_attributes)) { + //if(true) { + + $productViewHelper = new \Webkul\Product\Helpers\ConfigurableOption(); + $attributes = $productViewHelper->getConfigurationConfig($product); + + + $productSizeImage = $this->productAttributeValueRepository->findOneWhere([ + 'product_id' => $product->id, + 'attribute_id' => 32, + ]); + + //获取到他底部的商品内容 + // $attributes = $this->productRepository->getSuperAttributes($product); + + + foreach($attributes['attributes'] as $key=>$attribute) { + + $product_attr_sort_cache_key = "product_attr_sort_".$attribute['id']."_".$product->id; + $product_attr_sort = $redis->hgetall($product_attr_sort_cache_key); // get sku sort + + //var_dump($attribute); + $attribute['name'] = $attribute['code']; + $options = []; + foreach($attribute['options'] as $kk=>$option) { + // + $is_sold_out = false; + if($attribute['id']==23) { + $new_id = $option['products'][0]; + $new_product = $this->productRepository->find($new_id); + $NewproductBaseImage = product_image()->getProductBaseImage($new_product); + $option['image'] = @$NewproductBaseImage['large_image_url']; + $option['big_image'] = @$NewproductBaseImage['large_image_url']; + + }else{ + $option['image'] = $productBaseImage['large_image_url']; + $option['large_image'] = @$productBaseImage['large_image_url']; + } + + // 判断是否有对应的尺码内容 + + $option['is_sold_out'] = $is_sold_out; + $option['name'] = $option['label']; + unset($option['admin_name']); + + if(!empty($product_attr_sort)) { + $sort = isset($product_attr_sort[$option['id']]) ? intval($product_attr_sort[$option['id']]) : 4 ; + $option['sort'] = $sort; + $options[$sort] = $option; + }else{ + $options[] = $option; + } + //var_dump($options); + } + + //var_dump($options); + //array_multisort($options,) + //var_dump($options); + ksort($options); + //var_dump($options);exit; + + $tip = ""; + $tip_img = ""; + if($attribute['id']==24) { + $tip = trans('onebuy::app.product.Size Chart'); + if(isset($productSizeImage->text_value)) $tip_img = $productSizeImage->text_value; + if(empty($tip_img)) $tip = ""; + } + + $attribute['tip'] = $tip; + $attribute['tip_img'] = $tip_img; + + unset($attribute['translations']); //去掉多余的数据内容 + //var_dump($options); + $attribute['options'] = $options; + $attribute['image'] = $productBaseImage['large_image_url']; + $attribute['large_image'] = $productBaseImage['large_image_url']; + + $product_attributes[] = $attribute; + } + + Cache::put($cache_key, json_encode($product_attributes)); + + }else{ + $product_attributes = json_decode($product_attributes, JSON_OBJECT_AS_ARRAY); + } + + rsort($product_attributes); + //商品的背景图片获取 + + $productBgAttribute = $this->productAttributeValueRepository->findOneWhere([ + 'product_id' => $product->id, + 'attribute_id' => 29, + ]); + + + $productBgAttribute_mobile = $this->productAttributeValueRepository->findOneWhere([ + 'product_id' => $product->id, + 'attribute_id' => 30, + ]); + + + $app_env = config("app.env"); + + //var_dump($productBgAttribute); + + + $faqItems = $redis->hgetall($this->faq_cache_key); + ksort($faqItems); + // $comments = $redis->hgetall($this->cache_prefix_key."product_comments_".$product['id']); + + $comments = $product->reviews->where('status', 'approved')->take(10); + + $comments = $comments->map(function($comments) { + $comments->customer = $comments->customer; + $comments->images; + return $comments; + }); + //Log::info($product['id'].'--'.json_encode($comments)); + //Log::info($product['id'].'--'.count($comments)); + if(count($comments)==0) { + //Log::info($product['id'].'--'.$this->cache_prefix_key."product_comments_".$product['id']); + $comments = $redis->hgetall($this->cache_prefix_key."product_comments_".$product['id']); + foreach($comments as $key=>$comment) { + $comment = json_decode($comment); + $comment->comment = $comment->content; + $comments[$key] = $comment; + } + //var_dump($comments); + } + + $paypal_client_id = core()->getConfigData('sales.payment_methods.paypal_smart_button.client_id'); + + + //支持的区域 + $countries = config("countries"); + + $default_country = config('onebuy.default_country'); + + $airwallex_method = config('onebuy.airwallex.method'); + + $payments = config('onebuy.payments'); // config the payments status + + $payments_default = config('onebuy.payments_default'); + $brand = config('onebuy.brand'); + + $gtag = config('onebuy.gtag'); + + $fb_ids = config('onebuy.fb_ids'); + $ob_adv_id = config('onebuy.ob_adv_id'); + + $crm_channel = config('onebuy.crm_channel'); + + $quora_adv_id = config('onebuy.quora_adv_id'); + + return view('onebuy::product-detail', compact('gtag','app_env','product','package_products', 'product_attributes', 'skus','productBgAttribute','productBgAttribute_mobile','faqItems','comments','paypal_client_id','default_country','airwallex_method','payments','payments_default','brand','fb_ids','ob_adv_id','crm_channel','refer','quora_adv_id')); + } + + public function cms($slug, Request $request) { + // \Debugbar::disable(); /* 开启后容易出现前端JS报错的情况 */ + + $page = $this->cmsRepository->findByUrlKeyOrFail($slug); + + return view('onebuy::cms.page')->with('page', $page); + + } + + // airwallex + public function order_add_sync(Request $request) { + //var_dump($request->all()); + + $payment_method_input = $request->input('payment_method'); + + $input = $request->all(); + + $refer = $request->session()->get('refer'); + Log::info("refer checkout v1 ".$refer); + + $last_order_id = $request->session()->get('last_order_id'); // check the laster order id + //$last_order_id = "ddddd"; + $force = $request->input("force"); + + Log::info("last order id " . $last_order_id); + + if(!empty($last_order_id) && $force !="1") { + return response()->json(['error' => 'You Have already placed order, if you want to place another order please confirm your order','code'=>'202'], 400); + } + + + $payment_airwallex_vault = isset($input['payment_airwallex_vault']) ? $input['payment_airwallex_vault'] : 0; + $request->session()->put('payment_airwallex_vault', $payment_airwallex_vault); + + $products = $request->input("products"); + if(empty($products)) { + return response()->json(['error' => 'No product found in cart','code'=>'202'], 400); + } + // 添加到购物车 + Cart::deActivateCart(); + foreach($products as $key=>$product) { + //var_dump($product); + $product['quantity'] = $product['amount']; + if(!isset($product['variant_id'])) { + return response()->json(['error' => 'No product found in cart','code'=>'202'], 400); + } + $product['selected_configurable_option'] = $product['variant_id']; + + if(!empty($product['attr_id'])) { + $attr_ids = explode(',', $product['attr_id']); + foreach($attr_ids as $key=>$attr_id) { + $attr = explode('_', $attr_id); + $super_attribute[$attr[0]] = $attr[1]; + } + + $product['super_attribute'] = $super_attribute; + } + //Log::info("add product into cart ". json_encode($product)); + $cart = Cart::addProduct($product['product_id'], $product); + + if ( + is_array($cart) + && isset($cart['warning']) + ) { + return new JsonResource([ + 'message' => $cart['warning'], + ]); + } + + } + // 添加地址内容 + $addressData = []; + + + $addressData['billing'] = []; + $address1 = []; + array_push($address1, $input['address']); + $addressData['billing']['city'] = $input['city']; + $addressData['billing']['country'] = $input['country']; + $addressData['billing']['email'] = $input['email']; + $addressData['billing']['first_name'] = $input['first_name']; + $addressData['billing']['last_name'] = $input['second_name']; + $input['phone_full'] = str_replace('undefined+','', $input['phone_full']); + $addressData['billing']['phone'] = $input['phone_full']; + $addressData['billing']['postcode'] = $input['code']; + $addressData['billing']['state'] = $input['province']; + $addressData['billing']['use_for_shipping'] = true; + $addressData['billing']['address1'] = $address1; + + $addressData['billing']['address1'] = implode(PHP_EOL, $addressData['billing']['address1']); + + $shipping = []; + $address1 = []; + array_push($address1, $input['address']); + $shipping['city'] = $input['city']; + $shipping['country'] = $input['country']; + $shipping['email'] = $input['email']; + $shipping['first_name'] = $input['first_name']; + $shipping['last_name'] = $input['second_name']; + //undefined+ + $input['phone_full'] = str_replace('undefined+','', $input['phone_full']); + $shipping['phone'] = $input['phone_full']; + $shipping['postcode'] = $input['code']; + $shipping['state'] = $input['province']; + $shipping['use_for_shipping'] = true; + $shipping['address1'] = $address1; + $shipping['address1'] = implode(PHP_EOL, $shipping['address1']); + + + $addressData['shipping'] = $shipping; + $addressData['shipping']['isSaved'] = false; + $address1 = []; + array_push($address1, $input['address']); + $addressData['shipping']['address1'] = $address1; + $addressData['shipping']['address1'] = implode(PHP_EOL, $addressData['shipping']['address1']); + + // customer bill address info + if(@$input['shipping_address']=="other") { + $address1 = []; + array_push($address1, $input['bill_address']); + $billing = []; + $billing['city'] = $input['bill_city']; + $billing['country'] = $input['bill_country']; + $billing['email'] = $input['email']; + $billing['first_name'] = $input['bill_first_name']; + $billing['last_name'] = $input['bill_second_name']; + //undefined+ + $input['phone_full'] = str_replace('undefined+','', $input['phone_full']); + $billing['phone'] = $input['phone_full']; + $billing['postcode'] = $input['bill_code']; + $billing['state'] = $input['bill_province']; + //$billing['use_for_shipping'] = true; + $billing['address1'] = $address1; + $billing['address1'] = implode(PHP_EOL, $billing['address1']); + + // $billing['address1'] = implode(PHP_EOL, $billing['address1']); + + $addressData['billing'] = $billing; + } + + + Log::info("address" . json_encode($addressData)); + + + if ( + Cart::hasError() + || ! Cart::saveCustomerAddress($addressData) + ) { + return new JsonResource([ + 'redirect' => true, + 'data' => route('shop.checkout.cart.index'), + ]); + } + + + //处理配送方式 + $shippingMethod = "free_free"; // 包邮 + $shippingMethod = "flatrate_flatrate"; + // Cart::saveShippingMethod($shippingMethod); + + if ( + Cart::hasError() + || ! $shippingMethod + || ! Cart::saveShippingMethod($shippingMethod) + ) { + return response()->json([ + 'redirect_url' => route('shop.checkout.cart.index'), + ], Response::HTTP_FORBIDDEN); + } + + Cart::collectTotals(); + + $payment_method = "airwallex"; + + if($payment_method_input=="airwallex_klarna") $payment_method = "airwallex"; + if($payment_method_input=="airwallex_dropin") $payment_method = "airwallex"; + if($payment_method_input=="airwallex_google") $payment_method = "airwallex"; + if($payment_method_input=="airwallex_apple") $payment_method = "airwallex"; + if($payment_method_input=="airwallex") $payment_method = "airwallex"; + + if($payment_method=="airwallex_google") { + + } + + if($payment_method=="airwallex_apple") { + + } + + // + + if($payment_method=='airwallex') { + // + $payment = []; + $payment['description'] = $payment_method."-".$refer; + $payment['method'] = $payment_method; + $payment['method_title'] = $payment_method."-".$refer; + $payment['sort'] = "2"; + // Cart::savePaymentMethod($payment); + + if ( + Cart::hasError() + || ! $payment + || ! Cart::savePaymentMethod($payment) + ) { + return response()->json([ + 'redirect_url' => route('shop.checkout.cart.index'), + ], Response::HTTP_FORBIDDEN); + } + + // + Cart::collectTotals(); + $this->validateOrder(); + $cart = Cart::getCart(); + + // when enable the upselling and can config the upselling rule for carts + $upselling_enable = $request->session()->get('upselling_enable'); + if(config("Upselling.enable") && $upselling_enable) { + Log::info("upselling_enable". $upselling_enable); + $upselling = app('NexaMerchant\Upselling\Upselling'); + $upselling->applyUpselling($cart); + } + + $order = $this->orderRepository->create(Cart::prepareDataForOrder()); + // Cart::deActivateCart(); + // Cart::activateCartIfSessionHasDeactivatedCartId(); + // + $data['result'] = 200; + $data['order'] = $order; + if ($order) { + $orderId = $order->id; + + //customer id + $cus_id = isset($input['cus_id']) ? trim($input['cus_id']) : null; + + $airwallex_customer = []; + if(is_null($cus_id)) { + //Step 1: Create a Customer + $airwallex_customer = $this->airwallex->createCustomer($cart, $order->id); + if(!isset($airwallex_customer->id)) { + Log::info("airwallex-".$order->id."--".json_encode($airwallex_customer)); + return response()->json(['error' => 'create customer error','code'=>'203'], 400); + } + $cus_id = $airwallex_customer->id; + }else{ + $airwallex_customer['id'] = $cus_id; + } + + //create a airwallex payment order + $transactionManager = $this->airwallex->createPaymentOrder($cart, $order->id, $cus_id); + + if(!isset($transactionManager->client_secret)) { + response()->json(['error' => $transactionManager->body->message,'code'=>'203'], 400); + } + //var_dump($transactionManager); + + + + //Step 2: Generate a client secret for the Customer + $customerClientSecret = $this->airwallex->createCustomerClientSecret($cus_id); + + + if(!isset($transactionManager->client_secret)) { + response()->json(['error' => $transactionManager->body->message,'code'=>'203'], 400); + } + + Log::info("airwallex-".$order->id."--".json_encode($transactionManager)); + $data['client_secret'] = $transactionManager->client_secret; + $data['payment_intent_id'] = $transactionManager->id; + $data['currency'] = $transactionManager->currency; + $data['transaction'] = $transactionManager; + $data['customer'] = $airwallex_customer; + $data['customer_client_secret'] = $customerClientSecret; + $data['country'] = $input['country']; + $data['billing'] = $addressData['billing']; + } + + return response()->json($data); + } + + if($payment_method=='paypal_standard') { + //处理支付方式 + $payment = []; + $payment['description'] = "PayPal-".$refer; + $payment['method'] = "paypal_standard"; + $payment['method_title'] = "PayPal standard Button-".$refer; + $payment['sort'] = "1"; + // Cart::savePaymentMethod($payment); + + if ( + Cart::hasError() + || ! $payment + || ! Cart::savePaymentMethod($payment) + ) { + return response()->json([ + 'message' => 'save payment error', + ]); + } + + Cart::collectTotals(); + + $this->validateOrder(); + + $cart = Cart::getCart(); + + //$order = $this->orderRepository->create(Cart::prepareDataForOrder()); //todo + + + if ($redirectUrl = Payment::getRedirectUrl($cart)) { + $paypalStandard = app('Webkul\Paypal\Payment\Standard'); + $data = []; + $data['success'] = true; + $data['redirect'] = $redirectUrl; + $data['redirect_url'] = $redirectUrl; + $data['form'] = $paypalStandard->getFormFields(); + $data['pay_url'] = $paypalStandard->getPaypalUrl(); + $data['result'] = 200; + return response()->json($data); + }else{ + $data = []; + $data['result'] = 400; + $data['message'] = $redirectUrl; + return response()->json($data); + } + } + + // 商品更新到购物车中。http://45.79.79.208:8002/api/checkout/cart + // 订单基于购物车中的商品完成订单生成 + + } + + // paypal 生成订单动作 + /** + * + * @link https://gist.github.com/nicelizhi/76d2a09692459c1a22388366c5861521 input params + * @return + */ + public function order_addr_after(Request $request) { + $input = $request->all(); + + $last_order_id = $request->session()->get('last_order_id'); // check the laster order id + //$last_order_id = "ddddd"; + $force = $request->input("force"); + + Log::info("last order id " . $last_order_id); + + if(!empty($last_order_id) && $force !="1") { + return response()->json(['error' => 'You Have already placed order, if you want to place another order please confirm your order','code'=>'202'], 400); + } + + $refer = $request->session()->get('refer'); + + $payment_paypal_vault = isset($input['payment_paypal_vault']) ? $input['payment_paypal_vault'] : 0; + $request->session()->put('payment_paypal_vault', $payment_paypal_vault); + + $products = $request->input("products"); + Log::info("products". json_encode($products)); + if(empty($products)) { + return response()->json(['error' => 'No product found in cart','code'=>'202'], 400); + } + // 添加到购物车 + Cart::deActivateCart(); + foreach($products as $key=>$product) { + //var_dump($product); + $product['quantity'] = $product['amount']; + $product['selected_configurable_option'] = $product['variant_id']; + if(!empty($product['attr_id'])) { + $attr_ids = explode(',', $product['attr_id']); + foreach($attr_ids as $key=>$attr_id) { + $attr = explode('_', $attr_id); + $super_attribute[$attr[0]] = $attr[1]; + } + + $product['super_attribute'] = $super_attribute; + } + + //Log::info("add product into cart ". json_encode($product)); + $cart = Cart::addProduct($product['product_id'], $product); + if ( + is_array($cart) + && isset($cart['warning']) + ) { + return new JsonResource([ + 'message' => $cart['warning'], + ]); + } + } + // 添加地址内容 + $addressData = []; + $addressData['billing'] = []; + $address1 = []; + array_push($address1, $input['address']); + $addressData['billing']['city'] = $input['city']; + $addressData['billing']['country'] = $input['country']; + $addressData['billing']['email'] = $input['email']; + $addressData['billing']['first_name'] = $input['first_name']; + $addressData['billing']['last_name'] = $input['second_name']; + $input['phone_full'] = str_replace('undefined+','', $input['phone_full']); + $addressData['billing']['phone'] = $input['phone_full']; + $addressData['billing']['postcode'] = $input['code']; + $addressData['billing']['state'] = $input['province']; + $addressData['billing']['use_for_shipping'] = true; + $addressData['billing']['address1'] = $address1; + $addressData['shipping'] = []; + $addressData['shipping']['isSaved'] = false; + $address1 = []; + array_push($address1, ""); + $addressData['shipping']['address1'] = $address1; + + $addressData['billing']['address1'] = implode(PHP_EOL, $addressData['billing']['address1']); + + $addressData['shipping']['address1'] = implode(PHP_EOL, $addressData['shipping']['address1']); + + Log::info("paypal pay ".$refer.'--'.json_encode($addressData)); + + + + + if ( + Cart::hasError() + || ! Cart::saveCustomerAddress($addressData) + ) { + return new JsonResource([ + 'redirect' => true, + 'data' => route('shop.checkout.cart.index'), + ]); + } + + + //处理配送方式 + $shippingMethod = "free_free"; // 包邮 + $shippingMethod = "flatrate_flatrate"; + // Cart::saveShippingMethod($shippingMethod); + + + if ( + Cart::hasError() + || ! $shippingMethod + || ! Cart::saveShippingMethod($shippingMethod) + ) { + return response()->json([ + 'redirect_url' => route('shop.checkout.cart.index'), + ], Response::HTTP_FORBIDDEN); + } + + Cart::collectTotals(); + + //处理支付方式 + $payment = []; + $payment['description'] = "PayPal-".$refer; + $payment['method'] = "paypal_smart_button"; + $payment['method_title'] = "PayPal Smart Button-".$refer; + $payment['sort'] = "1"; + // Cart::savePaymentMethod($payment); + + $upselling_enable = $request->session()->get('upselling_enable'); + if(config("Upselling.enable") && $upselling_enable) { + Log::info("upselling_enable". $upselling_enable); + $upselling = app('NexaMerchant\Upselling\Upselling'); + $upselling->applyUpselling($cart); + } + + if ( + Cart::hasError() + || ! $payment + || ! Cart::savePaymentMethod($payment) + ) { + return response()->json([ + 'redirect_url' => route('shop.checkout.cart.index'), + ], Response::HTTP_FORBIDDEN); + } + + try { + $order = $this->smartButton->createOrder($this->buildRequestBody($input)); + $data = []; + $data['order'] = $order; + $data['code'] = 200; + $data['result'] = 200; + return response()->json($order); + } catch (\Exception $e) { + return response()->json($e->getMessage(), 400); + } + + // return response()->json($order); + } + + + /** + * + * + * + */ + public function confirm(Request $request) { + $payment_intent_id = $request->input("payment_intent_id"); + $order_id = $request->input("order_id"); + $cus_id = $request->input("cus_id"); + + $order = $this->orderRepository->find($order_id); + + if(is_null($order)) { + return response()->json([ + "message" => "Order not found" + ], 404); + } + + $transactionManager = $this->airwallex->confirmPayment($payment_intent_id, $order, $cus_id); + + $request->session()->put('last_order_id', $order_id); + + $data = []; + $data['payment'] = $transactionManager; + $data['code'] = 200; + $data['result'] = 200; + $data['order_id'] = $order_id; + $data['order_id'] = $order_id; + return response()->json($data); + + } + + /** + * + * 订单状态查询 + * paypal 订单生成 + * + */ + public function order_status(Request $request) { + + $refer = $request->session()->get('refer'); + Log::info("refer checkout v1 ".$refer); + + try { + $order = $this->smartButton->getOrder(request()->input('orderData.orderID')); + + // paypal caputre order + //$this->smartButton->captureOrder(request()->input('orderData.orderID')); + + // var_dump($order); + // var_dump($order['result']); + // return response()->json($order); + + Log::info("paypal ".json_encode($order)); + Log::info("paypal request ".json_encode($request->all())); + + $params = request()->input("params"); + if(!empty($params)) { + + $addressData = []; + $addressData['billing'] = []; + $address1 = []; + array_push($address1, $params['address']); + + $addressData['billing']['city'] = $params['city']; + $addressData['billing']['email'] = $params['email']; + $addressData['billing']['country'] = $params['country']; + $addressData['billing']['first_name'] = $params['first_name']; + $addressData['billing']['last_name'] = $params['second_name']; + $addressData['billing']['phone'] = $params['phone_full']; + $addressData['billing']['phone'] = $params['phone_full']; + $addressData['billing']['address1'] = $address1; + + $addressData['billing']['state'] = $params['province']; + $addressData['billing']['postcode'] = $params['code']; + + //$addressData['shipping'] = []; + $addressData['shipping']['isSaved'] = false; + //$address1 = []; + //array_push($address1, ""); + $addressData['shipping']['address1'] = $address1; + + $addressData['billing']['address1'] = implode(PHP_EOL, $addressData['billing']['address1']); + + $addressData['shipping']['address1'] = implode(PHP_EOL, $addressData['shipping']['address1']); + if(!isset($addressData['shipping']['email'])) { + $addressData['shipping'] = $addressData['billing']; + } + + + Log::error("paypal pay address ".$refer.'--'.json_encode($addressData)); + + if ( + Cart::hasError() + || ! Cart::saveCustomerAddress($addressData) + ) { + return new JsonResource([ + 'redirect' => true, + 'data' => route('shop.checkout.cart.index'), + ]); + } + + // if the status not eq completed, then capture the order + + if($order->result->status != "COMPLETED") { + $this->smartButton->captureOrder(request()->input('orderData.orderID')); + } + //$this->smartButton->captureOrder(request()->input('orderData.orderID')); + + //$this->smartButton->AuthorizeOrder(request()->input('orderData.orderID')); + + $request->session()->put('last_order_id', request()->input('orderData.orderID')); + + }else{ + + $order = (array)$order; + + //var_dump($order); + + $purchase_units = (array)$order['result']->purchase_units; + $input = (array)$purchase_units[0]->shipping; + $payer = (array)$order['result']->payer; + $payment_source = (array)$order['result']->payment_source; + $payment_source_paypal = (array)$payment_source['paypal']; + + //Log::info("paypal source".json_encode($payment_source)); + //Log::info("paypal source paypal".json_encode($payment_source_paypal)); + + // 添加地址内容 + $addressData = []; + $addressData['billing'] = []; + $address1 = []; + $address_line_2 = isset($input['address']->address_line_2) ? $input['address']->address_line_2 : ""; + array_push($address1, $input['address']->address_line_1. $address_line_2); + $addressData['billing']['city'] = isset($input['address']->admin_area_2) ? $input['address']->admin_area_2 : ""; + $addressData['billing']['country'] = $input['address']->country_code; + $addressData['billing']['email'] = $payer['email_address']; + $addressData['billing']['first_name'] = $payer['name']->given_name; + $addressData['billing']['last_name'] = $payer['name']->surname; + $national_number = isset($payment_source_paypal['phone_number']) ? $payment_source_paypal['phone_number']->national_number : ""; + $addressData['billing']['phone'] = $national_number; + $addressData['billing']['postcode'] = isset($input['address']->postal_code) ? $input['address']->postal_code : ""; + $addressData['billing']['state'] = isset($input['address']->admin_area_1) ? $input['address']->admin_area_1 : ""; + $addressData['billing']['use_for_shipping'] = true; + $addressData['billing']['address1'] = $address1; + $addressData['shipping'] = []; + $addressData['shipping']['isSaved'] = false; + $address1 = []; + array_push($address1, ""); + $addressData['shipping']['address1'] = $address1; + + $addressData['billing']['address1'] = implode(PHP_EOL, $addressData['billing']['address1']); + + $addressData['shipping']['address1'] = implode(PHP_EOL, $addressData['shipping']['address1']); + + if ( + Cart::hasError() + || ! Cart::saveCustomerAddress($addressData) + ) { + return new JsonResource([ + 'redirect' => true, + 'data' => route('shop.checkout.cart.index'), + ]); + } + + if($order['result']->status != "COMPLETED") { + $this->smartButton->captureOrder(request()->input('orderData.orderID')); + } + + //$this->smartButton->AuthorizeOrder(request()->input('orderData.orderID')); + + $request->session()->put('last_order_id', request()->input('orderData.orderID')); + + } + + + //Log::info("address data-".$refer.'--'.json_encode($addressData)); + + return $this->saveOrder(); + } catch (\Exception $e) { + Log::info("paypal pay exception". json_encode($e->getMessage())); + return response()->json($e->getMessage()); + return response()->json(json_decode($e->getMessage()), 400); + } + + } + + /** + * Saving order once captured and all formalities done. + * + * @return \Illuminate\Http\Response + */ + protected function saveOrder() + { + if (Cart::hasError()) { + return response()->json(['redirect_url' => route('shop.checkout.cart.index')], 403); + } + + try { + Cart::collectTotals(); + + $this->validateOrder(); + + $order = $this->orderRepository->create(Cart::prepareDataForOrder()); + + $this->orderRepository->update(['status' => 'processing'], $order->id); + + if ($order->canInvoice()) { + $this->invoiceRepository->create($this->prepareInvoiceData($order)); + } + + Cart::deActivateCart(); + + //session()->flash('order', $order); + + $outputorder = $order->shipping_address; + + return response()->json([ + 'success' => true, + 'outputorder' => $outputorder, + ]); + } catch (\Exception $e) { + session()->flash('error', trans('shop::app.common.error')); + + throw $e; + } + } + + /** + * Prepares order's invoice data for creation. + * + * @param \Webkul\Sales\Models\Order $order + * @return array + */ + protected function prepareInvoiceData($order) + { + $invoiceData = ['order_id' => $order->id]; + + foreach ($order->items as $item) { + $invoiceData['invoice']['items'][$item->id] = $item->qty_to_invoice; + } + + return $invoiceData; + } + + /** + * + * @link https://developer.paypal.com/docs/multiparty/checkout/save-payment-methods/during-purchase/js-sdk/paypal/ + * + */ + protected function buildRequestBody($input) + { + $cart = Cart::getCart(); + + $billingAddressLines = $this->getAddressLines($cart->billing_address->address1); + + $data = []; + + + + $data = [ + 'intent' => 'CAPTURE', + 'application_context' => [ + //'shipping_preference' => 'NO_SHIPPING', + 'shipping_preference' => 'GET_FROM_FILE', // 用户选择自己的地址内容 + ], + + 'purchase_units' => [ + [ + 'amount' => [ + 'value' => $this->smartButton->formatCurrencyValue((float) $cart->sub_total + $cart->tax_total + ($cart->selected_shipping_rate ? $cart->selected_shipping_rate->price : 0) - $cart->discount_amount), + 'currency_code' => $cart->cart_currency_code, + + 'breakdown' => [ + 'item_total' => [ + 'currency_code' => $cart->cart_currency_code, + 'value' => $this->smartButton->formatCurrencyValue((float) $cart->sub_total), + ], + + 'shipping' => [ + 'currency_code' => $cart->cart_currency_code, + 'value' => $this->smartButton->formatCurrencyValue((float) ($cart->selected_shipping_rate ? $cart->selected_shipping_rate->price : 0)), + ], + + 'tax_total' => [ + 'currency_code' => $cart->cart_currency_code, + 'value' => $this->smartButton->formatCurrencyValue((float) $cart->tax_total), + ], + + 'discount' => [ + 'currency_code' => $cart->cart_currency_code, + 'value' => $this->smartButton->formatCurrencyValue((float) $cart->discount_amount), + ], + ], + ], + + 'items' => $this->getLineItems($cart), + ], + ], + ]; + + if (! empty($cart->billing_address->phone)) { + $data['payer']['phone'] = [ + 'phone_type' => 'MOBILE', + + 'phone_number' => [ + 'national_number' => $this->smartButton->formatPhone($cart->billing_address->phone), + ], + ]; + } + + if ( + $cart->haveStockableItems() + && $cart->shipping_address + ) { + //$data['application_context']['shipping_preference'] = 'SET_PROVIDED_ADDRESS'; + + /* + $data['purchase_units'][0] = array_merge($data['purchase_units'][0], [ + 'shipping' => [ + 'address' => [ + 'address_line_1' => current($billingAddressLines), + 'address_line_2' => last($billingAddressLines), + 'admin_area_2' => $cart->shipping_address->city, + 'admin_area_1' => $cart->shipping_address->state, + 'postal_code' => $cart->shipping_address->postcode, + 'country_code' => $cart->shipping_address->country, + ], + ], + ]); + */ + } + $input['payment_vault'] = isset($input['payment_vault']) ? $input['payment_vault'] : "0"; + $paypal_vault = Session::get('paypal_vault'); + if($input['payment_vault']=='1') { + // for vault + if(empty($paypal_vault)) { + $data["payment_source"] = [ + "paypal" => [ + "attributes" => [ + "vault" => [ + "store_in_vault" => "ON_SUCCESS", + "usage_type" => "MERCHANT", + "customer_type" => "CONSUMER" + ] + ], + "experience_context" => [ + "return_url" => $input['payment_return_url'], + 'cancel_url' => $input['payment_cancel_url'], + ] + ] + ]; + }else{ + $data["payment_source"] = [ + "paypal" => [ + "vault_id" => $paypal_vault['id'], + "experience_context" => [ + "return_url" => $input['payment_return_url'], + 'cancel_url' => $input['payment_cancel_url'], + ] + ] + ]; + } + // $data["payment_source"] = [ + // "paypal" => [ + // "attributes" => [ + // "vault" => [ + // "store_in_vault" => "ON_SUCCESS", + // "usage_type" => "MERCHANT", + // "customer_type" => "CONSUMER" + // ] + // ], + // "experience_context" => [ + // "return_url" => route("checkout.v4.product.page", ["slug" => "8987102380314"]), + // 'cancel_url' => route("checkout.v4.product.page",["slug"=>"8987102380314"]), + // ] + // ] + // ]; + + + + } + + + + // if(!empty($paypal_vault)) { + // Log::info("paypal vault ". json_encode($paypal_vault)); + // $data["payment_source"]["paypal"]["vault_id"] = $paypal_vault['id']; + // } + + Log::info("post to paypal data ". json_encode($data)); + + return $data; + } + + /** + * Return cart items. + * + * @param string $cart + * @return array + */ + protected function getLineItems($cart) + { + $lineItems = []; + + foreach ($cart->items as $item) { + $lineItems[] = [ + 'unit_amount' => [ + 'currency_code' => $cart->cart_currency_code, + 'value' => $this->smartButton->formatCurrencyValue((float) $item->price), + ], + 'quantity' => $item->quantity, + 'name' => $item->name, + 'sku' => $item->sku, + 'category' => $item->getTypeInstance()->isStockable() ? 'PHYSICAL_GOODS' : 'DIGITAL_GOODS', + ]; + } + + return $lineItems; + } + + /** + * Return convert multiple address lines into 2 address lines. + * + * @param string $address + * @return array + */ + protected function getAddressLines($address) + { + $address = explode(PHP_EOL, $address, 2); + + $addressLines = [current($address)]; + + if (isset($address[1])) { + $addressLines[] = str_replace(["\r\n", "\r", "\n"], ' ', last($address)); + } else { + $addressLines[] = ''; + } + + return $addressLines; + } + + /** + * Validate order before creation. + * + * @return void|\Exception + */ + public function validateOrder() + { + $cart = Cart::getCart(); + + $minimumOrderAmount = core()->getConfigData('sales.order_settings.minimum_order.minimum_order_amount') ?: 0; + + if ( + auth()->guard('customer')->check() + && auth()->guard('customer')->user()->is_suspended + ) { + throw new \Exception(trans('shop::app.checkout.cart.suspended-account-message')); + } + + if ( + auth()->guard('customer')->user() + && ! auth()->guard('customer')->user()->status + ) { + throw new \Exception(trans('shop::app.checkout.cart.inactive-account-message')); + } + + if (! $cart->checkMinimumOrder()) { + throw new \Exception(trans('shop::app.checkout.cart.minimum-order-message', ['amount' => core()->currency($minimumOrderAmount)])); + } + + if ($cart->haveStockableItems() && ! $cart->shipping_address) { + throw new \Exception(trans('shop::app.checkout.cart.check-shipping-address')); + } + + if (! $cart->billing_address) { + throw new \Exception(trans('shop::app.checkout.cart.check-billing-address')); + } + + if ( + $cart->haveStockableItems() + && ! $cart->selected_shipping_rate + ) { + throw new \Exception(trans('shop::app.checkout.cart.specify-shipping-method')); + } + + if (! $cart->payment) { + throw new \Exception(trans('shop::app.checkout.cart.specify-payment-method')); + } + } + + /*** + * + * + */ + + public function checkout_success(Request $request) { + + + $order = []; + + // check the payment info + $order_id = $request->input('id'); + + $orderTrans = $this->orderTransactionRepository->where('transaction_id', $order_id)->select(['order_id'])->first(); + if(!is_null($orderTrans)) { + $order = $this->orderRepository->findOrFail($orderTrans->order_id); + }else{ + $order = $this->orderRepository->findOrFail($order_id); + } + + + $fb_ids = config('onebuy.fb_ids'); + $ob_adv_id = config('onebuy.ob_adv_id'); + $crm_channel = config('onebuy.crm_channel'); + $refer = $request->session()->get('refer'); + $gtag = config('onebuy.gtag'); + + $quora_adv_id = config('onebuy.quora_adv_id'); + + $countries = config("countries"); + + $default_country = config('onebuy.default_country'); + + return view('onebuy::checkout-success', compact('order', + "fb_ids", + "ob_adv_id", + "crm_channel", + "refer", + "gtag", + "quora_adv_id", + "countries", + "default_country" + )); + + + return view('onebuy::checkout-success', compact('product','fb_ids','ob_adv_id')); + } + + public function checkout_success_v4($order_id, Request $request) { + $order = []; + + $orderTrans = $this->orderTransactionRepository->where('transaction_id', $order_id)->select(['order_id'])->first(); + if(!is_null($orderTrans)) { + $order = $this->orderRepository->findOrFail($orderTrans->order_id); + }else{ + $order = $this->orderRepository->findOrFail($order_id); + } + + + $fb_ids = config('onebuy.fb_ids'); + $ob_adv_id = config('onebuy.ob_adv_id'); + $crm_channel = config('onebuy.crm_channel'); + $refer = $request->session()->get('refer'); + $gtag = config('onebuy.gtag'); + $gtm = config('onebuy.gtm'); + + $quora_adv_id = config('onebuy.quora_adv_id'); + + $countries = config("countries"); + + + $app_env = config("app.env"); + + $default_country = config('onebuy.default_country'); + $order_pre = config('shopify.order_pre'); + + $recommend_products = []; + + $paypal_id_token = $request->session()->get('paypal_id_token'); + $paypal_access_token = $request->session()->get('paypal_access_token'); + + //var_dump($paypal_access_token, $paypal_id_token); + + if(empty($paypal_id_token)) { + $paypal_id_token = $this->smartButton->getIDAccessToken(); + $paypal_access_token = $paypal_id_token->result->access_token; + $paypal_id_token = $paypal_id_token->result->id_token; + + + + $request->session()->put('paypal_id_token', $paypal_id_token); + $request->session()->put('paypal_access_token', $paypal_access_token); + } + + $payment_airwallex_vault = $request->session()->get('payment_airwallex_vault'); + + $payment_paypal_vault = $request->session()->get('payment_paypal_vault'); + + + $paypal_pay_acc = core()->getConfigData('sales.payment_methods.paypal_smart_button.client_id'); + $app_env = config("app.env"); + + + + return view('onebuy::checkout-success-v4', compact('order', + "fb_ids", + "ob_adv_id", + "crm_channel", + "refer", + "gtag", + 'gtm', + "quora_adv_id", + "countries", + "default_country", + "app_env", + "order_pre", + "paypal_id_token", + "paypal_pay_acc", + "payment_airwallex_vault", + "payment_paypal_vault", + "recommend_products" + )); + } + + public function checkout_success_v2($order_id, Request $request) { + $order = []; + + $orderTrans = $this->orderTransactionRepository->where('transaction_id', $order_id)->select(['order_id'])->first(); + if(!is_null($orderTrans)) { + $order = $this->orderRepository->findOrFail($orderTrans->order_id); + }else{ + $order = $this->orderRepository->findOrFail($order_id); + } + + + $fb_ids = config('onebuy.fb_ids'); + $ob_adv_id = config('onebuy.ob_adv_id'); + $crm_channel = config('onebuy.crm_channel'); + $refer = $request->session()->get('refer'); + $gtag = config('onebuy.gtag'); + $gtm = config('onebuy.gtm'); + + $quora_adv_id = config('onebuy.quora_adv_id'); + + $countries = config("countries"); + + $default_country = config('onebuy.default_country'); + $order_pre = config('shopify.order_pre'); + + $recommend_products = []; + + return view('onebuy::checkout-success-v2', compact('order', + "fb_ids", + "ob_adv_id", + "crm_channel", + "refer", + "gtag", + 'gtm', + "quora_adv_id", + "countries", + "default_country", + "order_pre", + "recommend_products" + )); + } + + public function checkout_success_v1($order_id, Request $request) { + $order = []; + + $orderTrans = $this->orderTransactionRepository->where('transaction_id', $order_id)->select(['order_id'])->first(); + if(!is_null($orderTrans)) { + $order = $this->orderRepository->findOrFail($orderTrans->order_id); + }else{ + $order = $this->orderRepository->findOrFail($order_id); + } + + + $fb_ids = config('onebuy.fb_ids'); + $ob_adv_id = config('onebuy.ob_adv_id'); + $crm_channel = config('onebuy.crm_channel'); + $refer = $request->session()->get('refer'); + $gtag = config('onebuy.gtag'); + $gtm = config('onebuy.gtm'); + + $quora_adv_id = config('onebuy.quora_adv_id'); + + $countries = config("countries"); + + $default_country = config('onebuy.default_country'); + + $recommend_products = []; + + return view('onebuy::checkout-success-v1', compact('order', + "fb_ids", + "ob_adv_id", + "crm_channel", + "refer", + "gtag", + 'gtm', + "quora_adv_id", + "countries", + "default_country", + "recommend_products" + )); + } + + public function order_query(Request $request) { + $order_id = $request->input("id"); + + $order = $this->orderRepository->find($order_id); + if(is_null($order)) { + return new JsonResource([ + 'order_id' => 0, + 'info' => [], + ]); + } + + return new JsonResource([ + 'order_id' => $order_id, + 'info' => $order, + ]); + } + + public function recommended_query(Request $request) { + + $checkout_path = $request->input("checkout_path"); + + //select four recommended products + + $shopify_store_id = config('shopify.shopify_store_id'); + + + $products = \Nicelizhi\Shopify\Models\ShopifyProduct::where("shopify_store_id",$shopify_store_id)->where("status", "active")->select(['product_id','title','handle',"variants","images"])->limit(10)->get(); + + + $recommended_info = []; + + $shopifyStore = Cache::get("shopify_store_".$shopify_store_id); + + if(empty($shopifyStore)){ + $shopifyStore = \Nicelizhi\Shopify\Models\ShopifyStore::where('shopify_store_id', $shopify_store_id)->first(); + Cache::put("shopify_store_".$shopify_store_id, $shopifyStore, 3600); + } + + $i = 0; + $max = 3; + foreach($products as $key=> $product) { + $images = $product->images; + $variants = $product->variants; + + $online = \Webkul\Product\Models\Product::where("sku", $product->product_id)->first(); + if(is_null($online)) { + continue; + } + + if($i>=$max) { + break; + } + + $i++; + + $recommended_info[$key] = [ + "title" => $product->title, + "handle" => $product->handle, + "product_id" => $product->product_id, + "discount_price" => $variants[0]['price'], + "origin_price" => $variants[0]['compare_at_price'], + "image_url" => $images[0]['src'], + "url" => $shopifyStore->shopify_app_host_name . "/products/" . $product->handle + ]; + } + + + + + return new JsonResource([ + 'checkout_path' => $checkout_path, + 'recommended_info' => $recommended_info, + 'currency_symbol' => core()->getCurrentCurrencyCode(), + 'recommended_info_title' => __('onebuy::app.You may also like') + ]); + + } + + public function order_log(Request $request) { + Log::info("request ". json_encode($request->all())); + $session_data = $request->session()->all(); + Log::info("session ". json_encode($session_data)); + $refer = $request->cookie('refer'); + Log::info("cookie refer ". $refer); + } +} diff --git a/src/Http/Controllers/Web/ProductV2Controller.php b/src/Http/Controllers/Web/ProductV2Controller.php new file mode 100644 index 0000000..6ed20c0 --- /dev/null +++ b/src/Http/Controllers/Web/ProductV2Controller.php @@ -0,0 +1,366 @@ +productRepository->findBySlug($slugOrPath); + + if ( + ! $product + || ! $product->visible_individually + || ! $product->url_key + || ! $product->status + ) { + abort(404); + } + + //visitor()->visit($product); + + $refer = $request->input("refer"); + + if(!empty($refer)) { + $request->session()->put('refer', $refer); + }else{ + $refer = $request->session()->get('refer'); + } + + + + //var_dump($product);exit; + + // 四个商品的价格情况 + $package_products = []; + $productBaseImage = product_image()->getProductBaseImage($product); + //$package_products = $this->makeProducts($product, [2,1,3,4]); + $package_products = \Nicelizhi\OneBuy\Helpers\Utils::makeProducts($product, [2,1,3,4]); + + + // skus 数据 + $skus = []; + $cache_key = "product_sku_".$product->id; + $size_cache_key = "product_sku_size_".$product->id; + $color_cache_key = "product_color_size_".$product->id; + $skus = Cache::get($cache_key); + $qty_items_size = Cache::get($size_cache_key); + $qty_items_color = Cache::get($color_cache_key); + $skus = []; + if(empty($skus)) { + $sku_products = $this->productRepository->where("parent_id", $product->id)->get(); + $attributeOptionRepository = app(AttributeOptionRepository::class); + + foreach($sku_products as $key=>$sku) { + $sku_id = $sku->id; + $sku_code = $sku->sku; + unset($sku); + /** + * + * + * {"name":"Women's thin no wire lace bra - Black \/ S","sku_code":"CJ02168-C#black-S#m","sku_id":44113194877163,"attribute_name":"S,Black","key":"S_Black"} + * + * + */ + $productAttribute = $this->productAttributeValueRepository->findOneWhere([ + 'product_id' => $sku_id, + 'attribute_id' => 2, + ]); + + //var_dump($productAttribute); + + $sku['name'] = $productAttribute['text_value']; + + + + $sku['sku_code'] = $sku_code; + $sku['sku_id'] = $sku_id; + + $colorAttribute = $this->productAttributeValueRepository->findOneWhere([ + 'product_id' => $sku_id, + 'attribute_id' => 23, + ]); + + $sizeAttribute = $this->productAttributeValueRepository->findOneWhere([ + 'product_id' => $sku_id, + 'attribute_id' => 24, + ]); + + $attr_id = ""; + $ColorattributeOptions = null; + if(!is_null($colorAttribute)) { + $ColorattributeOptions = $attributeOptionRepository->findOneWhere(['id'=>$colorAttribute['integer_value']]); + $attr_id = "24_".$colorAttribute['integer_value']; + } + $SizeattributeOptions = null; + if(!is_null($sizeAttribute)) { + $SizeattributeOptions = $attributeOptionRepository->findOneWhere(['id'=>$sizeAttribute['integer_value']]); + if(empty($attr_id)) { + $attr_id = "23_".$sizeAttribute['integer_value']; + }else{ + $attr_id .= ",23_".$sizeAttribute['integer_value']; + } + + } + + + + + + // $attribute_name = $ColorattributeOptions->admin_name.",".$SizeattributeOptions->admin_name; + //$attribute_name = $SizeattributeOptions->admin_name.",".$ColorattributeOptions->admin_name; + + $attribute_name = ""; + $sku_key = ""; + + + if(!is_null($SizeattributeOptions)) { + $attribute_name .= $SizeattributeOptions->admin_name; + $sku_key = $SizeattributeOptions->admin_name; + } + if(!is_null($ColorattributeOptions)) { + if(!empty($attribute_name)) { + $attribute_name.=",".$ColorattributeOptions->admin_name; + $sku_key.= "_".$ColorattributeOptions->admin_name; + }else{ + $attribute_name = $ColorattributeOptions->admin_name; + $sku_key = $ColorattributeOptions->admin_name; + } + } + + $sku['attribute_name'] = $attribute_name; + $sku['attr_id'] = $attr_id; + //$sku['attr_id'] = "24_".$colorAttribute['integer_value'].",23_".$sizeAttribute['integer_value']; + + //$sku['key'] = $ColorattributeOptions->admin_name."_".$SizeattributeOptions->admin_name; // 这个数据需要留意他的位置,JS判断会需要使用 + //$sku['key'] = $SizeattributeOptions->admin_name."_".$ColorattributeOptions->admin_name; // 这个数据需要留意他的位置,JS判断会需要使用 + $sku['key'] = $sku_key; + + // $qty_items_color[$ColorattributeOptions->admin_name][] = $SizeattributeOptions->admin_name; + // $qty_items_size[$SizeattributeOptions->admin_name][] = $ColorattributeOptions->admin_name; + + $skus[] = $sku; + } + Cache::put($cache_key, json_encode($skus)); + //Log::info(json_encode($qty_items_size)); + //Cache::put($size_cache_key, json_encode($qty_items_size)); + //Cache::put($color_cache_key, json_encode($qty_items_color)); + }else { + $skus = json_decode($skus, JSON_OBJECT_AS_ARRAY); + } + + + $product_attributes = []; + + $cache_key = "product_attributes_".$product->id; + $product_attributes = Cache::get($cache_key); + + $cache_key_1 = "product_category_".$product->id; + $product_category = Cache::get($cache_key_1); + if(empty($product_category)) { + $categories = $product->categories; + if(isset($categories[0])) { + $product_category_id = intval($categories[0]->id); + }else{ + $product_category_id = 9; + } + + Cache::put($cache_key_1, $product_category_id); + }else{ + //$product_category = json_decode($product_category); + //var_dump($product_category);exit; + $product_category_id = intval($product_category); + } + + // $productViewHelper = new \Webkul\Product\Helpers\ConfigurableOption(); + // $attributes = $productViewHelper->getConfigurationConfig($product); + // var_dump($attributes); + + if(empty($product_attributes)) { + + $productViewHelper = new \Webkul\Product\Helpers\ConfigurableOption(); + $attributes = $productViewHelper->getConfigurationConfig($product); + + + $productSizeImage = $this->productAttributeValueRepository->findOneWhere([ + 'product_id' => $product->id, + 'attribute_id' => 32, + ]); + + + //var_dump($customAttributeValues);exit; + + //获取到他底部的商品内容 + // $attributes = $this->productRepository->getSuperAttributes($product); + + foreach($attributes['attributes'] as $key=>$attribute) { + $attribute['name'] = $attribute['code']; + $options = []; + foreach($attribute['options'] as $kk=>$option) { + + // 获取商品图片内容 + $is_sold_out = false; + if($attribute['id']==23) { + $new_id = $option['products'][0]; + $new_product = $this->productRepository->find($new_id); + $NewproductBaseImage = product_image()->getProductBaseImage($new_product); + $option['image'] = @$NewproductBaseImage['medium_image_url']; + + }else{ + $new_id = $option['products'][0]; + $new_product = $this->productRepository->find($new_id); + $NewproductBaseImage = product_image()->getProductBaseImage($new_product); + $option['image'] = $productBaseImage['medium_image_url']; + + } + // 判断是否有对应的尺码内容 + $option['is_sold_out'] = $is_sold_out; + $option['name'] = $option['label']; + unset($option['admin_name']); + $options[] = $option; + //var_dump($option); + } + + $tip = ""; + $tip_img = ""; + if($attribute['id']==24) { + $tip = "Size Chart"; + if(isset($productSizeImage->text_value)) $tip_img = $productSizeImage->text_value; + if(empty($tip_img)) $tip = ""; + } + + $attribute['tip'] = $tip; + $attribute['tip_img'] = $tip_img; + + unset($attribute['translations']); //去掉多余的数据内容 + //var_dump($options); + $attribute['options'] = $options; + $attribute['image'] = $productBaseImage['medium_image_url']; + $product_attributes[] = $attribute; + } + + Cache::put($cache_key, json_encode($product_attributes)); + + }else{ + $product_attributes = json_decode($product_attributes, JSON_OBJECT_AS_ARRAY); + } + + rsort($product_attributes); + + //商品的背景图片获取 + + $productBgAttribute = $this->productAttributeValueRepository->findOneWhere([ + 'product_id' => $product->id, + 'attribute_id' => 29, + ]); + + + $productBgAttribute_mobile = $this->productAttributeValueRepository->findOneWhere([ + 'product_id' => $product->id, + 'attribute_id' => 30, + ]); + + + $app_env = config("app.env"); + + //var_dump($productBgAttribute); + + // 获取 faq 数据 + $redis = Redis::connection('default'); + $faqItems = $redis->hgetall($this->faq_cache_key); + + ksort($faqItems); + + $comments = $redis->hgetall($this->cache_prefix_key."product_comments_".$product['id']); + if(empty($comments)) $comments = $redis->hgetall("checkout_v1_product_comments_".$product['id']); + + + + //获取 paypal smart key + $paypal_client_id = core()->getConfigData('sales.payment_methods.paypal_smart_button.client_id'); + + + //支持的区域 + $countries = config("countries"); + + $default_country = config('onebuy.default_country'); + + $airwallex_method = config('onebuy.airwallex.method'); + + $payments = config('onebuy.payments'); // config the payments status + $payments_default = config('onebuy.payments_default'); + + $brand = config('onebuy.brand'); + + $fb_ids = config('onebuy.fb_ids'); + + $gtag = config('onebuy.gtag'); + + $ob_adv_id = config('onebuy.ob_adv_id'); + + $crm_channel = config('onebuy.crm_channel'); + + $quora_adv_id = config('onebuy.quora_adv_id'); + + + return view('onebuy::product-detail-v2', compact('gtag','app_env','product','package_products', 'product_attributes', 'skus','productBgAttribute','productBgAttribute_mobile','faqItems','comments','paypal_client_id','default_country','airwallex_method','payments','payments_default','brand','fb_ids','ob_adv_id','crm_channel','refer',"quora_adv_id")); + } + +} \ No newline at end of file diff --git a/src/Http/Controllers/Web/ProductV3Controller.php b/src/Http/Controllers/Web/ProductV3Controller.php new file mode 100644 index 0000000..83ba3fe --- /dev/null +++ b/src/Http/Controllers/Web/ProductV3Controller.php @@ -0,0 +1,250 @@ +productRepository->findBySlug($slugOrPath); + Cache::put($cache_key, $product); + } + + if ( + ! $product + || ! $product->visible_individually + || ! $product->url_key + || ! $product->status + ) { + abort(404); + } + + //visitor()->visit($product); + + $refer = $request->input("refer"); + + if(!empty($refer)) { + $request->session()->put('refer', $refer); + }else{ + $refer = $request->session()->get('refer'); + } + + // 四个商品的价格情况 + $package_products = []; + $productBaseImage = product_image()->getProductBaseImage($product); + //$package_products = $this->makeProducts($product, [2,1,3,4]); + $package_products = \Nicelizhi\OneBuy\Helpers\Utils::makeProducts($product, [2,1,3,4]); + + // 获取 faq 数据 + $redis = Redis::connection('default'); + + // skus 数据 + $skus = []; + + + + $product_attributes = []; + + $cache_key = "product_attributes_".$product->id; + $product_attributes = Cache::get($cache_key); + + + $product_attributes = []; + if(empty($product_attributes)) { + //if(true) { + + $productViewHelper = new \Webkul\Product\Helpers\ConfigurableOption(); + $attributes = $productViewHelper->getConfigurationConfig($product); + + + $productSizeImage = $this->productAttributeValueRepository->findOneWhere([ + 'product_id' => $product->id, + 'attribute_id' => 32, + ]); + + //获取到他底部的商品内容 + // $attributes = $this->productRepository->getSuperAttributes($product); + $product_attr_sort_cache_key = "product_attr_sort_23_".$product->id; + $product_attr_sort = $redis->hgetall($product_attr_sort_cache_key); // get sku sort + + foreach($attributes['attributes'] as $key=>$attribute) { + //var_dump($attribute); + $attribute['name'] = $attribute['code']; + $options = []; + foreach($attribute['options'] as $kk=>$option) { + // 获取商品图片内容 + $is_sold_out = false; + if($attribute['id']==23) { + $new_id = $option['products'][0]; + $new_product = $this->productRepository->find($new_id); + $NewproductBaseImage = product_image()->getProductBaseImage($new_product); + $option['image'] = @$NewproductBaseImage['large_image_url']; + $option['big_image'] = @$NewproductBaseImage['large_image_url']; + + }else{ + $option['image'] = $productBaseImage['large_image_url']; + $option['large_image'] = @$productBaseImage['large_image_url']; + } + + // 判断是否有对应的尺码内容 + + $option['is_sold_out'] = $is_sold_out; + $option['name'] = $option['label']; + unset($option['admin_name']); + + if($attribute['id']==23 && !empty($product_attr_sort)) { + $sort = isset($product_attr_sort[$option['id']]) ? intval($product_attr_sort[$option['id']]) : 4 ; + $option['sort'] = $sort; + $options[$sort] = $option; + }else{ + $options[] = $option; + } + //var_dump($options); + } + + //var_dump($options); + //array_multisort($options,) + //var_dump($options); + ksort($options); + + $tip = ""; + $tip_img = ""; + if($attribute['id']==24) { + $tip = trans('onebuy::app.product.Size Chart'); + if(isset($productSizeImage->text_value)) $tip_img = $productSizeImage->text_value; + if(empty($tip_img)) $tip = ""; + } + + $attribute['tip'] = $tip; + $attribute['tip_img'] = $tip_img; + + unset($attribute['translations']); //去掉多余的数据内容 + //var_dump($options); + $attribute['options'] = $options; + $attribute['image'] = $productBaseImage['large_image_url']; + $attribute['large_image'] = $productBaseImage['large_image_url']; + + $product_attributes[] = $attribute; + } + + Cache::put($cache_key, json_encode($product_attributes)); + + }else{ + $product_attributes = json_decode($product_attributes, JSON_OBJECT_AS_ARRAY); + } + + rsort($product_attributes); + //商品的背景图片获取 + + $productBgAttribute = $this->productAttributeValueRepository->findOneWhere([ + 'product_id' => $product->id, + 'attribute_id' => 29, + ]); + + + $productBgAttribute_mobile = $this->productAttributeValueRepository->findOneWhere([ + 'product_id' => $product->id, + 'attribute_id' => 30, + ]); + + + $app_env = config("app.env"); + + //var_dump($productBgAttribute); + + + $faqItems = $redis->hgetall($this->faq_cache_key); + ksort($faqItems); + $comments = $redis->hgetall($this->cache_prefix_key."product_comments_".$product['id']); + //获取 paypal smart key + $paypal_client_id = core()->getConfigData('sales.payment_methods.paypal_smart_button.client_id'); + + + //支持的区域 + $countries = config("countries"); + + $default_country = config('onebuy.default_country'); + + $airwallex_method = config('onebuy.airwallex.method'); + + $payments = config('onebuy.payments'); // config the payments status + + $payments_default = config('onebuy.payments_default'); + $brand = config('onebuy.brand'); + + $gtag = config('onebuy.gtag'); + + $fb_ids = config('onebuy.fb_ids'); + $ob_adv_id = config('onebuy.ob_adv_id'); + + $crm_channel = config('onebuy.crm_channel'); + + $quora_adv_id = config('onebuy.quora_adv_id'); + + // products display image + $product_image_lists = Cache::get("product_image_lists_".$product['id']); + + return view('onebuy::product-detail-v3', compact('gtag','app_env','product','package_products', 'product_attributes', 'skus','productBgAttribute','productBgAttribute_mobile','faqItems','comments','paypal_client_id','default_country','airwallex_method','payments','payments_default','brand','fb_ids','ob_adv_id','crm_channel','refer',"quora_adv_id","product_image_lists")); + } + +} diff --git a/src/Http/Controllers/Web/ProductV4Controller.php b/src/Http/Controllers/Web/ProductV4Controller.php new file mode 100644 index 0000000..16865fb --- /dev/null +++ b/src/Http/Controllers/Web/ProductV4Controller.php @@ -0,0 +1,395 @@ +productRepository->findBySlug($slugOrPath); + Cache::put($cache_key, $product); + } + + if ( + ! $product + || ! $product->visible_individually + || ! $product->url_key + || ! $product->status + ) { + abort(404); + } + + // log the refer info + $refer = $request->input("refer"); + if(!empty($refer)) { + $request->session()->put('refer', $refer); + }else{ + $refer = $request->session()->get('refer'); + } + + + $redis = Redis::connection('default'); + + $package_products = []; + $productBaseImage = product_image()->getProductBaseImage($product); + $package_products = \Nicelizhi\OneBuy\Helpers\Utils::makeProducts($product, [2,1,3,4]); + + $skus = []; + + $cache_key = "product_sku_".$product->id; + $size_cache_key = "product_sku_size_".$product->id; + $color_cache_key = "product_color_size_".$product->id; + $skus = Cache::get($cache_key); + $qty_items_size = Cache::get($size_cache_key); + $qty_items_color = Cache::get($color_cache_key); + + if(empty($skus)) { + $sku_products = $this->productRepository->where("parent_id", $product->id)->get(); + + $attributeOptionRepository = app(AttributeOptionRepository::class); + + foreach($sku_products as $key=>$sku) { + $sku_id = $sku->id; + $sku_code = $sku->sku; + unset($sku); + /** + * + * + * {"name":"Women's thin no wire lace bra - Black \/ S","sku_code":"CJ02168-C#black-S#m","sku_id":44113194877163,"attribute_name":"S,Black","key":"S_Black"} + * + * + */ + $productAttribute = $this->productAttributeValueRepository->findOneWhere([ + 'product_id' => $sku_id, + 'attribute_id' => 2, + ]); + + //var_dump($productAttribute); + + $sku['name'] = $productAttribute['text_value']; + + + + $sku['sku_code'] = $sku_code; + $sku['sku_id'] = $sku_id; + + // banner pic big url (pc) + $colorAttribute = $this->productAttributeValueRepository->findOneWhere([ + 'product_id' => $sku_id, + 'attribute_id' => 23, + ]); + + // banner pic mobile url + $sizeAttribute = $this->productAttributeValueRepository->findOneWhere([ + 'product_id' => $sku_id, + 'attribute_id' => 24, + ]); + + $attr_id = ""; + $ColorattributeOptions = null; + if(!is_null($colorAttribute)) { + $ColorattributeOptions = $attributeOptionRepository->findOneWhere(['id'=>$colorAttribute['integer_value']]); + $attr_id = "24_".$colorAttribute['integer_value']; + } + $SizeattributeOptions = null; + if(!is_null($sizeAttribute)) { + $SizeattributeOptions = $attributeOptionRepository->findOneWhere(['id'=>$sizeAttribute['integer_value']]); + if(empty($attr_id)) { + $attr_id = "23_".$sizeAttribute['integer_value']; + }else{ + $attr_id .= ",23_".$sizeAttribute['integer_value']; + } + + } + + $attribute_name = ""; + $sku_key = ""; + + + if(!is_null($SizeattributeOptions)) { + $attribute_name .= $SizeattributeOptions->admin_name; + $sku_key = $SizeattributeOptions->admin_name; + + //$qty_items_size[$SizeattributeOptions->admin_name][] = $ColorattributeOptions->admin_name; + } + if(!is_null($ColorattributeOptions)) { + if(!empty($attribute_name)) { + $attribute_name.=",".$ColorattributeOptions->admin_name; + $sku_key.= "_".$ColorattributeOptions->admin_name; + }else{ + $attribute_name = $ColorattributeOptions->admin_name; + $sku_key = $ColorattributeOptions->admin_name; + } + //$qty_items_color[$ColorattributeOptions->admin_name][] = $SizeattributeOptions->admin_name; + } + + + + // $attribute_name = $SizeattributeOptions->admin_name.",".$ColorattributeOptions->admin_name; + + // $sku['attribute_name'] = $attribute_name; + // $sku['attr_id'] = "24_".$colorAttribute['integer_value'].",23_".$sizeAttribute['integer_value']; + + $sku['attribute_name'] = $attribute_name; + $sku['attr_id'] = $attr_id; + + //$sku['key'] = $ColorattributeOptions->admin_name."_".$SizeattributeOptions->admin_name; // 这个数据需要留意他的位置,JS判断会需要使用 + //$sku['key'] = $SizeattributeOptions->admin_name."_".$ColorattributeOptions->admin_name; // 这个数据需要留意他的位置,JS判断会需要使用 + $sku['key'] = $sku_key; // 这个数据需要留意他的位置,JS判断会需要使用 + + + + + $skus[] = $sku; + } + Cache::put($cache_key, json_encode($skus)); + Cache::put($size_cache_key, json_encode($qty_items_size)); + Cache::put($color_cache_key, json_encode($qty_items_color)); + }else { + $skus = json_decode($skus, JSON_OBJECT_AS_ARRAY); + } + + $skus = (array) $skus; + + $product_attributes = []; + + $cache_key = "product_attributes_".$product->id; + $product_attributes = Cache::get($cache_key); + + + + //$product_attributes = []; + if(empty($product_attributes)) { + //if(true) { + + $productViewHelper = new \Webkul\Product\Helpers\ConfigurableOption(); + $attributes = $productViewHelper->getConfigurationConfig($product); + + + $productSizeImage = $this->productAttributeValueRepository->findOneWhere([ + 'product_id' => $product->id, + 'attribute_id' => 32, + ]); + + //获取到他底部的商品内容 + // $attributes = $this->productRepository->getSuperAttributes($product); + + + foreach($attributes['attributes'] as $key=>$attribute) { + + $product_attr_sort_cache_key = "product_attr_sort_".$attribute['id']."_".$product->id; + $product_attr_sort = $redis->hgetall($product_attr_sort_cache_key); // get sku sort + + //var_dump($attribute); + $attribute['name'] = $attribute['code']; + $options = []; + foreach($attribute['options'] as $kk=>$option) { + // 获取商品图片内容 + $is_sold_out = false; + if($attribute['id']==23) { + $new_id = $option['products'][0]; + $new_product = $this->productRepository->find($new_id); + $NewproductBaseImage = product_image()->getProductBaseImage($new_product); + $option['image'] = @$NewproductBaseImage['large_image_url']; + $option['big_image'] = @$NewproductBaseImage['large_image_url']; + + }else{ + $option['image'] = $productBaseImage['large_image_url']; + $option['large_image'] = @$productBaseImage['large_image_url']; + } + + // 判断是否有对应的尺码内容 + + $option['is_sold_out'] = $is_sold_out; + $option['name'] = $option['label']; + unset($option['admin_name']); + + if(!empty($product_attr_sort)) { + $sort = isset($product_attr_sort[$option['id']]) ? intval($product_attr_sort[$option['id']]) : 4 ; + $option['sort'] = $sort; + $options[$sort] = $option; + }else{ + $options[] = $option; + } + //var_dump($options); + } + + //var_dump($options); + //array_multisort($options,) + //var_dump($options); + ksort($options); + //var_dump($options);exit; + + $tip = ""; + $tip_img = ""; + if($attribute['id']==24) { + $tip = trans('onebuy::app.product.Size Chart'); + if(isset($productSizeImage->text_value)) $tip_img = $productSizeImage->text_value; + if(empty($tip_img)) $tip = ""; + } + + $attribute['tip'] = $tip; + $attribute['tip_img'] = $tip_img; + + unset($attribute['translations']); //去掉多余的数据内容 + //var_dump($options); + $attribute['options'] = $options; + $attribute['image'] = $productBaseImage['large_image_url']; + $attribute['large_image'] = $productBaseImage['large_image_url']; + + $product_attributes[] = $attribute; + } + + Cache::put($cache_key, json_encode($product_attributes)); + + }else{ + $product_attributes = json_decode($product_attributes, JSON_OBJECT_AS_ARRAY); + } + + if (!is_null($product_attributes)) rsort($product_attributes); + //商品的背景图片获取 + + $productBgAttribute = $this->productAttributeValueRepository->findOneWhere([ + 'product_id' => $product->id, + 'attribute_id' => 29, + ]); + + + $productBgAttribute_mobile = $this->productAttributeValueRepository->findOneWhere([ + 'product_id' => $product->id, + 'attribute_id' => 30, + ]); + + + $app_env = config("app.env"); + + $faqItems = $redis->hgetall($this->faq_cache_key); + ksort($faqItems); + $comments = $redis->hgetall($this->cache_prefix_key."product_comments_".$product['id']); + //获取 paypal smart key + $paypal_client_id = core()->getConfigData('sales.payment_methods.paypal_smart_button.client_id'); + + //支持的区域 + $countries = config("countries"); + + $default_country = config('onebuy.default_country'); + + $airwallex_method = config('onebuy.airwallex.method'); + + $payments = config('onebuy.payments'); // config the payments status + + $payments_default = config('onebuy.payments_default'); + $brand = config('onebuy.brand'); + + $gtag = config('onebuy.gtag'); + + $fb_ids = config('onebuy.fb_ids'); + $ob_adv_id = config('onebuy.ob_adv_id'); + + $crm_channel = config('onebuy.crm_channel'); + + $quora_adv_id = config('onebuy.quora_adv_id'); + + // get product reviews + + // products display image + $product_image_lists = Cache::get("product_image_lists_".$product['id']); + + $version = Cache::get("onebuy_".$product['sku']); + + //var_dump($version); + //var_dump($product_image_lists); + + + + return view('onebuy::product-detail-v4', compact('gtag', + 'app_env', + 'product', + 'package_products', + 'product_attributes', + 'skus', + 'productBgAttribute', + 'productBgAttribute_mobile', + 'faqItems', + 'comments', + 'paypal_client_id', + 'default_country', + 'airwallex_method', + 'payments', + 'payments_default', + 'brand', + 'fb_ids', + 'ob_adv_id', + 'crm_channel', + 'refer', + 'version', + 'product_image_lists', + 'quora_adv_id' + )); + + //return view('shop::products.view', compact('product')); default shop view + + + } + + + +} \ No newline at end of file diff --git a/src/Http/Controllers/Web/ProductV5Controller.php b/src/Http/Controllers/Web/ProductV5Controller.php new file mode 100644 index 0000000..bb0f362 --- /dev/null +++ b/src/Http/Controllers/Web/ProductV5Controller.php @@ -0,0 +1,500 @@ +productRepository->findBySlug($slugOrPath); + Cache::put($cache_key, $product); + } + + if ( + ! $product + || ! $product->visible_individually + || ! $product->url_key + || ! $product->status + ) { + abort(404); + } + + // log the refer info + $refer = $request->input("refer"); + if(!empty($refer)) { + $request->session()->put('refer', $refer); + }else{ + $refer = $request->session()->get('refer'); + } + + + $redis = Redis::connection('default'); + + $package_products = []; + $productBaseImage = product_image()->getProductBaseImage($product); + $package_products = \Nicelizhi\OneBuy\Helpers\Utils::makeProducts($product, [2,1,3,4]); + + $skus = []; + + $cache_key = "product_sku_".$product->id; + $size_cache_key = "product_sku_size_".$product->id; + $color_cache_key = "product_color_size_".$product->id; + $skus = Cache::get($cache_key); + $qty_items_size = Cache::get($size_cache_key); + $qty_items_color = Cache::get($color_cache_key); + + if(empty($skus)) { + $sku_products = $this->productRepository->where("parent_id", $product->id)->get(); + + $attributeOptionRepository = app(AttributeOptionRepository::class); + + foreach($sku_products as $key=>$sku) { + $sku_id = $sku->id; + $sku_code = $sku->sku; + unset($sku); + /** + * + * + * {"name":"Women's thin no wire lace bra - Black \/ S","sku_code":"CJ02168-C#black-S#m","sku_id":44113194877163,"attribute_name":"S,Black","key":"S_Black"} + * + * + */ + $productAttribute = $this->productAttributeValueRepository->findOneWhere([ + 'product_id' => $sku_id, + 'attribute_id' => 2, + ]); + + //var_dump($productAttribute); + + $sku['name'] = $productAttribute['text_value']; + + + + $sku['sku_code'] = $sku_code; + $sku['sku_id'] = $sku_id; + + // banner pic big url (pc) + $colorAttribute = $this->productAttributeValueRepository->findOneWhere([ + 'product_id' => $sku_id, + 'attribute_id' => 23, + ]); + + // banner pic mobile url + $sizeAttribute = $this->productAttributeValueRepository->findOneWhere([ + 'product_id' => $sku_id, + 'attribute_id' => 24, + ]); + + $attr_id = ""; + $ColorattributeOptions = null; + if(!is_null($colorAttribute)) { + $ColorattributeOptions = $attributeOptionRepository->findOneWhere(['id'=>$colorAttribute['integer_value']]); + $attr_id = "24_".$colorAttribute['integer_value']; + } + $SizeattributeOptions = null; + if(!is_null($sizeAttribute)) { + $SizeattributeOptions = $attributeOptionRepository->findOneWhere(['id'=>$sizeAttribute['integer_value']]); + if(empty($attr_id)) { + $attr_id = "23_".$sizeAttribute['integer_value']; + }else{ + $attr_id .= ",23_".$sizeAttribute['integer_value']; + } + + } + + $attribute_name = ""; + $sku_key = ""; + + + if(!is_null($SizeattributeOptions)) { + $attribute_name .= $SizeattributeOptions->admin_name; + $sku_key = $SizeattributeOptions->admin_name; + + //$qty_items_size[$SizeattributeOptions->admin_name][] = $ColorattributeOptions->admin_name; + } + if(!is_null($ColorattributeOptions)) { + if(!empty($attribute_name)) { + $attribute_name.=",".$ColorattributeOptions->admin_name; + $sku_key.= "_".$ColorattributeOptions->admin_name; + }else{ + $attribute_name = $ColorattributeOptions->admin_name; + $sku_key = $ColorattributeOptions->admin_name; + } + //$qty_items_color[$ColorattributeOptions->admin_name][] = $SizeattributeOptions->admin_name; + } + + + + // $attribute_name = $SizeattributeOptions->admin_name.",".$ColorattributeOptions->admin_name; + + // $sku['attribute_name'] = $attribute_name; + // $sku['attr_id'] = "24_".$colorAttribute['integer_value'].",23_".$sizeAttribute['integer_value']; + + $sku['attribute_name'] = $attribute_name; + $sku['attr_id'] = $attr_id; + + //$sku['key'] = $ColorattributeOptions->admin_name."_".$SizeattributeOptions->admin_name; // 这个数据需要留意他的位置,JS判断会需要使用 + //$sku['key'] = $SizeattributeOptions->admin_name."_".$ColorattributeOptions->admin_name; // 这个数据需要留意他的位置,JS判断会需要使用 + $sku['key'] = $sku_key; // 这个数据需要留意他的位置,JS判断会需要使用 + + + + + $skus[] = $sku; + } + Cache::put($cache_key, json_encode($skus)); + Cache::put($size_cache_key, json_encode($qty_items_size)); + Cache::put($color_cache_key, json_encode($qty_items_color)); + }else { + $skus = json_decode($skus, JSON_OBJECT_AS_ARRAY); + } + + $skus = (array) $skus; + + $product_attributes = []; + + $cache_key = "product_attributes_".$product->id; + $product_attributes = Cache::get($cache_key); + + + + //$product_attributes = []; + if(empty($product_attributes)) { + //if(true) { + + $productViewHelper = new \Webkul\Product\Helpers\ConfigurableOption(); + $attributes = $productViewHelper->getConfigurationConfig($product); + + + $productSizeImage = $this->productAttributeValueRepository->findOneWhere([ + 'product_id' => $product->id, + 'attribute_id' => 32, + ]); + + //获取到他底部的商品内容 + // $attributes = $this->productRepository->getSuperAttributes($product); + + + foreach($attributes['attributes'] as $key=>$attribute) { + + $product_attr_sort_cache_key = "product_attr_sort_".$attribute['id']."_".$product->id; + $product_attr_sort = $redis->hgetall($product_attr_sort_cache_key); // get sku sort + + //var_dump($attribute); + $attribute['name'] = $attribute['code']; + $options = []; + foreach($attribute['options'] as $kk=>$option) { + // 获取商品图片内容 + $is_sold_out = false; + if($attribute['id']==23) { + $new_id = $option['products'][0]; + $new_product = $this->productRepository->find($new_id); + $NewproductBaseImage = product_image()->getProductBaseImage($new_product); + $option['image'] = @$NewproductBaseImage['large_image_url']; + $option['big_image'] = @$NewproductBaseImage['large_image_url']; + + }else{ + $option['image'] = $productBaseImage['large_image_url']; + $option['large_image'] = @$productBaseImage['large_image_url']; + } + + // 判断是否有对应的尺码内容 + + $option['is_sold_out'] = $is_sold_out; + $option['name'] = $option['label']; + unset($option['admin_name']); + + if(!empty($product_attr_sort)) { + $sort = isset($product_attr_sort[$option['id']]) ? intval($product_attr_sort[$option['id']]) : 4 ; + $option['sort'] = $sort; + $options[$sort] = $option; + }else{ + $options[] = $option; + } + //var_dump($options); + } + + //var_dump($options); + //array_multisort($options,) + //var_dump($options); + ksort($options); + //var_dump($options);exit; + + $tip = ""; + $tip_img = ""; + if($attribute['id']==24) { + $tip = trans('onebuy::app.product.Size Chart'); + if(isset($productSizeImage->text_value)) $tip_img = $productSizeImage->text_value; + if(empty($tip_img)) $tip = ""; + } + + $attribute['tip'] = $tip; + $attribute['tip_img'] = $tip_img; + + unset($attribute['translations']); //去掉多余的数据内容 + //var_dump($options); + $attribute['options'] = $options; + $attribute['image'] = $productBaseImage['large_image_url']; + $attribute['large_image'] = $productBaseImage['large_image_url']; + + $product_attributes[] = $attribute; + } + + Cache::put($cache_key, json_encode($product_attributes)); + + }else{ + $product_attributes = json_decode($product_attributes, JSON_OBJECT_AS_ARRAY); + } + + if (!is_null($product_attributes)) rsort($product_attributes); + //商品的背景图片获取 + + $productBgAttribute = $this->productAttributeValueRepository->findOneWhere([ + 'product_id' => $product->id, + 'attribute_id' => 29, + ]); + + + $productBgAttribute_mobile = $this->productAttributeValueRepository->findOneWhere([ + 'product_id' => $product->id, + 'attribute_id' => 30, + ]); + + + $app_env = config("app.env"); + + $faqItems = $redis->hgetall($this->faq_cache_key); + ksort($faqItems); + $comments = $redis->hgetall($this->cache_prefix_key."product_comments_".$product['id']); + //获取 paypal smart key + $paypal_client_id = core()->getConfigData('sales.payment_methods.paypal_smart_button.client_id'); + + //支持的区域 + $countries = config("countries"); + + $default_country = config('onebuy.default_country'); + + $airwallex_method = config('onebuy.airwallex.method'); + + $payments = config('onebuy.payments'); // config the payments status + + $payments_default = config('onebuy.payments_default'); + $brand = config('onebuy.brand'); + + $gtag = config('onebuy.gtag'); + + $fb_ids = config('onebuy.fb_ids'); + $ob_adv_id = config('onebuy.ob_adv_id'); + + $crm_channel = config('onebuy.crm_channel'); + + $quora_adv_id = config('onebuy.quora_adv_id'); + + // get product reviews + + // products display image + $product_image_lists = Cache::get("product_image_lists_".$product['id']); + + $version = Cache::get("onebuy_".$product['sku']); + + //var_dump($version); + //var_dump($product_image_lists); + + + + return view('onebuy::product-detail-v5', compact('gtag', + 'app_env', + 'product', + 'package_products', + 'product_attributes', + 'skus', + 'productBgAttribute', + 'productBgAttribute_mobile', + 'faqItems', + 'comments', + 'paypal_client_id', + 'default_country', + 'airwallex_method', + 'payments', + 'payments_default', + 'brand', + 'fb_ids', + 'ob_adv_id', + 'crm_channel', + 'refer', + 'version', + 'product_image_lists', + 'quora_adv_id' + )); + + //return view('shop::products.view', compact('product')); default shop view + } + + public function success($order_id, Request $request) { + + $refer = $request->input("refer"); + + if(!empty($refer)) { + $request->session()->put('refer', $refer); + }else{ + $refer = $request->session()->get('refer'); + } + + $order = []; + + $payment_airwallex_vault = 0; //airwallex vault + $payment_paypal_vault = 0; // paypal vault + + $orderTrans = $this->orderTransactionRepository->where('transaction_id', $order_id)->select(['order_id','data'])->first(); + if(!is_null($orderTrans)) { + $order = $this->orderRepository->findOrFail($orderTrans->order_id); + $payment_paypal_vault = 1; + }else{ + $order = $this->orderRepository->findOrFail($order_id); + $orderTrans = $this->orderTransactionRepository->where('order_id', $order_id)->first(); + $payment_airwallex_vault = 1; + } + + $orderTransData = @json_decode($orderTrans->data, JSON_OBJECT_AS_ARRAY); + + $paypal_vault_id = ""; + + //var_dump($orderTransData['vault']);exit; + + if(isset($orderTransData['vault'])) { + $paypal_vault_id = $orderTransData['vault']['id']; + } + + + $fb_ids = config('onebuy.fb_ids'); + $ob_adv_id = config('onebuy.ob_adv_id'); + $crm_channel = config('onebuy.crm_channel'); + $gtag = config('onebuy.gtag'); + + $quora_adv_id = config('onebuy.quora_adv_id'); + + $countries = config("countries"); + + + $app_env = config("app.env"); + + $default_country = config('onebuy.default_country'); + $order_pre = config('shopify.order_pre'); + + $recommend_products = []; + + $paypal_id_token = $request->session()->get('paypal_id_token'); + $paypal_access_token = $request->session()->get('paypal_access_token'); + + //var_dump($paypal_access_token, $paypal_id_token); + + if(empty($paypal_id_token)) { + $paypal_id_token = $this->smartButton->getIDAccessToken(); + $paypal_access_token = $paypal_id_token->result->access_token; + $paypal_id_token = $paypal_id_token->result->id_token; + + + + $request->session()->put('paypal_id_token', $paypal_id_token); + $request->session()->put('paypal_access_token', $paypal_access_token); + } + + + $paypal_pay_acc = core()->getConfigData('sales.payment_methods.paypal_smart_button.client_id'); + $app_env = config("app.env"); + + // airwallex customer id from redis + $airwallex_customer_id = Redis::get("airwallex_customer_".$order->id); + + $payment_return_url = config('app.url')."/onebuy/checkout/v5/success/".$order->id; + $payment_cancel_url = config('app.url')."/onebuy/checkout/v5/success/".$order->id; + + + + return view('onebuy::checkout-success-v5', compact('order', + "fb_ids", + "ob_adv_id", + "crm_channel", + "refer", + "gtag", + "quora_adv_id", + "countries", + "default_country", + "app_env", + "order_pre", + "paypal_id_token", + "paypal_pay_acc", + "payment_airwallex_vault", + "payment_paypal_vault", + "paypal_vault_id", + "orderTrans", + "airwallex_customer_id", + "payment_return_url", + "payment_cancel_url", + "recommend_products" + )); + } + + + +} \ No newline at end of file