如何只添加一种货币进行贝宝付款
Posted
技术标签:
【中文标题】如何只添加一种货币进行贝宝付款【英文标题】:How to add only one currency for paypal payment 【发布时间】:2019-10-12 07:50:24 【问题描述】:我正在将 PayPal 与我的一个网站集成,但问题是他们希望印度客户只用 INR 付款,但这实际上使事情变得复杂。
我在我的网站中集成 PayPal 的方式取自此处描述的问题。
https://forum.yiiframework.com/t/yii2-paypal-integration/126032
但是当印度客户尝试使用美元付款时,付款失败我该如何避免这个问题我只想在我的网站中实现一种货币
【问题讨论】:
这个问题太笼统了,你必须首先在你的贝宝账户中启用多币种设置,你的账户应该设置为银行账户阅读这个线程forum.manager.io/t/multi-currency-paypal-account-issue/14975 我从贝宝支持Unfortunately domestic transaction is not possible to receive in USD and similarly international transaction should be in USD. There is no way to have single currency for both the transactions. Thanks and regards,
得到这个
【参考方案1】:
我从 PayPal 支持人员那里得到了这个:
很遗憾,国内交易无法以美元进行,同样的国际交易也应该以美元进行。两种交易都无法使用单一货币。谢谢和问候,
【讨论】:
【参考方案2】:看看 Dotplant2,它在后端使用 Yii2 和货币换算表。特别是这些文件。
-
components/payment/PaypalPayment.php 和 2. 他们的 CurrencyHelper.php 在 modules/shop/helpers。下面是一些代码 sn-ps。
贝宝支付
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;
【讨论】:
以上是关于如何只添加一种货币进行贝宝付款的主要内容,如果未能解决你的问题,请参考以下文章