我使用Stripe作为支付网关。我可以成功地获得一张卡的"令牌",就像Stripe所说的那样,这里有解释:https://stripe.com/docs/checkout#api很好。我成功地在我的条纹仪表板中接收到令牌。现在我要做的就是给那张卡(令牌)充钱。他们是这样说的:"你已经得到了用户的信用卡信息,现在该怎么办?现在你向他们收费。这发生在你的服务器上,最快的方法就是使用我们的客户端库。"他们在页面上提供了php所需的内容:
// Set your secret key: remember to change this to your live secret key in production
// See your keys here https://manage.stripe.com/account
Stripe::setApiKey("sk_test_68YYnDTKPzcxxRZbkxEJtOVq");
// 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
}
你可以看到他们是如何解释的:https://stripe.com/docs/tutorials/charges
这就是问题所在。1)这段代码中的哪个地方实际上指明了要充哪张卡?我的私人仪表盘里有这张卡的令牌ID号。我现在需要充值,但是这段代码没有提到任何关于这张卡的信息。我唯一看到的地方是:
$token = $_POST['stripeToken'];
'stripeToken'我把卡的身份证号码在这里吗?
2)我创建了一个简单的页面来运行这个: <div id="payment">
<form action="charge-cards.php">
<?php require 'Stripe.php';?>
<input type="submit">
</form>
</div><!--end payment-->
"charge-cards.php"是顶部由Stripe提供的代码。当我单击submit时,我得到以下错误:
Fatal error: Class 'Stripe' not found in /home/preemin/public_html/fun.dit /testing/charge-cards.php on line 5
有人能看出我做错了什么吗?谢谢!
我认为有必要后退一步,回顾一下基础知识。首先,为了提供安全的信用卡交易,并确保PCI合规性,Stripe提供了一个javascript库来加密和传输敏感的信用卡数据。它们提供了一个示例表单
请注意表单没有提交操作。还要特别注意一个事实,即所有敏感的卡片字段都没有name属性。此表单不应该通过他们提供的安全方法以外的任何方式提交。如果你想这样做,你就有可能承担责任。
当你提交表单时,使用他们提供的js类(参见第2步),它会给你一个卡片令牌,你说你已经有了。你可以在没有任何安全问题的情况下将卡片令牌存储在数据库中——它只是一个任意数字,对黑客来说没有任何意义。
一旦你有了这个令牌,你有两个选择:
- 立即使用一次性收费,或
- 存储它并创建一个可以多次收费的客户对象。
你需要意识到的是Stripe不存储卡片令牌!如果你失去了它,你就不能再找回来了。你必须生成一个新的。另外,可以多次使用它的唯一方法是创建一个客户对象。换句话说,除非您将其存储在客户对象中,否则它只能用于一次充电。因此,如果您在将附加到客户对象之前对其进行收费,则不再有效。
现在再次回到基础,正如IOIO MAD所描述的,在任何想要调用Stripe方法的php文件的顶部,必须做两件事:
- 包含Stripe php库
- 设置密钥
使用公钥的唯一时间是当您调用javascript来提交卡片表单时。
如果你想立即刷卡,你必须转过身,给服务器回一个电话,发送你刚刚得到的卡片哈希值。您可以使用ajax,或者将其粘贴到表单字段中,然后再发布。步骤3向您展示了一种方法。
但是让我们假设您想在实际刷卡之前做其他事情。为了扩展您的示例,假设我在一个页面上收集了客户的卡,然后在我的结帐页面上,我想提交费用:
<?php
require('path/to/Stripe.php'); // MUST be done before any Stripe:: call
Stripe::setApiKey("sk_test_mkGsLqEW6SLnZa487HYfJVLf");
// retrieve stored card hash. Could have got it to this page in any number of
// ways, including: stored in cookie, stored in session, stored in database.
// Let's assume stored in session for sake of simplicity of example
$cardHash = $_SESSION['stripeToken'];
?>
<div id="payment">
<form action="charge-cards.php">
<?php require 'Stripe.php';?>
<input name="stripeToken" type="hidden" value="<?= $cardHash ?>" />
<input type="submit">
</form>
</div>
当这个表单被提交时,charge-cards.php将从$_POST
变量中获得$cardHash
,然后你将按照Stripe给出的示例:
<?php
require('path/to/Stripe.php'); // MUST be done before any Stripe:: call
Stripe::setApiKey("sk_test_mkGsLqEW6SLnZa487HYfJVLf");
// 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
}
?>
如果仍然有问题,请使用适当的调试技巧来检查$_POST变量是否包含卡片散列。如果以上都失败,请联系Stripe客户支持。他们的API是一流的,文档和支持也是一流的。
编辑4/15/13OP使用快速结帐方法并使用自定义按钮。以上大部分内容仍然适用。
Custom Buttons部分中概述的代码为自定义按钮分配了一个click处理程序,该处理程序返回一个回调函数,该函数在执行时将隐藏的输入附加到表单中,将stripeToken分配给该字段,并提交表单。在提交表单之前,console.log或警告stripeToken以确保您有一个合法的值:
$('#customButton').click(function(){
var token = function(res){
var $input = $('<input type=hidden name=stripeToken />').val(res.id);
// alert the value of stripeToken
alert('stripeToken = ' + res.id);
$('form').append($input).submit();
};
This将与This结合使用:
<form action="/charge" method="post">
<script src="https://checkout.stripe.com/v2/checkout.js" class="stripe-button"
data-key="pk_test_yourprivatekeyhere"></script>
</form>
所以应该发生的是在用户按下checkout按钮之后,表单应该有一个名为'stripeToken'的隐藏输入字段,其中包含令牌值。您可以从表单中获取令牌并稍后提交它。或者,您可以监听'token'事件,该事件将绑定到您的按钮:
jQuery(function($){
var $button = $('#customButton');
$button.on('token', function(e, token){
$button.after('<hr /><p>Your Stripe token is: <strong>' + token.id + '</strong></p>');
});
});
免责声明:我还没有使用简单的结帐方法。这段代码没有测试
我认为可能是你错过了一些"包括",Class 'Stripe' not found
意味着PHP文件组成的类'Stripe'还没有包括在调用之前@
注意:需要PHP>= 5.2环境
下载Stripe PHP库
解压到你的HOME!
让我们创建一个名为config.php的文件,我们将在其中设置一些初始配置。
<?php
require_once('./lib/Stripe.php');
$stripe = array(
"secret_key" => "sk_test_mkGsLqEW6SLnZa487HYfJVLf",
"publishable_key" => "pk_test_czwzkTp2tactuLOEOqbMTRzG"
);
Stripe::setApiKey($stripe['secret_key']);
?>
然后在你的代码文件上面(假设你的。php)
include "config.php";
// Get the credit card details submitted by the form
$token = $_POST['stripeToken'];
//rest as you go!