Commit 5aa56a42 by Bartosz Kubicki

Init repo

parents
File added
<?php
namespace Meetanshi\Paymulti\Helper;
use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Helper\Context;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Store\Model\ScopeInterface;
use Magento\Store\Model\StoreManagerInterface;
/**
* Class Data
* @package Meetanshi\Paymulti\Helper
*/
class Data extends AbstractHelper
{
/**
*
*/
const MEETANSHI_MODULE_ENABLE = 'paymulti/general/active';
/**
* @var StoreManagerInterface
*/
protected $storeManager;
/**
* @var OrderInterface
*/
protected $order;
/**
* @var
*/
protected $extraPrice;
/**
* @var
*/
protected $itemPrice;
/**
* Data constructor.
* @param Context $context
* @param StoreManagerInterface $storeManager
* @param OrderInterface $order
*/
public function __construct(
Context $context,
StoreManagerInterface $storeManager,
OrderInterface $order
) {
$this->storeManager = $storeManager;
$this->order = $order;
parent::__construct($context);
}
/**
* @return array
*/
public static function getSupportedCurrency()
{
return ['AUD', 'CAD', 'CZK', 'DKK', 'EUR', 'HKD', 'HUF', 'ILS', 'JPY', 'MXN',
'NOK', 'NZD', 'PLN', 'GBP', 'SGD', 'SEK', 'CHF', 'USD', 'TWD', 'THB', 'INR'];
}
/**
* @return bool
*/
public static function shouldConvert()
{
return !self::isActive();
}
/**
* @return mixed
*/
public function isActive()
{
return $this->scopeConfig->getValue(self::MEETANSHI_MODULE_ENABLE, ScopeInterface::SCOPE_STORE);
}
/**
* @param $quote
* @return mixed
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function getConvertedGrandTotal($quote)
{
$toCurrency = $this->getCurrentCurrency();
$currentCurrency = $this->storeManager->getStore()->getCurrentCurrencyCode();
if ($toCurrency == $currentCurrency) {
return $quote->getGrandTotal();
} else {
return $this->getConvertedBaseAmount($quote->getBaseGrandTotal());
}
}
/**
* @return mixed
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function getCurrentCurrency()
{
return $this->storeManager->getStore()->getCurrentCurrency()->getCode();
}
/**
* @param $value
* @return mixed
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function getConvertedBaseAmount($value)
{
$baseCurrency = $this->storeManager->getStore()->getBaseCurrencyCode();
$currentCurrency = $this->getCurrentCurrency();
$amount = $this->convertCurrency($value, $baseCurrency, $currentCurrency);
return $amount;
}
/**
* @param $amountValue
* @param null $currencyCodeFrom
* @param null $currencyCodeTo
* @return mixed
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function convertCurrency($amountValue, $currencyCodeFrom = null, $currencyCodeTo = null)
{
return $this->storeManager->getStore()->getBaseCurrency()->convert($amountValue, $currencyCodeTo);
}
/**
* @return array
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function getCurrencyArray()
{
return [$this->storeManager->getStore()->getBaseCurrencyCode()];
}
/**
* @param $orderID
* @return mixed
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function getPaymentOrderCurrency($orderID)
{
$order = $this->order->load($orderID);
if ($order) {
$payment = $order->getPayment();
return $payment->getAdditionalInformation('payment_currency');
}
return $this->getCurrentCurrency();
}
/**
* @param $identifier
* @return mixed
*/
public function getConfig($identifier)
{
return $this->scopeConfig->getValue(
$identifier,
ScopeInterface::SCOPE_STORE
);
}
/**
* @param $key
* @param $value
*/
public function addExtraPrice($key, $value)
{
$this->extraPrice[$key] = $value;
}
/**
* @param $i
* @param $key
* @param $value
*/
public function addItemPrice($i, $key, $value)
{
$this->itemPrice[$i][$key] = $value;
}
/**
* @param array $request
* @return array
*/
public function convertRequest(array &$request)
{
$itemAmount = 0;
$extraprice = 0;
foreach ($this->itemPrice as $item) {
$itemAmount = $itemAmount + ((int)$item['qty'] * (float)$item['amount']);
}
foreach ($this->extraPrice as $key => $value) {
$extraprice = (float)$extraprice + (float)$value;
}
$baseprice = $extraprice + $itemAmount;
$request['AMT'] = number_format($baseprice,2);
$request['ITEMAMT'] = $itemAmount;
return $request;
}
}
<?php
namespace Meetanshi\Paymulti\Model;
use Magento\Paypal\Model\Express\Checkout as ExpressCheckout;
use Meetanshi\Paymulti\Helper\Data;
use Magento\Paypal\Model\Express as PaypalExpress;
/**
* Class Express
* @package Meetanshi\Paymulti\Model
*/
class Express extends PaypalExpress
{
/**
* @var Data
*/
protected $helper;
/**
* Express constructor.
* @param \Magento\Framework\Model\Context $context
* @param \Magento\Framework\Registry $registry
* @param \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory
* @param \Magento\Framework\Api\AttributeValueFactory $customAttributeFactory
* @param \Magento\Payment\Helper\Data $paymentData
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
* @param \Magento\Payment\Model\Method\Logger $logger
* @param \Magento\Paypal\Model\ProFactory $proFactory
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
* @param \Magento\Framework\UrlInterface $urlBuilder
* @param \Magento\Paypal\Model\CartFactory $cartFactory
* @param \Magento\Checkout\Model\Session $checkoutSession
* @param \Magento\Framework\Exception\LocalizedExceptionFactory $exception
* @param \Magento\Sales\Api\TransactionRepositoryInterface $transactionRepository
* @param \Magento\Sales\Model\Order\Payment\Transaction\BuilderInterface $transactionBuilder
* @param Data $helper
* @param array $data
*/
public function __construct(
\Magento\Framework\Model\Context $context,
\Magento\Framework\Registry $registry,
\Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory,
\Magento\Framework\Api\AttributeValueFactory $customAttributeFactory,
\Magento\Payment\Helper\Data $paymentData,
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
\Magento\Payment\Model\Method\Logger $logger,
\Magento\Paypal\Model\ProFactory $proFactory,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Framework\UrlInterface $urlBuilder,
\Magento\Paypal\Model\CartFactory $cartFactory,
\Magento\Checkout\Model\Session $checkoutSession,
\Magento\Framework\Exception\LocalizedExceptionFactory $exception,
\Magento\Sales\Api\TransactionRepositoryInterface $transactionRepository,
\Magento\Sales\Model\Order\Payment\Transaction\BuilderInterface $transactionBuilder,
Data $helper,
array $data = []
)
{
$this->helper = $helper;
parent::__construct($context, $registry, $extensionFactory, $customAttributeFactory, $paymentData, $scopeConfig, $logger, $proFactory, $storeManager, $urlBuilder, $cartFactory, $checkoutSession, $exception, $transactionRepository, $transactionBuilder, null, null, $data);
}
/**
* @param \Magento\Sales\Model\Order\Payment $payment
* @param float $amount
* @return $this|PaypalExpress
* @throws \Magento\Framework\Exception\LocalizedException
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
protected function _placeOrder(\Magento\Sales\Model\Order\Payment $payment, $amount)
{
$order = $payment->getOrder();
// prepare api call
$token = $payment->getAdditionalInformation(ExpressCheckout::PAYMENT_INFO_TRANSPORT_TOKEN);
$cart = $this->_cartFactory->create(['salesModel' => $order]);
if ($this->helper->isActive()) {
$amount = number_format($this->helper->getConvertedGrandTotal($order), 2);
$currencyCode = $this->helper->getCurrentCurrency();
} else {
$amount = $amount;
$currencyCode = $order->getBaseCurrencyCode();
}
$payment->setAdditionalInformation('payment_currency', $currencyCode);
$api = $this->getApi()->setToken(
$token
)->setPayerId(
$payment->getAdditionalInformation(ExpressCheckout::PAYMENT_INFO_TRANSPORT_PAYER_ID)
)->setAmount(
$amount
)->setPaymentAction(
$this->_pro->getConfig()->getValue('paymentAction')
)->setNotifyUrl(
$this->_urlBuilder->getUrl('paypal/ipn/')
)->setInvNum(
$order->getIncrementId()
)->setCurrencyCode(
$currencyCode
)->setPaypalCart(
$cart
)->setIsLineItemsEnabled(
$this->_pro->getConfig()->getValue('lineItemsEnabled')
);
if ($order->getIsVirtual()) {
$api->setAddress($order->getBillingAddress())->setSuppressShipping(true);
} else {
$api->setAddress($order->getShippingAddress());
$api->setBillingAddress($order->getBillingAddress());
}
// call api and get details from it
$api->callDoExpressCheckoutPayment();
$this->_importToPayment($api, $payment);
return $this;
}
}
<?php
namespace Meetanshi\Paymulti\Model\Paypal;
use Magento\Paypal\Model\Config as PayPalConfig;
/**
* Class Config
* @package Meetanshi\Paymulti\Model\Paypal
*/
class Config extends PayPalConfig
{
/**
* @var \Meetanshi\Paymulti\Helper\Data
*/
protected $helper;
/**
* @var array
*/
protected $_supportedCurrencyCodes = [
'AUD',
'CAD',
'CZK',
'DKK',
'EUR',
'HKD',
'HUF',
'ILS',
'JPY',
'MXN',
'NOK',
'NZD',
'PLN',
'GBP',
'RUB',
'SGD',
'SEK',
'CHF',
'TWD',
'THB',
'USD',
'INR',
];
/**
* Config constructor.
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
* @param \Magento\Directory\Helper\Data $directoryHelper
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
* @param \Magento\Payment\Model\Source\CctypeFactory $cctypeFactory
* @param \Magento\Paypal\Model\CertFactory $certFactory
* @param \Meetanshi\Paymulti\Helper\Data $helper
* @param array $params
*/
public function __construct(
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
\Magento\Directory\Helper\Data $directoryHelper,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Payment\Model\Source\CctypeFactory $cctypeFactory,
\Magento\Paypal\Model\CertFactory $certFactory,
\Meetanshi\Paymulti\Helper\Data $helper,
$params = []
) {
parent::__construct($scopeConfig, $directoryHelper, $storeManager, $cctypeFactory, $certFactory, $params);
$this->helper = $helper;
}
/**
* @param string $code
* @return bool
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function isCurrencyCodeSupported($code)
{
if ($this->helper->isActive()) {
$this->_supportedCurrencyCodes =
array_merge($this->_supportedCurrencyCodes, $this->helper->getCurrencyArray());
}
if (in_array($code, $this->_supportedCurrencyCodes)) {
return true;
}
if ($this->getMerchantCountry() == 'BR' && $code == 'BRL') {
return true;
}
if ($this->getMerchantCountry() == 'MY' && $code == 'MYR') {
return true;
}
if ($this->getMerchantCountry() == 'TR' && $code == 'TRY') {
return true;
}
return false;
}
}
<?php
namespace Meetanshi\Paymulti\Model\Source;
/**
* Class Currency
* @package Meetanshi\Paymulti\Model\Source
*/
class Currency extends \Magento\Config\Model\Config\Source\Locale\Currency
{
/**
* @var \Magento\Framework\Locale\ListsInterface
*/
protected $_localeLists;
/**
* @var \Magento\Store\Model\StoreManagerInterface
*/
protected $_currencySymbol;
/**
* @var \Meetanshi\Paymulti\Helper\Data
*/
protected $_helper;
/**
* Currency constructor.
* @param \Magento\Framework\Locale\ListsInterface $localeLists
* @param \Magento\Store\Model\StoreManagerInterface $currencySymbol
* @param \Meetanshi\Paymulti\Helper\Data $helper
*/
public function __construct(
\Magento\Framework\Locale\ListsInterface $localeLists,
\Magento\Store\Model\StoreManagerInterface $currencySymbol,
\Meetanshi\Paymulti\Helper\Data $helper
) {
$this->_localeLists = $localeLists;
$this->_currencySymbol = $currencySymbol;
$this->_helper = $helper;
parent::__construct($localeLists);
}
/**
* @return array
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function toOptionArray()
{
$_supportedCurrencyCodes = $this->_helper->getSupportedCurrency();
$_availableCurrencyCodes = $this->_currencySymbol->getStore()->getAvailableCurrencyCodes(true);
;
if (!$this->_options) {
$this->_options = $this->_localeLists->getOptionCurrencies();
}
$options = [];
foreach ($this->_options as $option) {
if (in_array($option['value'], $_supportedCurrencyCodes) && in_array($option['value'], $_availableCurrencyCodes)) {
$options[] = $option;
}
}
return $options;
}
}
{
"name": "meetanshi/magento2-paypal-multi-currency",
"description": "Magento 2 paypal multi currency",
"require": {
"php": "~5.5.0|~5.6.0|~7.0.0|7.0.2|7.0.4|~7.0.6|~7.1.0|~7.2.0|~7.3.0"
},
"type": "magento2-module",
"version": "1.0.4",
"license": [
"OSL-3.0"
],
"autoload": {
"files": [
"registration.php"
],
"psr-4": {
"Meetanshi\\Paymulti\\": ""
}
},
"support": {
"email": "support@meetanshi.com"
},
"authors": [
{
"name": "Jignesh Parmar",
"email": "support@meetanshi.com",
"homepage": "https://meetanshi.com"
}
]
}
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Acl/etc/acl.xsd">
<acl>
<resources>
<resource id="Magento_Backend::admin">
<resource id="Magento_Backend::stores">
<resource id="Magento_Backend::stores_settings">
<resource id="Magento_Config::config">
<resource id="Meetanshi_Paymulti::config_paymulti" title="Paypal Multi Currency" />
</resource>
</resource>
</resource>
</resource>
</resources>
</acl>
</config>
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
<router id="admin">
<route id="paymulti" frontName="paymulti">
<module name="Meetanshi_Paymulti" />
</route>
</router>
</config>
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
<system>
<tab id="meetanshi" translate="label" class="meetanshi" sortOrder="99998">
<label></label>
</tab>
<section id="paymulti" translate="label" type="text" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
<label>PayPal Multi Currency</label>
<tab>meetanshi</tab>
<resource>Meetanshi_Paymulti::config_paymulti</resource>
<group id="general" translate="label" type="text" sortOrder="100" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Configuration</label>
<field id="active" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
<label>PayPal Multi Currency</label>
<source_model>Magento\Config\Model\Config\Source\Enabledisable</source_model>
</field>
</group>
</section>
</system>
</config>
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
<default>
<paymulti>
<general>
<active>1</active>
</general>
</paymulti>
</default>
</config>
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
<preference for="Magento\Paypal\Model\Express\Checkout" type="Meetanshi\Paymulti\Model\Express\Checkout"/>
<preference for="Magento\Paypal\Model\Api\Nvp" type="Meetanshi\Paymulti\Model\Api\Nvp"/>
<preference for="Magento\Paypal\Model\Express" type="Meetanshi\Paymulti\Model\Express"/>
<preference for="Magento\Paypal\Model\Config" type="Meetanshi\Paymulti\Model\Paypal\Config" />
</config>
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Meetanshi_Paymulti" setup_version="1.0.4" schema_version="1.0.4">
</module>
</config>
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Meetanshi_Paymulti',
__DIR__
);
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<head>
<css src="Meetanshi_Paymulti::css/meetanshi.css"/>
</head>
</page>
.meetanshi .title:before {
background-image: url(https://meetanshi.com/media/logo.png);
background-size: 150px 19px;
display: inline-block;
width: 150px;
height: 19px;
content: "";
background-repeat: no-repeat;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment