/** * @inheritdoc */ public function content() { /** @var Order $order */ $order = $this->order; $order->calculate(); $payer = (new Payer())->setPaymentMethod('paypal'); $priceSubTotal = 0; /** @var ItemList $itemList */ $itemList = array_reduce($order->items, function ($result, $item) use(&$priceSubTotal) { /** @var OrderItem $item */ /** @var Product $product */ $product = $item->product; $price = CurrencyHelper::convertFromMainCurrency($item->price_per_pcs, $this->currency); $priceSubTotal = $priceSubTotal + $price * $item->quantity; /** @var ItemList $result */ return $result->addItem((new Item())->setName($product->name)->setCurrency($this->currency->iso_code)->setPrice($price)->setQuantity($item->quantity)->setUrl(Url::toRoute(['@product', 'model' => $product, 'category_group_id' => $product->category->category_group_id], true))); }, new ItemList()); $priceTotal = CurrencyHelper::convertFromMainCurrency($order->total_price, $this->currency); $details = (new Details())->setShipping($priceTotal - $priceSubTotal)->setSubtotal($priceSubTotal)->setTax(0); $amount = (new Amount())->setCurrency($this->currency->iso_code)->setTotal($priceTotal)->setDetails($details); $transaction = (new Transaction())->setAmount($amount)->setItemList($itemList)->setDescription($this->transactionDescription)->setInvoiceNumber($this->transaction->id); $urls = (new RedirectUrls())->setReturnUrl($this->createResultUrl(['id' => $this->order->payment_type_id]))->setCancelUrl($this->createFailUrl()); $payment = (new Payment())->setIntent('sale')->setPayer($payer)->setTransactions([$transaction])->setRedirectUrls($urls); $link = null; try { $link = $payment->create($this->apiContext)->getApprovalLink(); } catch (\Exception $e) { $link = null; } return $this->render('paypal', ['order' => $order, 'transaction' => $this->transaction, 'approvalLink' => $link]); }
/** * @inheritdoc */ public function rules() { return [[['productListingSortId'], 'integer'], ['productListingSortId', 'default', 'value' => 1], ['productListingSortId', 'in', 'range' => array_keys(ProductListingSort::enabledSorts())], ['listViewType', 'default', 'value' => Yii::$app->getModule('shop')->listViewType], ['listViewType', 'in', 'range' => ['listView', 'blockView'], 'strict' => true], ['productsPerPage', 'default', 'value' => Yii::$app->getModule('shop')->productsPerPage], ['productsPerPage', 'integer', 'max' => 50], ['userCurrency', 'default', 'value' => CurrencyHelper::getMainCurrency()->iso_code], ['userCurrency', 'in', 'range' => Currency::getIsoCodes(), 'strict' => true]]; }
/** * */ public static function handlePurchase() { if (null === ($order = Order::getOrder())) { return; } /** @var Currency $currency */ $currency = static::$currency; $ya = ['action' => 'purchase', 'currency' => $currency->iso_code, 'orderId' => $order->id, 'products' => []]; foreach ($order->items as $item) { $ya['products'][] = ['id' => $item->product->id, 'name' => $item->product->name, 'category' => self::getCategories($item->product), 'price' => CurrencyHelper::convertCurrencies($item->product->price, $item->product->currency, $currency), 'quantity' => $item->quantity]; } $js = 'window.DotPlantParams = window.DotPlantParams || {};'; $js .= 'window.DotPlantParams.ecYandex = ' . Json::encode($ya) . ';'; \Yii::$app->getView()->registerJs($js, View::POS_BEGIN); }
protected static function getPrice(Product $model, $mainCurrency, $price) { return number_format(CurrencyHelper::convertCurrencies($price, $model->currency, $mainCurrency), 2, '.', '') . ' ' . $mainCurrency->iso_code; }
/** * @param OrderTransaction $transaction * @param Currency $currency * @return float|int */ protected function getTotalSumOrderTransaction(OrderTransaction $transaction, Currency $currency) { return CurrencyHelper::convertFromMainCurrency($transaction->total_sum, $currency); }
/** * @param YmlModel $config * @param Product $model * @return OfferTag|null */ private function offerSimplified(YmlModel $config, Product $model) { $offer = new OfferTag('offer', [], ['id' => $model->id, 'available' => 'true']); $price = static::getOfferValue($config, 'offer_price', $model, 0); $price = CurrencyHelper::convertCurrencies($price, $model->currency, $this->currency); if ($price <= 0 || $price >= 1000000000) { return null; } $values = []; $name = static::getOfferValue($config, 'offer_name', $model, null); if (true === empty($name)) { return null; } if (mb_strlen($name) > 120) { $name = mb_substr($name, 0, 120); $name = mb_substr($name, 0, mb_strrpos($name, ' ')); } $values[] = new OfferTag('name', htmlspecialchars(trim(strip_tags($name)))); $values[] = new OfferTag('price', $price); $values[] = new OfferTag('currencyId', $this->currency->iso_code); /** @var Category $category */ if (null === ($category = $model->category)) { return null; } $values[] = new OfferTag('categoryId', $category->id); $values[] = new OfferTag('url', Url::toRoute(['@product', 'model' => $model, 'category_group_id' => $category->category_group_id], true)); $picture = static::getOfferValue($config, 'offer_picture', $model, static::$_noImg); if (static::$_noImg !== $picture) { $picture = htmlspecialchars(trim($picture, '/')); $picture = implode('/', array_map('rawurlencode', explode('/', $picture))); $values[] = new OfferTag('picture', trim($config->shop_url, '/') . '/' . $picture); } $description = static::getOfferValue($config, 'offer_description', $model, null); if (false === empty($description)) { if (mb_strlen($description) > 175) { $description = mb_substr($description, 0, 175); $description = mb_substr($description, 0, mb_strrpos($description, ' ')); } $values[] = new OfferTag('description', htmlspecialchars(trim(strip_tags($description)))); } if (static::USE_OFFER_PARAM == $config->offer_param) { foreach (static::getOfferParams($model) as $k => $v) { $values[] = new OfferTag('param', $v['value'], ['name' => $k, 'unit' => $v['unit']]); } } return $offer->setValue($values); }
/** * @param array $products * @return array */ private function productsModelsToArray($products) { return array_reduce($products, function ($res, $item) { /** @var Product $model */ $model = $item['model']; $i = ['id' => $model->id, 'name' => $model->name, 'price' => CurrencyHelper::convertToMainCurrency($model->price, $model->currency), 'currency' => CurrencyHelper::getMainCurrency()->iso_code]; if (isset($item['quantity'])) { $i['quantity'] = $item['quantity']; } $res[] = $i; return $res; }, []); }
/** * @param Product $product * @param Order|null $order * @param SpecialPriceList $specialPrice * @param $price * @return float */ public static function getCurrencyPriceProduct(Product $product, Order $order = null, SpecialPriceList $specialPrice, $price) { return CurrencyHelper::convertToMainCurrency($price, $product->currency); }