PHP SuitHow to implement PayPal Express checkout for simple (and single) payment on your web site?
PayPal allows their users to implement checkout functions using their API. But implementing the full API is not a piece of cake. There are some libraries on the web, but the most of them are paid, closed or too complex.
If you wanna implement simple and single-item payments (i.e. "pay for service" or "pay for source code"), you can use my, free and open, library described here.
This library was written as a part of my framework, but it's usable standalone too, I hope. ;)
How to implement, step-by-step:
There are two demo files in the /controllers folder. The main payment script is the pay.php. You should see everything is straightforward and simple. You should create an instantion of the PayPal class and call their doExpressCheckout method. This method has these parameters:
If everything is OK, the user is redirected to PayPal page to log in and confirm the payment. If something was wrong, the array is returned. This array contains the error information (Response codes).
If everything went OK, the user is redirected to PayPal page, s/he can confirm or cancel his/her payment, and then is redirected back to your web, to the URL defined in the getReturnTo() resp. getReturnToCancel() functions, as discussed above.
In the "cancel" page you haven't to do anything special, just say "Your payment was cancelled".
In the "return" page (check /controllers/ppreturn.php) you have to do one simple operation - call the doPayment() method of the PayPal object. This method calls the API function which confirms the money transfer and execute it. The doPayment() method returns the response code you can use to decide if the payment is OK (and display serial number or unlock the access or something else).
You can call the getCheckoutDetails() method to obtain the detail information about the transaction. The list of returned values is very rich. Here is the invoice number (INVNUM) you can use to pair the payment with the appropriate order.
If you want to play with this library at PayPal Sandbox, you have to generate PayPal object as new PayPal();. But on the production server you have use new PayPal(true); to prevent unwanted side effect, such as money lost... ;)
Legal disclaimer: THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR, MARTIN MALY, BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Useful links:
PayPal API overview
PayPal API reference
Advanced:
Customizing Express Checkout
Obtain the library from Google Code: svn checkout http://paypalphp.googlecode.com/svn/trunk/ paypalphp-read-only
| Attachment | Date | Size |
|---|---|---|
| 03/10/08 10:35 am | 6.34 KB |