如何为Paypal付款仅添加一种货币

时间:2019-05-26 13:58:43

标签: php mysql paypal yii2

我正在将PayPal与我的一个网站集成在一起,但问题是他们希望印度客户仅以INR进行付款,但这实际上使想法变得复杂。

我将PayPal集成到我的网站中的方式来自于此处描述的问题。

https://forum.yiiframework.com/t/yii2-paypal-integration/126032

但是,当印度客户尝试使用美元付款时,付款失败了,如何避免这个问题,我只希望在我的网站中实现一种货币

2 个答案:

答案 0 :(得分:1)

看看Dotplant2,它在后端使用Yii2和货币换算表。特别是这些文件。

  1. 组件/付款/ PaypalPayment.php和2. CurrencyHelper.php在模块/商店/帮助者处。这是一些代码片段。

PaypalPayment

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,
        ]);
    }

CurrencyHelper

<?php
namespace app\modules\shop\helpers;

use app\modules\shop\models\Currency;
use app\modules\shop\models\UserPreferences;

class CurrencyHelper
{
    /**
     * @var Currency $userCurrency
     * @var Currency $mainCurrency
     */
    static protected $userCurrency = null;
    static protected $mainCurrency = null;

    /**
     * @return Currency
     */
    static public function getUserCurrency()
    {
        if (null === static::$userCurrency) {
            static::$userCurrency = static::findCurrencyByIso(UserPreferences::preferences()->userCurrency);
        }

        return static::$userCurrency;
    }

    /**
     * @param Currency $userCurrency
     * @return Currency
     */
    static public function setUserCurrency(Currency $userCurrency)
    {
        return static::$userCurrency = $userCurrency;
    }

    /**
     * @return Currency
     */
    static public function getMainCurrency()
    {
        return null === static::$mainCurrency
            ? static::$mainCurrency = Currency::getMainCurrency()
            : static::$mainCurrency;
    }

    /**
     * @param string $code
     * @param bool|true $useMainCurrency
     * @return Currency
     */
    static public function findCurrencyByIso($code)
    {
        $currency = Currency::find()->where(['iso_code' => $code])->one();
        $currency = null === $currency ? static::getMainCurrency() : $currency;
        return $currency;
    }

    /**
     * @param float|int $input
     * @param Currency $from
     * @param Currency $to
     * @return float|int
     */
    static public function convertCurrencies($input = 0, Currency $from, Currency $to)
    {
        if (0 === $input) {
            return $input;
        }

        if ($from->id !== $to->id) {
            $main = static::getMainCurrency();
            if ($main->id === $from->id && $main->id !== $to->id) {
                $input = $input / $to->convert_rate * $to->convert_nominal;
            } elseif ($main->id !== $from->id && $main->id === $to->id) {
                $input = $input / $from->convert_nominal * $from->convert_rate;
            } else {
                $input = $input / $from->convert_nominal * $from->convert_rate;
                $input = $input / $to->convert_rate * $to->convert_nominal;
            }
        }

        return $to->formatWithoutFormatString($input);
    }

    /**
     * @param float|int $input
     * @param Currency $from
     * @return float|int
     */
    static public function convertToUserCurrency($input = 0, Currency $from)
    {
        return static::convertCurrencies($input, $from, static::getUserCurrency());
    }

    /**
     * @param float|int $input
     * @param Currency $from
     * @return float|int
     */
    static public function convertToMainCurrency($input = 0, Currency $from)
    {
        return static::convertCurrencies($input, $from, static::getMainCurrency());
    }

    /**
     * @param float|int $input
     * @param Currency $to
     * @return float|int
     */
    static public function convertFromMainCurrency($input = 0, Currency $to)
    {
        return static::convertCurrencies($input, static::getMainCurrency(), $to);
    }

    /**
     * @param Currency $currency
     * @param string|null $locale
     * @return string
     */
    static public function getCurrencySymbol(Currency $currency, $locale = null)
    {
        $locale = null === $locale ? \Yii::$app->language : $locale;

        $result = '';
        try {
            $fake = $locale . '@currency=' . $currency->iso_code;
            $fmt = new \NumberFormatter($fake, \NumberFormatter::CURRENCY);
            $result = $fmt->getSymbol(\NumberFormatter::CURRENCY_SYMBOL);
        } catch (\Exception $e) {
            $result = preg_replace('%[\d\s,]%i', '', $currency->format(0));
        }

        return $result;
    }
}

答案 1 :(得分:0)

我是从PayPal支持部门获得的:

  

不幸的是,国内交易不能以美元接收,类似地,国际交易应该以美元进行。两种交易都无法使用单一货币。谢谢和问候,

相关问题