簡體   English   中英

我使用這個PHP代碼錯了嗎? (使用Stripe)

[英]Am I using this PHP code wrong? (working with 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

這是出問題的地方。 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

誰能看到我做錯了什么?謝謝!

我認為退一步並審查基礎知識是值得的。 首先,為了提供安全的信用卡交易,並確保PCI合規性,Stripe提供了一個加密和傳輸敏感信用卡數據的JavaScript庫。 它們提供了一個示例表單

請仔細注意表單沒有提交操作的事實。 還要特別注意這些敏感卡片字段都沒有name屬性。 除了他們提供的安全方法之外, 不得以任何方式提交此表單。 試圖這樣做會讓您承擔責任。

當您提交該表單時,使用他們提供的js類(請參閱步驟2 ),它會返回一個卡片令牌,您說您已經擁有該令牌。 您可以將該卡令牌存儲在數據庫中而不會出現任何安全問題 - 它只是一個任意數字,對黑客來說沒有任何意義。

擁有該令牌后,您有兩種選擇:

  1. 立即使用它來進行一次性充電,或者
  2. 存儲它並創建一個可以多次充電的客戶對象。

您需要意識到Stripe不存儲該卡令牌! 如果你失去它,你就無法取回它。 你必須生成一個新的。 此外, 您可以將其用於多次收費的唯一方法是創建客戶對象 換句話說,除非您將其存儲在客戶對象上,否則只需一次充電即可。 因此, 如果您在將其附加到客戶對象之前對其收費,則它不再有效

現在再回到基礎知識,正如IOIO MAD所描述的那樣,你必須在任何你要調用Stripe方法的php文件的頂部做兩件事:

  1. 包括Stripe php庫
  2. 設置密鑰

使用公鑰的唯一時間是當您進行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/13 OP正在使用快速結賬方法並使用自定義按鈕。 以上大部分仍然適用。

自定義按鈕部分中列出的代碼為自定義按鈕分配了一個單擊處理程序,該按鈕返回一個回調函數,該函數在執行時將隱藏的輸入附加到表單,將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>');
  });
});

免責聲明:我還沒有使用簡單的結賬方法。 此代碼未經過測試

我想可能你錯過了一些“包含”, Class 'Stripe' not found意味着在調用@之前沒有包含Class'Stripe'的PHP文件

注意:需要PHP> = 5.2環境

下載Stripe PHP庫

把它提取到你的家!

讓我們創建一個名為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!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM