【问题标题】:Is there a way to get a billing token from Recurly without using recurly.js?有没有办法在不使用 recurly.js 的情况下从 Recurly 获取计费令牌?
【发布时间】:2023-03-08 08:24:01
【问题描述】:

我们正在迁移到 Recurly 进行计费,并计划使用 recurly.js api 在生产环境中生成计费令牌,但与此同时,在其他环境中进行测试确实很困难。理想情况下,我希望能够将信用卡信息从我的服务器发送到 Recurly 端点,并取回一个计费令牌。

对我来说最简单的方法是什么?如果答案是“使用 recurly.js api”,我该怎么做? Recurly 站点上的唯一示例是向服务器提交表单的网页。我想要相反,我的服务器调用网页或其他端点,并在响应中获取令牌。

【问题讨论】:

    标签: recurly


    【解决方案1】:

    我就是有这个问题。这是我通过 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);
    

    希望对您有所帮助。

    【讨论】:

    • 谢谢 Richard,我没有使用 PHP,但是这段代码很好地展示了如何做到这一点,并且可以很容易地转换为其他语言。
    【解决方案2】:

    这是我在使用 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);
          });
        }
      });
    }
    
    
    
    

    【讨论】:

      【解决方案3】:
      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_Public_Token_Key,该公钥可以在recurly api_keys 找到。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-10-18
        • 1970-01-01
        • 1970-01-01
        • 2023-03-27
        • 2017-02-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多