1

I am building a custom checkout with the Recurly PHP Client. Since we use our own gateway logic, we are forced to use the Create Purchase method, as opposed to Create Subscription (as Create Subscription doesn't accept gateway_code as a parameter). Create Subscription returns the created subscription - easy!

But Create Purchase returns an Invoice Collection. It's possible to tease this apart to find the newly-created $subscription object but this hardly seems like the intended process. Is there (hopefully) a cleaner way of going about it?

My method for purchasing is as follows - see code comments.

protected static function create_subscription( $user_id, $args ) {

    $result = false;

    $purchase = new Recurly_Purchase();
    $purchase->currency          = $args['currency'];
    $purchase->collection_method = 'automatic';
    $purchase->gateway_code      = $args['gateway_code'];

    $account             = new Recurly_Account( $user_id );
    $account->email      = $args['email'];
    $account->first_name = $args['billing_first_name'];
    $account->last_name  = $args['billing_last_name'];
    $account->vat_number = $args['vat_number'];

    $billing_info           = new Recurly_BillingInfo();
    $billing_info->token_id = $args['recurly_token'];

    $account->billing_info = $billing_info;

    $purchase->account = $account;

    $subscription            = new Recurly_Subscription();
    $subscription->plan_code = $args['plan_code'];

    $purchase->subscriptions = array( $subscription );

    try {
        // "invoice" is the method to transact a Recurly_Purchase.
        $purchase = Recurly_Purchase::invoice( $purchase );
        if( $purchase instanceof Recurly_InvoiceCollection ) {
           // this seems incredibly janky and error-prone
           $result = reset( $purchase->charge_invoice->line_items )->subscription->get();
        }
    } catch ( Exception $e ) {
        $result = $e;
    }
    // I need this to return the $subscription object generated by the purchase
    return $result;

}
Isaac Lubow
  • 3,557
  • 5
  • 37
  • 53

1 Answers1

0

When you create a subscription, a successful response will include the UUID for that subscription. I am not a PHP developer but it might look something like this:

 $subscription = new Recurly_Subscription();
 $subscription->plan_code = $args['plan_code'];
 $subscription->account = $account;
 $subscription->currency = $args['currency'];
 $subscription->create();
 $uuid = isset($subscription->uuid);
 $result = Recurly_Subscription::get($uuid);
 return $result;

Also, please note that as of Recurly API version 2.17+ you can now pass gateway_code as a body param to create subscription AND create purchase as you initially hoped to do. Here is a link to the Recurly API Release notes, which indicates when the change was made.

Ben Hulan
  • 537
  • 2
  • 7
  • Thanks, but I was trying to get the subscription from a `Recurly_InvoiceCollection` - not create a subscription from scratch. I should also note that you still _**cannot**_ pass `gateway_code` to `create_purchase` - as I noted in the question, we can't use `create_subscription` for this reason. – Isaac Lubow Jun 12 '19 at 21:53
  • Ok, no worries. For the record, you *can* pass `gateway_code` to `create_purchase`, which is listed as the penultimate parameter in the same link you posted in your original question. My answer cited `create_subscription`, but I added the link to `create_purchase` for clarity. – Ben Hulan Jun 13 '19 at 19:25
  • They claimed that was true but I've gotten independent confirmation from the developers that it is still not available. The documentation is wrong. – Isaac Lubow Jun 17 '19 at 00:19