Billing API Flow

533 views
Skip to first unread message

Brad Madigan

unread,
Aug 3, 2012, 9:44:37 AM8/3/12
to shopify-a...@googlegroups.com
Hey. I set up my apps billing using the billing api using something like this:

...
      "recurring_application_charge"=>array
      (
        "name"=>$plan->plan_name,
        "price"=>$plan->plan_price,
        "return_url"=>URL::to('/shopify/billing'),
      )
...
$response = $shopify('POST', '/admin/recurring_application_charges.json', $shopify_data);

I get back a response and an id to save into my apps db. Which seems to be working great.

But I"m curious if its just suppose to send me a response like that or was it suppose 'Ask' the merchant if they okay the recurring billing charges.

Everything just stays on my app and seems to work fine, but I'm afraid I did it wrong.
Thanks

Luckner Jr. Jean-Baptiste

unread,
Aug 3, 2012, 9:52:46 AM8/3/12
to shopify-a...@googlegroups.com
Hey Brad,

Here is my setup - using PHP as well:

$charge = array(
        'recurring_application_charge' => array(
          'price' => '3.99',
          'name' => 'AtCost Membership Plan',
          'return_url' => Router::url('/charges/confirm', true),
          'test' => true
        )
      );

try {
        $recurringApplicationCharge = $sc->call('POST', '/admin/recurring_application_charges.json', $charge);

      // Save the charge
      $data = array(
        'Charge' => array(
          'shop_id' => $this->Session->read('shopify.shopData.id'),
          'amount' => '3.99',
          'plan_id' => 1,
          'test' => true
        )
      );
      $this->loadModel('Charge');
      $charge = $this->Charge->save($data);
      $this->Session->write('shopify.charge_id', $charge['Charge']['id']);

        $this->redirect($recurringApplicationCharge['confirmation_url']);
      } catch (Exception $e){
        pr($e);
      }


This API call will generate the charge and ask the customer to confirm the charge. When they accept it, they get redirected to the return url that you provided in the first api call. At that point, you still need to activate the charge with the charge id you are given. Like this:

$chargeId = $this->request->query['charge_id'];

    if (is_null($chargeId) || empty($chargeId)){
      // Throw error here
    }

    // Activate the charge
    $sc = parent::get_shopify_client();
    $activated = $sc->call('POST', '/admin/recurring_application_charges/' . $chargeId . '/activate.json');

    // Save the activation timestamp
    $this->Charge->read(null, $this->Session->read('shopify.charge_id'));
    $this->Charge->set('activated_on', date('Y-m-d H:i:s'));
    $this->Charge->save();

Hope this helps!

Luckner Jr.
--
 
 
 


Brad Madigan

unread,
Aug 3, 2012, 10:51:55 AM8/3/12
to shopify-a...@googlegroups.com
Hey Luckner

Thanks for the info. One thing that I missed was the 'confirmation_url' in the response.
I got it working!!! Sort of

One thing that I cannot seem to verify is if the Merchant clicks 'Decline' rather than approve.
No matter which link I clicked, I would get redirected to my 'Return-URL' with the same query variable.


I would get the same link if I clicked 'Approve' or 'Decline'

I am running in Test mode so I don't know if that has something to do with it or not.
B

Luckner Jr. Jean-Baptiste

unread,
Aug 3, 2012, 10:54:35 AM8/3/12
to shopify-a...@googlegroups.com
I have no idea about that one just yet. I am actually at that testing point in my app as well and have yet to test that scenario.

The only thing that I can foresee is that maybe when you try to activate the charge it would fail since there is no charge created on Shopify's end.

But that seems too loose for my liking.

Maybe there is someone else in the community that can shed some light on this? Edward?
--
 
 
 


Luckner Jr. Jean-Baptiste

unread,
Aug 3, 2012, 11:00:08 AM8/3/12
to shopify-a...@googlegroups.com
There is no specific mention of a different scenario if the user denies the charge in the documentation - http://api.shopify.com/billing.html

It seems as though we have to check whether or not the charge was activated or not.

Maybe the use of this call : current_plan = ShopifyAPI::RecurringApplicationCharge.current can help determine if there was a successful charge created or not. I have not tried it out yet.

Also, as far as I know, the activate api call does not return anything - except HTTP 200. maybe that is different if there is no charge to activate?

Luckner Jr.


On 12-08-03 10:51 AM, Brad Madigan wrote:
--
 
 
 


Brad Madigan

unread,
Aug 3, 2012, 11:10:07 AM8/3/12
to shopify-a...@googlegroups.com
I think we have to do this call and check the:

GET /admin/recurring_application_charges/#{id}.json

And check the 'status' field.
ie. 'Pending', 'Approved', 'Declined'
Do this in your request_url

Is that correct you think?


--
 
 
 



--

-----------------------------------------
Brad Madigan

Luckner Jr. Jean-Baptiste

unread,
Aug 3, 2012, 11:13:04 AM8/3/12
to shopify-a...@googlegroups.com
That would make sense to me. It is an extra call - would be nice if we got the status alongside the charge_id - but at least it is not expensive to do (hopefully).

I'll try it later on - if you get to it first, want to share your feedback?
--
 
 
 


Brad Madigan

unread,
Aug 3, 2012, 11:44:58 AM8/3/12
to shopify-a...@googlegroups.com
Hey

I think its working great by using that extra call.

    $response = $shopify('GET', '/admin/recurring_application_charges/12345667.json');

    if($response['status'] == 'accepted') {
        echo 'We are good to go!';
    }

You need to use the original id when you first created the transaction (The POST) and not the charge_id when using the GET.
I hope that makes sense.

Luckner Jr. Jean-Baptiste

unread,
Aug 3, 2012, 11:47:13 AM8/3/12
to shopify-a...@googlegroups.com
That's awesome! Glad it all worked out.

Yeah I know which one you mean. I currently save the charge at that point and update the charge after the activation process is complete.

Let me know when your app is up and running - would love to check it out.
--
 
 
 


Reply all
Reply to author
Forward
0 new messages