jforrest / Chargify-PHP-Client

PHP client for Chargify
Please let me know if you have any questions.
MIT License
19 stars 16 forks source link

create subscription for an existing user? Seems to only work for new users... #4

Closed ghost closed 13 years ago

ghost commented 13 years ago

I'm probably doing something wrong, but I was trying to add a subscription to an existing user. (I need to be able to switch them at a later date). It appears that the create subscription, when given a user object, it tries to create a user and subscription at the same time and doesn't send just a customer_id along with a "sign this existing user up" type of format. You can see in the request it's including my existing userid (999999). - <not really 999999>.

Anyway - I've hacked around it and created my own quick function to POST the expected format so I'm ok - but wanted to ask or inform you in case it's a bug after all.

POSTDATA

<?xml version="1.0" encoding="utf-8"?>

12341334 sinbad waymycityIL93933US122010MYNAMEblahmyemail@myprovider.comMYNAMEblah659999992010-07-27T18:13:11-04:002010-07-27T18:49:09-04:00
freetrial

END POSTDATA

RESULT

stdClass Object ( [response] => <?xml version="1.0" encoding="UTF-8"?>

Customer: cannot be blank. A Customer must be specified for the subscription to be valid.
[code] => 422
[meta] => Array
    (
        [url] => https://company.chargify.com/subscriptions.xml
        [content_type] => application/xml; charset=utf-8
        [http_code] => 422
        [header_size] => 730
        [request_size] => 1148
        [filetime] => -1
        [ssl_verify_result] => 0
        [redirect_count] => 0
        [total_time] => 0.204355
        [namelookup_time] => 2.1E-5
        [connect_time] => 0.01465
        [pretransfer_time] => 0.077318
        [size_upload] => 0
        [size_download] => 182
        [speed_download] => 890
        [speed_upload] => 0
        [download_content_length] => 182
        [upload_content_length] => 0
        [starttransfer_time] => 0.204326
        [redirect_time] => 0
    )

) END RESULT

Looks like when I have an existing user the request should look like this (using a customer_id rather than a full set of customer_attributes):

Scenario: Create a subscription using an existing customer ID Given I have 1 product And I have 1 customer And I have this xml subscription data """ <?xml version="1.0" encoding="UTF-8"?>

[@product.handle] [@customer.id] 1 10 2020
  """

I put this function in ChargifySubscription and was able to use my existing customer id...

public function updateExistingCustomerSubscription($producthandle, $customer, $cc, $format = 'XML') {

    $subscriptionRequest =  
    '<?xml version="1.0" encoding="UTF-8"?>
    <subscription>
      <product_handle>' . $producthandle . '</product_handle>
      <customer_id>' . $customer->id . '</customer_id>
      <credit_card_attributes>
        <full_number>' . $cc->full_number . '</full_number>
        <expiration_month>' . $cc->expiration_month . '</expiration_month>
        <expiration_year>' . $cc->expiration_year . '</expiration_year>
      </credit_card_attributes>
    </subscription>';

    return $this->connector->requestCreateSubscription($subscriptionRequest, $format);
}
jforrest commented 13 years ago

Did you try setting the customer_id field on the subscription object? I think this would be the best way to accomplish what you are trying. Basically, whatever properties on the subscription object that you set will be present in the xml. You can call the getXML() function on the subscription object to verify it matches what you need (it will match what is sent to the chargify API).

ghost commented 13 years ago

Hmm, your solution is so much more elegant than mine. That's what I get for being an IT guy and not a developer. :) You rock.

That worked - thanks!