Easier way to integrate PayPal express checkout?

13,512

Solution 1

This is how i automatically redirect to PayPal with all the form details;

<form action="https://www.paypal.com/cgi-bin/webscr" method="post" id="paypal">

 <input type="hidden" name="cmd" value="_xclick" />
  <input type="hidden" name="cbt" value="Return to example" />
 <input type="hidden" name="business" value="email" />
 <input type="hidden" name="item_name" value="example Purchase" />
 <input type="hidden" name="amount" value="9.99">
 <input type="hidden" name="button_subtype" value="services" />
 <input type="hidden" name="no_shipping" value="1">
 <input type="hidden" name="return" value="URL" />
 <input type="hidden" name="notify_url" value="URL"/>
 <input type="hidden" name="cancel_return" value="URL" />
<input type="hidden" name="currency_code" value="USD"/>
<input type="hidden" name="image_url" value="" />
<input type="hidden" id="custom" name="custom" value="invoice_id to track"/>
 <input type="hidden" class="btn btn-primary" style="width:100%" alt="PayPal - The safer, easier way to pay online!"/>

</form>

For multiple products, you can simply add more products to the form, example;

<input type="hidden" name="item_name_1" value="Item #1">
<input type="hidden" name="amount_1" value="1.00">
<input type="hidden" name="item_name_2" value="Item #2">
<input type="hidden" name="amount_2" value="2.00">

However, using this method is not all great

All the data would need to be generated with PHP and input into the page, you would also need to check the transaction when the IPN calls back to ensure its been paid.

<script type="text/javascript">
  function myfunc () {
     var frm = document.getElementById("paypal");
     frm.submit();
  }
  window.onload = myfunc;
</script>

Solution 2

You may want to use the new PayPal SDK. They have a good set of sample code, including code for express checkout and IPN. Try here https://www.x.com/developers/paypal/documentation-tools/paypal-sdk-index Get the SDK for Express checkout. At this time, they should be at SDK 98 for PHP.

You won't have to worry about the Curl, the SDK takes care of all that for you. A typical call might be something like this.

$setECResponse = $paypalService->SetExpressCheckout($setECReq);

This line of code is modeled after the samples. It's all object oriented. They provide you with classes. In this case there is a request object you fill out, the examples show exactly how to do it; just use the samples as your template.

It sounds like you want to do PayPal Express checkout, this way you won't have to handle credit cards or anything like that. The user is redirected to the PayPal website and all the financial transactions happen there. The user is redirected back to your site. Then you have a page where the user can review the order and click submit if they approve. When the user clicks submit, you call a PayPal API telling PayPal that the transaction is approved. PayPal then executes the transaction and sends you back a confirmation with a transaction id. You can then call getTransactionDetails and display the confirmation to the customer. You can additionally put those transaction details into a database.

Here are the APIs you can call for this. These are modeled closely to the sample code they provide

$paypalService->SetExpressCheckout($setECReq);

control goes to PayPal URL, and the user goes through a few pages there. control returns to you.

your order review page $paypalService->GetExpressCheckoutDetails($getExpressCheckoutReq);

your order confirmation page

$paypalService->GetExpressCheckoutDetails($getECReq);

$paypalService->DoExpressCheckoutPayment($DoECReq);

Tells PayPal to do the transaction.

$paypalService->GetTransactionDetails($request);

Here you can put transaction details into a database. You can also send yourself a mail with all the details, that way you will know whenever a transaction occurs.

IPN can be a bit tricky. There is a sample IPN listener that they provide, that will help. You will need to set up your listener URL on the PayPal website. You will also need to set up an SSL certificate.

The SDKs are fairly new, but PayPal is working on an even newer way to do things, developer.paypal.com. It just came out within the last month or so. You may want to look into that too.

Share:
13,512

Related videos on Youtube

Jon
Author by

Jon

Updated on June 04, 2022

Comments

  • Jon
    Jon almost 2 years

    I have built a product generation and display plugin for the Wordpress CMS and I am now trying to integrate some form of PayPal integration for the checkout process.

    I have the cart, the products, the shipping, totals, all that figured out on my end and I was hoping someone could point me in the simplest direction of sending this information to PayPal. I understand some methods of doing this are not that secure and others make you jump through hoops like some sort of show dog. I've been trying to learn how to use cURL and then how to get it to work with PHP - it really seems like a bit of a mess. I do now have cURL working on my WAMP server ... but..

    Is there a better way or should I continue to learn cURL?

    I can format the data however it needs to be to send off to PayPal and would not mind doing this with JavaScript - this is not a pay-wall and every order is checked for accuracy by a human - so someone messing with the client-side script will not bother me. I also definitely want to send them to PayPal, I want no part of storing/processing their credit card information. It would, however, be nice to have IPN. Can someone point me in the right direction or assure me that I already am headed that way?

    Thanks alot.

  • Jon
    Jon about 11 years
    Thank you for the quick reply. Will this work for a whole cart of products as well or just the one at a time? Could you provide any information on how you handle the IPN?
  • Jon
    Jon about 11 years
    Thanks again, generating the HTML needed for this form to get sent off to PayPal should not be a problem at all. It looks like the only missing puzzle piece would be figuring out and handling the IPN.
  • Harry Beasant
    Harry Beasant about 11 years
    Yeah that is one of the most annoying parts of the process, might be worth you going onto the paypal documentation website and grabbing the default PHP IPN script, then just query the database using the unique_id you put as a custom field in the form to grab the transaction info and check everything.
  • Maruti Mohanty
    Maruti Mohanty over 10 years
    Guys I have a question, the form action is direct to the paypal, and how do u check the security. I mean what if someone make the changes in the html and checks out a lesser amount... how do u check all this before submittng the form
  • Admin
    Admin over 10 years
    That you will confirm in IPN (Instant Payment Notification) you receive from PayPal, and only then proceed with whatever actions you would allow for successful payment.