2

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.

3 Answers3

9

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.

Richard
  • 279
  • 3
  • 4
0

Here is the function I use in an Angular5/Typescript environment using RecurlyJS. It's important to recognize the difference between a billing token for credit card vs bank account.

private getRecurlyToken(locationId): Promise<string> {
  return new Promise<string>((resolve, reject) => {
    let recurly = window['recurly'];
    recurly.configure({publicKey: Config['RECURLY_PUBLIC_KEY'], parent: false});
    if(this.paymentMethod.value === 'card') {
      recurly.token({
        number: this.cardInfoForm.value.cardNumber,
        month: this.cardInfoForm.value.expMonth,
        year: this.cardInfoForm.value.expYear,
        first_name: this.cardInfoForm.value.firstName,
        last_name: this.cardInfoForm.value.lastName,
        cvv: this.cardInfoForm.value.cardCVC,
        address1: this.billingAddressForm.value.address1,
        address2: this.billingAddressForm.value.address2,
        city: this.billingAddressForm.value.city,
        state: this.billingAddressForm.value.state,
        postal_code: this.billingAddressForm.value.postalCode,
        country: 'US',
      }, (error, token) => {
        if(error && error.code === 'validation') { return reject(this.validationError('credit card', error)); }
        else if(error) { return reject(error); }
        resolve(token.id);
      });
    } else {
      recurly.bankAccount.token({
        name_on_account: this.bankInfoForm.value.nameOnAccount,
        account_number: this.bankInfoForm.value.accountNumber,
        account_number_confirmation: this.bankInfoForm.value.accountConfirmation,
        routing_number: this.bankInfoForm.value.routingNumber,
        account_type: this.bankInfoForm.value.accountType,
        address1: this.billingAddressForm.value.address1,
        address2: this.billingAddressForm.value.address2,
        city: this.billingAddressForm.value.city,
        state: this.billingAddressForm.value.state,
        postal_code: this.billingAddressForm.value.postalCode,
        country: 'US',
      }, (error, token) => {
        if(error && error.code === 'validation') { return reject(this.validationError('bank', error)); }
        else if(error) { return reject(error); }
        resolve(token.id);
      });
    }
  });
}



Ben Hulan
  • 537
  • 2
  • 7
0
const toUrlEncoded = obj => Object.keys(obj).map(k => encodeURIComponent(k) + '=' + encodeURIComponent(obj[k])).join('&');


const data = toUrlEncoded({
          key: 'Recurly_Public_Token_Key',
          version: '4.12.0',
          first_name: 'Vasyl',
          last_name: 'Pupkin',
          number: '4111111111111111',
          year: '2022',
          month: '12',
          cvv: '999',
          address1: 'Freedom st.',
          country: 'US',
          city: 'NY',
          state: 'NY',
          postal_code: '51200',
});

await axios({
    url: 'https://api.recurly.com/js/v1/token',
    method: 'post',
    data,
});

Don't forget to update Recurly_Public_Token_Key with your public key which can be found at recurly api_keys.

Ilarion Halushka
  • 2,083
  • 18
  • 13