mannyotr
mannyotr

Reputation: 83

PayPal API Process

I have read and read through the links and documentation about the PayPal API, but to be honest, I am pretty confused as to what it is I need to do.

I am trying to set up a simple API where a user on my website clicks a button which takes him to PayPal to make a payment. After he makes the payment, all I want is for the PayPal API to update a record on my database with the confirmation.

So far the process works perfectly one way. I am using this code to get the users to make their payments:

<form action="https://www.paypal.com/cgi-bin/webscr" method="post" target="_top">
<input type="hidden" name="cmd" value="_xclick">
<input type="hidden" name="business" value="[email protected]">
<input type="hidden" name="currency_code" value="USD">
<input type="hidden" name="item_name" value="Gift Certificate">
<input type="hidden" name="item_number" value="RI001CI3481">
<input type="hidden" name="amount" value="313">
<input type="hidden" name="return" value="http://mywebsite.com/paypal/thankyou">
<input type="submit" value="PayPal">
</form>

After the user finishes the transaction, they are returned to my 'return' page.

I get an email when the transaction is complete. That's how I now a payment has been made. I then go to PayPal, confirm the payment, and update my database record to mark the transaction complete.

Now, what do I have to do to have PayPal automatically update my database when that payment has been made? If you could point me to a simple to follow document or sample, that would be great. As I said, I have read through some of the available documentation online but for someone like me it's a bit confusing.

Thanks, Manny

Upvotes: 1

Views: 199

Answers (3)

Jason Harmon
Jason Harmon

Reputation: 129

While the overall coverage isn't yet at the same level of IPN, PayPal also now has Webhooks: https://developer.paypal.com/docs/integration/direct/rest-webhooks-overview/ Webhooks are a much more modern form factor than IPN, and is supported in the REST SDKs, hopefully that helps.

Upvotes: 1

Joel
Joel

Reputation: 87

I think that IPN is one answer, but it is prone to issues and can fail if your servers or PayPal's servers are having issues. Many people who use IPN have only one server, and perform maintenance late at night, and IPN may attempt a notification but fails because the server is down for maintenance. IPN will just fail silently. A better alternative is to use an API like Express Checkout where you set up the look and feel of the page, set txn details, etc. with SetExpressCheckout and take the customer to the PayPal page to check out, and afterwards they are returned to your site. At this point you run DoExpressCheckoutPayment to complete the transaction and then when your response contains "ACK=Success" you can call GetExpressCheckoutDetails to get more detail than you would see with IPN and not have to worry if you didn't receive a response from PayPal as with IPN. You would have a request / response as with any API call and you can log your responses to be able to see when things go wrong, as well as to get details of the transaction. Often it seems that people explaining EC and even PayPal docs show to call setEC, then getEc, then doEC but I usually call set, do and then get once the txn is successful. I'm sure there are scenarios when someone would need / want to call set, get, do, but for IPN we only care once the txn is successful. ALSO, IPN will not send unless there is a txn. You can set your code to allow for situations when you receive an error and act accordingly, like when you get an error for declined card or similar. You can log the error, send email / SMS, log to DB table, etc.

Here is the documentation to integrate Express Checkout: https://developer.paypal.com/docs/classic/products/express-checkout/ Tons of links here since EC can be used for order / auth / capture or simple sales or subscriptions, etc.

Here is the list of parameters for SetExpressCheckout: https://developer.paypal.com/docs/classic/api/merchant/SetExpressCheckout_API_Operation_NVP/

Here is the list of parameters for DoExpressCheckoutPayment: https://developer.paypal.com/docs/classic/api/merchant/DoExpressCheckoutPayment_API_Operation_NVP/

Here is the list of parameters for GetExpressCheckoutDetails: https://developer.paypal.com/docs/classic/api/merchant/GetExpressCheckoutDetails_API_Operation_NVP/

Upvotes: 1

Drew Angell
Drew Angell

Reputation: 26056

What you're showing here is not using the API. PayPal Standard is just basic HTML form method for setting up payments with PayPal. It sounds like that part is already working for you how you need, so that's fine.

To update the database, you want to use Instant Payment Notification (IPN). It's still not technically an API, but it is a push service that will POST transaction data about any transaction that hits your PayPal account to a listener script that you have setup on your server.

Within this script you can process the data however you need to. Update your database, generate custom email notifications, hit 3rd party web services, etc.

There are some good IPN templates available for PHP on GitHub / Packagist. PayPal also provides a very basic starter template for IPN.

Upvotes: 1

Related Questions