Using PayPal WPS with Cartridge (Mezzanine / Django)

I recently built a web site using Mezzanine, a CMS built on top of Django. I decided to go with Mezzanine (which I've never used before) for two reasons: it nicely enhances Django's admin experience (plus it enhances, but doesn't get in the way of, the Django developer experience); and there's a shopping cart app called Cartridge that's built on top of Mezzanine, and for this particular site (a children's art class business in Sydney) I needed shopping cart / e-commerce functionality.

This suite turned out to deliver virtually everything I needed out-of-the-box, with one exception: Cartridge currently lacks support for payment methods that require redirecting to the payment gateway and then returning after payment completion (such as PayPal Website Payments Standard, or WPS). It only supports payment methods where payment is completed on-site (such as PayPal Website Payments Pro, or WPP). In this case, with the project being small and low-budget, I wanted to avoid the overhead of dealing with SSL and on-site payment, so PayPal WPS was the obvious candidate.

Turns out that, with a bit of hackery, making Cartridge play nice with WPS isn't too hard to achieve. Here's how you go about it.

Install dependencies

Note / disclaimer: this section is mostly copied from my Django Facebook user integration with whitelisting article from over two years ago, because the basic dependencies are quite similar.

I'm assuming that you've already got an environment set up, that's equipped for Django development. I.e. you've already installed Python (my examples here are tested on Python 2.7), a database engine (preferably SQLite on your local environment), pip (recommended), and virtualenv (recommended). If you want to implement these examples fully, then as well as a dev environment with these basics set up, you'll also need a server to which you can deploy a Django site, and on which you can set up a proper public domain or subdomain DNS (because the PayPal API won't actually talk to your localhost, it refuses to do that).

You'll also need a PayPal (regular and "sandbox") account, which you will use for authenticating with the PayPal API.

Here are the basic dependencies for the project. I've copy-pasted this straight out of my requirements.txt file, which I install on a virtualenv using pip install -E . -r requirements.txt (I recommend you do the same):

-e git+https://github.com/dcramer/django-paypal.git@4d582243#egg=django_paypal

Note: for dcramer/django-paypal, which has no versioned releases, I'm using the latest git commit as of writing this. I recommend that you check for a newer commit and update your requirements accordingly. For the other dependencies, you should also be able to update version numbers to latest stable releases without issues (although Mezzanine 3.0.x / Cartridge 0.9.x is only compatible with Django 1.6.x, not Django 1.7.x which is still in beta as of writing this).

Once you've got those dependencies installed, make sure this Mezzanine-specific setting is in your settings.py file:

# If True, the south application will be automatically added to the

Then, let's get a new project set up per Mezzanine's standard install:

mezzanine-project myproject
cd myproject
python manage.py createdb

python manage.py migrate --all

(When it asks "Would you like to install an initial demo product and sale?", I've gone with "yes" for my test / demo project; feel free to do the same, if you'd like some products available out-of-the-box with which to test checkout / payment).

This will get the Mezzanine foundations installed for you. The basic configuration of the Django / Mezzanine settings file, I leave up to you. If you have some experience already with Django (and if you've got this far, then I assume that you do), you no doubt have a standard settings template already in your toolkit (or at least a standard set of settings tweaks), so feel free to use it. I'll be going over the settings you'll need specifically for this app, in just a moment.

Fire up ye 'ol runserver, open your browser at http://localhost:8000/, and confirm that the "Congratulations!" default Mezzanine home page appears for you. Also confirm that you can access the admin. And that's the basics set up!

Basic Django / Mezzanine / Cartridge site: default look after install.

Basic Django / Mezzanine / Cartridge site: default look after install.

At this point, you should also be able to test out adding an item to your cart and going to checkout. After entering some billing / delivery details, on the 'payment details' screen it should ask for credit card details. This is the default Cartridge payment setup: we'll be switching this over to PayPal shortly.

Configure Django settings

I'm not too fussed about what else you have in your Django settings file (or in how your Django settings are structured or loaded, for that matter); but if you want to follow along, then you should have certain settings configured per the following guidelines (note: much of these instructions are virtually the same as the cartridge-payments install instructions):

  • Your TEMPLATE_CONTEXT_PROCESSORS is to include (as well as 'mezzanine.conf.context_processors.settings'):

    (See the TEMPLATE_CONTEXT_PROCESSORS documentation for the default value of this setting, to paste into your settings file).

  • Re-configure the SHOP_CHECKOUT_FORM_CLASS setting to this:
    SHOP_CHECKOUT_FORM_CLASS = 'payments.multipayments.forms.base.CallbackUUIDOrderForm'
  • Configure the SECONDARY_PAYMENT_PROCESSORS setting to this:
        ('paypal', {
            'name' : 'Pay With Pay-Pal',
            'form' : 'payments.multipayments.forms.paypal.PaypalSubmissionForm'
  • Set a value for the PAYPAL_CURRENCY setting, for example:
    # Currency type.
  • Set a value for the PAYPAL_BUSINESS setting, for example:
    # Business account email. Sandbox emails look like this.
    PAYPAL_BUSINESS = 'cartwpstest@blablablaaaaaaa.com'
  • Set a value for the PAYPAL_RECEIVER_EMAIL setting, for example:
  • Set a value for the PAYPAL_RETURN_WITH_HTTPS setting, for example:
    # Use this to enable https on return URLs.  This is strongly recommended! (Except for sandbox)
  • Configure the PAYPAL_RETURN_URL setting to this:
    # Function that returns args for `reverse`. 
    # URL is sent to PayPal as the for returning to a 'complete' landing page.
    PAYPAL_RETURN_URL = lambda cart, uuid, order_form: ('shop_complete', None, None)
  • Configure the PAYPAL_IPN_URL setting to this:
    # Function that returns args for `reverse`. 
    # URL is sent to PayPal as the URL to callback to for PayPal IPN.
    # Set to None if you do not wish to use IPN.
    PAYPAL_IPN_URL = lambda cart, uuid, order_form: ('paypal.standard.ipn.views.ipn', None, {})
  • Configure the PAYPAL_SUBMIT_URL setting to this:
    # URL the secondary-payment-form is submitted to
    # For real use set to 'https://www.paypal.com/cgi-bin/webscr'
    PAYPAL_SUBMIT_URL = 'https://www.sandbox.paypal.com/cgi-bin/webscr'
  • Configure the PAYPAL_TEST setting to this:
    # For real use set to False
    PAYPAL_TEST = True
  • Configure the EXTRA_MODEL_FIELDS setting to this:
            {"blank" : False, "max_length" : 36, "default": ""},

    After doing this, you'll probably need to manually create a migration in order to get this field added to your database (per Mezzanine's field injection caveat docs), and you'll then need to apply that migration (in this example, I'm adding the migration to an app called 'content' in my project):

    mkdir /projectpath/content/migrations
    touch /projectpath/content/migrations/__init__.py
    python manage.py schemamigration cartridge.shop --auto --stdout > /projectpath/content/migrations/0001_cartridge_shop_add_callback_uuid.py

    python manage.py migrate --all

  • Your INSTALLED_APPS is to include (as well as the basic 'mezzanine.*' apps, and 'cartridge.shop'):

    (You'll need to re-run python manage.py migrate --all after enabling these apps).

Implement PayPal payment

Here's how you do it:

  • Add this to your urlpatterns variable in your urls.py file (replace the part after paypal-ipn- with a random string of your choice):
        (r'^paypal-ipn-8c5erc9ye49ia51rn655mi4xs7/', include('paypal.standard.ipn.urls')),
  • Although it shouldn't be necessary, I've found that I need to copy the templates provided by explodes/cartridge-payments into my project's templates directory, otherwise they're ignored and Cartridge's default payment template still gets used:

    cp -R /projectpath/lib/python2.7/site-packages/payments/multipayments/templates/shop /projectpath/templates/

  • Place the following code somewhere in your codebase (per the django-paypal docs, I placed it in the models.py file for one of my apps):
    # ...
    from importlib import import_module
    from mezzanine.conf import settings
    from cartridge.shop.models import Cart, Order, ProductVariation, \
    from paypal.standard.ipn.signals import payment_was_successful
    # ...
    def payment_complete(sender, **kwargs):
        """Performs the same logic as the code in
        cartridge.shop.models.Order.complete(), but fetches the session,
        order, and cart objects from storage, rather than relying on the
        request object being passed in (which it isn't, since this is
        triggered on PayPal IPN callback)."""
        ipn_obj = sender
        if ipn_obj.custom and ipn_obj.invoice:
            s_key, cart_pk = ipn_obj.custom.split(',')
            SessionStore = import_module(settings.SESSION_ENGINE) \
            session = SessionStore(s_key)
                cart = Cart.objects.get(id=cart_pk)
                    order = Order.objects.get(
                    for field in order.session_fields:
                        if field in session:
                            del session[field]
                        del session["order"]
                    except KeyError:
                    # Since we're manually changing session data outside of
                    # a normal request, need to force the session object to
                    # save after modifying its data.
                    for item in cart:
                            variation = ProductVariation.objects.get(
                        except ProductVariation.DoesNotExist:
                            variation.update_stock(item.quantity * -1)
                    code = session.get('discount_code')
                    if code:
                        DiscountCode.objects.active().filter(code=code) \
                            .update(uses_remaining=F('uses_remaining') - 1)
                except Order.DoesNotExist:
            except Cart.DoesNotExist:

    This little snippet that I whipped up, is the critical spoonful of glue that gets PayPal WPS playing nice with Cartridge. Basically, when a successful payment is realised, PayPal WPS doesn't force the user to redirect back to the original web site, and therefore it doesn't rely on any redirection in order to notify the site of success. Instead, it uses PayPal's IPN (Instant Payment Notification) system to make a separate, asynchronous request to the original web site – and it's up to the site to receive this request and to process it as it sees fit.

    This code uses the payment_was_successful signal that django-paypal provides (and that it triggers on IPN request), to do what Cartridge usually takes care of (for other payment methods), on success: i.e. it clears the user's shopping cart; it updates remaining quantities of products in stock (if applicable); it triggers Cartridge's "product purchased" actions (e.g. email an invoice / receipt); and it updates a discount code (if applicable).

  • Apply a hack to cartridge-payments (file lib/python2.7/site-packages/payments/multipayments/forms/paypal.py) per this diff:

    After line 25 (charset = forms.CharField(widget=forms.HiddenInput(), initial='utf-8')), add this:

        custom = forms.CharField(required=False, widget=forms.HiddenInput())

    After line 49 ((tax_price if tax_price else const.Decimal('0'))), add this:

                s_key = request.session.session_key
                # for Django 1.4 and above
                s_key = request.session._session_key

    After line 70 (self.fields['business'].initial = settings.PAYPAL_BUSINESS), add this:

    self.fields['custom'].initial = ','.join([s_key, str(request.cart.pk)])
  • Apply a hack to django-paypal (file src/django-paypal/paypal/standard/forms.py) per these instructions:

    After line 15 ("%H:%M:%S %b. %d, %Y PDT",), add this:

                          "%H:%M:%S %d %b %Y PST",    # note this
                          "%H:%M:%S %d %b %Y PDT",    # and that

That should be all you need, in order to get checkout with PayPal WPS working on your site. So, deploy everything that's been done so far to your online server, log in to the Django admin, and for some of the variations for the sample product in the database, add values for "number in stock".

Then, log out of the admin, and navigate to the "shop" section of the site. Try out adding an item to your cart.

Basic Django / Mezzanine / Cartridge site: adding an item to shopping cart.

Basic Django / Mezzanine / Cartridge site: adding an item to shopping cart.

Once on the "your cart" page, continue by clicking "go to checkout". On the "billing details" page, enter sample billing information as necessary, then click "next". On the "payment" page, you should see a single button labelled "pay with pay-pal".

Basic Django / Mezzanine / Cartridge site: 'go to pay-pal' button.

Basic Django / Mezzanine / Cartridge site: 'go to pay-pal' button.

Click the button, and you should be taken to the PayPal (sandbox, unless configured otherwise) payment landing page. For test cases, log in with a PayPal test account, and click 'Pay Now' to try out the process.

Basic Django / Mezzanine / Cartridge site: PayPal payment screen.

Basic Django / Mezzanine / Cartridge site: PayPal payment screen.

If payment is successful, you should see the PayPal confirmation page, saying "thanks for your order". Click the link labelled "return to email@here.com" to return to the Django site. You should see Cartridge's "order complete" page.

Basic Django / Mezzanine / Cartridge site: order complete screen.

Basic Django / Mezzanine / Cartridge site: order complete screen.

And that's it, you're done! You should be able to verify that the IPN callback was triggered, by checking that the "number in stock" has decreased to reflect the item that was just purchased, and by confirming that an order email / confirmation email was received.

Finished process

I hope that this guide is of assistance, to anyone else who's looking to integrate PayPal WPS with Cartridge. The difficulties associated with it are also documented in this mailing list thread (to which I posted a rough version of what I've illustrated in this article). Feel free to leave comments here, and/or in that thread.

Hopefully the hacks necessary to get this working at the moment, will no longer be necessary in the future; it's up to the maintainers of the various projects to get the fixes for these committed. Ideally, the custom signal implementation won't be necessary either in the future: it would be great if Cartridge could work out-of-the-box with PayPal WPS. Unfortunately, the current architecture of Cartridge's payment system simply isn't designed for something like IPN, it only plays nicely with payment methods that keep the user on the Django site the entire time. In the meantime, with the help of this article, you should at least be able to get it working, even if more custom code is needed than what would be ideal.

Comments are closed


you have to add paypal receiver email to your settings before adding 'payments.multipayments',
'paypal.standard.ipn', to installed apps or your migration will fail.
PAYPAL_BUSINESS = 'cartwpstest@blablablaaaaaaa...'



Hey Jeremy,

Just tried this out... orders seem to go through successfully... they're saved in the admin with the correct transaction ID... but cartridge's cart does not empty after the order has gone through... if a user then tries to purchase the item again... paypal then says the invoice has been paid...


@Rad: thanks – I've moved the adding of 'payments.multipayments' and 'paypal.standard.ipn' to INSTALLED_APPS, to the bottom of the list in the 'configure settings' section.

@Steve: hmmm, sounds like the IPN callback isn't happening correctly, not sure what's going wrong for you… check that everything is configured correctly on your Django end, and on your PayPal end.

Nice share.
Orders are saved successfully. But when the payment is complete in url and I click on return to site, it gives error of 404 (Page not found). Please help get out of it, so that I can successfully download the invoice.


Hello Jeremy! Thanks so much for taking the time to share this. I encountered one problem at the end. When purchase is finished and I click "return to seller's shop" Paypal redirects me to
Obviously return URL is wrong but I don't know where to correct it. I guess I have to change it here:
PAYPAL_RETURN_URL = lambda cart, uuid, order_form: ('shop_complete', None, None)
but I don't know how.

Would you help me, please?

When i do migrate --all

it shows following error

django.db.utils.OperationalError: table "pages_page" already exists


I also get "Page not found error" when complete the order it save successfully but "unprocessed" so how we complete or process the order in cartridge

Hi, i follow your Blog for paypal intebration but i developed a digital download site so i want to return name of product from paypal in "complete.html" how we done that?
Plsease help me,

Thanks in advance


@David: re: "Paypal redirects me to ". Please note that you can't test the checkout / payment process in your local environment (i.e. on, you must deploy it to an online server, that has an actual URL (preferably its own domain / subdomain, although just an IP should work too).

PayPal can't send the IPN back to your localhost. You don't need to configure your site's base URL (as far as I'm aware), django-paypal will work it out automatically based on your environment.

@Sandeep, @Kartik: sounds like you may be having the same problem - if you're only trying this locally, you'll need to deploy your code to a VPS or other web-facing server in order to test the process.

Also, please make sure you're using the exact versions of all dependencies, as I specified in my requirements.txt in the article. While I encourage you to use the latest stable version of all packages, I haven't tested all this using versions newer than what I've indicated here, and I cannot guarantee that this whole fragile setup will work with upgraded packages. Feel free to let me know if any changes are needed to the setup for use with different versions of any of the dependencies.

@Kartik: re: "i want to return name of product from paypal in complete.html". Sorry, not sure how you do that, please post to support forums elsewhere if you need further help. Also, can you please stop spamming - you also sent me your comments via the contact form, this is not necessary.

Hi,Thanks for reply me Sorry for spamming ,
in cartridge when perform transaction and i complete that but still cart is not empty previous item is as it is in cart so,why cart is not clear at transaction complete,And at some times transaction is complste but in admin side in order it makes no effect can't make entry of that why it happens,
Please help and quick reply i am in trouble!,

Thanks in advance

Thanks so much Jeremy, this is a greatly helpful article. I deployed to vps. it works now. I changed
order = Order.objects.get( transaction_id=ipn_obj.invoice)
in your payment_complete() function to
order = Order.objects.get(key=s_key)

This fixed the 404 issue

Hello Jeremy! i search a lot but can't find solution in cartridge when in goes in complete.html it shows a "Http404- Page Not Found "Error order is also not save in databse ,i am search in code so i found that in shop/views.py in def complete(request, template="shop/complete.html"): method in this line try:
def complete(request,template="shop/complete.html"):
order = Order.objects.from_request(request)
except Order.DoesNotExist:
raise Http404 <-- From this line it shows 404

items = order.items.all()

in above line it not get order object so how i solve this problem ,
Plase help!!

Thanks in advance!

Jim Smith

Having a bit of trouble with this guide not getting the button to appear at all just keep getting the original card payment screen I have followed your steps apart from mezzanine set-up which I followed the official guide as yours didn't give me a shop option. Any idea what might be the issue.

Ben Brown

Thanks dude this worked first time and saved me a load of time. For anyone else that's interested there is a good way to make these changes permanent. If you use app engine it makes you put all the modules you use in your project directory if you do this for these two it will make deployment easier. But it will make upgrading harder.