我们正在转向Recurly进行计费,并计划使用recurly.js api在生产中生成计费令牌,但与此同时,在其他环境中测试真的很难。理想情况下,我希望能够将信用卡信息从我的服务器发送到Recurly端点,并获取结算令牌。
对我来说,最简单的方法是什么?如果答案是'请使用recurly.js api',我该怎么做? Recurly站点上的唯一示例是将表单提交给服务器的网页。我想要相反,我的服务器调用网页或其他端点,并在响应中获取令牌。
答案 0 :(得分:7)
我刚才有这个问题。这是我通过curl(在PHP中)从recurly.js使用的URL收集测试令牌的解决方案。
// 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);
希望有所帮助。
答案 1 :(得分:0)
这是我在使用RecurlyJS的Angular5 / Typescript环境中使用的函数。认识到信用卡帐单令牌与银行帐户之间的区别很重要。
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);
});
}
});
}
答案 2 :(得分: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,
});
请不要忘记使用recurly api_keys上的公共密钥更新Recurly_Public_Token_Key
。