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
namespace PhpPot\Service;
require_once 'vendor/stripe/autoload.php';
use \Stripe\Stripe;
use \Stripe\Customer;
use \Stripe\ApiOperations\Create;
use \Stripe\Charge;
class StripePayment
{
private $apiKey;
private $stripeService;
public function __construct()
{
require_once "config.php";
$this->apiKey = STRIPE_SECRET_KEY;
$this->stripeService = new \Stripe\Stripe();
$this->stripeService->setVerifySslCerts(false);
$this->stripeService->setApiKey($this->apiKey);
}
public function addCustomer($customerDetailsAry)
{
$customer = new Customer();
$customerDetails = $customer->create($customerDetailsAry);
return $customerDetails;
}
public function chargeAmountFromCard($cardDetails)
{
$customerDetailsAry = array(
'email' => $cardDetails['email'],
'source' => $cardDetails['token']
);
$customerResult = $this->addCustomer($customerDetailsAry);
$charge = new Charge();
$cardDetailsAry = array(
'customer' => $customerResult->id,
'amount' => $cardDetails['amount']*100 ,
'currency' => $cardDetails['currency_code'],
'description' => $cardDetails['item_name'],
'metadata' => array(
'order_id' => $cardDetails['item_number']
)
);
$result = $charge->create($cardDetailsAry);
return $result->jsonSerialize();
}
}
Example 3: cancel stripe subscription php
$stripe = new \Stripe\StripeClient(
'sk_test_4eC39HqLyjWDarjtT1zdp7dc'
);
$stripe->subscriptions->update(
'sub_36VrPHS2vVxJMq',
['metadata' => ['order_id' => '6735']]
);
{
"id": "sub_36VrPHS2vVxJMq",
"object": "subscription",
"application_fee_percent": null,
"billing_cycle_anchor": 1387222772,
"billing_thresholds": null,
"cancel_at": null,
"cancel_at_period_end": false,
"canceled_at": 1445989053,
"collection_method": "charge_automatically",
"created": 1386790772,
"current_period_end": 1447702772,
"current_period_start": 1444678772,
"customer": "cus_36Akh5CetFGg9T",
"days_until_due": null,
"default_payment_method": null,
"default_source": null,
"default_tax_rates": [],
"discount": null,
"ended_at": 1445989053,
"items": {
"object": "list",
"data": [
{
"id": "si_18SfBn2eZvKYlo2C1fwOImYF",
"object": "subscription_item",
"billing_thresholds": null,
"created": 1386790772,
"metadata": {},
"price": {
"id": "40",
"object": "price",
"active": true,
"billing_scheme": "per_unit",
"created": 1386694689,
"currency": "usd",
"livemode": false,
"lookup_key": null,
"metadata": {
"charset": "utf-8",
"content": "40"
},
"nickname": null,
"product": "prod_BTcfj5EqyqxDVn",
"recurring": {
"aggregate_usage": null,
"interval": "week",
"interval_count": 5,
"usage_type": "licensed"
},
"tiers_mode": null,
"transform_quantity": null,
"type": "recurring",
"unit_amount": 5465,
"unit_amount_decimal": "5465"
},
"quantity": 1,
"subscription": "sub_36VrPHS2vVxJMq",
"tax_rates": []
}
],
"has_more": false,
"url": "/v1/subscription_items?subscription=sub_36VrPHS2vVxJMq"
},
"latest_invoice": null,
"livemode": false,
"metadata": {
"order_id": "6735"
},
"next_pending_invoice_item_invoice": null,
"pause_collection": null,
"pending_invoice_item_interval": null,
"pending_setup_intent": null,
"pending_update": null,
"schedule": null,
"start_date": 1386790772,
"status": "canceled",
"transfer_data": null,
"trial_end": 1387222772,
"trial_start": 1386790772
}
Example 4: payment with stripe in php
<?php if(!empty($successMessage)) { ?>
<div id="success-message"><?php echo $successMessage; ?></div>
<?php } ?>
<div id="error-message"></div>
<form id="frmStripePayment" action="" method="post">
<div class="field-row">
<label>Card Holder Name</label> <span id="card-holder-name-info"
class="info"></span><br> <input type="text" id="name"
name="name" class="demoInputBox">
</div>
<div class="field-row">
<label>Email</label> <span id="email-info" class="info"></span><br>
<input type="text" id="email" name="email" class="demoInputBox">
</div>
<div class="field-row">
<label>Card Number</label> <span id="card-number-info"
class="info"></span><br> <input type="text" id="card-number"
name="card-number" class="demoInputBox">
</div>
<div class="field-row">
<div class="contact-row column-right">
<label>Expiry Month / Year</label> <span id="userEmail-info"
class="info"></span><br> <select name="month" id="month"
class="demoSelectBox">
<option value="08">08</option>
<option value="09">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
</select> <select name="year" id="year"
class="demoSelectBox">
<option value="18">2018</option>
<option value="19">2019</option>
<option value="20">2020</option>
<option value="21">2021</option>
<option value="22">2022</option>
<option value="23">2023</option>
<option value="24">2024</option>
<option value="25">2025</option>
<option value="26">2026</option>
<option value="27">2027</option>
<option value="28">2028</option>
<option value="29">2029</option>
<option value="30">2030</option>
</select>
</div>
<div class="contact-row cvv-box">
<label>CVC</label> <span id="cvv-info" class="info"></span><br>
<input type="text" name="cvc" id="cvc"
class="demoInputBox cvv-input">
</div>
</div>
<div>
<input type="submit" name="pay_now" value="Submit"
id="submit-btn" class="btnAction"
onClick="stripePay(event);">
<div id="loader">
<img alt="loader" src="LoaderIcon.gif">
</div>
</div>
<input type='hidden' name='amount' value='0.5'> <input type='hidden'
name='currency_code' value='USD'> <input type='hidden'
name='item_name' value='Test Product'> <input type='hidden'
name='item_number' value='PHPPOTEG#1'>
</form>