为了账号安全,请及时绑定邮箱和手机立即绑定

如何仅添加一种货币进行贝宝付款

如何仅添加一种货币进行贝宝付款

PHP
萧十郎 2021-06-29 04:59:38
我正在将 PayPal 与我的一个网站集成,但问题是他们希望印度客户仅以印度卢比付款,但这实际上使事情变得复杂。我在我的网站中集成 PayPal 的方式取自这里描述的问题。https://forum.yiiframework.com/t/yii2-paypal-integration/126032但是,当印度客户尝试使用美元付款时,付款失败了我该如何避免这个问题我只想在我的网站中实现一种货币
查看完整描述

2 回答

?
森林海

TA贡献2011条经验 获得超2个赞

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


components/payment/ PaypalPayment.php 和 2. 他们的CurrencyHelper.php,位于 modules/shop/helpers。下面是一些代码片段。

贝宝支付


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,

        ]);

    }

货币助手


<?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;

    }

}

查看完整回答
反对 回复 2021-07-09
?
繁星点点滴滴

TA贡献1803条经验 获得超3个赞

我从贝宝支持那里得到了这个:

不幸的是,国内交易无法以美元接收,同样的国际交易应以美元接收。两种交易都无法使用单一货币。感谢致敬,


查看完整回答
反对 回复 2021-07-09
  • 2 回答
  • 0 关注
  • 143 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信