Everything in my system was in good condition, but since yesterday I get an error like this I could not understand why?
Error: Do not pass PAY-XXX or PAYID-XXX directly into createOrder. Pass the EC-XXX token instead
This is my Smart Payment Button script
<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>
This is my backend Create-Payment function:
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;
}
And this is Execute-Payment function
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){
}
}
What is wrong?
2
Answers
If any one getting same problem like this i was able to find work around from this one now i have tried the solution with python but i think if any one can do the same thing from any other language
payPal sending us a array of link so what i am doing is getting the link from the list
then i am getting the array of links and search for
and if you pass this one it will work.
i don’t know why PayPal send this inside a URL this makes it extra work to retrieve but it’s work. hope this one helps any one since we can’t find this in documentation in PayPal
Thanks for @Thalinda Bandara for his answer. if anyone looking for js code here is:
Javascript: