Have the below code - works fine.. BUT... As the DB is updated to PAID before the charge, and if the charge is declined, the DB is still updated to PAID.
Want to achieve -
If charge successful and not declined, charge customer THEN update DB to PAID..
NOT
Update DB to PAID, then charge customer...
// Create connection
$conn = mysqli_connect($hostname, $username,$password, $database);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "UPDATE details SET PaymentStatus='PAID' WHERE MembershipNo ='12345'";
if (mysqli_query($conn, $sql)) {
// starting the session
require_once('config.php');
$token = $_POST['stripeToken'];
$email = $_POST['stripeEmail'];
$SOSamount = ($_SESSION['price']*100);
$FirstName = ($_SESSION['FirstName']);
$LastName = ($_SESSION['LastName']);
$customer = \Stripe\Customer::create(array(
'email' => $email,
'source' => $token,
'description' => "$FirstName $LastName - Membership"
));
$charge = \Stripe\Charge::create(array(
'customer' => $customer->id,
'amount' => $amount,
'currency' => 'aud',
'description' => "Membership - $FirstName $LastName"
));
mysqli_close($conn);
You should wrap all calls to Stripe's API in a try/catch block to handle possible errors.
You can find more information in Stripe's API reference:
https://stripe.com/docs/api/php#errors
https://stripe.com/docs/api/php#error_handling
Basically, your code should look like this:
try {
$customer = \Stripe\Customer::create(...);
$charge = \Stripe\Charge::create(...);
} catch(\Stripe\Error\Card $e) {
// The card was declined, display an error message to your customer
// and end execution
} catch (Exception $e) {
// Another error happened, display an error message to your customer
// and end execution
}
// The charge was completed successfully, update your DB
Thx altoids
Found this snippet, moved Query..
if ($charge->paid == true) {
$conn = mysqli_connect($hostname, $username, $password, $database);
$sql = "UPDATE details SET PaymentStatus='PAID' WHERE MembershipNo ='12345'";
mysqli_query($conn, $sql);
}
Related
I have a simple form that registers users. The form also uses Stripe to process payments. Stripe's API is super clean but for some reason I can't seem to figure this out. Please tell me if i'm doing something wrong here.
Right now I'm in a test account with Stripe, but every time I register a user in stripe it keeps creating a new plan. I know in my code below I have "Stripe\Plan::create" that's because I don't know how to add a new customer to an existing plan. Is this normal the way I'm doing this currently?
In the screenshot below you will see that it will create a new plan for that customer and assign them to it. Is it possible to just keep adding new customers to an existing plan?
// Include configuration file
require_once 'stripe/config.php';
// Include Stripe PHP library
require_once 'vendor/autoload.php';
$payment_id = $statusMsg = $api_error = '';
$ordStatus = 'error';
// Check whether stripe token is not empty
if(!empty($_POST['stripeToken'])){
// Retrieve stripe token, card and user info from the submitted form data
$token = $_POST['stripeToken'];
$users_email = $_POST['email'];
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$full_name = $first_name.' '.$last_name;
// Set API key
\Stripe\Stripe::setApiKey(STRIPE_API_KEY);
// Add customer to stripe
$customer = \Stripe\Customer::create(array(
'email' => $users_email,
'name' => $full_name,
'source' => $token
));
// Create a plan
try {
$plan = \Stripe\Plan::create([
'amount' => 3000,
'currency' => 'usd',
'interval' => 'month',
// 'product' => ['name' => 'Standard Plan'],
'product' => 'prod_ktItFVvqgr4edf',
]);
}catch(Exception $e) {
$api_error = $e->getMessage();
}
if(empty($api_error) && $plan){
// Creates a new subscription
try {
$subscription = \Stripe\Subscription::create(array(
"customer" => $customer->id,
"items" => array(
array(
"plan" => $plan->id,
),
),
));
}catch(Exception $e) {
$api_error = $e->getMessage();
}
if(empty($api_error) && $subscription){
// Retrieve subscription data
$subsData = $subscription->jsonSerialize();
// Check whether the subscription activation is successful
if($subsData['status'] == 'active'){
// Subscription info
$subscrID = $subsData['id'];
$custID = $subsData['customer'];
$planID = $subsData['plan']['id'];
$created = date("Y-m-d H:i:s", $subsData['created']);
$status = $subsData['status'];
// Insert into database
// Insert into database
}else{
$statusMsg = "Subscription activation failed!";
}
}else{
$statusMsg = "Subscription creation failed! ".$api_error;
}
}else{
$statusMsg = "Plan creation failed! ".$api_error;
}
}else{
$statusMsg = "Error on form submission, please try again.";
}
To create a subscription with an existing plan, simply pass in that older plan's ID when creating the subscription instead of creating a new plan for each customer. In your // Create a plan block, either retrieve an existing plan and use that to create the subscription or create a new plan if needed. Or better yet, store the plan ID's in your own database for easier retrieval.
It seems I am missing something when trying to handle some errors with Stripe in test mode with PHP.
Payment are successful when I enter a test card number like 4242424242424242 (for VISA) but I have no error and nothing is recorded when I enter a bad card number.
It is like if the button "Pay" was not working although it displays an error message if I use a real correct card number in test mode.
Does someone know what am I doing wrong here ? Thanks.
Here is the PHP code :
// Check whether stripe token is not empty
if(!empty($_POST['stripeToken'])){
// Retrieve stripe token, card and user info from the submitted form data
$token = $_POST['stripeToken'];
$name = $_POST['name'];
$email = $_POST['email'];
$card_number = $_POST['card_number'];
$card_exp_month = $_POST['card_exp_month'];
$card_exp_year = $_POST['card_exp_year'];
$card_cvc = $_POST['card_cvc'];
$itemPrice = $_POST['itemPrice'];
$itemNumber = $_POST['itemNumber'];
$Ticket_number = $_POST['Ticket_number'];
// Include Stripe PHP library
require_once 'stripe-php/init.php';
// Set API key
\Stripe\Stripe::setApiKey(STRIPE_API_KEY);
// Add customer to stripe
$customer = \Stripe\Customer::create(array(
'email' => $email,
'source' => $token
));
// Unique order ID
$orderID = strtoupper(str_replace('.','',uniqid('', true)));
// Convert price to cents
$itemPrice = ($itemPrice*100);
// Charge a credit or a debit card
$charge = \Stripe\Charge::create(array(
'customer' => $customer->id,
'amount' => $itemPrice,
'currency' => $currency,
'description' => $itemName,
'metadata' => array(
'order_id' => $orderID
)
));
// Retrieve charge details
$chargeJson = $charge->jsonSerialize();
// Check whether the charge is successful
if($chargeJson['amount_refunded'] == 0 && empty($chargeJson['failure_code']) && $chargeJson['paid'] == 1 && $chargeJson['captured'] == 1){
// Order details
$transactionID = $chargeJson['balance_transaction'];
$paidAmount = $chargeJson['amount'];
$paidCurrency = $chargeJson['currency'];
$payment_status = $chargeJson['status'];
// Include database connection file
include_once 'dbConnect.php';
$itemPrice = ($itemPrice/100);
$paidAmount = ($paidAmount/100);
// Insert tansaction data into the database
$sql = "INSERT INTO orders(name,email,card_number,card_exp_month,card_exp_year,item_name,item_number,item_price,item_price_currency,paid_amount,paid_amount_currency,txn_id,payment_status,created,modified) VALUES('".$name."','".$email."','".$card_number."','".$card_exp_month."','".$card_exp_year."','".$itemName."','".$itemNumber."','".$itemPrice."','".$currency."','".$paidAmount."','".$paidCurrency."','".$transactionID."','".$payment_status."',NOW(),NOW())";
$insert = $db->query($sql);
$payment_id = $db->insert_id;
// If the order is successful
if($payment_status == 'succeeded'){
$ordStatus = 'success';
$statusMsg = '';
}else{
$statusMsg = "Your Payment has Failed!";
}
}else{
print '<pre>';print_r($chargeJson);
$statusMsg = "Transaction has been failed!";
}
}else{
$statusMsg = "Error on form submission.";
}
?>
Thank you for your answers but in fact it was a dummy mistake : I did not declare an id to display my error. I had to add the following code in my form :
<p id="payment-status" role="alert"></p>
On this page payment.php, I would like to create a customer for payment on stripe, but I have nothing in my page, nothing in the console, there is written NULL, I don't know how to make it work.
<?php
$token = $_POST['token'];
$name = $_POST['nom'];
require_once('assets/vendor/stripe/init.php');
// (switch to the live key later)
\Stripe\Stripe::setApiKey("sk_test_p5p8Apo5Mn2LIn1po1tg5tYe33df6vQf");
try
{
$customer = \Stripe\Customer::create([
"name" => $name,
"source" => $token,
]);
$reponse= json_decode($customer);
var_dump($reponse);
}
catch(Exception $e)
{
error_log("unable to sign up customer:" . $_POST['stripeEmail'].
", error:" . $e->getMessage());
}
I have this credit card adding when a customer first signs up
// CREDIT CARD CODE (STRIPE)
$q_get_user = "select * from users where `id` = '$user_id' ";
$r_get_user = mysqli_query($conn,$q_get_user);
$get_user = mysqli_fetch_assoc($r_get_user);
if(1) {
\Stripe\Stripe::setApiKey("sk_live_9N676756776");
try {
$t = \Stripe\Token::create(
array(
"card" => array(
"name" => $get_user['first_name']." ".$get_user['last_name'],
"number" => $credit_num,
"exp_month" => $credit_month,
"exp_year" => $credit_year,
"cvc" => $credit_ccv
)
)
);
if($t->id != '') {
try {
$c = \Stripe\Customer::create(array(
"description" => "Customer for ".$get_user['email'],
"source" => $t->id)
);
if($c->id != '') {
$stripe_token_response = mysqli_real_escape_string($conn, json_encode($t));
$stripe_token_id = mysqli_real_escape_string($conn, $t->id);
$stripe_customer_response = mysqli_real_escape_string($conn, json_encode($c));
$stripe_customer_id = mysqli_real_escape_string($conn, $c->id);
$stripe_card_id = mysqli_real_escape_string($conn, $c->default_source);
}
} catch (Exception $e) {
//print_r($e->getMessage());
header('Location: /credits?error=cc&message='.urlencode($e->getMessage()));die;
}
}
} catch (Exception $e) {
//print_r($e->getMessage());
header('Location: /credits?error=cc&message='.urlencode($e->getMessage()));die;
}
}
// END - CREDIT CARD CODE (STRIPE)
How can I make it inside of it being for a new customer for it to add to an existing customer? Therefore the customer is adding a new card (they will have more than one)
You are sending card details through the API directly, which is probably not something you want to do. This means that you get the card numbers on your server which has some serious PCI compliance implications. I would strongly advise you to modify your integration so that you always tokenize the card details first by using Stripe.js or Stripe Checkout client-side to send the card details to Stripe directly and get a unique card token (tok_XXX) that you'd then send safely to your server to create the Customer or add as a Card.
You can find a description of the 'card update' process here; the only difference you need is that, instead of doing this to replace the card:
$cu->source = $_POST['stripeToken']; // obtained with Checkout
You want to do this to add a new one:
$customer->sources->create(array("source" => $t->id));
I'm using CURL to request large XML Files from an API.
To prevent memory leaks I use this CURL option to stream the data and send it to the function curlCallback:
curl_setopt($ch, CURLOPT_WRITEFUNCTION, array($splitter, 'curlCallback'));
In the curlCallback I prepare the incoming XML Stream and call the function below to store every main XML Node in the MySQL Database. Everything works well but:
I want to optimize the efficiency to store the data in the MySQL Database. This is the actual code:
public function processLine($str) {
$prdData = simplexml_load_string($str);
// connect to mysql db
$servername = "localhost";
$username = "";
$password = "";
$dbname = 'temp';
$db = new \PDO('mysql:host=' . $servername . ';dbname=' . $dbname . ';charset=utf8mb4',
$username,
$password,
array(
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
\PDO::ATTR_PERSISTENT => false
)
);
try {
$stmt = $db->prepare("INSERT IGNORE INTO Product (PRDNO, DSCRD ,DSCRF, DSCRLONGD, DSCRLONGF, PRTNO, SMCAT, DEL, BNAMD) VALUES (:prdno, :dscrd, :dscrf, :dscrlongd, :dscrlongf, :prtno, :smcat, :del, :bnamd)");
// MySQL Transaction
$db->beginTransaction();
$stmt->bindParam(':prdno', $prdData->PRDNO);
$stmt->bindParam(':dscrd', $prdData->DSCRD);
$stmt->bindParam(':dscrf', $prdData->DSCRF);
$stmt->bindParam(':dscrlongd', $prdData->DSCRLONGD);
$stmt->bindParam(':dscrlongf', $prdData->DSCRLONGF);
$stmt->bindParam(':prtno', $prdData->PRTNO);
$stmt->bindParam(':smcat', $prdData->SMCAT);
$stmt->bindParam(':del', $prdData->DEL);
$stmt->bindParam(':bnamd', $prdData->BNAMD);
$stmt->execute();
$db->commit();
} catch (PDOException $e) {
error_log(date("d.m.Y H:i:s") . ' | ' . $e->getMessage() . PHP_EOL, 3, '/var/www/html/log/import.log');
$db->rollBack();
}
}
How can I optimize this to just send one transaction including for example 100 Rows?