首页 文章

我使用这个PHP代码错了吗? (使用Stripe)

提问于
浏览
3

我是'm using Stripe for a payment gateway. I can successfully obtain a card' s "token"正如Stripe所说的那样,这里解释一下:https://stripe.com/docs/checkout#api很好 . 我在条带仪表板中成功收到了令牌 . 现在我需要做的是实际收取该卡(令牌) . 以下是他们的说法:"You've got your user's credit card details, now what? Now you charge them money. This happens on your server, and the fastest way to do it is by using one of our client libraries."他们在该页面上提供所需的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提供的顶部代码 . 当我点击提交时,我收到以下错误:

Fatal error: Class 'Stripe' not found in /home/preemin/public_html/fun.dit       /testing/charge-cards.php on line 5

谁能看到我做错了什么?谢谢!

2 回答

  • 16

    我认为退一步并审查基础知识是值得的 . 首先,为了提供安全的信用卡交易,并确保PCI合规性,Stripe提供了一个加密和传输敏感信用卡数据的JavaScript库 . 他们提供example form

    请仔细注意表单没有提交操作的事实 . 还要特别注意这些敏感卡片字段都没有name属性 . 除了他们提供的安全方法之外,不得以任何方式提交此表单 . 试图这样做会让您承担责任 .

    当您提交该表单时,使用他们提供的js类(请参阅Step 2),它会返回一个卡片令牌,您说您已经拥有该令牌 . 您可以将该卡令牌存储在数据库中而不会出现任何安全问题 - 它只是一个任意数字,对黑客来说没有任何意义 .

    拥有该令牌后,您有两种选择:

    • 立即使用它来进行一次性充电,或者

    • 存储它并创建一个可以多次充电的客户对象 .

    你需要意识到的是 Stripe does NOT store that card token! 如果你失去它,你就无法收回它 . 你必须生成一个新的 . 另外, the only way you can use it for multiple charges is to create a customer object . 换句话说,除非您将其存储在客户对象上,否则只需一次充电即可 . 所以 if you charge against it before attaching it to a customer object, it's no longer valid .

    现在再回到基础知识,正如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, // 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/13 OP正在使用quick checkout method并使用自定义按钮 . 以上大部分仍然适用 .

    自定义按钮部分中列出的代码为自定义按钮分配了一个单击处理程序,该按钮返回一个回调函数,该函数在执行时,将隐藏的输入附加到表单,将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>');
      });
    });
    

    免责声明:我还没有使用简单的结账方法 . 这段代码是未经测试

  • 2

    我想可能你错过了一些"include", Class 'Stripe' not found 意味着在调用@之前没有包含类'Stripe'的PHP文件

    注意:需要PHP> = 5.2环境

    下载The Stripe PHP library

    把它提取到你的家!

    让我们创建一个名为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']);
    ?>
    

    然后在您的代码文件ABOVE(假设yours.php)

    include "config.php";
     // Get the credit card details submitted by the form
      $token = $_POST['stripeToken'];
     //rest as you go!
    

相关问题