3

I want to make a payment gateway using Stripe. Here is my code. The config file and first of all i add a stripe library in confiig file. I want a token from this. How do I make or generate a token from stripe?

<?php
require_once('./lib/Stripe.php');

$stripe = array(
    secret_key      => 'sk_test_SrG9Yb8SrhcDNkqsGdc5eKu1',
    publishable_key => 'pk_test_8ZBVXSwrHDKuQe6dgMNfk8Wl'
);

Stripe::setApiKey($stripe['secret_key']);
?>


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

<form action="charge.php" method="post">
<script src="https://checkout.stripe.com/v2/checkout.js" class="stripe-button"
data-key="<?php echo $stripe['publishable_key']; ?>"
data-amount="5000" data-description="One year's subscription"></script>
</form>


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

<form action="charge.php" method="post">
<script src="https://checkout.stripe.com/v2/checkout.js" class="stripe-button"
data-key="<?php echo $stripe['publishable_key']; ?>"
data-amount="5000" data-description="One year's subscription"></script>
</form>
RST
  • 3,899
  • 2
  • 20
  • 33
ravi shah
  • 111
  • 1
  • 1
  • 4
  • did you take a look at this section https://stripe.com/docs/api/php#tokens? There are create examples for bankaccount and card. – RST Jan 12 '15 at 09:10

4 Answers4

8
require_once('../lib/Stripe.php');
                Stripe::setApiKey("sk_test_SrG9Yb8SrhcDNkqsGdc5eKu1");

                $result = Stripe_Token::create(
                    array(
                        "card" => array(
                            "name" => $user['name'],
                            "number" => base64decrypt($user['card_number']),
                            "exp_month" => $user['month'],
                            "exp_year" => $user['year'],
                            "cvc" => base64decrypt($user['cvc_number'])
                        )
                    )
                );

                $token = $result['id'];

                $charge = Stripe_Charge::create(array(
                      "amount" => $data_input['amount']*100,
                      "currency" => "usd",
                      "card" => $token,
                      "description" => "Charge for test@example.com" 
                ));
ravi shah
  • 111
  • 1
  • 1
  • 4
  • I found an answer of this question Using this method you can not compulsory to create user we can direct create key to use the strip payment gateway... – ravi shah Jan 20 '15 at 11:26
3

I found this code snippet on their API Documentation.

You should try to put this code on your charge.php

// Set your secret key: remember to change this to your live secret key in production
// See your keys here https://dashboard.stripe.com/account
Stripe::setApiKey("sk_test_BQokikJOvBiI2HlWgH4olfQ2");

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

Let me know if you still have the problem to grab this token

Brad Larson
  • 170,088
  • 45
  • 397
  • 571
brainware
  • 81
  • 7
1

Updated for stripe 3.12.0

namespace Stripe;

require_once('stripe-php-3.12.0/vendor/autoload.php');
require_once('stripe-php-3.12.0/lib/Stripe.php');


Stripe::setApiKey("yourAPIKey");

// Get the credit card details submitted by the form

$status;

if(isset($_POST['amount'])
{
    $amount = $_POST['amount'] * 100;


$token = Token::create(
                    array(
                            "card" => array(
                            "name" => $user['name'],
                            "number" => $user['card_number'],
                            "exp_month" => $user['month'],
                            "exp_year" => $user['year'],
                            "cvc" => $user['cvc_number']
                        )
                    )
                );


// Create the charge on Stripe's servers - this will charge the user's card

try {
  $charge = Charge::create(array(

    "amount" => $amount , // amount in cents, again
    "currency" => "usd",
    "source" => $token,
    "description" => "Example charge"
    ));} catch(Error\Card $e) {

        $status = "error: " . $e;

} catch(Error\Card $e) {
  // The card has been declined

  $status = "declined: " . $e;
}
}
else
{
    //echo "missing params";

    $status = "missing params";
}
Randall Ridley
  • 363
  • 3
  • 13
0

You may check their docs. In this page they show how to get the token in different languages (Java, PHP and so on, and not only in JavaScript, as showed in their step-by-step guide)

https://stripe.com/docs/api#token_object
Marcelo Noguti
  • 830
  • 1
  • 9
  • 17