[英]STRIPE Payment integration with PHP
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. 具有以下代码-可以正常工作。.但是...由于在充电之前DB已更新为PAID,并且如果拒绝了费用,则DB仍将更新为PAID。
Want to achieve - 想要实现-
If charge successful and not declined, charge customer THEN update DB to PAID.. 如果收费成功但未拒绝,则向客户收费,然后将数据库更新为PAID。
NOT 不
Update DB to PAID, then charge customer... 将DB更新为PAID,然后向客户收费...
// 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. 您应该将对Stripe API的所有调用包装在try/catch
块中,以处理可能的错误。
You can find more information in Stripe's API reference: 您可以在Stripe的API参考中找到更多信息:
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 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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.