Search code examples
recurly

Is there a way to get a billing token from Recurly without using recurly.js?


We are moving to Recurly for our billing, and plan to use the recurly.js api to generate billing tokens on production, but in the meantime, it's really hard to test on other environments. Ideally, I would like to be able to send credit card information from my server to a Recurly endpoint, and get back a billing token.

What is the easiest way for me to do that? And if the answer is 'use the recurly.js api', how do I do that? The only examples on the Recurly site are a web page that submits a form to a server. I want the opposite, my server calls a web page or other endpoints, and gets the token in the response.


Solution

  • I just has this very issue. Here's my solution to gather a test token via curl (in PHP) from the URL that recurly.js uses.

    // Billing token generator helper
    public function getBillingToken($data) {
        $data['version'] = '3.1.0';
        $data['key'] = $this->recurlyPublicKey;
    
        $ch = curl_init(); 
        curl_setopt($ch, CURLOPT_URL, "https://api.recurly.com/js/v1/token?".http_build_query($data)); 
        curl_setopt($ch, CURLOPT_TIMEOUT, 20); 
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:38.0) Gecko/20100101 Firefox/38.0"); 
        $result = curl_exec($ch);
        curl_close($ch);
        $reply = json_decode($result, true);
        return $reply['id'];
    }
    
    $billingData = array(
        'first_name' => 'John',
        'last_name' => 'jones',
        'number' => '4111111111111111',
        'month' => '12',
        'year' => '2016',
        'cvv' => '123',
        'address1' => 'Some address',
        'country' => 'AU',
        'city' => 'Melbourne',
        'state' => 'Victoria',
        'postal_code' => '3001',
    );
    
    $token = getBillingToken($billingData);
    

    Hope that helps.