0

I'm using Charge to provide a payment mechanism on a website I'm working on. I'm also using the 'coupons' in Charge to provide a means for users to apply a discount to a purchase at the point of payment. However I would like to validate and display the discount on the payment page before the users submits so they can be sure that the discount has been applied.

I have a bespoke module which I am using to create and manage my basket so I'm not adverse to writing a Stripe API call if required but wondered if perhaps I was missing something that's already built into charge by default?

1

In the end I just went ahead and wrote a small module to handle this...

function Checkout_validate_coupon_url() {

    // Construct the ACT URL
    $url = $this->site_url."?ACT=".ee()->functions->fetch_action_id( $this->package_name, 'charge_validate_coupon' );

    return $url;

}

// --------------------------------------------------------------------

function Charge_validate_coupon() {

    // Check for coupon
    $coupon = ee()->input->get('coupon');
    $price = ee()->input->get('price');

    if (is_null($coupon) OR is_null($price)) {
        return FALSE;
    }

    if (!$coupon = ee()->db->where('code', $coupon)->get('charge_coupon')->row_array()) {
        return FALSE;
    }

    // New data
    $data = array(
        'price' => $price
        );

    // Percentage_off OR Amount_off
    if ($coupon['type']=="percentage") {
        $data['price_discounted'] = $price - ($price * ($coupon['percentage_off'] / 100));
    } else {
        $data['price_discounted'] = $price - $coupon['amount_off'];
    }

    // If this is an ajax request return a JSON response
    if (ee()->input->is_ajax_request()) {
        ee()->output->send_ajax_response(array(
            'success' => TRUE,
            'data' => $data
            ));
    }

    return $data;

}

And then added an AJAX call to my Charge payment form which fired on keyup/blur of the coupon input. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.