Reputation: 1868
I'm creating a subscription site using Stripe and want to set it up with 3 billing options:
Want to display these in 3 side-by-side divs
MY QUESTION is... How can I set the forms up to use 1 action script?
I assume by using if / else statements, but I'm a little rusty on my coding.
If the amount equals the amount of 1 of the 3 plans I would like that to determine the plan that gets sent to Stripe.
Here are my 3 Forms:
<form action="/premium/premium_charge.php" method="POST">
<script
src="https://checkout.stripe.com/checkout.js" class="stripe-button"
data-key="<?php echo $stripe['publishable']; ?>"
data-email="<?php echo escape($data->email); ?>"
data-image="/images/logo-stripe.png"
data-name="My Site"
data-description = "1 Month Membership"
data-amount="1299"
data-currency= "usd"
data-label="1 MONTH">
</script>
</div>
</form>
<div align="center">
<form action="/premium/premium_charge.php" method="POST">
<script
src="https://checkout.stripe.com/checkout.js" class="stripe-button"
data-key="<?php echo $stripe['publishable']; ?>"
data-email="<?php echo escape($data->email); ?>"
data-image="/images/logo-stripe.png"
data-name="My Site"
data-description = "6 Month Membership"
data-amount="7000"
data-currency= "usd"
data-label="6 MONTHS">
</script>
</div>
</form>
<div align="right">
<form action="/premium/premium_charge.php" method="POST">
<script
src="https://checkout.stripe.com/checkout.js" class="stripe-button"
data-key="<?php echo $stripe['publishable']; ?>"
data-email="<?php echo escape($data->email); ?>"
data-image="/images/logo-stripe.png"
data-name="My Site"
data-description = "Annual Membership"
data-amount="12000"
data-currency= "usd"
data-label="YEAR">
</script>
</div>
</form>
Here is my Action Script with the if statements: As you can see I'm trying to pull over the data-amounts from the above form to use in my IF Statements, and pass on to my $customer plan, but pretty sure I butchered this up pretty bad.. :0{
$x = ("data-amount")==1299;
$y = ("data-amount")==7000;
$z = ("data-amount")==12000;
if($x == true){
echo ('001');
}
elseif($y == true){
echo ('002');
}
elseif($z == true){
echo ('003');
}
require_once '../core/init.php';
if(isset($_POST['stripeToken'])) {
$token = $_POST['stripeToken'];
try{
$customer = \Stripe\Customer::create(array(
'email' => $data->email,
"source" => $token, // obtained with Stripe.js
'plan' => $x->$y->$z
));
If someone could guide me I would appreciate it.
Upvotes: 2
Views: 760
Reputation: 17503
The amount and currency that are passed to the Checkout form (via the data-amount
and data-currency
parameters) are used for display purposes only. They are not sent to your backend PHP code.
This is by design: unless the amount is explicitly chosen by the customer (e.g. if you're accepting donations), you should not trust an amount that is sent by the customer's browser, as it is trivial to change it.
What you should do instead is send an identifier that can be used in your server-side code to determine the correct amount to be charged. Since you're using subscriptions, you can directly use the subscription plan's ID.
Let's say your three plans' IDs are 1month
, 6months
and 12months
respectively. You could include a hidden parameter with a different value for each of your offers:
<form action="/premium/premium_charge.php" method="POST">
<input type="hidden" name="plan" value="1month" />
<script
src="https://checkout.stripe.com/checkout.js" class="stripe-button"
data-key="<?php echo $stripe['publishable']; ?>"
data-email="<?php echo escape($data->email); ?>"
data-image="/images/logo-stripe.png"
data-name="My Site"
data-description = "1 Month Membership"
data-amount="1299"
data-currency= "usd"
data-label="1 MONTH">
</script>
</div>
</form>
You'd repeat this for the other 2 forms, changing the value of the plan
parameter to 6months
and 12months
respectively.
Then, in your server-side code, you can retrieve the value of the plan
parameter along with the stripeToken
and stripeEmail
parameters sent by Checkout, and use those to create the customer with the correct subscription:
$customer = \Stripe\Customer::create(array(
'source' => $_POST['stripeToken'],
'email' => $_POST['stripeEmail'],
'plan' => $_POST['plan']
));
Upvotes: 2