Drupal ubercart coupons support for the Stripe coupons

You may probably wonder: my Drupal site uses ubercart to handle all my orders. Out of the various payment gateways I’m most impressed with Stripe, and it is supported by ubercart too. Perfect! Just one small problem though: the stripe add-on doesn’t support coupons.

Why? What if I add a feature to add coupon support?

The short answer for this question is quite straightforward: Don’t do it.

A longer answer requires a bit of backtracking. Ubercart is a module suite that intends to include every aspect of an e-commerce site experience; whereas Stripe focuses more on providing a subscription-tracking service. What that means is that while Ubercart and Stripe can work together on recurring products, it lacks support for one-off products.

So that leaves the integration of ubercart coupons to Stripe to a very specific case: coupons that apply only to recurring charges. If this is what you’re looking for, read on.

The change is quite simile, as it requires a few lines of extra code on the uc_stripe.module file

diff --git a/drupal/sites/all/modules/contrib/uc_stripe/uc_stripe.module b/drupal/sites/all/modules/contrib/uc_stripe/uc_stripe.module
index 1a6440c..fbcc2ea 100644
--- a/drupal/sites/all/modules/contrib/uc_stripe/uc_stripe.module
+++ b/drupal/sites/all/modules/contrib/uc_stripe/uc_stripe.module
@@ -489,6 +489,13 @@ function uc_stripe_process($order, &$fee) {
       if ($customer_id) {
         try {
           $customer = Stripe_Customer::retrieve($customer_id);
+
+          if(module_exists('uc_coupon')) {
+            if(isset($order->data['coupon']) && $order->data['coupon']) {
+              $customer->coupon = $order->data['coupon'];
+              $customer->save();
+            }
+          }
         }
         catch (Exception $e) {
           drupal_set_message(t("Unable to retrieve customer subscription"), 'error');
@@ -510,6 +517,13 @@ function uc_stripe_process($order, &$fee) {
   if ($create_new) {
     $data['email'] = $order->primary_email;
     $data['description'] = $customer_name;
+    
+    if(module_exists('uc_coupon')) {
+      if(isset($order->data['coupon']) && $order->data['coupon']) {
+        $data['coupon'] = $order->data['coupon'];
+      }
+    }
+
     try {
       $customer = Stripe_Customer::create($data);
       // Write this customer record to our database

Caveats:
– As stated above, this is only valid for recurring products;
– Stripe only allows a user to have at most one coupon applying to all plans at any given time, coupons created through ubercart have to be available to all coupons;

As I’ve said, if you want coupons created with ubercart to be usable on Stripe, then this may serve to be a useful reference for you; otherwise this is not a recommended solution for you.

Leave a comment

Your email address will not be published. Required fields are marked *