Stripe: 验证可发布和秘密的API密钥[英] Stripe: Validating Publishable and Secret API Keys

本文是小编为大家收集整理的关于Stripe: 验证可发布和秘密的API密钥的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在建立一个Web应用程序,允许用户销售音乐节目的门票.为了处理票务买家和显示煽动者之间的付款,我使用条纹. 基本上,Show Instigator在我的应用程序上创建他的展示页面,用户可以购买此节目的门票.

为了创建一个节目,Instigator填写表单(显示的名称,显示日期,显示将发生的地方,哪些乐队将会播放.)此表单还需要显示举例师提供他的可发布的和秘密条纹密钥.我的应用程序使用这些标记来检索信用购物车信息(在客户端)和流程付款(在服务器端).

问题是,我想确保显示一个增量者提供有效和现有条纹密钥.我不希望我的用户偶然跨得付款错误,因为Show Instigator没有提供有效的条纹密钥.

所以,我的问题是: 如何验证可发布的和密钥是否有效和存在?实现这一目标的最佳策略是什么?谢谢!

推荐答案

我不知道任何可以专门用于验证键的文件的API调用.这是一个可能尝试的建议:

要求您的合作伙伴提供有效的信用卡,并通知他们,以便验证其条纹键,您将为他们的卡提供0.50美元的收费,将立即退还.

作为表单验证的一部分,当给出两个键时,提交隐藏的表单包含创建卡令牌所需的所有数据.您应该能够检查创建卡响应处理程序并确定是否是可发布的键有效.

如果从包含卡令牌的条带服务器获得成功的响应,请右转和提交a测试充电 $ 0.50(最低电荷金额).

确保您正确捕获所有条带异常.我相信一个无效的密钥,你应该抓住一个 stripe_invalidrequesterror .如果抛出异常,您可以向用户报告.

如果没有抛出错误,则将进行充电.由于您不想为合作伙伴收取费用,因此您需要从条带响应中捕获充电ID,并立即退款.

其他推荐答案

get它!

要验证您的可发布键,您需要使用 curl 询问新令牌的条纹. 如果给定的密钥无效,则响应将包含与"无效的API键""开始的错误消息.

这是PHP中编写的一个例子:

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "https://api.stripe.com/v1/tokens");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "card[number]=4242424242424242&card[exp_month]=12&card[exp_year]=2017&card[cvc]=123");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD, $publishableKey . ":");

$response = json_decode(curl_exec($ch),true);

if( curl_errno($ch) ){
    echo 'Error:' . curl_error($ch);
}
curl_close ($ch);

if(substr($response["error"]["message"],0, 24 ) == "Invalid API Key provided"){
    echo "Invalid API Key provided";
}

验证秘密键的同一想法.

其他推荐答案

验证秘密密钥很简单,只需将条带API调用在服务器端上的任何命令.

但是对于公钥......我找到了与stripe.js的方式:

let stripe = Stripe( <public key to test> );
setTimeout( ()=>{
    stripe.createToken('pii', {personal_id_number: 'test'})
        .then( result =>{
            if( result.token )
               // public key is valid :o)
            else 
              // nope !
        })
}, 300 )

注意调用stripe.createToken()之前的超时.如果你不这样做,createDoken()返回的承诺永远不会回来.

更新:刚收到条纹的确认;这是一种有效且可接受的方法.

本文地址:https://www.itbaoku.cn/post/1937879.html

问题描述

I'm builiding a web application that allows our users to sell tickets for music shows. In order to handle the payments between ticket buyers and show instigators, I use Stripe. Basically, the show instigator creates his show's page on my application, and the users can buy tickets for this show.

In order to create a show, the instigator fills in a form (Show's name, show's date, where the show will take place, what bands will be playing, etc.) This form also requires the show instigator to provide both his Publishable and Secret Stripe keys. My app uses both these tokens to retrieve credit cart information (on the client side) and process payments (on the server side).

The problem is, I want to make sure that show instigators provide valid and existing Stripe keys. I wouldn't want my users to stumble across payments errors because show instigators did not provide valid Stripe keys.

So, my question is: How can I verify that Publishable and Secret keys are valid and existing? What's the best strategy to achieve this? Thanks!

推荐答案

I am not aware of any documented api call that can be made specifically to validate keys. Here is a suggestion you might try:

Require your partners to provide a valid credit card and inform them that in order to validate their Stripe keys, you will be making a $0.50 charge to their card that will be immediately refunded.

As part of your form validation, when both keys are given, submit a hidden form that contains all the data necessary to create a card token. You should be able to examine the response in your create card token response handler and determine if the publishable key is valid.

If you get a successful response back from the stripe server containing a card token, turn right around and submit a test charge for $0.50 (the minimum charge amount).

Make sure you're properly catching all the stripe exceptions. I believe with an invalid secret key, you should catch a Stripe_InvalidRequestError. If an exception is thrown you can report to the user.

If no errors are thrown, the charge will be made. Since you don't want to charge your partners, you'll want to capture the charge id from the stripe response and immediately refund the charge.

其他推荐答案

Got it!

To validate your publishable keys you need to ask stripe for a new token using cURL. If the given key is invalid the response will contain an error message starting with "Invalid API Key provided".

Here's an example written in PHP:

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "https://api.stripe.com/v1/tokens");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "card[number]=4242424242424242&card[exp_month]=12&card[exp_year]=2017&card[cvc]=123");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD, $publishableKey . ":");

$response = json_decode(curl_exec($ch),true);

if( curl_errno($ch) ){
    echo 'Error:' . curl_error($ch);
}
curl_close ($ch);

if(substr($response["error"]["message"],0, 24 ) == "Invalid API Key provided"){
    echo "Invalid API Key provided";
}

Same idea for validating your secret keys.

其他推荐答案

Validating the secret key is easy, simply calling the Stripe API with any command on the server side.

But for the public key... I found a way with Stripe.js :

let stripe = Stripe( <public key to test> );
setTimeout( ()=>{
    stripe.createToken('pii', {personal_id_number: 'test'})
        .then( result =>{
            if( result.token )
               // public key is valid :o)
            else 
              // nope !
        })
}, 300 )

Note the timeout before calling stripe.createToken(). If you don't do it, the promise returned by createToken() will never come back.

UPDATE: Just received a confirmation from Stripe; this it is a valid and acceptable method.