PayPal REST - Shipping info and transaction ID - php

Here is a simplified version of my "ExecutePayment" php file:
$payment = Payment::get($paymentId, $apicontext);
$execution = new PaymentExecution();
$execution->setPayerId($_GET['PayerID']);
$payment->execute($execution, $apicontext);
var_dump($payment);
The payment executes successfully, but the $payment variable contains no info about the customer's address, and no transaction ID. Even if I call $payment = Payment::get($paymentId, $apicontext); again after executing the payment, there is no useful information contained in the Payment object.
However, in the PayPal.log file, I see the customer's address, and the transaction ID. How do I access this data aside from reading the log file itself?

Apparently, the important object is the one returned from $payment->execute()
PayPal's sample on github is misleading... had to go through the PayPal API code to find my fix. :P

Related

Got Http response code 400 when Execute the paypal Payments

I use paypal/rest-api-sdk-php package to process paypal payment. when success payment it's working fine. but, bank decline payment we got error.
$payment = Payment::get($payment_id, $this->apiContext);
$execution = new PaymentExecution();
$execution->setPayerId($request->PayerID);
//Execute the payment
$result = $payment->execute($execution, $this->apiContext);
if ($result->getState() == 'approved') {
$paypalPaymentInfos->payment_status=1;
$paypalPaymentInfos->transaction_id=$payment_id;
$paypalPaymentInfos->save();
}
Examine the response body to determine the reason for the 400 error. It could be a normal processing decline, or it could be something else.
The rest-api-sdk-php has been deprecated for a long time and uses the old v1/payments API. Do not use it for anything.
You should integrate with the v2/checkout/orders API for payment processing : use either the Checkout-PHP-SDK (which was also recently deprecated) or direct HTTPS integration with the REST API endpoints (no PHP SDK). For payer approval use https://developer.paypal.com/demo/checkout/#/pattern/server rather than any redirect away from your site.

PHP PayPal integration - Cancel link in approval e-mail

I need some help with PayPal integration.
I use this library:
https://github.com/paypal/PayPal-PHP-SDK/wiki/Installation-Composer
and I use WebHooks to receive notifications of payments.
I create payment link like this:
$apiContext = new \PayPal\Rest\ApiContext(
new \PayPal\Auth\OAuthTokenCredential($paypal_client_id, $paypal_client_secret)
);
$apiContext->setConfig([
'mode'=>$paypal_mode
]);
$payer = new \PayPal\Api\Payer();
$payer->setPaymentMethod('paypal');
$amount = new \PayPal\Api\Amount();
$amount->setTotal('1.00');
$amount->setCurrency(Config::get('paypal.currency'));
$transaction = new \PayPal\Api\Transaction();
$transaction->setAmount($amount);
$redirectUrls = new \PayPal\Api\RedirectUrls();
$redirectUrls->setReturnUrl(Uri::create(Config::get('paypal.redirect_url')))
->setCancelUrl(Uri::create(Config::get('paypal.cancel_url')));
$payment = new \PayPal\Api\Payment();
$payment->setIntent('sale')
->setPayer($payer)
->setTransactions(array($transaction))
->setRedirectUrls($redirectUrls);
$payment->create($apiContext);
$payment_url = $payment->getApprovalLink();
Then, I run this link, log in to PayPal and make a payment.
Now, I receive WebHook with event PAYMENTS.PAYMENT.CREATED - so, OK, but after several minutes I get an e-mail:
"Would you like to complete your PayPal payment at xxx?
To finish, go back to xxx and give your final approval."
And text „give your final approval” is linked to my Cancel Page witch I entered into setCancelUrl() metod.
I don’t receive any more webhooks.
Do you have any ideas?
See https://developer.paypal.com/docs/integration/direct/payments/paypal-payments/
You have reached step 3, the payment was created and approved.
You are apparently missing step 4 to actually execute the payment and create a PayPal transaction.... so, no money has moved yet.
Moreover, all of the above and your work so far with the PayPal-PHP-SDK is for an old version of the PayPal REST API, v1/payments. I recommend setting all of those things aside and instead trying out the new v2/orders PHP SDK: https://github.com/paypal/Checkout-PHP-SDK
Here is a demo pattern of the best UI: https://developer.paypal.com/demo/checkout/#/pattern/server
Here is a reference for the server-side integration: https://developer.paypal.com/docs/checkout/reference/server-integration/

PHP PayPal Sandbox: Facilitator balance not updated

I'm trying to include the REST API SDK for PHP in my project. Therefore I've tried to execute the given example, e.g. "PayPal Payments - similar to Express Checkout in Classic APIs" using CreatePaymentUsingPayPal.php (and ExecutePayment.php). This works fine, the amount is withdrawn from the buyers account and the transaction is listed in both accounts (buyer and facilitator).
But the amount is not send to the facilitators balance, although the transaction is displayed as "Completed". So what am I missing here? I thought the amount will be send directly to the facilitators balance!??
I've set "Allow payments sent to me in a currency I do not hold:" to "Yes, accept and convert them to Euros" on this page.
So USD and EUR are not processed correctly (amount is not booked on facilitators balance).
Creating a new payment via the following lines (haven't change something from the SDK sample code above):
$payment = new Payment();
$payment->setIntent("sale")
->setPayer($payer)
->setRedirectUrls($redirectUrls)
->setTransactions(array($transaction));
$payment->create($this->apiContext);
After that the success method contains something like that:
// success method; i'm using Symfony 3.3
$paymentId = $request->query->get('paymentId');
$payment = Payment::get($paymentId, $this->apiContext);
$execution = new PaymentExecution();
$execution->setPayerId($request->query->get('PayerID'));
$result = $payment->execute($execution, $this->apiContext);
Payment state is now "approved".

How add notify url to REST API paypal?

I created simple payment in paypal
$payer = new Payer();
$payer->setPaymentMethod("paypal");
$item1 = new Item();
$item1->setName('some name')
->setCurrency('USD')
->setQuantity(1)
->setSku("my id product")
->setPrice(11);
$itemList = new ItemList();
$itemList->setItems(array($item1));
$amount = new Amount();
$amount->setCurrency("USD")
->setTotal(11);
$transaction = new Transaction();
$transaction->setAmount($amount)
->setItemList($itemList)
->setDescription("Payment description")
->setInvoiceNumber(uniqid());
$baseUrl = "https://my_site_address.com";
$redirectUrls = new RedirectUrls();
$redirectUrls->setReturnUrl("$baseUrl/ExecutePayment/?success=true")
->setCancelUrl("$baseUrl/ExecutePayment/?success=false");
$payment = new Payment();
$payment->setIntent("sale")
->setPayer($payer)
->setRedirectUrls($redirectUrls)
->setTransactions(array($transaction));
$request = clone $payment;
try {
$payment->create($apiContext);
} catch (Exception $ex) {
print_r($ex);
exit(1);
}
$approvalUrl = $payment->getApprovalLink();
print_r($approvalUrl);
After execute this code everything is alright, I am redirected to Paypal sandbox and after log in i can make a pay. But I don't know how, where i can set my notify_url?
Anyone can help ?
the direct and short answer to your question is yes you can set a "notifyUrl" via REST API:
$transaction = new Transaction();
$transaction->setAmount($amount)
->setItemList($itemList)
->setDescription(" ...... ")
->setNotifyUrl("your notifyUrl here")
->setInvoiceNumber($invoiceNo);
BUT this is not the correct way receiving REST Notifications. Use "Webhooks" instead (though Webhooks using PHP is soo hacky, but at the end you find a solution. if you need more details on how to do it, just comment this answer).
Back to NotofyUrl:
for testing i was able to receive notifications via notifyUrl, BUT after implementing a webhook, PayPal still sending notifications to my old already removed NotifyUrl for every new Payment. i deleted it from the code, i deactivated IPN in my PayPal account. I tried many things, but PayPal still sending IPNs for every Payment. Now i receive 2 Notofications, one via NofityUrl and the second via WebhookEvent Listener. no idea whats going wrong here. So i wouldn't recommend the usage of notifyUrl, instead just implement a webhookEvent listener.
The parameter for notify url can't be used in REST API calls. If you want to set up the IPN url, you have to set it within your Paypal account:
Here is the steps:
1. Login paypal account
2. Click profile and under Selling preferences, choose Instant Payment Notification preferences
3. Click Choose IPN setting
4. Type the URL that you receive IPN message and choose Enabled

Paypal Checkout Express doesn't show the order details

I'm using the lib PayPal-PHP-SDK on Laravel 4
and I have a problem with showing the order details.
I followed the steps from Paypal Web Checkout
and it doesn't showing me the order details.
it says "You'll be able to see your order details before you pay."
- Image Link
This is the method when I get the link to the payment:
public function checkout()
{
$apiContext = new ApiContext($this->cred, 'Request' . time());
$apiContext->setConfig($this->sdkConfig);
$payer = new Payer();
$payer->setPaymentMethod("paypal");
$amount = new Amount();
$amount->setCurrency("EUR");
$amount->setTotal("10");
$transaction = new Transaction();
$transaction->setDescription("Example");
$transaction->setCustom('abv');
$transaction->setAmount($amount);
$redirectUrls = new RedirectUrls();
$redirectUrls->setReturnUrl(URL::to('/') . '/payment/success/');
$redirectUrls->setCancelUrl(URL::to('/') . '/payment/cancel/');
$payment = new Payment();
$payment->setIntent("sale");
$payment->setPayer($payer);
$payment->setRedirectUrls($redirectUrls);
$payment->setTransactions(array($transaction));
$payment->create($apiContext);
print $payment->getApprovalLink();
}
I don't see any itemized details in your request. It would help to see a sample of the raw request getting sent to PayPal, but from what I can see those parameters are simply not getting included.
On a side note, you may be interested in checking out my PHP class library for PayPal. It's available on GitHub and Packagist and works with Composer, so it works very nicely with Laravel, and it's a lot easier to use than PayPal's SDK. It sets up all the parameters you might want to use, including itemized details, so all you have to do is fill in the parameters and you're done.
As Andrew mentioned, you are missing itemized details in your request. This is how you could do it. http://paypal.github.io/PayPal-PHP-SDK/sample/doc/payments/CreatePaymentUsingPayPal.html
As you can see the create payment has an array of transactions that has an item_list attribute, that needs to be set. You can see how to do so, using our SDK in above link.
We attach a lot of samples in our SDK now, that could be ran in your local machine with just one command. Please follow the instructions here
The command you need to run is:
php -f <paypal SDK Directory>/paypal/rest-api-sdk-php/sample/index.php
This will host a local server at localhost:5000, that would allow you to run the samples located in samples directory.
PayPal-PHP-SDK is getting developed strongly, and we are adding more and more features to it. Please visit our SDK page to view all the supporting documentations, API specs, Samples, etc.

Categories