2015-06-10 10 views
10

Ciągle otrzymuję kod błędu 400 na desce rozdzielczej paska. Wygląda na to, że używam tego samego tokena paska więcej niż jeden raz, a to powoduje błąd. Poniżej znajduje się mój kod.Błąd paska 400 - Nie można użyć tokena paska więcej niż raz

JS:

<script src="https://checkout.stripe.com/checkout.js"></script> 
    <script> 

    var handler = StripeCheckout.configure({ 
     key: 'pk_test_******************', 
     image: '/img/documentation/checkout/marketplace.png', 
     token: function(token) { 
      /*$.post("php/charge.php",{stripeToken:token.id},function(data,status){ 
       console.log("Data: "+ data+"\nStatus: "+status); 
      });*/ 
      alert(token.used);//alerts false 
      $.post("php/charge.php",{stripeToken:token.id}); 
      alert(token.used);// still alerts false 
     } 
     }); 

     $('#myButton').on('click', function(e) { 
     // Open Checkout with further options 
     handler.open({ 
      name: 'Demo Site', 
      description: '2 widgets', 
      currency: "cad", 
      amount: 2000 
     }); 
     e.preventDefault(); 
     }); 

     // Close Checkout on page navigation 
     $(window).on('popstate', function() { 
     handler.close(); 
     }); 
    </script> 

PHP:

<?php 
    require_once('config.php'); 

    $token = $_POST['stripeToken']; 

    $customer = \Stripe\Customer::create(array(
     'email' => '[email protected]', 
     'card' => $token 
)); 

    //try { 
    $charge = \Stripe\Charge::create(array(
     "amount" => 1000, // amount in cents, again 
     "currency" => "cad", 
     "source" => $token, 
     "description" => "Example charge") 
    ); 
    //}catch(\Stripe\Error\Card $e) { 
     // The card has been declined 
    //} 
?> 

Czy ktoś może powiedzieć mi dlaczego nie mogę pobierać klienta? Jak często używam klucza?

Odpowiedz

28

Używasz tokena dwa razy.

Po pierwsze, podczas tworzenia klienta. Po drugie, podczas próby naładowania karty.

Zamiast tego można utworzyć klienta i, a następnie przekazać $customer->id do Stripe podczas tworzenia opłatą:

$charge = \Stripe\Charge::create(array(
    "amount" => 1000, // amount in cents, again 
    "currency" => "cad", 
    "customer" => $customer->id, 
    "description" => "Example charge") 
); 
+0

rozwiązałeś to ..... Dziękuję @Tushar – alaboudi

+4

Może pasek zmienił API od tego czasu, ale n ow musisz podać "klient", a nie "źródło", jeśli chcesz użyć identyfikatora klienta. Więc tablica będzie: array ("amout" => 1000, "currency" => "cad", "customer" => $ customer-> id, 'description' => 'example charge') – WoodyDRN

1

trzeba stworzyć klientowi go wielokrotnie ładować.

1) Dodaj znacznik karty kredytowej do klienta i tworzyć klient

2) Za pomocą klienta ID, aby naładować użytkowników

if (isset($_POST['stripeToken'])){ 

     $token = $_POST['stripeToken']; 

// Create a Customer 
$customer = \Stripe\Customer::create(array(
    "source" => $token, 
    "description" => "Example customer") 
); 

// Charge the Customer instead of the card 
\Stripe\Charge::create(array(
    "amount" => 1000, # amount in cents, again 
    "currency" => "usd", 
    "customer" => $customer->id) 
); 
    } 

więcej wizyta: https://stripe.com/docs/tutorials/charges

Powiązane problemy