如何在条带支付网关中生成令牌
Posted
技术标签:
【中文标题】如何在条带支付网关中生成令牌【英文标题】:How to generate token in stripe payment gateway 【发布时间】:2015-03-10 00:01:42 【问题描述】:我想使用 Stripe 制作支付网关。这是我的代码。配置文件,首先我在配置文件中添加了一个条带库。我想要一个令牌。如何从条带制作或生成令牌?
<?php
require_once('./lib/Stripe.php');
$stripe = array(
secret_key => 'sk_test_SrG9Yb8SrhcDNkqsGdc5eKu1',
publishable_key => 'pk_test_8ZBVXSwrHDKuQe6dgMNfk8Wl'
);
Stripe::setApiKey($stripe['secret_key']);
?>
<?php require_once('config.php'); ?>
<form action="charge.php" method="post">
<script src="https://checkout.stripe.com/v2/checkout.js" class="stripe-button"
data-key="<?php echo $stripe['publishable_key']; ?>"
data-amount="5000" data-description="One year's subscription"></script>
</form>
<?php require_once('config.php'); ?>
<form action="charge.php" method="post">
<script src="https://checkout.stripe.com/v2/checkout.js" class="stripe-button"
data-key="<?php echo $stripe['publishable_key']; ?>"
data-amount="5000" data-description="One year's subscription"></script>
</form>
【问题讨论】:
你看过stripe.com/docs/api/php#tokens这个部分了吗?有银行账户和卡的创建示例。 【参考方案1】:require_once('../lib/Stripe.php');
Stripe::setApiKey("sk_test_SrG9Yb8SrhcDNkqsGdc5eKu1");
$result = Stripe_Token::create(
array(
"card" => array(
"name" => $user['name'],
"number" => base64decrypt($user['card_number']),
"exp_month" => $user['month'],
"exp_year" => $user['year'],
"cvc" => base64decrypt($user['cvc_number'])
)
)
);
$token = $result['id'];
$charge = Stripe_Charge::create(array(
"amount" => $data_input['amount']*100,
"currency" => "usd",
"card" => $token,
"description" => "Charge for test@example.com"
));
【讨论】:
我找到了这个问题的答案使用这种方法你可以不强制创建用户我们可以直接创建密钥来使用条形支付网关...【参考方案2】:我在他们的API Documentation 上找到了这个代码 sn-p。
您应该尝试将此代码放在您的 charge.php
// Set your secret key: remember to change this to your live secret key in production
// See your keys here https://dashboard.stripe.com/account
Stripe::setApiKey("sk_test_BQokikJOvBiI2HlWgH4olfQ2");
// Get the credit card details submitted by the form
$token = $_POST['stripeToken'];
// Create the charge on Stripe's servers - this will charge the user's card
try
$charge = Stripe_Charge::create(array(
"amount" => 1000, // amount in cents, again
"currency" => "usd",
"card" => $token,
"description" => "payinguser@example.com")
);
catch(Stripe_CardError $e)
// The card has been declined
如果您仍然无法获取此令牌,请告诉我
【讨论】:
【参考方案3】:已针对条带 3.12.0 更新
namespace Stripe;
require_once('stripe-php-3.12.0/vendor/autoload.php');
require_once('stripe-php-3.12.0/lib/Stripe.php');
Stripe::setApiKey("yourAPIKey");
// Get the credit card details submitted by the form
$status;
if(isset($_POST['amount'])
$amount = $_POST['amount'] * 100;
$token = Token::create(
array(
"card" => array(
"name" => $user['name'],
"number" => $user['card_number'],
"exp_month" => $user['month'],
"exp_year" => $user['year'],
"cvc" => $user['cvc_number']
)
)
);
// Create the charge on Stripe's servers - this will charge the user's card
try
$charge = Charge::create(array(
"amount" => $amount , // amount in cents, again
"currency" => "usd",
"source" => $token,
"description" => "Example charge"
)); catch(Error\Card $e)
$status = "error: " . $e;
catch(Error\Card $e)
// The card has been declined
$status = "declined: " . $e;
else
//echo "missing params";
$status = "missing params";
【讨论】:
【参考方案4】:您可以查看他们的文档。在此页面中,他们展示了如何以不同的语言(Java、PHP 等,而不仅仅是 javascript,如他们的分步指南所示)获取令牌
https://stripe.com/docs/api#token_object
【讨论】:
以上是关于如何在条带支付网关中生成令牌的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Google Pay 和 Apple Pay 中为 Authorize.net 生成支付令牌(在移动应用交易中)?