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

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

我在我的网站中集成 PayPal 的方式取自这里描述的问题。

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

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


萧十郎
浏览 144回答 2
2回答

森林海

看看 Dotplant2,它在后端使用 Yii2 和货币转换表。特别是这些文件。components/payment/ PaypalPayment.php 和 2. 他们的CurrencyHelper.php,位于 modules/shop/helpers。下面是一些代码片段。贝宝支付public function content()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; /** @var Order $order */&nbsp; &nbsp; &nbsp; &nbsp; $order = $this->order;&nbsp; &nbsp; &nbsp; &nbsp; $order->calculate();&nbsp; &nbsp; &nbsp; &nbsp; $payer = (new Payer())->setPaymentMethod('paypal');&nbsp; &nbsp; &nbsp; &nbsp; $priceSubTotal = 0;&nbsp; &nbsp; &nbsp; &nbsp; /** @var ItemList $itemList */&nbsp; &nbsp; &nbsp; &nbsp; $itemList = array_reduce($order->items, function($result, $item) use (&$priceSubTotal) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /** @var OrderItem $item */&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /** @var Product $product */&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $product = $item->product;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $price = CurrencyHelper::convertFromMainCurrency($item->price_per_pcs, $this->currency);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $priceSubTotal = $priceSubTotal + ($price * $item->quantity);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /** @var ItemList $result */&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return $result->addItem(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (new Item())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->setName($product->name)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->setCurrency($this->currency->iso_code)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->setPrice($price)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->setQuantity($item->quantity)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->setUrl(Url::toRoute([&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '@product',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'model' => $product,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'category_group_id' => $product->category->category_group_id&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ], true))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; }, new ItemList());&nbsp; &nbsp; &nbsp; &nbsp; $priceTotal = CurrencyHelper::convertFromMainCurrency($order->total_price, $this->currency);&nbsp; &nbsp; &nbsp; &nbsp; $details = (new Details())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->setShipping($priceTotal - $priceSubTotal)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->setSubtotal($priceSubTotal)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->setTax(0);&nbsp; &nbsp; &nbsp; &nbsp; $amount = (new Amount())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->setCurrency($this->currency->iso_code)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->setTotal($priceTotal)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->setDetails($details);&nbsp; &nbsp; &nbsp; &nbsp; $transaction = (new Transaction())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->setAmount($amount)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->setItemList($itemList)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->setDescription($this->transactionDescription)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->setInvoiceNumber($this->transaction->id);&nbsp; &nbsp; &nbsp; &nbsp; $urls = (new RedirectUrls())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->setReturnUrl($this->createResultUrl(['id' => $this->order->payment_type_id]))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->setCancelUrl($this->createFailUrl());&nbsp; &nbsp; &nbsp; &nbsp; $payment = (new Payment())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->setIntent('sale')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->setPayer($payer)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->setTransactions([$transaction])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->setRedirectUrls($urls);&nbsp; &nbsp; &nbsp; &nbsp; $link = null;&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $link = $payment->create($this->apiContext)->getApprovalLink();&nbsp; &nbsp; &nbsp; &nbsp; } catch (\Exception $e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $link = null;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return $this->render('paypal', [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'order' => $order,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'transaction' => $this->transaction,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'approvalLink' => $link,&nbsp; &nbsp; &nbsp; &nbsp; ]);&nbsp; &nbsp; }货币助手<?phpnamespace app\modules\shop\helpers;use app\modules\shop\models\Currency;use app\modules\shop\models\UserPreferences;class CurrencyHelper{&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* @var Currency $userCurrency&nbsp; &nbsp; &nbsp;* @var Currency $mainCurrency&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; static protected $userCurrency = null;&nbsp; &nbsp; static protected $mainCurrency = null;&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* @return Currency&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; static public function getUserCurrency()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (null === static::$userCurrency) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; static::$userCurrency = static::findCurrencyByIso(UserPreferences::preferences()->userCurrency);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return static::$userCurrency;&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* @param Currency $userCurrency&nbsp; &nbsp; &nbsp;* @return Currency&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; static public function setUserCurrency(Currency $userCurrency)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return static::$userCurrency = $userCurrency;&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* @return Currency&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; static public function getMainCurrency()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return null === static::$mainCurrency&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ? static::$mainCurrency = Currency::getMainCurrency()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : static::$mainCurrency;&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* @param string $code&nbsp; &nbsp; &nbsp;* @param bool|true $useMainCurrency&nbsp; &nbsp; &nbsp;* @return Currency&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; static public function findCurrencyByIso($code)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $currency = Currency::find()->where(['iso_code' => $code])->one();&nbsp; &nbsp; &nbsp; &nbsp; $currency = null === $currency ? static::getMainCurrency() : $currency;&nbsp; &nbsp; &nbsp; &nbsp; return $currency;&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* @param float|int $input&nbsp; &nbsp; &nbsp;* @param Currency $from&nbsp; &nbsp; &nbsp;* @param Currency $to&nbsp; &nbsp; &nbsp;* @return float|int&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; static public function convertCurrencies($input = 0, Currency $from, Currency $to)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (0 === $input) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return $input;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if ($from->id !== $to->id) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $main = static::getMainCurrency();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ($main->id === $from->id && $main->id !== $to->id) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $input = $input / $to->convert_rate * $to->convert_nominal;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } elseif ($main->id !== $from->id && $main->id === $to->id) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $input = $input / $from->convert_nominal * $from->convert_rate;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $input = $input / $from->convert_nominal * $from->convert_rate;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $input = $input / $to->convert_rate * $to->convert_nominal;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return $to->formatWithoutFormatString($input);&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* @param float|int $input&nbsp; &nbsp; &nbsp;* @param Currency $from&nbsp; &nbsp; &nbsp;* @return float|int&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; static public function convertToUserCurrency($input = 0, Currency $from)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return static::convertCurrencies($input, $from, static::getUserCurrency());&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* @param float|int $input&nbsp; &nbsp; &nbsp;* @param Currency $from&nbsp; &nbsp; &nbsp;* @return float|int&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; static public function convertToMainCurrency($input = 0, Currency $from)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return static::convertCurrencies($input, $from, static::getMainCurrency());&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* @param float|int $input&nbsp; &nbsp; &nbsp;* @param Currency $to&nbsp; &nbsp; &nbsp;* @return float|int&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; static public function convertFromMainCurrency($input = 0, Currency $to)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return static::convertCurrencies($input, static::getMainCurrency(), $to);&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* @param Currency $currency&nbsp; &nbsp; &nbsp;* @param string|null $locale&nbsp; &nbsp; &nbsp;* @return string&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; static public function getCurrencySymbol(Currency $currency, $locale = null)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $locale = null === $locale ? \Yii::$app->language : $locale;&nbsp; &nbsp; &nbsp; &nbsp; $result = '';&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $fake = $locale . '@currency=' . $currency->iso_code;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $fmt = new \NumberFormatter($fake, \NumberFormatter::CURRENCY);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $result = $fmt->getSymbol(\NumberFormatter::CURRENCY_SYMBOL);&nbsp; &nbsp; &nbsp; &nbsp; } catch (\Exception $e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $result = preg_replace('%[\d\s,]%i', '', $currency->format(0));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return $result;&nbsp; &nbsp; }}

繁星点点滴滴

我从贝宝支持那里得到了这个:不幸的是,国内交易无法以美元接收,同样的国际交易应以美元接收。两种交易都无法使用单一货币。感谢致敬,
打开App,查看更多内容
随时随地看视频慕课网APP