magento paypal多货币支付 快速支付

通过清心醉

magento paypal多货币支付 快速支付

以下为快速支付方式,其实和标准支付差不多,只是多了些修改:

这个功能的修改涉及到5个核心文件

先来说下需要修改的几个文件路径

app/code/core/Mage/Paypal/Model/Express.php

app/code/core/Mage/Paypal/Model/Cart.php

app/code/core/Mage/Paypal/Model/Express/Checkout.php

app/code/core/Mage/Sales/Model/Order/Payment.php

app/code/core/Mage/Core/Model/Store.php

主要要修改什么 先在这里说明一下,大家都知道paypal是可以支付多种货币的,关于这点可以下载官网的示例来看,想要改变支付的货币种类 要修改的一个是传给paypal的参数也就是货币符号,另外一个就是价格,虽然magento前台页面以及购物车看到的是日元,关键在于再给paypal传产品价格的时候用的是基础货币,而不是当前货币换算出来的结果,下面我就来发出关键代码,为有需要的同学提供方便

app/code/core/Mage/Paypal/Model/Express.php

/**
* Order payment
*
* @param Mage_Sales_Model_Order_Payment $payment
* @param float $amount
* @return Mage_Paypal_Model_Express
*/
public function order(Varien_Object $payment, $amount)
{
<span style=”white-space:pre”> </span>…
//修改 :去掉getBaseCurrentCurrency() 中的Base
$formatedPrice = $order->getCurrentCurrency()->formatTxt($amount);
<span style=”white-space:pre”> </span>…

/**
* Capture payment
*
* @param Mage_Sales_Model_Order_Payment $payment
* @param float $amount
* @return Mage_Paypal_Model_Express
*/
public function capture(Varien_Object $payment, $amount)
{
n style=”white-space:pre”> </span>…
$formatedPrice = $order->getCurrentCurrency()->formatTxt($amount);//修改

n style=”white-space:pre”> </span>…

/**
* Place an order with authorization or capture action
*
* @param Mage_Sales_Model_Order_Payment $payment
* @param float $amount
* @return Mage_Paypal_Model_Express
*/
protected function _placeOrder(Mage_Sales_Model_Order_Payment $payment, $amount)
{
$order = $payment->getOrder();

// prepare api call
$token = $payment->getAdditionalInformation(Mage_Paypal_Model_Express_Checkout::PAYMENT_INFO_TRANSPORT_TOKEN);
$api = $this->_pro->getApi()
->setToken($token)
->setPayerId($payment->
getAdditionalInformation(Mage_Paypal_Model_Express_Checkout::PAYMENT_INFO_TRANSPORT_PAYER_ID))
->setAmount($amount)
->setPaymentAction($this->_pro->getConfig()->paymentAction)
->setNotifyUrl(Mage::getUrl(‘paypal/ipn/’))
->setInvNum($order->getIncrementId())
->setCurrencyCode(Mage::app()->getStore()->getCurrentCurrencyCode())//修改
->setPaypalCart(Mage::getModel(‘paypal/cart’, array($order)))
->setIsLineItemsEnabled($this->_pro->getConfig()->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;
}

/**
* Call DoAuthorize
*
* @param int $amount
* @param Varien_Object $payment
* @param string $parentTransactionId
* @return Mage_Paypal_Model_Api_Abstract
*/
protected function _callDoAuthorize($amount, $payment, $parentTransactionId)
{
$api = $this->_pro->resetApi()->getApi()
->setAmount($amount)
->setCurrencyCode(Mage::app()->getStore()->getCurrentCurrencyCode())//修改
->setTransactionId($parentTransactionId)
->callDoAuthorization();

$payment->setAdditionalInformation($this->_authorizationCountKey,
$payment->getAdditionalInformation($this->_authorizationCountKey) + 1
);

return $api;
}
app/code/core/Mage/Paypal/Model/Cart.php
/**
* (re)Render all items and totals
*/
protected function _render()
{
if (!$this->_shouldRender) {
return;
}

// regular items from the sales entity
$this->_items = array();
foreach ($this->_salesEntity->getAllItems() as $item) {
if (!$item->getParentItem()) {
$this->_addRegularItem($item);
}
}
end($this->_items);
$lastRegularItemKey = key($this->_items);

// regular totals
$shippingDescription = ”;
if ($this->_salesEntity instanceof Mage_Sales_Model_Order) {
$shippingDescription = $this->_salesEntity->getShippingDescription();
$this->_totals = array(
self::TOTAL_SUBTOTAL => $this->_salesEntity->getSubtotal(),//修改
self::TOTAL_TAX => $this->_salesEntity->getTaxAmount(),//修改
self::TOTAL_SHIPPING => $this->_salesEntity->getShippingAmount(),//修改
self::TOTAL_DISCOUNT => abs($this->_salesEntity->getDiscountAmount()),//修改
);
$this->_applyHiddenTaxWorkaround($this->_salesEntity);
} else {
$address = $this->_salesEntity->getIsVirtual() ?
$this->_salesEntity->getBillingAddress() : $this->_salesEntity->getShippingAddress();
$shippingDescription = $address->getShippingDescription();
$this->_totals = array (
self::TOTAL_SUBTOTAL => $this->_salesEntity->getSubtotal(),//修改
self::TOTAL_TAX => $address->getTaxAmount(),//修改
self::TOTAL_SHIPPING => $address->getShippingAmount(),//修改
self::TOTAL_DISCOUNT => abs($address->getDiscountAmount()),//修改
);
$this->_applyHiddenTaxWorkaround($address);
}
$originalDiscount = $this->_totals[self::TOTAL_DISCOUNT];
/**
* Check the line items and totals according to PayPal business logic limitations
*/
protected function _validate()
{
$this->_areItemsValid = false;
$this->_areTotalsValid = false;

$referenceAmount = $this->_salesEntity->getGrandTotal();//修改

$itemsSubtotal = 0;
foreach ($this->_items as $i) {
$itemsSubtotal = $itemsSubtotal + $i[‘qty’] * $i[‘amount’];
}
$sum = $itemsSubtotal + $this->_totals[self::TOTAL_TAX];
if (!$this->_isShippingAsItem) {
$sum += $this->_totals[self::TOTAL_SHIPPING];
}
if (!$this->_isDiscountAsItem) {
$sum -= $this->_totals[self::TOTAL_DISCOUNT];
}

/**
* Add a usual line item with amount and qty
*
* @param Varien_Object $salesItem
* @return Varien_Object
*/
protected function _addRegularItem(Varien_Object $salesItem)
{
if ($this->_salesEntity instanceof Mage_Sales_Model_Order) {
$qty = (int) $salesItem->getQtyOrdered();
$amount = (float) $salesItem->getPrice();//修改
// TODO: nominal item for order
} else {
$qty = (int) $salesItem->getTotalQty();
$amount = $salesItem->isNominal() ? 0 : (float) $salesItem->getCalculationPrice();//修改
}
// workaround in case if item subtotal precision is not compatible with PayPal (.2)
$subAggregatedLabel = ”;
if ($amount – round($amount, 2)) {
$amount = $amount * $qty;
$subAggregatedLabel = ‘ x’ . $qty;
$qty = 1;
}

// aggregate item price if item qty * price does not match row total
if (($amount * $qty) != $salesItem->getRowTotal()) {//修改
$amount = (float) $salesItem->getRowTotal();//修改
$subAggregatedLabel = ‘ x’ . $qty;
$qty = 1;
}

return $this->addItem($salesItem->getName() . $subAggregatedLabel, $qty, $amount, $salesItem->getSku());
}

/**
* Add “hidden” discount and shipping tax
*
* Go ahead, try to understand ]:->
*
* Tax settings for getting “discount tax”:
* – Catalog Prices = Including Tax
* – Apply Customer Tax = After Discount
* – Apply Discount on Prices = Including Tax
*
* Test case for getting “hidden shipping tax”:
* – Make sure shipping is taxable (set shipping tax class)
* – Catalog Prices = Including Tax
* – Shipping Prices = Including Tax
* – Apply Customer Tax = After Discount
* – Create a shopping cart price rule with % discount applied to the Shipping Amount
* – run shopping cart and estimate shipping
* – go to PayPal
*
* @param Mage_Core_Model_Abstract $salesEntity
*/
private function _applyHiddenTaxWorkaround($salesEntity)
{
$this->_totals[self::TOTAL_TAX] += (float)$salesEntity->getHiddenTaxAmount();//修改
$this->_totals[self::TOTAL_TAX] += (float)$salesEntity->getShippingHiddenTaxAmount();//修改
}
app/code/core/Mage/Paypal/Model/Express/Checkout.php
/**
* Checkout with PayPal image URL getter
* Spares API calls of getting “pal” variable, by putting it into cache per store view
* @return string
*/
public function getCheckoutShortcutImageUrl()
{
// get “pal” thing from cache or lookup it via API
$pal = null;
if ($this->_config->areButtonsDynamic()) {
$cacheId = self::PAL_CACHE_ID . Mage::app()->getStore()->getId();
$pal = Mage::app()->loadCache($cacheId);
if (-1 == $pal) {
$pal = null;
} elseif (!$pal) {
$pal = null;
$this->_getApi();
try {
$this->_api->callGetPalDetails();
$pal = $this->_api->getPal();
Mage::app()->saveCache($pal, $cacheId, array(Mage_Core_Model_Config::CACHE_TAG));
} catch (Exception $e) {
Mage::app()->saveCache(-1, $cacheId, array(Mage_Core_Model_Config::CACHE_TAG));
Mage::logException($e);
}
}
}

return $this->_config->getExpressCheckoutShortcutImageUrl(
Mage::app()->getLocale()->getLocaleCode(),
$this->_quote->getGrandTotal(),//修改去掉base
$pal
);
}

/**
* Reserve order ID for specified quote and start checkout on PayPal
* @return string
*/
public function start($returnUrl, $cancelUrl)
{
$this->_quote->collectTotals();

if (!$this->_quote->getGrandTotal() && !$this->_quote->hasNominalItems()) {
Mage::throwException(Mage::helper(‘paypal’)->__(‘PayPal does not support processing orders with zero amount. To complete your purchase, proceed to the standard checkout process.’));
}

$this->_quote->reserveOrderId()->save();
// prepare API
$this->_getApi();
$this->_api->setAmount($this->_quote->getGrandTotal())//修改去掉base
//->setCurrencyCode($this->_quote->getBaseCurrencyCode())///修改为当前货币code
->setCurrencyCode(Mage::app()->getStore()->getCurrentCurrencyCode())
->setInvNum($this->_quote->getReservedOrderId())
->setReturnUrl($returnUrl)
->setCancelUrl($cancelUrl)
->setSolutionType($this->_config->solutionType)
->setPaymentAction($this->_config->paymentAction)
app/code/core/Mage/Sales/Model/Order/Payment.php
/**
* Capture the payment online
* Requires an invoice. If there is no invoice specified, will automatically prepare an invoice for order
* Updates transactions hierarchy, if required
* Updates payment totals, updates order status and adds proper comments
*
* TODO: eliminate logic duplication with registerCaptureNotification()
*
* @return Mage_Sales_Model_Order_Payment
* @throws Mage_Core_Exception
*/
public function capture($invoice)
{
if (is_null($invoice)) {
$invoice = $this->_invoice();
$this->setCreatedInvoice($invoice);
return $this; // @see Mage_Sales_Model_Order_Invoice::capture()
}
$amountToCapture = $this->_formatAmount($invoice->getGrandTotal());//修改
$order = $this->getOrder();

/**
* Decide whether authorization transaction may close (if the amount to capture will cover entire order)
* @param float $amountToCapture
* @return bool
*/
protected function _isCaptureFinal($amountToCapture)
{
$amountToCapture = $this->_formatAmount($amountToCapture, true);
$orderGrandTotal = $this->_formatAmount($this->getOrder()->getGrandTotal(), true);//修改
if ($orderGrandTotal == $this->_formatAmount($this->getAmountPaid(), true) + $amountToCapture)//修改

{

if (false !== $this->getShouldCloseParentTransaction()) {
$this->setShouldCloseParentTransaction(true);
}
return true;
}
return false;
}
app/code/core/Mage/Core/Model/Store.php

/**
* Round price
*
* @param mixed $price
* @return double
*/
public function roundPrice($price)
{ //类的内容修改为以下: JYP是使用计价货币
if(Mage::app()->getStore()->getCurrentCurrencyCode()==’JPY’){
return round($price,0);
}else{
return round($price, 2);
}
}

/**
* Format price with currency filter (taking rate into consideration)
*
* @param double $price
* @param bool $includeContainer
* @return string
*/
public function formatPrice($price, $includeContainer = true)
{  //重写该类的方法.
if ($this->getCurrentCurrency()) {
if(Mage::app()->getStore()->getCurrentCurrencyCode()==’JPY’){
return $this->getCurrentCurrency()->format($price, array(‘precision’=>0), $includeContainer);
}else{
return $this->getCurrentCurrency()->format($price, array(), $includeContainer);
}
}
return $price;
}

关于作者

清心醉 administrator

发表评论

如果喜欢作者的文章,您可以打赏给作者:

TRC20(虚拟货币):


ERC20(虚拟货币):


Bitcoin(BTC):