The PayPal payment gateway and 4D Business Kit 2.2 Useful informations This document describes how to integrate 4D Business Kit 2.2 with the PayPal payment gateway. In this documentation, we suppose that the owner of the TEST shop has a PayPal merchant account whose login is: merchant@shop.com. You will need to replace this login by your own PayPal merchant account in the example pages. NB: For a PayPal account to receive payment with credit cards, the PayPal account needs to be a Premier or Business account. PayPal gives some documentation about its payment gateway. Two manuals are used here. 1. The PayPal Shopping Cart Manual https://www.paypal.com/en_us/pdf/shopping_cart.pdf In this manual, we use the following chapter: Add PayPal Checkout to Your 3 rd Party Cart. Method 1. Passing the Aggregate Cart Amount to PayPal. 2. The PayPal Instant Payment Notification (IPN) Manual https://www.paypal.com/en_us/pdf/ipn.pdf Part I: Add PayPal Checkout to 4D Business Kit According to The PayPal Shopping Cart Manual: You may aggregate your entire shopping cart and pass the total amount into PayPal s Buy Now Code (that is, you will need to post a single name for the entire cart and the total price of the cart s contents as though it were a purchase of a single item). 4D Business Kit provides an example page called payment_paypal.htm (NB: This page is linked to the TEST shop). This page describes a way to pass information to PayPal through an html form. Content of the payment_paypal.htm html page: <!--#4DBK//Currency--> <!--#4DBKVarSet/VT02=4DBKField/CdCurrency--> <!--#4DBK//Amount--> <!--#4DBKVarSet/VT01=4DBKField/PrOrderINV.4DBKVar/VT02,#####0.00--> <!--#4DBK//business field--> <!--#4DBKVarSet/VT15= merchant@shop.com --> <!--#4DBK//Transaction ID for 4D Business Kit--> <!--#4DBKOrderCodeNew-->
<!--#4DBKVarSet/VT16=4DBKOrderCode--> <!--#4DBK//Custom CallBack Parameter--> <!--#4DBKVarSet/VT17=4DBKField/SessionID,X--> <form method="post" action="https://www.paypal.com/cgi-bin/webscr"> <!--Allow posting extended variables--> <input type=hidden name="cmd" value="_ext-enter"> <input type=hidden name="redirect_cmd" value="_xclick"> <input type=hidden name="business" value="<!--#4dbkvar/vt15-->"> <input type=hidden name="item_name" value="<!--#4dbkvar/vt14-->"> <input type=hidden name="amount" value="<!--#4dbkvar/vt01-->"> <input type=hidden name="currency_code" value="<!--#4dbkvar/vt02-->"> <input type=hidden name="return" value="http://<!--#4dbkfield/host-- >/Test_Site/WebPagesUS/home.htm<!--#4DBKField/SessionID-->"> <input type=hidden name="item_number" value="<!--#4dbkvar/vt16-->"> <input type=hidden name="custom" value="<!--#4dbkvar/vt17-->"> <input type=hidden name="notify_url" value="http://<!--#4dbkfield/host-- >/4daction/4DBKHttpPostCallBack/custom/payment_PayPal_cb.htm/1"> <!--Extended variables--> <input type=hidden name="email" value="<!--#4dbkfield/cusemail-->"> <input type=hidden name="first_name" value="<!--#4dbkfield/cuslabelship,lst&1&space-- >"> <input type=hidden name="last_name" value="<!--#4dbkfield/cuslabelship,lst&2&space-- >"> <input type=hidden name="address1" value="<!--#4dbkfield/cusstreetship,lst&1&cr-->"> <input type=hidden name="address2" value="<!--#4dbkfield/cusstreetship,lst&2&cr-->"> <input type=hidden name="city" value="<!--#4dbkfield/cuscityinvo-->"> <input type=hidden name="state" value="<!--#4dbkfield/cusstateinvo-->"> <input type=hidden name="zip" value="<!--#4dbkfield/cuszipinvo-->"> <input type=hidden name="night_phone_a" value="<!--#4dbkfield/cusphone,sub&1&3-->"> <input type=hidden name="night_phone_b" value="<!--#4dbkfield/cusphone,sub&4&3-->"> <input type=hidden name="night_phone_c" value="<!--#4dbkfield/cusphone,sub&7&3-->"> <input type=image" src="http://images.paypal.com/images/x-click-but01.gif" name="submit" alt="make payment with PayPal - it's fast, free and secure!" > </form> Explanations about the hidden fields used in this form: Some parameters are required for the payment gateway, they are: - business : Email address on your PayPal account. - Item_name : this will be the name for the whole shopping Cart (127 characters limit). Here we are using a summary of our shopping cart but we could also use the number of the order to avoid the 127 characters limitation. - amount : this will be the total price of all items in the Shopping Cart. - image : The image for the button your buyer will press to initiate the PayPal payment process. You can substitute your own image by replacing the src with the URL of your image. Others additional parameters used by 4D Business Kit: - custom: Described in in the PayPal Shopping Cart Manual as an Optional passthrough variable that will never be presented to your customer. 256-character limit. In fact, we need to use a custom field in order to store the client s session
ID. This way, the gateway will send us back this session ID with the result of the transaction and we will be able to acknowledge the payment. - return : the internet URL where your customer will be returned after completing payment. - notify_url: This hidden field allows to set a call-back URL. This URL will be used by PayPal to warn 4D Business Kit of the result of a transaction through PayPal Instant Payment Notification process. - item_number: we use this field to store the number of the order in 4D Business Kit The callback URL used by PayPal to send the result of the transaction is: http://<!--#4dbkfield/host-- >/4daction/4DBKHttpPostCallBack/custom/payment_PayPal_cb.htm/1 About parameters used with the 4DBKHttpPostCallBack entry point: - «custom» : this is the name of the field posted by the payment gateway in which the session ID is stored. - «payment_paypal_cb.htm» : this is the name of the script that will be triggered by the 4DBKHttpPostCallBack entry point. - «1» : this parameter means that 4D Business Kit will create a debug file associated to the excution of the script. This file will be found in a subfolder Gateway of the Logs folder of the current site. Part II : Use PayPal Instant Payment Notification According to PayPal Instant Payment Notification Manual (IPN): Instant Payment Notification allows you to integrate your PayPal payments with your website s back-end operations, so you get immediate notification and authentication of the PayPal payments you receive. Content of the script payment_paypal_cb.htm which is the target of PayPal Instant Payment Notification <!--#4DBKStoreSet/TEST--> <!--#4DBKVarSet/VT10=4DBKHttpPostParms/payment_status--> payment_status:<!--#4dbkvar/vt10--> <!--#4DBKVarSet/VT11=4DBKHttpPostParms/txn_id--> txn_id:<!--#4dbkvar/vt11--> <!--#4DBKVarSet/VT12=4DBKHttpPostParms/receiver_email--> receiver_email:<!--#4dbkvar/vt12--> <!--#4DBKVarSet/VT13=4DBKHttpPostParms/mc_gross--> mc_gross:<!--#4dbkvar/vt13--> <!--#4DBKVarSet/VT14=4DBKHttpPostParms/mc_currency--> mc_currency:<!--#4dbkvar/vt14--> <!--#4DBKVarSet/VT15=4DBKHttpPostParms/item_name--> item_name:<!--#4dbkvar/vt15--> <!--#4DBKVarSet/VT16=4DBKHttpPostParms/item_number--> item_number:<!--#4dbkvar/vt16--> <!--#4DBKVarSet/VT20=4DBKHttpPostParms--> Full POST sent by the IPN:<!--#4DBKVar/VT20--> <!--#4DBKVarSet/VT20+="cmd=_notify-validate&"--> Modified POST:<!--#4DBKVar/VT20-->
We have to confirm to PayPal that 4DBK has received the Instant Payment Notification by constructing an HTTP POST <!--#4DBKScriptProcess/US_PayPal.txt,4DBKVar/VT20,,,,,1--> PAYPAL RESPONSE :<!--#4DBKScriptText--> <!--#4DBKHttpPostResponse/OK--> <!--#4DBKVarSet/VT17=4DBKOrderCode--> Order Code (has to be the same as item_number):<!--#4dbkvar/vt17--> <!--#4DBKVarSet/VT18=4DBKField/PrOrderINV,#####0.00--> Total amount of the order (has to be the same as mc_gross): <!--#4DBKVar/VT18--> <!--#4DBKVarSet/VT30=4DBKScriptText--> <!--#4DBKIf("4DBKVar/VT30"="VERIFIED")--> <!--#4DBKIf("4DBKVar/VT10"="Completed")--> <!--#4DBKIf("4DBKVar/VT12"="merchant@shop.com")--> <!--#4DBKIf("4DBKVar/VT13"="4DBKVar/VT18")--> <!--#4DBKIf("4DBKVar/VT16"="4DBKVar/VT17")--> ORDER VALIDATED <!--#4DBKOrderValidate--> <!--#4DBKOrderClear--> ITEM NUMBER IS NOT CORRECT PRICE IS NOT CORRECT FOR THE ORDER Receiver_email is not correct Payment_status is not completed INVALID ANSWER Explanations about what is done in this script file: First, we use the 4DBKHttpPostParms command to get the value of some of the fields send by the payment gateway through its POST. The value of those fields are mandatory to do some checking before validating the order. According to PayPal Instant Payment Notification Manual (IPN), here is what is to be done after our server has received the Instant Payment Notification: Once your server has received the Instant Payment Notification, you will need to confirm it by constructing an HTTP POST to PayPal. Your POST should be sent to https://www.paypal.com/cgi-bin/webscr You must post all of the form variables you received exactly as you received them. You will also need to append a variable named cmd with the value _notifyvalidate (e.g. cmd=_notify-validate) to the POST string. PayPal will respond to the post with a single word, VERIFIED or INVALID, in the body of the response. In our script, the 4DBKHttpPostParms command allows us to get the whole POST coming from PayPal
We use the 4DBKScriptProcess command to post-back the IPN data. This command will execute an HTTP request depending on what has been specified in the parameter script file US_PayPal.txt. In this script file we will trigger 4D Business Kit to do a POST in secured mode at the following addres: www.paypal.com/cgi-bin/webscr. The content of this POST will be specified through the arg1 parameter which store the original modified POST. Content of the script file US_PayPal.txt ####################################### # Payment gateway script for 4D Business Kit # # PayPal # ####################################### # 4DBKScriptText returns "VERIFIED" or "INVALID" # definition [platform=]win # how to send the request [mode=]http [host=]www.paypal.com [port=]443 # end of request : if one of these string is found (PayPal does not use Content-Length) [erx=]verified [erx2=]invalid # result processing [res_start=] [res_offset=] [res_end=] # fields processing [res_fields=][crlf] [res_code1=] [res_code2=] [res_text=]11 [res_tid=] # the request to send [req=]post /cgi-bin/webscr HTTP/1.1[crlf] Host: www.paypal.com[crlf] Content-type: application/x-www-form-urlencoded[crlf] Content-length: [length][crlf][crlf] [arg1] After the 4DBKScriptProcess command has executed, the 4DBKScriptText variable will contain PayPal response: either VERIFIED or INVALID. When a VERIFIED response is received, the following checks are performed: - We Check that the payment_status is Completed. - We make sure the receiver_email is an email address registered in our PayPal account.
- We Check that the price ( mc_gross ) and item ( item_number ) are correct for the order. As soon as those checks are validated, we can validate the order.