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/
Related
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 and where can I start debugging Guest Checkout with PayPal?
I had the site running fine for over a year and now suddenly the Guest Checkout on the mobile website stopped working. I'm using PayPal PHP SDK and nothing have been changed in the code. The guest checkout it's just not showing on mobile devices but works fine on desktop.
I have searched the documentation and the forum I saw couple of similar questions but no answer to them.
Things mentioned:
Clear browser cache, not the case random users complaining, but have tried it myself and still no joy.
Your business needs to be set up to accept credit/debit cards, it is as said it worked smoothly over a year, now all of the sudden it doesn't work.
Checked logs on server but there is no error log at all.
I tried to see developers support on PayPal but more or less it only points to "Ask the community" and no real way of contacting technical support.
Update relevant part of code used:
use PayPal\Api\Amount;
use PayPal\Api\Details;
use PayPal\Api\Item;
use PayPal\Api\ItemList;
use PayPal\Api\Payer;
use PayPal\Api\Payment;
use PayPal\Api\RedirectUrls;
use PayPal\Api\Transaction;
use PayPal\Api\ShippingAddress;
$apiContext = new \PayPal\Rest\ApiContext(
new \PayPal\Auth\OAuthTokenCredential(
'XXX', // ClientID
'YYY' // ClientSecret
)
);
$payer = new Payer();
$payer->setPaymentMethod("paypal");
// ### Itemized information
// (Optional) Lets you specify item wise
// information
$item1 = new Item();
$item1->setName($name)
->setCurrency($currency)
->setQuantity($quantity)
->setPrice($price)
->setDescription($description)
->setSku($productid);
$myitem=array($item1);
$itemList->setItems($myitem);
$details = new Details();
$details->setShipping($shipping)
->setTax($tax)
->setSubtotal($subtotal);
$amount = new Amount();
$amount->setCurrency($currency)
->setTotal($totalprice)
->setDetails($details);
$transaction = new Transaction();
$transaction->setAmount($amount)
->setItemList($itemList)
->setDescription($description);
$baseUrl = "https://........";
$redirectUrls = new RedirectUrls();
$redirectUrls->setReturnUrl("$baseUrl/execute.php")
->setCancelUrl("$baseUrl/cancel_execute.php?id=$productid");
$payment = new Payment();
$payment->setIntent("sale")
->setPayer($payer)
->setRedirectUrls($redirectUrls)
->setTransactions(array($transaction));
try {
$payment->create($apiContext);
} catch (PayPal\Exception\PPConnectionException $ex) {
echo "Exception: " . $ex->getMessage() . PHP_EOL;
//echo "<pre>";
//var_dump($ex->getData());
exit(1);
}
foreach($payment->getLinks() as $link) {
if($link->getRel() == 'approval_url') {
$redirectUrl = $link->getHref();
break;
}
}
$payment_id = $payment->getId();
Ok so here is the update, after 2 days of hell and countless backward and forward with PayPal, I spoke with different persons on different channels and I got the following answers:
1) This never worked on mobile because it is insecure, it does work on iPad. - What a joke, it worked for over a year and actually it doesn't work on iPad at the moment.
2) Your account is not set up to accept Credit Card payment. - GGGRRRRR I know it isn't I was doing PayPal type sale, which should allow a guest checkout!
3) This is from their ticketing system (Sorry what in English please??? )
I have enabled this option for you. Please note that direct payments
by credit card will not work 100% of the cases, and if you do some
tests maybe does not work.
The process that allows or not to pay as a guest is a process is
beyond our control, we can only Maximize the allowance.
4) If a customer tried to use the card to many times it won't allow it to use it. - But they had NO CHANCE of even trying to use it because they are only presented with a sign up page
5) There is no guest option - LOL!!!!!!!!!
6) Try clearing the cache - LOL!!!!!!!
Solution?
I ended up paying for "PayPal Pro" rewriting my checkout process and now I'm processing Credit Cards directly, currently in Sandbox and waiting for PayPal to vet my webpage that it does satisfy their needs and give me the green light for the pro account (which should be no later than Monday).
Did the problem actually got sorted? - NO
If anybody stumbles across this post and know a way on how to get Guest checkout to work, please do not hesitate to share, other than that my personal conclusion is that after this weekend I will seek to use another (possibly cheaper) payment processing gateway and only keep PayPal as a secondary option is somebody wants to sign out using PayPal Express.
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
I am new to Rest Api's . I am trying to create a payment using paypal express checkout via php sdk rest api.I have downloaded and installed their official sdk.
Now I can create a normal payment and everything is working fine.Now I want to set landing page type to billing.In documentation page I have been told to set landing_page_type to billing. How can I do that in my php script.
Link : https://developer.paypal.com/docs/api/#flowconfig-object
My php script looks something like
$transaction = new Transaction();
$transaction->setAmount($amount)
->setItemList($itemList)
->setDescription("Payment description")
->setInvoiceNumber(uniqid());
$payment = new Payment();
$payment->setIntent("sale")
->setPayer($payer)
->setRedirectUrls($redirectUrls)
->setTransactions(array($transaction));
$payment->create($apiContext);
So As for I understood ,I must create new object named flow config and add landing page to it.I tried something like
$billing = new FlowConfig();
$billing->setLandingPageType("billing");
What to do next ? How to integrage this $billing in my $payment
To set landing page type, you should create a Payment Experience and specify the landing page in the request:
https://developer.paypal.com/docs/integration/direct/rest-experience-overview/
https://developer.paypal.com/docs/api/#payment-experience
PHP SDK sample:
https://github.com/paypal/PayPal-PHP-SDK/blob/master/sample/payment-experience/CreateWebProfile.php
There is a payment experience profile ID in the response.
Then add the ExperienceProfileId to the request of Create a Payment like below:
$payment->setIntent("sale")
->setPayer($payer)
->setRedirectUrls($redirectUrls)
->setTransactions(array($transaction));
->setExperienceProfileId(**********)
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.