Do not pass PAY-XXX or PAYID-XXX directly into createOrder

Energy40
Contributor
Contributor
Hello there. Everything in my system was in good condition, but since yesterday I get an error like this I could not understand why? 
Do not pass PAY-XXX or PAYID-XXX directly into createOrder. Pass the EC-XXX token instead
My smart buttons script like that:
<
script>
paypal.Buttons({
style: {
layout: 'vertical',
color: 'black',
shape: 'rect',
label: 'paypal',
tagline: false,
size: 'responsive',
},

createOrder: function() {


// Now return the `token` to the client
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>
And server side

Create-Payment
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',
'ClientSecret'
));
$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") // Similar to `item_number` in Classic API
->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;
}
 
 
Login to Me Too
2 REPLIES 2

sergiumocan666
Contributor
Contributor

Same issue 😞 paypal returns PAY-XXX id and not EC-XXX. What can we do with that? Is the problem in the intent of the order?

Login to Me Too

Echolove
Contributor
Contributor

I encountered the same problem. I used the payment created by Payment-> create () method, only pay_id, no ec-token

Login to Me Too

Haven't Found your Answer?

It happens. Hit the "Login to Ask the community" button to create a question for the PayPal community.