stripe php api library code example
Example 1: payment with stripe in php
<script type="text/javascript" src="https://js.stripe.com/v2/"></script>
<script src="vendor/jquery/jquery-3.2.1.min.js" type="text/javascript"></script>
<script>
function cardValidation () {
var valid = true;
var name = $('#name').val();
var email = $('#email').val();
var cardNumber = $('#card-number').val();
var month = $('#month').val();
var year = $('#year').val();
var cvc = $('#cvc').val();
$("#error-message").html("").hide();
if (name.trim() == "") {
valid = false;
}
if (email.trim() == "") {
valid = false;
}
if (cardNumber.trim() == "") {
valid = false;
}
if (month.trim() == "") {
valid = false;
}
if (year.trim() == "") {
valid = false;
}
if (cvc.trim() == "") {
valid = false;
}
if(valid == false) {
$("#error-message").html("All Fields are required").show();
}
return valid;
}
Stripe.setPublishableKey("<?php echo STRIPE_PUBLISHABLE_KEY; ?>");
function stripeResponseHandler(status, response) {
if (response.error) {
$("#submit-btn").show();
$( "#loader" ).css("display", "none");
$("#error-message").html(response.error.message).show();
} else {
var token = response['id'];
$("#frmStripePayment").append("<input type='hidden' name='token' value='" + token + "' />");
$("#frmStripePayment").submit();
}
}
function stripePay(e) {
e.preventDefault();
var valid = cardValidation();
if(valid == true) {
$("#submit-btn").hide();
$( "#loader" ).css("display", "inline-block");
Stripe.createToken({
number: $('#card-number').val(),
cvc: $('#cvc').val(),
exp_month: $('#month').val(),
exp_year: $('#year').val()
}, stripeResponseHandler);
return false;
}
}
</script>
Example 2: payment with stripe in php
<?php
use \PhpPot\Service\StripePayment;
if (!empty($_POST["token"])) {
require_once 'StripePayment.php';
$stripePayment = new StripePayment();
$stripeResponse = $stripePayment->chargeAmountFromCard($_POST);
require_once "DBController.php";
$dbController = new DBController();
$amount = $stripeResponse["amount"] /100;
$param_type = 'ssdssss';
$param_value_array = array(
$_POST['email'],
$_POST['item_number'],
$amount,
$stripeResponse["currency"],
$stripeResponse["balance_transaction"],
$stripeResponse["status"],
json_encode($stripeResponse)
);
$query = "INSERT INTO tbl_payment (email, item_number, amount, currency_code, txn_id, payment_status, payment_response) values (?, ?, ?, ?, ?, ?, ?)";
$id = $dbController->insert($query, $param_type, $param_value_array);
if ($stripeResponse['amount_refunded'] == 0 && empty($stripeResponse['failure_code']) && $stripeResponse['paid'] == 1 && $stripeResponse['captured'] == 1 && $stripeResponse['status'] == 'succeeded') {
$successMessage = "Stripe payment is completed successfully. The TXN ID is " . $stripeResponse["balance_transaction"];
}
}
?>