2

I'm trying to integrate Charge with Craft Commerce.

Basically I need to

a) use Stripe to set up subscription for the amount based on what's in the cart (this seems to be possible out-of-the-box)

b) on successful charge - mark order as complete (I think I can use 'charge.onCharge' event here)

c) listen to webhooks - when the next recurring payment succeeds or fails, make change to order.

Is there a way to write custom action for (c) without hacking into Charge core?

I see there is Charge_Actions service fireing some events, but how do I reference/listen to those actions in a custom plugin that I create?

1

I don't see events being triggered in the Actions Service (in my version of Charge anyway), so you won't be able to listen for those. You'll most likely have to modify the core, if you do, document that and when you update charge, you can easily put your modifications back into place or do a pull request to charge in hopes that event gets added to the core.

| improve this answer | |
1

So, in order to achieve this I had to make a couple of small hacks to the module core, namely in Charge_ActionsService.php

First, I added default case to triggerActionByType function:

default : {
    $processor = explode("_", $type);
    $class = $processor[0];
    $function = $processor[1];
    craft()->$class->$function($details, $chargeModel);
    break;
}

This allows me to call actions from external files. $processor will be class of service file in my plugin, $function would be name of function to be called. There are separated by _ in config (I'll explain this later on)

Next, I had to comment out

//if (!in_array($type, $this->baseActions)) continue;

to allow any actions, not just base ones.

Then in my templates, I based off example provided in charge/actions example template.

Options used:

{% set options = {
    'planAmount' : cart.totalPrice,
    'planInterval' : 'month',
    'planIntervalCount' : '1',
    'actions' : {
        'onSuccess': {
            'chargeCommerce_updateOrder' : { 'orderId': cart.id, 'status': 'subscriptionActive', 'complete': true }
        },
        'onFailure': {
            'chargeCommerce_updateOrder' : { 'orderId': cart.id, 'status': 'subscriptionCancelled' },
            'email' : { 0: 'payment-failed-customer', 1: 'payment-failed-admin'  }
        },
    }} %}

chargeCommerce_updateOrder is the reference to function updateOrder in ChargeCommerceService.php

| improve this answer | |

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.