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;
}
}
- 2 回答
- 0 关注
- 143 浏览
添加回答
举报