chargify / chargify_api_ares

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

Unable to create subscription (using JSON format) #147

Closed tomsabin closed 2 years ago

tomsabin commented 7 years ago

Hi, I'm struggling to create subscriptions after following the examples.

We have a product with the handle: go-monthly-100kwh but when creating a new subscription (and customer) with the following

Chargify::Subscription.create(
  product_handle: "go-monthly-100kwh",
  customer_attributes: {
    first_name: "Tom",
    last_name: "Sabin",
    email: "tomsabin@example.com"
  },
  credit_card_attributes: {
    full_number: "4242424242424242",
    expiration_month: "1",
    expiration_year: "2026",
    cvv: "101"
  }
)

I receive the following response:

DEPRECATION WARNING: Returning errors as an array of strings is deprecated. (called from __pry__ at (pry):113)
=> #<Chargify::Subscription:0x007fa15e3268e0
 @attributes=
  {"product_handle"=>"go-monthly-100kwh",
   "customer_attributes"=>
    #<Chargify::Subscription::CustomerAttributes:0x007fa15e326390
     @attributes={"first_name"=>"Tom", "last_name"=>"Sabin", "email"=>"tomsabin@example.com"},
     @persisted=false,
     @prefix_options={}>,
   "credit_card_attributes"=>
    #<Chargify::Subscription::CreditCardAttributes:0x007fa15e325b98
     @attributes={"full_number"=>"4242424242424242", "expiration_month"=>"1", "expiration_year"=>"2026", "cvv"=>"101"},
     @persisted=false,
     @prefix_options={}>},
 @errors=
  #<ActiveResource::Errors:0x007fa15e325238
   @base=#<Chargify::Subscription:0x007fa15e3268e0 ...>,
   @messages=
    {:base=>
      ["Product must be specified and be a valid product provided by the seller.",
       "A Customer must be specified for the subscription to be valid."]}>,
 @persisted=false,
 @prefix_options={},
 @remote_errors=#<ActiveResource::ResourceInvalid: Failed.  Response code = 422.  Response message = Unprocessable Entity.>,
 @validation_context=nil>

I'm struggling to get this working via both the API using a rest client and this ruby gem. However, creating the subscription manually on the GUI is successful. Any help would be appreciated, I fear that it may be something very simple that I am overlooking.

Thanks

tomsabin commented 7 years ago

Turns out changing the format from :json to :xml solves this.

robg commented 6 years ago

In case anyone else runs into this, instead of switching to xml format, you can instead add:

Chargify::Base.include_root_in_json = true

to an initializer or similar.