Skip to content

Instantly share code, notes, and snippets.

@micc83
Created May 28, 2021 15:39
Show Gist options
  • Save micc83/b7d4d1a48dd65782ea67f9a70aec5091 to your computer and use it in GitHub Desktop.
Save micc83/b7d4d1a48dd65782ea67f9a70aec5091 to your computer and use it in GitHub Desktop.
Stripe SEPA full php example
<?php
/**
* @see https://stripe.com/docs/billing/subscriptions/sepa-debit
*/
use Stripe\Customer;
use Stripe\SetupIntent;
use Stripe\Stripe;
use Stripe\Subscription;
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
require_once('../vendor/autoload.php');
const CUSTOMER_ID = "";
const PUBLIC_KEY = "";
const SECRET_KEY = "";
const BASE_PRODUCT_PRICE_ID = "";
Stripe::setApiKey(SECRET_KEY);
if (isset($_POST['pm'])) {
$paymentMethod = $_POST['pm'];
/**
* Registra metodo di pagamento di default
*/
Customer::update(
CUSTOMER_ID,
[
'invoice_settings' => [
'default_payment_method' => $paymentMethod
],
]
);
/**
* Crea sottoscrizione a zero
*/
Subscription::create([
'customer' => CUSTOMER_ID,
'items' => [
[
'price' => BASE_PRODUCT_PRICE_ID,
],
],
'billing_cycle_anchor' => time() + (60 * 60 * 24 * 365),
]);
return;
}
$setup_intent = SetupIntent::create([
'payment_method_types' => ['sepa_debit'],
'customer' => CUSTOMER_ID,
]);
$client_secret = $setup_intent->client_secret;
?>
<html>
<head>
<title>Submit Payment</title>
<script src="https://js.stripe.com/v3/"></script>
<link rel="stylesheet" href="https://cdn.rawgit.com/Chalarangelo/mini.css/v3.0.1/dist/mini-default.min.css">
</head>
<body>
<div style="max-width: 100%;width: 1024px;margin: 0 auto">
<form action="sepa.php" method="post" id="payment-form">
<input type="hidden" name="">
<div class="form-row inline">
<div class="col">
<label for="accountholder-name">
Name
</label>
<input
id="accountholder-name"
name="accountholder-name"
placeholder="Jenny Rosen"
required
/>
</div>
<div class="col">
<label for="email">
Email Address
</label>
<input
id="email"
name="email"
type="email"
placeholder="[email protected]"
required
/>
</div>
</div>
<div class="form-row">
<!--
Using a label with a for attribute that matches the ID of the
Element container enables the Element to automatically gain focus
when the customer clicks on the label.
-->
<label for="iban-element">
IBAN
</label>
<div id="iban-element" style="padding: 10px; background: #fff;display: inline-block;width: 300px">
<!-- A Stripe Element will be inserted here. -->
</div>
<p>IBAN DI TEST: IT40S0542811101000000123456</p>
</div>
<!-- Add the client_secret from the SetupIntent as a data attribute -->
<button id="submit-button" data-secret="<?= $client_secret ?>">
Set up SEPA Direct Debit
</button>
<!-- Display mandate acceptance text. -->
<div id="mandate-acceptance">
By providing your payment information and confirming this payment, you
authorise (A) and Stripe, our payment service provider, to
send instructions to your bank to debit your account and (B) your bank to
debit your account in accordance with those instructions. As part of your
rights, you are entitled to a refund from your bank under the terms and
conditions of your agreement with your bank. A refund must be claimed
within 8 weeks starting from the date on which your account was debited.
Your rights are explained in a statement that you can obtain from your
bank. You agree to receive notifications for future debits up to 2 days
before they occur.
</div>
<!-- Used to display form errors. -->
<div id="error-message" role="alert"></div>
</form>
</div>
<script>
var stripe = Stripe('<?= PUBLIC_KEY ?>');
var elements = stripe.elements();
// Custom styling can be passed to options when creating an Element.
var style = {
base: {
color: '#32325d',
fontSize: '16px',
'::placeholder': {
color: '#aab7c4'
},
':-webkit-autofill': {
color: '#32325d',
},
},
invalid: {
color: '#fa755a',
iconColor: '#fa755a',
':-webkit-autofill': {
color: '#fa755a',
},
},
};
var options = {
supportedCountries: ['SEPA'],
// Elements can use a placeholder as an example IBAN that reflects
// the IBAN format of your customer's country. If you know your
// customer's country, we recommend that you pass it to the Element as the
// placeholderCountry.
placeholderCountry: 'IT',
};
// Create an instance of the IBAN Element
var iban = elements.create('iban', options);
// Add an instance of the IBAN Element into the `iban-element` <div>
iban.mount('#iban-element');
var form = document.getElementById('payment-form');
var accountholderName = document.getElementById('accountholder-name');
var email = document.getElementById('email');
var submitButton = document.getElementById('submit-button');
var clientSecret = submitButton.dataset.secret;
form.addEventListener('submit', function (event) {
event.preventDefault();
console.log('Caricamento in corso...');
var response = stripe.confirmSepaDebitSetup(
clientSecret,
{
payment_method: {
sepa_debit: iban,
billing_details: {
name: accountholderName.value,
email: email.value,
},
},
}
);
response.then(result => {
console.log(result);
if (!result.setupIntent || result.setupIntent.status !== "succeeded") {
alert('Errore, qualcosa è andato storto')
}
var paymentMethod = result.setupIntent.payment_method;
const formData = new FormData();
formData.append('pm', paymentMethod);
fetch('sepa.php', {
method: 'POST',
body: formData,
})
.then(response => console.log(response))
.catch(error => console.error('Error:', error));
})
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment