Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing Django-oscar COD

It has been 1 week now, and I cannot figure out the way to implement COD in django-oscar. I would be really thankful if someone can at least give me a head start to accomplish this task. I've tried to use few Github repositories, but either they are outdated or not clear enough to be used with the project. A head start will be good enough for me to get started from scratch. Thanks

like image 591
Shazia Nusrat Avatar asked Apr 14 '17 09:04

Shazia Nusrat


2 Answers

I have not yet implemented COD for Oscar, but I have been digging in the oscar source code for weeks for another non-standard payment integration. The hard part is that payment involves the checkout, basket, partner, order and payment apps. Keep the source code of the original checkout views.py file close, and refer to it often.

So what are the crucial steps:

  1. All payment handling happens in the PaymentDetailsView of the checkout app, so you need a forked checkout app to start with to create a custom PaymentDetailsView subclass.
  2. You need to override the submit() method where you create an order number, freeze the basket, save it in the session and send the pre_payment signal (just copy/paste the source code). Because payment will not be taken here, you will need to remove that part from your custom submit() implementation at this point.
  3. You will then have to continue as if payment has taken place, and call handle_order_placement(), which will call handle_successful_order()
  4. Afterwards, when payment has taken place upon arrival of your products you will need to implement handle_payment() for the relevant order number, and trigger the post_payment signal.

That's about it in a nutshell, but obviously there is some exception handling needed as well. Hope this will get you started!

like image 69
dentemm Avatar answered Nov 14 '22 13:11

dentemm


I have recently implemented COD on django-oscar. Here's what you need to do.

You should understand how the checkout process of oscar works. Let me show you in the bit. There is one 'checkout' app in the oscar which handles all the payment, placing order like stuffs. So, first of all you need to fork that app with below command.

./manage.py oscar_fork_app checkout apps/shop

Then add it in the INSTALLED_APPS at settings.

Now, If you look closely in the checkout app's views.py, there is this class PaymentDetailsView which handles the placing order and payment stuff.

Now, at your preview.html page, you can add gateway option as 'cod'. When user selects this 'cod' option and submit for 'place order', you can check for submitted gateway code at the handle_payment method of the forked class PaymentDetailsView.

CorePaymentDetailsView = get_class('checkout.views', 'PaymentDetailsView')

class PaymentDetailsView(CorePaymentDetailsView):
    """
    currently used to redirect to preview page
    Handles the payment and cod.
    """
    def handle_payment(self, order_number, total, **kwargs):
        self.amount = float(total.excl_tax)
        gateway_code = self.request.POST.get('gateway_code', None)
        if gateway_code and gateway_code == 'cash-on-delivery':
            # Record payment source and event
            source_type, is_created = SourceType.objects.get_or_create(
                name='cash-on-delivery')
            source = source_type.sources.model(
                source_type=source_type,
                amount_allocated=total.excl_tax)
            self.add_payment_source(source)
            self.add_payment_event('CREATED', total.excl_tax)
            return

When you return from the handle_payment method, it continues executing the submit method of the same class and places the order.

After that you can mention at the dashboard that, this product has cash-on-delivery option. After delivery you can capture another payment-event as 'CASH-RECEIVED'.

Ask if have any other query.

like image 26
Jay Modi Avatar answered Nov 14 '22 13:11

Jay Modi