Skip to content

Instantly share code, notes, and snippets.

@robjshaw
Last active June 4, 2021 22:29
Show Gist options
  • Save robjshaw/5002921 to your computer and use it in GitHub Desktop.
Save robjshaw/5002921 to your computer and use it in GitHub Desktop.
paypal webcheckout with coldfusion
<!--- STEP 1 - ?event=shop-cart.cfm --->
<cfscript>
stDetails = structNew();
stDetails.cartTotal = 200.00;
stDetails.invoiceno = 3;
</cfscript>
<cfhttp url="https://api-3t.paypal.com/nvp" method="post">
<!--- auth --->
<cfhttpparam type="formfield" name="USER" value="#application.pp.username#" />
<cfhttpparam type="formfield" name="PWD" value="#application.pp.password#" />
<cfhttpparam type="formfield" name="SIGNATURE" value="#application.pp.signature#" />
<!--- order info --->
<cfhttpparam type="formfield" name="METHOD" value="SetExpressCheckout" />
<cfhttpparam type="formfield" name="VERSION" value="93.0" />
<cfhttpparam type="formfield" name="LOCALE" value="AU" />
<cfhttpparam type="formfield" name="PAYMENTREQUEST_0_AMT" value="#stDetails.cartTotal#" />
<cfhttpparam type="formfield" name="PAYMENTREQUEST_0_INVNUM" value="#stDetails.invoiceno#" />
<cfhttpparam type="formfield" name="PAYMENTREQUEST_0_CURRENCYCODE" value="AUD" />
<cfhttpparam type="formfield" name="PAYMENTREQUEST_0_DESC" value="your products" />
<cfhttpparam type="formfield" name="returnURL" value="http://#cgi.server_name#/index.cfm?event=shop-cart-process" />
<cfhttpparam type="formfield" name="cancelURL" value="http://#cgi.server_name#/index.cfm?event=shop-cart-process" />
</cfhttp>
<!--- credit to @davidsirr on nice processing of the response ---->
<cfset PPresponse = {token='',ack='',CORRELATIONID=''} />
<cfif cfhttp.filecontent GT ''>
<cfloop list="#cfhttp.filecontent#" delimiters="&" index="curItem">
<cfset PPresponse[listFirst(curItem,'=')] = URLDecode(listLast(curItem,'=')) />
</cfloop>
</cfif>
<cfif PPresponse.ack contains 'SUCCESS' AND PPresponse.token NEQ ''>
<cflocation addtoken="no" url="https://www.paypal.com/webscr?cmd=_express-checkout&token=#PPresponse.token#" />
<cfelse>
<cfthrow type="cart.paypal" message="An error occured connecting to PayPal" />
</cfif>
<!--- Step 2 - ?event=shop-cart-process --->
<cfhttp url="https://api-3t.paypal.com/nvp" method="post">
<!--- auth --->
<cfhttpparam type="formfield" name="USER" value="#application.pp.username#" />
<cfhttpparam type="formfield" name="PWD" value="#application.pp.password#" />
<cfhttpparam type="formfield" name="SIGNATURE" value="#application.pp.signature#" />
<!--- order info --->
<cfhttpparam type="formfield" name="METHOD" value="GetExpressCheckoutDetails" />
<cfhttpparam type="formfield" name="VERSION" value="93.0" />
<cfhttpparam type="formfield" name="token" value="#url.token#" />
</cfhttp>
<cfset PPresponse = {} />
<cfif cfhttp.filecontent GT ''>
<cfloop list="#cfhttp.filecontent#" delimiters="&" index="curItem">
<cfset PPresponse[listFirst(curItem,'=')] = URLDecode(listLast(curItem,'=')) />
</cfloop>
</cfif>
<cfif structKeyExists(PPresponse,'ack') AND PPresponse.ack eq "success">
<cfscript>
session.cart.payerid = url.payerid;
session.cart.token = url.token;
session.shipping = {};
session.shipping.email = PPresponse.email;
session.shipping.deliveryName = PPresponse.firstname & ' ' & PPresponse.lastname;
session.shipping.countryCode = PPresponse.shiptocountrycode;
session.shipping.country = PPresponse.shiptocountryname;
session.shipping.deliveryName = PPresponse.shiptoname;
session.shipping.suburb = PPresponse.shiptocity;
session.shipping.state = PPresponse.shiptostate;
session.shipping.address1 = PPresponse.shiptostreet;
if(structKeyExists(PPresponse,'shiptostreet2')){
session.shipping.address2 = PPresponse.shiptostreet2;
}else{
session.shipping.address2 = '';
}
session.shipping.postcode = PPresponse.shiptozip;
</cfscript>
<cflocation url="?view=shop-cart-success" addtoken="false" />
<cfelse>
<cfset errorMessage = 'Checkout failed: An error occured connecting to PayPal'>
</cfif>
<!--- Step 3 - ?view=shop-cart-success --->
<cfhttp url="https://api-3t.paypal.com/nvp" method="post">
<!--- auth --->
<cfhttpparam type="formfield" name="USER" value="#application.pp.username#" />
<cfhttpparam type="formfield" name="PWD" value="#application.pp.password#" />
<cfhttpparam type="formfield" name="SIGNATURE" value="#application.pp.signature#" />
<!--- order info --->
<cfhttpparam type="formfield" name="METHOD" value="DoExpressCheckoutPayment" />
<cfhttpparam type="formfield" name="VERSION" value="93.0" />
<cfhttpparam type="formfield" name="LOCALE" value="AU" />
<cfhttpparam type="formfield" name="PAYERID" value="#session.cart.payerid#" />
<cfhttpparam type="formfield" name="TOKEN" value="#session.cart.token#" />
<cfhttpparam type="formfield" name="PAYMENTREQUEST_0_AMT" value="#session.cart.cartTotal#" />
<cfhttpparam type="formfield" name="PAYMENTREQUEST_0_INVNUM" value="#session.cart.invoiceid#" />
<cfhttpparam type="formfield" name="PAYMENTREQUEST_0_CURRENCYCODE" value="AUD" />
<cfhttpparam type="formfield" name="PAYMENTREQUEST_0_DESC" value="your products" />
</cfhttp>
<cfset PPresponse = {token='',ack='',PAYMENTINFO_0_TransactionID=''} />
<cfif cfhttp.filecontent GT ''>
<cfloop list="#cfhttp.filecontent#" delimiters="&" index="curItem">
<cfset PPresponse[listFirst(curItem,'=')] = URLDecode(listLast(curItem,'=')) />
</cfloop>
</cfif>
<!--- check on the success --->
<cfif PPresponse.ack EQ "success">
<cfscript>
session.cart.success = true;
</cfscript>
<cflocation url="/index.cfm?view=shop-cart-thankyou" addtoken="false" />
<cfelse>
<cfthrow type="paypal.DoExpressCheckoutPayment" />
</cfif>
<!--- process a successful order however you like! --->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment