Created
March 6, 2016 13:35
-
-
Save alovak/440d08fb35736634592c to your computer and use it in GitHub Desktop.
plans-btfl-js.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
################################## | |
config.php | |
<?php | |
$plans = array( | |
'basic' => array('cents' => 1000, 'price' => '10.00'), | |
'pro' => array('cents' => 2000, 'price' => '20.00'), | |
); | |
?> | |
############################ | |
plans.php | |
<?php | |
inlcude('config.php'); | |
// render list using $plans something like this | |
?> | |
<ul> | |
<li class="plan" data-name="basic" data-amount="<?php echo $plans['basic'].cents ?>">Basic <?php echo $plans['basic'].price ?> AED</li> | |
<li class="plan" data-name="pro" data-amount="<?php echo $plans['pro'].cents ?>">Basic <?php echo $plans['pro'].price ?> AED</li> | |
</ul> | |
<form id="registrationForm" action="registration.php"> | |
<input type="hidden" name="plan" value="" /> | |
<input type="button" id="register" value="Register" /> | |
</form> | |
<script> | |
var planAmountInCents = 0; | |
$('li.plan').click(function(e) { | |
planAmountInCents = $(this).data('amount'); | |
$('input[name=plan]').val($(this).data('name')); | |
}) | |
$('button#register').click(function(e) { | |
e.preventDefault(); | |
StartCheckout.config({ | |
key: "<?php echo $openApiKey ?>", | |
complete: function(params) { | |
frmCheckout = $("#registrationForm"); | |
frmCheckout.append("<input type='hidden' name='payfortToken' value='" + params.token.id + "'>"); | |
frmCheckout.append("<input type='hidden' name='payfortEmail' value='" + params.email + "'>"); | |
frmCheckout.submit(); | |
} | |
}); | |
StartCheckout.open({ | |
amount: planAmountInCents, // = AED 100.00 | |
currency: "AED" | |
}); | |
}) | |
</script> | |
############################### | |
registration.php | |
<?php | |
require 'config.php'; | |
$token = $_POST['startToken']; | |
$email = $_POST['startEmail']; //or you can use $user.email if you have | |
$plan = $_POST['plan']; | |
try { | |
$charge = Start_Charge::create(array( | |
"amount" => $plans[$plan].cents, | |
"currency" => $currency, | |
"card" => $token, | |
"email" => $email, | |
"ip" => $_SERVER["REMOTE_ADDR"], | |
"description" => "Charge for " . $plan . " plan" | |
)); | |
echo "<h1>You were registered</h1>"; | |
} catch (Start_Error $e) { | |
$error_code = $e->getErrorCode(); | |
$error_message = $e->getMessage(); | |
/* depending on $error_code we can show different messages */ | |
if ($error_code === "card_declined") { | |
echo "<h1>Sorry, registration failed</h1>"; | |
echo "<h1>Your card was declined. Please, call you bank or use different card</h1>"; | |
} else { | |
echo "<h1>Sorry, registration failed</h1>"; | |
echo "<h1>Charge was not processed</h1>"; | |
echo "<p>".$error_message."</p>"; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment