Paypal Api 不要直接传递 PAY-XXX 或 PAYID-XXX
Posted
技术标签:
【中文标题】Paypal Api 不要直接传递 PAY-XXX 或 PAYID-XXX【英文标题】:Paypal Api Do not pass PAY-XXX or PAYID-XXX directly 【发布时间】:2019-12-02 06:07:43 【问题描述】:我的系统中的一切都处于良好状态,但从昨天开始我收到这样的错误,我不明白为什么?
错误:不要将 PAY-XXX 或 PAYID-XXX 直接传递到 createOrder。改为传递 EC-XXX 令牌
这是我的智能支付按钮脚本
<script>
paypal.Buttons(
style:
layout: 'vertical',
color: 'black',
shape: 'rect',
label: 'paypal',
tagline: false,
size: 'responsive',
,
createOrder: function()
var SETEC_URL = '/api/create-payment';
var checkBox = document.getElementById("ship_to_different");
var note = $("#ordernote").val();
if (checkBox.checked == true)
var body = $("#checkoutt, #data").serializeArray();
else
$('input[name=note]').val(note);
var body = $("#data").serializeArray();
$("#wait").show();
return fetch(SETEC_URL,
method: 'post',
headers:
'content-type': 'application/json'
,
body: JSON.stringify(
body: body
)
).then(function(res)
return res.json();
).then(function(data)
return data.id;
);
,
commit: false,
onApprove: function(data)
var EXECUTE_URL = '/api/execute-payment';
return fetch(EXECUTE_URL,
method: 'post',
headers:
'content-type': 'application/json'
,
body: JSON.stringify(
paymentID: data.paymentID,
payerID: data.payerID,
)
).then(function(response)
console.log(response);
if (response.statusText == 'OK')
var checkBox =
document.getElementById("ship_to_different");
var note = $("#ordernote").val();
if (checkBox.checked == true)
var xdata = $("#checkoutt, #data").serialize();
else
$('input[name=note]').val(note);
var xdata = $("#data").serialize();
$.ajax(
type: 'post',
url: 'check-data',
data: xdata,
success: function()
$("#wait").hide();
$("#success").show();
,
error: function(request)
json = $.parseJSON(request.responseText);
$.each(json.errors, function(key, value)
$('#error' + key).html('');
$('#error' + key).append('<p
class = "erro" > '+value+' < /p>');
);
);
)
, onError: function(err)
console.log(err);
).render('#paypal-button-container');
</script>
这是我的后端创建支付功能:
public function createorder(Request $request)
$body = json_decode(json_encode($request['body']),true);
foreach($body as $valpay)
if($valpay['name'] == '_token')
unset($valpay);
elseif($valpay['name'] == 'title[]')
$titlepay[] = $valpay['value'];
elseif($valpay['name'] == 'product_id[]')
$product_idpay[] = $valpay['value'];
elseif($valpay['name'] == 'price[]')
$pricepay[] = $valpay['value'];
elseif($valpay['name'] == 'quantity[]')
$quantitypay[] = $valpay['value'];
elseif($valpay['name'] == 'ssh')
$shippingpay = $valpay['value'];
elseif($valpay['name'] == 'total')
$totalpay = $valpay['value'];
elseif($valpay['name'] == 'subtotal')
$subtotalpay = $valpay['value'];
elseif($valpay['name'] == 'tax')
$taxpay = $valpay['value'];
elseif($valpay['name'] == 'city')
$city = $valpay['value'];
elseif($valpay['name'] == 'country')
$country = $valpay['value'];
elseif($valpay['name'] == 'state')
$state = $valpay['value'];
elseif($valpay['name'] == 'street')
$street = $valpay['value'];
elseif($valpay['name'] == 'zip')
$zip = $valpay['value'];
elseif($valpay['name'] == 'coupon')
$coupon = $valpay['value'];
elseif($valpay['name'] == 'discount')
$discount = $valpay['value'];
elseif($valpay['name'] == 'data')
$data = $valpay['value'];
elseif($valpay['name'] == 'x1')
$gift = $valpay['value'];
elseif($valpay['name'] == 'cost')
$cost = $valpay['value'];
$apiContext = new ApiContext(
new OAuthTokenCredential(
'clientid',
'secret'
));
$payer = new Payer();
$payer->setPaymentMethod("paypal");
if(isset($gift))
$item = new Item();
$item->setName('Gift Coupon')
->setCurrency('USD')
->setQuantity('1')
->setPrice($cost);
$items[] = $item;
$subtotalpay = $cost;
$totalpay = $cost;
else
foreach ($product_idpay as $key => $p_id)
$item[$key] = new Item();
$item[$key]->setName($titlepay[$key])
->setCurrency('USD')
->setQuantity($quantitypay[$key])
->setSku("123123")
->setPrice($pricepay[$key]);
$items[] = $item[$key];
if(isset($coupon))
$subtotalpay = $coupon;
$item[$key+1] = new Item();
$item[$key+1]->setName('Coupon')
->setCurrency('USD')
->setQuantity("1")
->setSku("test") // Similar to `item_number` in Classic API
->setPrice('-'.$discount);
$items[] = $item[$key+1];
if($taxpay == '1')
$tax = round($subtotalpay * 8.625/100,2);
else
$tax = 0;
$itemList = new ItemList();
$itemList->setItems($items);
$details = new Details();
$details->setShipping($shippingpay)
->setTax($tax)
->setSubtotal($subtotalpay);
$amount = new Amount();
$amount->setCurrency("USD")
->setTotal($totalpay)
->setDetails($details);
$transaction = new Transaction();
$transaction->setAmount($amount)
->setItemList($itemList)
->setDescription("Payment description")
->setInvoiceNumber(uniqid());
$redirectUrls = new RedirectUrls();
$redirectUrls->setReturnUrl("http://homee.test/checkout")
->setCancelUrl("http://homee.test/checkout");
$inputFields = new InputFields();
$inputFields->setNoShipping(1);
$webProfile = new WebProfile();
$webProfile->setName('test'. uniqid())->setInputFields($inputFields);
$webProfileId = $webProfile->create($apiContext)->getId();
$payment = new Payment();
$payment->setExperienceProfileId($webProfileId);
$payment->setIntent("sale")
->setPayer($payer)
->setRedirectUrls($redirectUrls)
->setTransactions(array($transaction));
$request = clone $payment;
try
$payment->create($apiContext);
catch (PayPalConnectionException $ex)
echo $ex->getCode(); // Prints the Error Code
echo $ex->getData(); // Prints the detailed error message
die($ex);
catch (Exception $ex)
die($ex);
$approvalUrl = $payment->getApprovalLink();
return $payment;
这是执行支付功能
public function execute(Request $request)
$apiContext = new ApiContext(
new OAuthTokenCredential(
'clientid',
'secret'
));
$paymentId = $request->paymentID;
$payment = Payment::get($paymentId, $apiContext);
$execution = new PaymentExecution();
$execution->setPayerId($request->payerID);
try
$result = $payment->execute($execution, $apiContext);
$res = $result->transactions[0]->related_resources[0]->sale->id;
$res2 = $result->transactions[0]->invoice_number;
$res3 = $result->getId();
Transactions::create([
'ip' => \Request::ip(),
'transaction_id' => $res,
'invoice_id' => $res2,
'payment_id' => $res3
]);
Refunds::create([
'ip' => \Request::ip(),
'transaction_id' => $res,
'invoice_id' => $res2,
'payment_id' => $res3,
'amount' => '0'
]);
catch (Exception $e)
echo $e->getMessage();
exit(1);
public function validateorder(Request $request)
if($request['status'])
$messages = array(
'zip' => 'Post Code Field is Required.'
);
$validator = Validator::make($request->all(), [
'street' => 'required',
'city' => 'required',
'state' => 'required',
'zip' => 'required',
]);
elseif($request['guest'])
$validator = Validator::make($request->all(), [
'name' => 'required',
'email' => 'required',
'country' => 'required',
'phone' => 'required',
'street' => 'required',
'city' => 'required',
'state' => 'required',
'zip' => 'required',
]);
elseif($request['x1'])
$validator = Validator::make($request->all(), [
'email' => 'required'
]);
try
if ($validator->fails())
return response()->json($validator->errors(), 422);
catch (\Exception $e)
怎么了?
【问题讨论】:
【参考方案1】:如果有人遇到类似的问题,我现在可以从这个问题中找到解决方法,我已经尝试了使用 python 的解决方案,但我认为是否有人可以使用任何其他语言做同样的事情
payPal 向我们发送了一系列链接,所以我正在做的是从列表中获取链接
"intent":"sale",
"payer":
"payment_method":"paypal"
,
"redirect_urls":
"return_url":"http://localhost:3000/payment/execute",
"cancel_url":"http://localhost:3000/"
,
"transactions":[
"amount":
"total":"5.00",
"currency":"USD"
,
"description":"This is the payment transaction description.",
"item_list":
"items":[
"name":"item",
"sku":"item",
"price":"5.00",
"currency":"USD",
"quantity":1
]
,
"related_resources":[
]
],
"id":"PAYID-MG5D3JA33asdasdasdasdASDASD815R",
"state":"created",
"create_time":"2021-12-15T19:10:28Z",
"links":[
"href":"https://api.sandbox.paypal.com/v1/payments/payment/PAYID-MG5SSDASDASDSADSAKD564815R",
"rel":"self",
"method":"GET"
,
"href":"https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=EC-5K121231232094281T",
"rel":"approval_url",
"method":"REDIRECT"
,
"href":"https://api.sandbox.paypal.com/v1/payments/payment/PAYID-MG5ADSADASDASDSA4564815R/execute",
"rel":"execute",
"method":"POST"
]
然后我得到链接数组并搜索
approval_url 然后做小的正则表达式/搜索并获取代码
token = ''
links = payment.links
for i in links:
if(i.rel=="approval_url"):
token = i.href.split("EC-",1)[1]
return token
如果你通过了这个,它就会起作用。 我不知道为什么 PayPal 会在 URL 中发送这个,这使得检索变得额外工作,但它的工作。希望这个对任何人都有帮助,因为我们在 PayPal 的文档中找不到这个
【讨论】:
以上是关于Paypal Api 不要直接传递 PAY-XXX 或 PAYID-XXX的主要内容,如果未能解决你的问题,请参考以下文章
无法将变量传递给 PayPal Payments Rest API 的 JS 脚本
用于传递产品和金额的简单 PayPal Javascript API