[Zope] Simple paypal IPN external method or product

Marcel Maré list at webtothemax.com
Sun Jul 10 10:40:49 EDT 2005


Maybe you got it solved already, but here is some of my stuff (which 
works ;-):

I looked into PloneMall, but it's code is in some closed CVS and looked 
very complex. I decided to create my own shop starting from the product 
Simple Cart Item (by Aaron). In the end it was much expanded.


This is the code that does the PayPal checkout - largely stolen from 
the product Simple Cart Item:

from Products.PythonScripts.standard import urlencode

'''This is the digester for the cart string.  Currently this digester 
formats a
string to be sent to a Pay Pal gateway, but could be modifed for other 
gateways'''

portal_props=context.portal_properties
shop_props = portal_props.mshop_properties
shop_tool = context.mshop_tool

r= context.REQUEST
s = r.SESSION

# retrieve the cartList
cartlist=s.get("cartList", [])

# calculate items total
cart_total = 0.0
for item in cartlist:
     item_total = item['item_total']
     cart_total += item_total

# calculate payment costs (depends on items bought)
shipping= shop_tool.getPaymentCosts("paypal", cart_total)
shipping += shop_props.shipping


#cart preamble
cart_commands = {"cmd"           : "_cart",
                  "upload"        : "1",
                  "business"      : shop_props.paypal_business,
                  "currency_code" : "EUR",
                  "invoice"       : DateTime(),
                  "no_shipping"   : "0",
                  "handling_cart" : shipping,
                  "cancel_return" : context.portal_url() + 
'/sys/pp_cancel_callback',
                  "return"        : context.portal_url() + 
'/sys/pp_success_callback',
                  "image_url"     : context.portal_url() + 
'/pp_shoplogo.gif',
                  "notify_url"    : context.portal_url() + '/pp_ipn',
                  }


# formats cart items
l = 0

for item in cartlist:
     cart_item = item
     l+=1
     d ={'item_name_%d' % l : cart_item['name'],
         'on0_%d' % l       : 'variant',
         'os0_%d' % l       : cart_item['variant'],
         'on1_%d' % l       : 'maat',
         'os1_%d' % l       : cart_item['item_size'],
         'amount_%d' % l    : cart_item['price'],
         'quantity_%d' % l  : cart_item['quantity']
         }

     cart_commands.update(d)

#points to (sandbox) paypal account
if shop_props.paypal_use_sandbox:
     url = shop_props.paypal_sandbox_url
else:
     url = shop_props.paypal_url

context.plone_log("Commands send to paypal url (%s):%s" % (url, 
str(cart_commands)))

redirect_url = '%s?%s' % (url, urlencode(cart_commands))

state.setNextAction('redirect_to:string:'+redirect_url)
return state


=======================
This script calls into a plone tool (MShopTool): it is only a script to 
handle the callback from PayPal:

# MShopTool does the real work
mst = context.mshop_tool
result = mst.handlePayPalIpn(context.REQUEST)

=======================
This is the relevant code from the tool MShopTool:


     def handlePayPalIpn(self, request):
         log("Starting PayPal IPN")
         portal_props=getToolByName(self, 'portal_properties')
         shop_props = portal_props.mshop_properties

         #points to (sandbox) paypal account
         if shop_props.paypal_use_sandbox:
             url = shop_props.paypal_sandbox_url
         else:
             url = shop_props.paypal_url

         # copy the incoming form vars
         formvars = request.form
         # append validation command
         formvars['cmd']='_notify-validate'
         # remove Zope funny var:
         if '-C' in formvars:
             del formvars['-C']
         data = urllib.urlencode(formvars)

         response = urllib.urlopen(url,data).read()
         if response=="VERIFIED":
             log("PayPal IPN verification call: VERIFIED response. \n", 
formvars )
             self.notifyShopPayPalResult(formvars)
         elif (response=="INVALID" or 
formvars['receiver_email']!=shop_props.paypal_business):   # message 
compromised or not intended for us
             logerror("PayPal IPN verification call: INVALID response. 
\n", formvars )
         else:
             logerror("PayPal IPN verification call failed. \n", 
formvars )


HTH


M.J. Maré
WebToTheMax

On 23-jun-05, at 19:47, Ed Colmar wrote:

> I found the Bastion Ledger and Bastion Paypal connection, but it is 
> way way overkill for what I want to do.
> Does anyone have a simple(r) IPN script they can point me towards?
>
> TIA!
>
> -e-
> _______________________________________________
> Zope maillist  -  Zope at zope.org
> http://mail.zope.org/mailman/listinfo/zope
> **   No cross posts or HTML encoding!  **
> (Related lists - http://mail.zope.org/mailman/listinfo/zope-announce
> http://mail.zope.org/mailman/listinfo/zope-dev )


M.J. Maré
WebToTheMax

T +31-20-4 100 242
F +31-20-4 100 243
W www.webtothemax.com
E mjmare at webtothemax.com
J mjm at anywise.jabber.com




More information about the Zope mailing list