我使用这个PHP代码错了吗? (使用Stripe)
我正在使用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 这是出问题的地方. $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提供的顶部代码.当我点击提交时,我收到以下错误: Fatal error: Class 'Stripe' not found in /home/preemin/public_html/fun.dit /testing/charge-cards.php on line 5 谁能看到我做错了什么?谢谢! 解决方法
我认为退一步并审查基础知识是值得的.首先,为了提供安全的信用卡交易,并确保PCI合规性,Stripe提供了一个加密和传输敏感信用卡数据的JavaScript库.他们提供
example form
请仔细注意表单没有提交操作的事实.还要特别注意这些敏感卡片字段都没有name属性.除了他们提供的安全方法之外,不得以任何方式提交此表单.试图这样做会让您承担责任. 当您提交该表单时,使用他们提供的js类(参见Step 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,然后您将按照example given by 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,again "currency" => "usd","description" => "payinguser@example.com") ); } catch(Stripe_CardError $e) { // The card has been declined } ?> 如果您仍然遇到问题,请使用适当的调试技巧来检查$_POST变量是否包含卡片哈希.如果一切都失败了,请联系Stripe客户支持.他们的API是一流的,他们的文档和支持也是如此. 编辑4/15/13 自定义按钮部分中列出的代码为自定义按钮分配了一个单击处理程序,该按钮返回一个回调函数,该函数在执行时,将隐藏的输入附加到表单,将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(); }; 这将与此一起使用: <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’的隐藏输入字段,其中包含标记值.您可以从表单中获取该令牌并在以后提交.或者,您可以收听“令牌”事件,该事件将绑定到您的按钮: jQuery(function($){ var $button = $('#customButton'); $button.on('token',function(e,token){ $button.after('<hr /><p>Your Stripe token is: <strong>' + token.id + '</strong></p>'); }); }); 免责声明:我还没有使用简单的结账方法.此代码未经过测试 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |