I'm trying to include a identifier in the transaction so I know which transaction is when it returns from Paypal. I've tried to use the reference_id as it's like the better option (because is a temporal identifier that I assign to the transaction).
The problem is that when I set it to the transaction, Paypal refuse to accept the json, it returns:
{
"name":"MALFORMED_REQUEST",
"message":"Incoming JSON request does not map to API request",
"information_link":"https://developer.paypal.com/webapps/developer/docs/api/#MALFORMED_REQUEST",
"debug_id":"6835f984b6735"
}
More or less I use the example code:
// Create new payer and method
$payer = new Payer();
$payer->setPaymentMethod("paypal");
// Set redirect urls
$redirectUrls = new RedirectUrls();
$redirectUrls->setReturnUrl($urlRetorno)
->setCancelUrl($urlCancelar);
// Set payment amount
$amount = new Amount();
$amount->setCurrency($moneda)
->setTotal($total);
// Set transaction object
$transaction = new Transaction();
$transaction->setAmount($amount)
->setDescription($descripcion);
// If I comment this line it runs ok.
$transaction->setReferenceId($referenceId);
// Create the full payment object
$payment = new Payment();
$payment->setIntent('sale')
->setPayer($payer)
->setRedirectUrls($redirectUrls)
->setTransactions(array($transaction));
echo $payment->toJSON();
// Create payment with valid API context
try {
$payment->create($apiContext);
// Get PayPal redirect URL and redirect user
$approvalUrl = $payment->getApprovalLink();
// Finalmente le redirigimos a PayPal para que apruebe el pago
//redirige($approvalUrl, 'Intentando redirigir a PayPal.');
} catch (PayPal\Exception\PayPalConnectionException $ex) {
echo $ex->getCode();
echo $ex->getData();
die($ex);
} catch (Exception $ex) {
die($ex);
}
And this is the result (calling toJSON()):
{
"intent":"sale",
"payer":{
"payment_method":"paypal"
},
"redirect_urls":{
"return_url":"https://example.com/return.php",
"cancel_url":"https://example.com/cancel.php"
},
"transactions":[
{
"amount":{
"currency":"MXN",
"total":"1515"
},
"description":"Suscripci\u00f3n c\u00f3digo PP-19119",
"reference_id":"304171757041"
}
]
}
I have found the reference_id in the API documentation so I supposed I could use it.
After posting on the GitHub repositories about it here is the conclusion.
reference_id is a read only value and you should use invoice_number to track it.
You can read about it in https://github.com/paypal/PayPal-PHP-SDK/issues/828 and currently tracked in https://github.com/paypal/PayPal-REST-API-issues/issues/69.
Related
I'm building paypal subscription system and i'm using web-hooks for notifying system on subscription created, active, etc.
However, it's required that user must return to success url and site execute $agreement->execute($token, $apiContext)); to make it work.
Let's say for some reason user never get back to return url then you will never execute payment and user will never get their subscription.
I've looked around on paypal documentation and couldn't find any solution.
Here's my code:
Subscribe.php:
$agreement = new Agreement();
$agreement->setName('Basic Plan')
->setDescription('Some info')
->setStartDate($date);
$plan = new Plan();
$plan->setId('PLAN_ID');
$agreement->setPlan($plan);
// Add Payer
$payer = new Payer();
$payer->setPaymentMethod('paypal');
$agreement->setPayer($payer);
// Add Shipping Address
$shippingAddress = new ShippingAddress();
$shippingAddress->setLine1('111 First Street')
->setCity('Saratoga')
->setState('CA')
->setPostalCode('95070')
->setCountryCode('US');
$agreement->setShippingAddress($shippingAddress);
// ### Create Agreement
try {
$agreement = $agreement->create($apiContext);
$agreement->getApprovalLink()
// method
$approvalUrl = $agreement->getApprovalLink();
redirect($approvalUrl);
} catch (Exception $ex) {
print_r($ex->getData());
}
index.php
if (isset($_GET['status']) && $_GET['status'] == 'success') {
$token = $_GET['token'];
$agreement = new \PayPal\Api\Agreement();
try {
// ## Execute Agreement
// Execute the agreement by passing in the token
echo "<pre>";
print_r($agreement->execute($token, $apiContext));
} catch (Exception $ex) {
exit(1);
}
} else {
echo "User Cancelled the Approval";
}
I am new to PayPal SDK and trying to use the PayPal Checkout SDK in a Laravel application.
I have followed most of the instruction from the following github page, the first call to the function create-payment seems to work; however when I press continue on the PayPal pop-up window to execute the transaction it fails and produces the following error:
Error: Request to post /api/execute-payment failed with 500 error. Correlation id: unknown
{
"message": "Got Http response code 400 when accessing https://api.sandbox.paypal.com/v1/payments/payment/PAY-3RB62059V6076291ALRCHT6Y/execute.",
"exception": "PayPal\\Exception\\PayPalConnectionException",
"file": "C:\\websites\\online-webstore\\vendor\\paypal\\rest-api-sdk-php\\lib\\PayPal\\Core\\PayPalHttpConnection.php",
"line": 207, {...}
Checking the link: https://api.sandbox.paypal.com/v1/payments/payment/PAY-3RB62059V6076291ALRCHT6Y/execute, I get the error:
{"name":"AUTHENTICATION_FAILURE","message":"Authentication failed due to invalid authentication credentials or a missing Authorization header.","links":[{"href":"https://developer.paypal.com/docs/api/overview/#error","rel":"information_link"}]}
This is my setup so for the client side:
<script src="https://www.paypalobjects.com/api/checkout.js"></script>
<div id="paypal-button"></div>
<script>
paypal.Button.render({
env: 'sandbox', // Or 'production'
// Set up the payment:
// 1. Add a payment callback
payment: function(data, actions) {
// 2. Make a request to your server
return actions.request.post('/api/create-payment')
.then(function(res) {
// 3. Return res.id from the response
return res.id;
});
},
// Execute the payment:
// 1. Add an onAuthorize callback
onAuthorize: function(data, actions) {
// 2. Make a request to your server
return actions.request.post('/api/execute-payment', {
paymentID: data.paymentID,
payerID: data.payerID
})
.then(function(res) {
console.log(res);
alert('PAYMENT WENT THROUGH!!');
}).catch(function(err){
console.log("Error "+err);
});
}
}, '#paypal-button');
</script>
In my controller is set up like so:
class CheckoutController extends Controller
{
private $apiContext;
private $client_id;
private $secret;
public function __construct()
{
$this->middleware('auth', ['except'=>['createPayment', 'executePayment']]);
// Detect if we are running in live mode or sandbox
if(config('paypal.settings.mode') == 'live'){
$this->client_id = config('paypal.live_client_id');
$this->secret = config('paypal.live_secret');
} else {
$this->client_id = config('paypal.sandbox_client_id');
$this->secret = config('paypal.sandbox_secret');
}
// Set the Paypal API Context/Credentials
$this->apiContext = new ApiContext(new OAuthTokenCredential($this->client_id, $this->secret));
$this->apiContext->setConfig(config('paypal.settings'));
}
public function createPayment () {
$payer = new Payer();
$payer->setPaymentMethod("paypal");
$item1 = new Item();
$item1->setName('Ground Coffee 40 oz')
->setCurrency('USD')
->setQuantity(1)
->setSku("123123") // Similar to `item_number` in Classic API
->setPrice(7.5);
$item2 = new Item();
$item2->setName('Granola bars')
->setCurrency('USD')
->setQuantity(5)
->setSku("321321") // Similar to `item_number` in Classic API
->setPrice(2);
$itemList = new ItemList();
$itemList->setItems(array($item1, $item2));
$details = new Details();
$details->setShipping(1.2)
->setTax(1.3)
->setSubtotal(17.50);
$amount = new Amount();
$amount->setCurrency("USD")
->setTotal(20)
->setDetails($details);
$transaction = new Transaction();
$transaction->setAmount($amount)
->setItemList($itemList)
->setDescription("Payment description")
->setInvoiceNumber(uniqid());
$baseUrl = \URL::to('/');
$redirectUrls = new RedirectUrls();
$redirectUrls->setReturnUrl("http://online-webstore/paypalRedirect/true")
->setCancelUrl("http://online-webstore/paypalRedirect/false");
$payment = new Payment();
$payment->setIntent("sale")
->setPayer($payer)
->setRedirectUrls($redirectUrls)
->setTransactions(array($transaction));
$request = clone $payment;
try {
$payment->create($this->apiContext);
} catch (Exception $ex) {
exit(1);
}
$approvalUrl = $payment->getApprovalLink();
return $payment;
}
public function executePayment (Request $request) {
$paymentId = $request->paymentID;
$payment = Payment::get($paymentId, $this->apiContext);
$execution = new PaymentExecution();
$execution->setPayerId($request->PayerID);
try {
$result = $payment->execute($execution, $this->apiContext);
} catch (PayPal\Exception\PayPalConnectionException $ex) {
echo $ex->getData(); // Prints the detailed error message
die($ex);
}
return $result;
}
}
I have also correctly included my credential in my .env file and running it in sandbox mode.
Can someone direct me where I maybe going wrong also I am happy to provided any more information if required.
I had good look through the log file and I noticed the error:
{"name":"VALIDATION_ERROR","details":[{"field":"payer_id","issue":"Payer ID is invalid"}],"message":"Invalid request - see details","information_link":"https://developer.paypal.com/docs/api/payments/#errors","debug_id":"81885e7bbe957"}
It turns out that I made a slight typo in my code:
$execution->setPayerId($request->PayerID);
It should actually be:
$execution->setPayerId($request->payerID);
you have to check your CLIENT_SECRET and CLIENT_ID. in my case it's wrong that's why I facing this error but when I change these keys then it's working fine. check
It's for couple of days, I'm using php sdk. It was working before, but now they are not executing my subscription and charge payment. I only get PAYMENT.SALE.PENDING event whenever a payment has made.
Please help, this is driving me crazy.
I get the payment status "approved", but I'm not getting the 'PAYMENT.SALE.COMPLETED' webhook, I don't know what's wrong.
Here is my code:
create:
public function doPayPalCharge($returnUrl, $cancelUrl)
{
$apiContext = $this->apiContext;
$money = $this->getAmount();
$customData = $this->getMetadata();
// Create new payer and method
$payer = new Payer();
$payer->setPaymentMethod("paypal");
// Set redirect urls
$redirectUrls = new RedirectUrls();
$redirectUrls->setReturnUrl($returnUrl)
->setCancelUrl($cancelUrl);
// Set payment amount
$amount = new Amount();
$amount->setCurrency("USD")
->setTotal($money);
// Set transaction object
$transaction = new Transaction();
$transaction->setAmount($amount)
//can not use json_encode here, because can not decode data from web hook
->setCustom(base64_encode(serialize($customData->all)))
->setDescription("Payment description");
// Create the full payment object
$payment = new Payment();
$payment->setIntent('sale')
->setPayer($payer)
->setRedirectUrls($redirectUrls)
->setTransactions([$transaction]);
$request = clone $payment;
// Create payment with valid API context
try
{
$payment->create($apiContext);
$token = $this->getPayPalTokenFromUrl($payment->getApprovalLink());
} catch (PayPalConnectionException $e)
{
self::payPalLog($e, '[PayPalConnectionException]', (array)$request);
} catch (\Exception $e)
{
self::payPalLog($e, '[Create Payment Failed]', (array)$request);
}
return isset($token) ? $token : '';
}
execute:
public function confirmPayPalCharge($payment_id, $payer_id)
{
$apiContext = $this->apiContext;
// Get payment object by passing paymentId
$payment = Payment::get($payment_id, $apiContext);
// Execute payment with payer id
$execution = new PaymentExecution();
$execution->setPayerId($payer_id);
try
{
// Execute payment
$payment = $payment->execute($execution, $apiContext);
} catch (PayPalConnectionException $e)
{
self::payPalLog($e, '[PayPalConnectionException]');
} catch (\Exception $e)
{
self::payPalLog($e);
}
return isset($payment) ? $payment : null;
}
I am using PayPal paypal/rest-api-sdk-php for payment gateway for my laravel application.
But when the link is click for confirmation, I get Got Http response code 400 when accessing https://api.sandbox.paypal.com/v1/payments/payment.
Following is my method of implementation in Laravel 5.1 and AngularJS:
angular.module('MyApp')
.factory('Account', function($http){
return {
buyNow: function(pid){
console.log(pid)
return $http.put('/api/buynow',pid);
}
}
});
public function buyNowAPI(Request $request)
{
$pid = $request->input('pid');
$product = Product::find($pid);
$baseUrl = url().'/#/app/product?orderId=$pid';
$payment = $this->makePaymentUsingPayPal($product->price,'MYR',$product->title,"$baseUrl&success=true", "$baseUrl&success=false");
dd($payment->getLinks());
return response()->json($pid);
}
public function makePaymentUsingPayPal($total, $currency, $paymentDesc, $returnUrl, $cancelUrl)
{
$payer = new Payer();
$payer->setPaymentMethod("paypal");
// Specify the payment amount.
$amount = new Amount();
$amount->setCurrency($currency);
$amount->setTotal($total);
$transaction = new Transaction();
$transaction->setAmount($amount);
$transaction->setDescription($paymentDesc);
$redirectUrls = new RedirectUrls();
$redirectUrls->setReturnUrl($returnUrl);
$redirectUrls->setCancelUrl($cancelUrl);
$payment = new Payment();
$payment->setRedirectUrls($redirectUrls);
$payment->setIntent("sale");
$payment->setPayer($payer);
$payment->setTransactions(array($transaction));
$payment->create($this->getApiContext());
return $payment;
}
The laravel code implementation is based on example that available in git for paypal/rest-api-sdk-php based example called The Pizza App rest-api-sample-app .
Thanks!!
You need to follow the instructions mentioned here:
https://github.com/paypal/PayPal-PHP-SDK/wiki/exception-%27PayPal%5CException%5CPayPalConnectionException%27-with-message-%27Got-Http-response-code-400-when-accessing
try {
$payment->create($apiContext);
} catch (PayPal\Exception\PayPalConnectionException $ex) {
echo $ex->getCode(); // Prints the Error Code
echo $ex->getData(); // Prints the detailed error message
die($ex);
} catch (Exception $ex) {
die($ex);
}
I'm trying to create and execute a payment with PayPal REST API through PHP SDK (sandbox environment) like showing below. The payment creation ($payment->create) work fine but the payment execution ($payment->execute) return "Incoming JSON request does not map to API request".
The JSON request is created by the SDK, then what can be the problem?
Thanks in advance.
$payer = new Payer();
$payer->setPaymentMethod("paypal");
$item = new Item();
$item->setName('Any name')
->setCurrency('EUR')
->setQuantity(1)
->setPrice(0.99);
$itemList = new ItemList();
$itemList->setItems(array($item));
$details = new Details();
$details->setTax(0)
->setSubtotal(0.99);
$amount = new Amount();
$amount->setCurrency('EUR')
->setTotal(0.99)
->setDetails($details);
$transaction = new Transaction();
$transaction->setAmount($amount)
->setItemList($itemList)
->setDescription('Any description')
->setInvoiceNumber(uniqid());
$redirectUrls = new RedirectUrls();
$redirectUrls->setReturnUrl(BASE_URL.'/payment/?success=true')
->setCancelUrl(BASE_URL.'/payment/?success=false');
$payment = new Payment();
$payment->setIntent('sale')
->setPayer($payer)
->setRedirectUrls($redirectUrls)
->setTransactions(array($transaction));
try {
$payment->create($apiContext);
$execution = new PaymentExecution();
$result = $payment->execute($execution, $apiContext);
} catch (Exception $ex) {
//Function for extract the error message,
//the error message can be showing with
//a simple var_dump($ex)
$exception = self::getException($ex);
}
Am not a PHP dev (.Net), so based on reading the above, check into this section in your code:
$execution = new PaymentExecution();
$result = $payment->execute($execution, $apiContext);
You are sending a new PaymentExecution without the payer_id and the Payment.Id which you would obtain after the user approves your paypal Payment request that you created.
That said, I don't see that part (though as above, not a PHP dev) so I could be wrong. The steps are:
Create the Payment
Go into the Approval Flow -> user is redirected to Paypal and approves the Payment you created in #1 -> and is redirected back to your site (the returnUrl)
a. the PayerID will be in the querystring in this process
b. the paymentId will also be in the querystring in this process
After the user is redirected back to your site from PayPal (your returnUrl) - Execute a new Payment, set it's id to the one you obtained (#2b), sending the PaymentExecution with its PayerID set to the what you you obtained (#2a)
In c# (you'll need to convert this to PHP):
var _payment = new Payment { id = the_payment_id_you_obtained };
var _payExec = new PaymentExecution { payer_id = the_payer_id_you_obtained };
var _response = _payment.Execute(context, _payExec);
Hth...
It was enough put
$payment->setIntent('authorize')
instead of
$payment->setIntent('sale')
and eliminate the execution
$execution = new PaymentExecution(); $result = $payment->execute($execution, $apiContext);
Then, after
$payment->create
I used the "href" from
$payment->links
to do the redirect. Everything went perfectly. Thank you all.