Paypal Transaction API Getting Total 不是一个有效的数值
Posted
技术标签:
【中文标题】Paypal Transaction API Getting Total 不是一个有效的数值【英文标题】:Paypal Transaction API Getting Total is not a valid numeric value 【发布时间】:2020-05-10 05:46:01 【问题描述】:我尝试了所有方法,但不知道为什么我不断收到错误消息“总计不是有效的数值”。一切都很好,直到我尝试为 $details 添加运费和税金。当我返回 $details 时,我得到这个 “shipping”:“49.99”,“tax”:“0.12”,“subtotal”:“50.81”。 $request 中的一切都很好,我只是不明白我做错了什么。似乎一切都加起来了,我并不总是得到错误。
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Sold_Item;
use App\Customer_Order;
use Auth;
use Session;
use Illuminate\Support\Facades\Mail;
use App\Mail\SendOrder;
use App\Mail\OrderConfirmation;
use Carbon\Carbon;
use PayPal\Api\Amount;
use PayPal\Api\Details;
use PayPal\Api\Item;
use PayPal\Api\ItemList;
use PayPal\Api\Payer;
use PayPal\Api\Payment;
use PayPal\Api\RedirectUrls;
use PayPal\Api\Transaction;
use PayPal\Api\ExecutePayment;
use PayPal\Api\PaymentExecution;
class PaypalController extends Controller
public function payment(Request $request)
if (empty($request->contact_name) || empty($request->phone_number) || empty($request->email_address) || empty($request->address) || empty($request->country) || empty($request->spr) || empty($request->city) || empty($request->zipcode))
return back()->withErrors(['error','Error Please Retry']);
else
$apiContext = new \PayPal\Rest\ApiContext(
new \PayPal\Auth\OAuthTokenCredential(
"AaOWETQOsvghR3ih4FfxrTLwEWMDPUqfPw7wK9d4pTNF9i546wgN4X6eGvMdffl7qk9JCj5YsauIV0cc",
"EMMCcMlheBQjBgnWY_SIDt9GISV_Vu5ha-BelnJHUEx3kB6TqsPsBp0kKuA6A2HJY2MgFyo4ZaiPkQGl"
)
);
$apiContext->setConfig(
array(
'mode' => 'live',
'log.LogEnabled' => true,
'log.FileName' => 'PayPal.log',
'log.LogLevel' => 'FINE', // PLEASE USE `FINE` LEVEL FOR LOGGING IN LIVE ENVIRONMENTS
'cache.enabled' => true,
'validation.level' => 'log'
)
);
$payer = new Payer();
$payer->setPaymentMethod("paypal");
// Set redirect URLs
$redirectUrls = new RedirectUrls();
$redirectUrls->setReturnUrl('https://beneplants.com/process')
->setCancelUrl('https://beneplants.com/shopcart');
$k = 1;
$rrr = array();
$total_price =0;
foreach(session('cart') as $sku => $itemss)
$total_price += $itemss['price'];
$"item_".$k = new Item();
$rrr[] = $"item_".$k->setName($itemss['name']) /** item name **/
->setCurrency('CAD')
->setQuantity(1)
->setPrice($itemss['price']); /** unit price **/
$k++;
$item_list = new ItemList();
$item_list->setItems($rrr);
$total_price += $request->shipping_cost;
$total_price += $request->tax;
$details = new Details();
$details->setShipping($request->shipping_cost)
->setTax($request->tax)
->setSubtotal($total_price);
$amount = new Amount();
$amount->setCurrency("CAD")
->setTotal($details);
// Set transaction object
$transaction = new Transaction();
$transaction->setAmount($amount)
->setItemList($item_list)
->setDescription('Your transaction description');
// Create the full payment object
$payment = new Payment();
$payment->setIntent('sale')
->setPayer($payer)
->setRedirectUrls($redirectUrls)
->setTransactions(array($transaction));
try
$payment->create($apiContext);
$customer_order = [
"contact_name" => $request->contact_name,
"email_address"=>$request->email_address,
"phone_number" => $request->phone_number,
"address" => $request->address,
"unit" => $request->unit,
"country" => $request->country,
"spr" => $request->spr,
"city" => $request->city,
"zipcode" => $request->zipcode,
"total_price"=>$request->total,
"shipping_cost"=>$request->shipping_cost,
"tax"=>$request->tax
];
session()->put('paypal', $customer_order);
// Get PayPal redirect URL and redirect the customer
$approvalUrl = $payment->getApprovalLink();
// $approvalUrl = 'https://www.sandbox.paypal.com/cgi-bin/webscr';
return redirect($approvalUrl);
// Redirect the customer to $approvalUrl
catch (PayPal\Exception\PayPalConnectionException $ex)
echo $ex->getCode();
echo $ex->getData();
die($ex);
catch (Exception $ex)
die($ex);
【问题讨论】:
【参考方案1】:你有这段代码:
$details = new Details();
$details->setShipping($request->shipping_cost)
->setTax($request->tax)
->setSubtotal($total_price);
$amount = new Amount();
$amount->setCurrency("CAD")
->setTotal($details); //<--HERE
你看,你使用了Details
类的对象作为setTotal()
的值。我相信你需要用->setTotal($total_price);
替换->setTotal($details);
【讨论】:
我在访问 api.paypal.com/v1/payments/payment 时收到 Got Http 响应代码 400。当我这样做时 那么现在是另一个错误吗?最后的总价格错误现在已修复?以上是关于Paypal Transaction API Getting Total 不是一个有效的数值的主要内容,如果未能解决你的问题,请参考以下文章
使用 ActiveMerchant 退款(Paypal checkout Express)
Paypal 退款服务抛出 TRANSACTION_REFUSED 错误