title: Magento - Single Coupon marked as used when payment pending
body: <p>I have got a problem with Magento single coupon code that is marked as having been used at the time the customer clicks on the <B>Place Order</B> button. If the Paypal payment fails or the client leaves the page before the order is complete, he won't able to go back and re-order with this coupon which is set to be only used once, and has been marked already been used.</p>
  
  <p>I have found a piece of code that decreases the number of times the coupons has been used by the user and allows him to reuse the coupon. Unfortunately, he gets an error when trying to connect the Paypal page when clicking the place order button. In order to be able to use the coupon another time and access the Paypal page, I have to delete the lines in SQL database in tables <B>salesrule_coupon_usage</B> and <B>salesrule_customer</B> with this customer's ID. </p>
  
  <p>Here is the code I need to change to automatically delete coupon usage information for a customer ID:</p>
  
  <pre><code>public function cancel($observer)
  {
      $order = $observer-&gt;getEvent()-&gt;getPayment()-&gt;getOrder();
      if ($order-&gt;canCancel()) {
          if ($code = $order-&gt;getCouponCode()) {
              $coupon = Mage::getModel('salesrule/coupon')-&gt;load($code, 'code');
              if ($coupon-&gt;getTimesUsed() &gt; 0) {
                  $coupon-&gt;setTimesUsed($coupon-&gt;getTimesUsed() - 1);
                  $coupon-&gt;save();
              }
  
              $rule = Mage::getModel('salesrule/rule')-&gt;load($coupon-&gt;getRuleId());
              error_log("\nrule times used=" . $rule-&gt;getTimesUsed(), 3, "var/log/debug.log");
              if ($rule-&gt;getTimesUsed() &gt; 0) {
                  $rule-&gt;setTimesUsed($rule-&gt;getTimesUsed()-1);
                  $rule-&gt;save();
              }
  
              if ($customerId = $order-&gt;getCustomerId()) {
                  if ($customerCoupon = Mage::getModel('salesrule/rule_customer')-&gt;loadByCustomerRule($customerId, $rule-&gt;getId())) {
                      $couponUsage = new Varien_Object();
                      Mage::getResourceModel('salesrule/coupon_usage')-&gt;loadByCustomerCoupon($couponUsage, $customerId, $coupon-&gt;getId());
  
                      if ($couponUsage-&gt;getTimesUsed() &gt; 0) {
                          /* I can't find any #@$!@$ interface to do anything but increment a coupon_usage record */
                          $resource = Mage::getSingleton('core/resource');
                          $writeConnection = $resource-&gt;getConnection('core_write');
                          $tableName = $resource-&gt;getTableName('salesrule_coupon_usage');
  
                          $query = "UPDATE {$tableName} SET times_used = times_used-1 "
                              .  "WHERE coupon_id = {$coupon-&gt;getId()} AND customer_id = {$customerId} AND times_used &gt; 0";
  
                          $writeConnection-&gt;query($query);
                      }
  
                      if ($customerCoupon-&gt;getTimesUsed() &gt; 0) {
                          $customerCoupon-&gt;setTimesUsed($customerCoupon-&gt;getTimesUsed()-1);
                          $customerCoupon-&gt;save();
                      }
                  }
              }
          }
      }
  }
  </code></pre>