How to generate token in stripe payment gateway - php

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>

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

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

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

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

Related

I am using stripe in my laravel application, when I submit payment I get this error [duplicate]

I keep receiving error code 400 on my stripe dashboard. It seems like im using the same stripe token more than once and this produces an error. Below is my code.
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' => 'test#test.com',
'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
//}
?>
Can anyone tell my why I cant charge a customer? How am I using a key multiple times?
You do use the token twice.
First, when creating the customer.
Second, when trying to charge the card.
Instead, you can create a customer and and then pass $customer->id to Stripe when you create the charge:
$charge = \Stripe\Charge::create(array(
"amount" => 1000, // amount in cents, again
"currency" => "cad",
"customer" => $customer->id,
"description" => "Example charge")
);
You have to create the customer to charge him multiple times.
1) Add the Credit card token to customer and create customer
2) Use the customer Id to charge users
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)
);
}
for more help visit: https://stripe.com/docs/tutorials/charges

I'm using Stripe API in my code and I have an error : You cannot use a Stripe token more than once: tok_1EIw1gKIAV9zC39qQnFXKzi6

I got an error saying :
Customer cus_EmVoBWWrqnWw4W does not have a linked source with ID tok_1EIw1gKIAV9zC39qQnFXKzi6.
here is my code :
\Stripe\Stripe::setApiKey("sk_test_rUFQ9fpJoK9TlRVJs1nSYd6C");
$user = new User();
$email = $user->getEmail();
//creation du client
$customer = \Stripe\Customer::create(array(
"email" => $email,
));
$charge = \Stripe\Charge::create(array(
"amount" => 2000,
"currency" => "eur",
"source" => $request ->request->get('stripeToken'),
"description" => "Stripe connect - Order 36",
"customer" => $customer->id
));
\Stripe\Stripe::setApiKey("sk_test_rUFQ9fpJoK9TlRVJs1nSYd6C");
$refund = \Stripe\Refund::create([
'charge' => $charge,
'amount' => 1000,
]);
You need to save the card to the customer and then charge the customer. After that, unless the customer has multiple sources and you want to use a specific one, you won't need to pass source to Charge::create.

How to do payment using PayFort payment gateway?

I am trying to add a payment using the PayFort payment gateway, but it fails with this error message:
Charge was not processed Request params are invalid.
Please see my code and give any instructions or suggestions for it:
$api_keys = array(
"secret_key" => "test_sec_k_965cd1f7f333f998c907b",
"open_key" => "test_open_k_d6830e5f0f276ebb9046"
);
/* convert 10.00 AED to cents */
$amount_in_cents = 10.00 * 100;
$currency = "AED";
$customer_email = "myMailId#gmail.com";
Start::setApiKey($api_keys["secret_key"]);
try {
$charge = Start_Charge::create(array(
"amount" => $amount_in_cents,
"currency" => $currency,
"card" => '4242424242424242',
"email" => 'myMailId2#gmail.com',
"ip" => $_SERVER["REMOTE_ADDR"],
"description" => "Charge Description"
));
echo "<h1>Successfully charged 10.00 AED</h1>";
echo "<p>Charge ID: ".$charge["id"]."</p>";
echo "<p>Charge State: ".$charge["state"]."</p>";
die;
} catch (Start_Error $e) {
$error_code = $e->getErrorCode();
$error_message = $e->getMessage();
if ($error_code === "card_declined") {
echo "<h1>Charge was declined</h1>";
} else {
echo "<h1>Charge was not processed</h1>";
}
echo "<p>".$error_message."</p>";
die;
}
I found answer
Start::setApiKey($sadad_detail["open_key"]); //Important
$token = Start_Token::create(array(
"number" => $this->input->post("card-number"),
"exp_month" => $this->input->post("expiry-month"),
"exp_year" => $this->input->post("expiry-year"),
"cvc" => $this->input->post("card-cvv"),
"name" => $this->input->post("card-holder-name")
));
//echo "<pre>"; print_r($token); echo '</pre>';
Start::setApiKey($sadad_detail["secret_key"]); //Important
$currency = getCustomConfigItem('currency_code');
$charge = Start_Charge::create(array(
"amount" => (int)200,
"currency" => $currency,
"card" => $token['id'],
"email" => 'test#gmail.com',
"ip" => $_SERVER["REMOTE_ADDR"],
"description" => "Charge Description"
));
Create token first using "open_key" and start charge using "secret_key".
It's work fine. Thanks all.

How to cancel the stripe payment and debit the money to client account

Hello I am using following code for transaction
require_once('Stripe/lib/Stripe.php');
Stripe::setApiKey("<Api key>");
$customer = Stripe_Charge::create(array(
"amount" => 1500,
"currency" => "usd",
"card" => $_POST['stripeToken'],
"description" => "Charge for Facebook Login code."
));
echo "<pre>";print_r($customer);die;
// Charge the Customer instead of the card
$charge = Stripe_Charge::create(array(
"amount" => 1500,
"currency" => "usd",
"customer" => $customer->id)
);
echo 'Transaction Id '.$charge->id;
I have one cancel button if i click on that the payment which done by user will be revert back to client account
You'll most likely need to refund the money you charged from the card and then have the Cancel button run the charge on the customer instead.
This can be accomplished as followed:
(1) Retrieve the charge's identifier:
$charge = \Stripe\Charge::create(array(
...
));
// you can use the var_dump function to see what's inside the $charge object
// var_dump($charge);
$chargeID = $charge->id;
(2) Refund the user:
$re = \Stripe\Refund::create(array(
"charge" => $chargeID
));
(3) Charge the customer instead:
// Charge the Customer instead of the card
$charge = Stripe_Charge::create(array(
"amount" => 1500,
"currency" => "usd",
"customer" => $customer->id)
);
echo 'Transaction Id '.$charge->id;

Class 'Stripe\StripeCharge' not found

I've had a look at a few answer to this quesition on Stack Overflow, but there solutions didnt work for me. Im receiving the following error whilst testing the Stripe API
Class 'Stripe\StripeCharge' not found
This is the code that I am using:
require_once('app/init.php');
\Stripe\Stripe::setApiKey($stripe['private']);
if(isset($_POST['stripeToken'])){
$token = $_POST['stripeToken'];
try {
\Stripe\StripeCharge::create(array(
"amount" => 2000,
"currency" => "gbp",
"card" => "$token",
"description" => $Email
));
} catch(Stripe_CardError $e){
//Error Payment
}
}
echo $_POST['stripeToken'];
/* Stripe Vairables */
$stripe = [
'publishable' => 'hidden',
'private' => 'hidden'
];
I didn't use composer to pull this as it works with the include of the "init" file (supposedly). Any help would be great!
The correct name of the class is \Stripe\Charge, not \Stripe\StripeCharge. You can see an example of a charge creation request in the API documentation: https://stripe.com/docs/api/php#create_charge
Also, the card parameter was renamed to source in the 2015-02-18 API version.
Another problem is that you assign the $stripe array with your API keys at the end. You need to assign it before you can use it in the call to \Stripe\Stripe::setApiKey().
There were a few other minor mistakes. Here is a corrected version of your code:
require_once('app/init.php');
/* Stripe variables */
$stripe = [
'publishable' => 'hidden',
'private' => 'hidden'
];
\Stripe\Stripe::setApiKey($stripe['private']);
if(isset($_POST['stripeToken'])) {
$token = $_POST['stripeToken'];
$email = $_POST['stripeEmail'];
try {
$charge = \Stripe\Charge::create(array(
"amount" => 2000,
"currency" => "gbp",
"source" => $token,
"description" => $email
));
// echo $charge->id;
} catch(\Stripe\Error\Card $e) {
// Card error
}
}

Categories