/** * Fetch quote totals data * * @param Quote $quote * @return array */ public function map(Quote $quote) { $totals = [Totals::BASE_GRAND_TOTAL => $quote->getBaseGrandTotal(), Totals::GRAND_TOTAL => $quote->getGrandTotal(), Totals::BASE_SUBTOTAL => $quote->getBaseSubtotal(), Totals::SUBTOTAL => $quote->getSubtotal(), Totals::BASE_SUBTOTAL_WITH_DISCOUNT => $quote->getBaseSubtotalWithDiscount(), Totals::SUBTOTAL_WITH_DISCOUNT => $quote->getSubtotalWithDiscount(), Totals::BASE_CURRENCY_CODE => $quote->getBaseCurrencyCode(), Totals::QUOTE_CURRENCY_CODE => $quote->getQuoteCurrencyCode()]; $shippingAddress = $quote->getShippingAddress(); $totals[Totals::DISCOUNT_AMOUNT] = $shippingAddress->getDiscountAmount(); $totals[Totals::BASE_DISCOUNT_AMOUNT] = $shippingAddress->getBaseDiscountAmount(); $totals[Totals::SHIPPING_AMOUNT] = $shippingAddress->getShippingAmount(); $totals[Totals::BASE_SHIPPING_AMOUNT] = $shippingAddress->getBaseShippingAmount(); $totals[Totals::SHIPPING_DISCOUNT_AMOUNT] = $shippingAddress->getShippingDiscountAmount(); $totals[Totals::BASE_SHIPPING_DISCOUNT_AMOUNT] = $shippingAddress->getBaseShippingDiscountAmount(); $totals[Totals::TAX_AMOUNT] = $shippingAddress->getTaxAmount(); $totals[Totals::BASE_TAX_AMOUNT] = $shippingAddress->getBaseTaxAmount(); $totals[Totals::SHIPPING_TAX_AMOUNT] = $shippingAddress->getShippingTaxAmount(); $totals[Totals::BASE_SHIPPING_TAX_AMOUNT] = $shippingAddress->getBaseShippingTaxAmount(); $totals[Totals::SUBTOTAL_INCL_TAX] = $shippingAddress->getSubtotalInclTax(); $totals[Totals::BASE_SUBTOTAL_INCL_TAX] = $shippingAddress->getBaseSubtotalTotalInclTax(); $totals[Totals::SHIPPING_INCL_TAX] = $shippingAddress->getShippingInclTax(); $totals[Totals::BASE_SHIPPING_INCL_TAX] = $shippingAddress->getBaseShippingInclTax(); return $totals; }
/** * Reserve order ID for specified quote and start checkout on PayPal * * @param string $returnUrl * @param string $cancelUrl * @param bool|null $button * @return string * @throws \Magento\Framework\Model\Exception */ public function start($returnUrl, $cancelUrl, $button = null) { $this->_quote->collectTotals(); if (!$this->_quote->getGrandTotal() && !$this->_quote->hasNominalItems()) { throw new \Magento\Framework\Model\Exception(__('PayPal can\'t process orders with a zero balance due. ' . 'To finish your purchase, please go through the standard checkout process.')); } $this->_quote->reserveOrderId()->save(); // prepare API $this->_getApi(); $solutionType = $this->_config->getMerchantCountry() == 'DE' ? \Magento\Paypal\Model\Config::EC_SOLUTION_TYPE_MARK : $this->_config->getConfigValue('solutionType'); $this->_api->setAmount($this->_quote->getBaseGrandTotal())->setCurrencyCode($this->_quote->getBaseCurrencyCode())->setInvNum($this->_quote->getReservedOrderId())->setReturnUrl($returnUrl)->setCancelUrl($cancelUrl)->setSolutionType($solutionType)->setPaymentAction($this->_config->getConfigValue('paymentAction')); if ($this->_giropayUrls) { list($successUrl, $cancelUrl, $pendingUrl) = $this->_giropayUrls; $this->_api->addData(['giropay_cancel_url' => $cancelUrl, 'giropay_success_url' => $successUrl, 'giropay_bank_txn_pending_url' => $pendingUrl]); } if ($this->_isBml) { $this->_api->setFundingSource('BML'); } $this->_setBillingAgreementRequest(); if ($this->_config->getConfigValue('requireBillingAddress') == PaypalConfig::REQUIRE_BILLING_ADDRESS_ALL) { $this->_api->setRequireBillingAddress(1); } // suppress or export shipping address if ($this->_quote->getIsVirtual()) { if ($this->_config->getConfigValue('requireBillingAddress') == PaypalConfig::REQUIRE_BILLING_ADDRESS_VIRTUAL) { $this->_api->setRequireBillingAddress(1); } $this->_api->setSuppressShipping(true); } else { $address = $this->_quote->getShippingAddress(); $isOverridden = 0; if (true === $address->validate()) { $isOverridden = 1; $this->_api->setAddress($address); } $this->_quote->getPayment()->setAdditionalInformation(self::PAYMENT_INFO_TRANSPORT_SHIPPING_OVERRIDDEN, $isOverridden); $this->_quote->getPayment()->save(); } // add line items /** @var $cart \Magento\Payment\Model\Cart */ $cart = $this->_cartFactory->create(array('salesModel' => $this->_quote)); $this->_api->setPaypalCart($cart)->setIsLineItemsEnabled($this->_config->getConfigValue('lineItemsEnabled')); // add shipping options if needed and line items are available $cartItems = $cart->getAllItems(); if ($this->_config->getConfigValue('lineItemsEnabled') && $this->_config->getConfigValue('transferShippingOptions') && !empty($cartItems)) { if (!$this->_quote->getIsVirtual() && !$this->_quote->hasNominalItems()) { $options = $this->_prepareShippingOptions($address, true); if ($options) { $this->_api->setShippingOptionsCallbackUrl($this->_coreUrl->getUrl('*/*/shippingOptionsCallback', ['quote_id' => $this->_quote->getId()]))->setShippingOptions($options); } } } $this->_config->exportExpressCheckoutStyleSettings($this->_api); /* Temporary solution. @TODO: do not pass quote into Nvp model */ $this->_api->setQuote($this->_quote); $this->_api->callSetExpressCheckout(); $token = $this->_api->getToken(); $this->_redirectUrl = $button ? $this->_config->getExpressCheckoutStartUrl($token) : $this->_config->getPayPalBasicStartUrl($token); $payment = $this->_quote->getPayment(); $payment->unsAdditionalInformation(self::PAYMENT_INFO_TRANSPORT_BILLING_AGREEMENT); // Set flag that we came from Express Checkout button if (!empty($button)) { $payment->setAdditionalInformation(self::PAYMENT_INFO_BUTTON, 1); } elseif ($payment->hasAdditionalInformation(self::PAYMENT_INFO_BUTTON)) { $payment->unsAdditionalInformation(self::PAYMENT_INFO_BUTTON); } $payment->save(); return $token; }
/** * Send email id payment was failed * * @param \Magento\Sales\Model\Quote $checkout * @param string $message * @param string $checkoutType * @return $this */ public function sendPaymentFailedEmail($checkout, $message, $checkoutType = 'onepage') { $this->inlineTranslation->suspend(); $template = $this->_scopeConfig->getValue('checkout/payment_failed/template', \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $checkout->getStoreId()); $copyTo = $this->_getEmails('checkout/payment_failed/copy_to', $checkout->getStoreId()); $copyMethod = $this->_scopeConfig->getValue('checkout/payment_failed/copy_method', \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $checkout->getStoreId()); $bcc = array(); if ($copyTo && $copyMethod == 'bcc') { $bcc = $copyTo; } $_receiver = $this->_scopeConfig->getValue('checkout/payment_failed/receiver', \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $checkout->getStoreId()); $sendTo = array(array('email' => $this->_scopeConfig->getValue('trans_email/ident_' . $_receiver . '/email', \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $checkout->getStoreId()), 'name' => $this->_scopeConfig->getValue('trans_email/ident_' . $_receiver . '/name', \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $checkout->getStoreId()))); if ($copyTo && $copyMethod == 'copy') { foreach ($copyTo as $email) { $sendTo[] = array('email' => $email, 'name' => null); } } $shippingMethod = ''; if ($shippingInfo = $checkout->getShippingAddress()->getShippingMethod()) { $data = explode('_', $shippingInfo); $shippingMethod = $data[0]; } $paymentMethod = ''; if ($paymentInfo = $checkout->getPayment()) { $paymentMethod = $paymentInfo->getMethod(); } $items = ''; foreach ($checkout->getAllVisibleItems() as $_item) { /* @var $_item \Magento\Sales\Model\Quote\Item */ $items .= $_item->getProduct()->getName() . ' x ' . $_item->getQty() . ' ' . $checkout->getStoreCurrencyCode() . ' ' . $_item->getProduct()->getFinalPrice($_item->getQty()) . "\n"; } $total = $checkout->getStoreCurrencyCode() . ' ' . $checkout->getGrandTotal(); foreach ($sendTo as $recipient) { $transport = $this->_transportBuilder->setTemplateIdentifier($template)->setTemplateOptions(array('area' => \Magento\Framework\App\Area::AREA_FRONTEND, 'store' => $checkout->getStoreId()))->setTemplateVars(array('reason' => $message, 'checkoutType' => $checkoutType, 'dateAndTime' => $this->_localeDate->date(), 'customer' => $checkout->getCustomerFirstname() . ' ' . $checkout->getCustomerLastname(), 'customerEmail' => $checkout->getCustomerEmail(), 'billingAddress' => $checkout->getBillingAddress(), 'shippingAddress' => $checkout->getShippingAddress(), 'shippingMethod' => $this->_scopeConfig->getValue('carriers/' . $shippingMethod . '/title', \Magento\Store\Model\ScopeInterface::SCOPE_STORE), 'paymentMethod' => $this->_scopeConfig->getValue('payment/' . $paymentMethod . '/title', \Magento\Store\Model\ScopeInterface::SCOPE_STORE), 'items' => nl2br($items), 'total' => $total))->setFrom($this->_scopeConfig->getValue('checkout/payment_failed/identity', \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $checkout->getStoreId()))->addTo($recipient['email'], $recipient['name'])->addBcc($bcc)->getTransport(); $transport->sendMessage(); } $this->inlineTranslation->resume(); return $this; }