chargify / chargify_api_ares

A Chargify API wrapper for Ruby using ActiveResource
http://chargify.com
MIT License
161 stars 95 forks source link

Cannot create customer with Subscription in JSON #107

Closed caseykickserv closed 9 years ago

caseykickserv commented 9 years ago

Code being used:

@subscription = Chargify::Subscription.new(
      product_handle: @plan.chargify_api_handle,
      customer_attributes: {
        first_name: @billing_profile.first_name,
        last_name: @billing_profile.last_name,
        email: @billing_profile.email,
        reference: @account.uid
      },
      coupon_code: @plan_subscription.coupon_code,
      components: component_attributes
    )

@subscription.save!

Response says:

@messages={:base=>["unknown attribute: customer_attributes"]}

As you can see, the customer_attributes argument does not work. The API docs found at https://docs.chargify.com/api-subscriptions#api-usage-json-subscriptions-create indicate it should.

Sorry there is no failing test and pull, but I wasn't sure what the proper fix would be within this gem.

wsmoak commented 9 years ago

Can you try it with Subscription.create instead of Subscription.new ? It works for me that way, including with customer_attributes.

That's what is shown in the subscription examples as well: https://github.com/chargify/chargify_api_ares/blob/master/examples/subscriptions.rb

I don't think it's necessary to call save afterwards, 'create' will have already communicated with the Chargify server and created a new subscription in your site.

caseykickserv commented 9 years ago

@wsmoak does #create return an exception? I am using #save! So I can fail fast during a chain of API calls.

wsmoak commented 9 years ago

It doesn't appear so. The easiest thing is to provoke a 422 with a 'bad' test card (2 or 3 if you're using the test gateway, see https://docs.chargify.com/gateway-configuration .)

I don't think the new and save sequence is going to work for subscription creation. Look at the code for the save method, it strips off all the nested attributes because you are meant to modify them directly.

https://github.com/chargify/chargify_api_ares/blob/master/lib/chargify_api_ares/resources/subscription.rb

caseykickserv commented 9 years ago

Indeed :cry: . Ok I will work through it that way. Thanks for your help.