2015-06-10 8 views
10

Continuo a ricevere il codice di errore 400 sul mio dashboard stripe. Sembra che io stia usando lo stesso token stripe più di una volta e questo produce un errore. Di seguito è il mio codice.Errore righe 400 - Impossibile utilizzare il token stripe più di una volta

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 
    //} 
?> 

Qualcuno può dire a mia perché non posso scongiuro un cliente? Come sto usando una chiave più volte?

risposta

28

Si usa il token due volte.

In primo luogo, quando si crea il cliente. Secondo, quando si tenta di caricare la carta.

Invece, è possibile creare un cliente e quindi far passare $customer->id di banda quando si crea la carica:

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

l'hai risolto ..... Grazie @Tushar – alaboudi

+4

Forse le stripe hanno cambiato l'API da allora, ma n devi fornire "cliente" e non "fonte" se desideri utilizzare l'ID cliente. Quindi la matrice sarebbe: array ('amout' => 1000, 'currency' => 'cad', 'customer' => $ customer-> id, 'description' => 'example charge') – WoodyDRN

1

È necessario creare al cliente di pagare lui più volte.

1) Aggiungere il token Carta di credito a clienti e creare cliente

2) Utilizzare il cliente Id di addebitare agli utenti

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) 
); 
    } 

per aiutare di più visita: https://stripe.com/docs/tutorials/charges

Problemi correlati