A typical Pay Function would work like this:
This file contains 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
function pay(){ | |
global $global; | |
if($_GET[test]){ | |
$pkey="pk_test_***********************"; | |
$key="sk_test_***********************"; | |
}else{ | |
$pkey="pk_live_***********************"; | |
$key="sk_live_***********************"; | |
} | |
$out.=" | |
<div class='bb_main_content_title'>Make a Payment:</div> | |
<div class='bb_main_content_paragraph'> | |
Please use this page to make payments with your credit or debit card.<br> | |
This is a secure payment service provided by our payment provider <a href=http://www.stripe.com target=_blank>Stripe</a>. We do not store any of your card information on our servers/system - all data is stored and processed securely by <a href=http://www.stripe.com target=_blank>Stripe</a>. | |
</div> | |
"; | |
if($global[fields][name] && $_POST['stripeToken'] && is_numeric($global[fields][payNUM]) && $global[fields][payNUM]*1>1 && $global[fields][payNUM]*1<3000 && $_POST['stripeEmail']){ | |
require_once($_SERVER[DOCUMENT_ROOT].'/engine/3rdparty/stripe/Stripe.php'); | |
Stripe::setApiKey($key); | |
// Get the credit card details submitted by the form | |
$token = $_POST['stripeToken']; | |
$amt=$global[fields][payNUM]*1; | |
try { | |
// | |
$customer = Stripe_Customer::create(array( | |
'email' => $_POST['stripeEmail'], | |
'card' => $token | |
)); | |
$charge = Stripe_Charge::create(array( | |
'customer' => $customer->id, | |
'amount' => $amt*100, | |
'currency' => 'GBP', | |
'description' => $global[fields][name] | |
)); | |
$out.=(" | |
<div class='bb_main_content_affirm'>Thanks, payment of £".display_currency($global[fields][payNUM])." has been made.</div></div> | |
"); | |
} catch(Stripe_CardError $e) { | |
$body = $e->getJsonBody(); | |
$err = $body['error']; | |
$out.=(" | |
<div class='bb_main_content_alert'>Payment failed: ".$err['message']." (".$err['code'].") </div> | |
"); | |
} | |
}elseif($global[fields][name] && is_numeric($global[fields][payNUM]) && $global[fields][payNUM]*1>1 && $global[fields][payNUM]*1<3000){ | |
$out.=" | |
<form action=\"/?Pay".($_GET[test]?"&test=1":"")."\" method=\"POST\"> | |
<b>Please click the button below to load the secure payment form:</b> | |
<div style=clear:both;height:15px;></div> | |
<input type=hidden name=global[fields][payNUM] value=\"".$global[fields][payNUM]."\"> | |
<input type=hidden name=global[fields][name] value=\"".$global[fields][name]."\"> | |
<div style=width:300px;float:left;> | |
<script | |
src=\"https://checkout.stripe.com/checkout.js\" class=\"stripe-button\" | |
data-key=\"$pkey\" | |
data-currency=\"GBP\" | |
data-amount=\"".($global[fields][payNUM]*100)."\" | |
data-name=\"Make a Payment\" | |
data-allowRememberMe=false | |
data-description=\"From ".$global[fields][name]." (£".display_currency($global[fields][payNUM]).")\" | |
data-label=\"Load the secure payment form\" | |
data-image=\"/portal/".DATABASE_NAME."/UserFiles/Image/logo.png\"> | |
</script> | |
</div> | |
".($_GET[test]?"<div style=float:left;margin-left:15px;background:yellow;color:black;padding:6px;>Test Mode</div>":"")." | |
<div style=clear:both;height:15px;></div> | |
<b>Important:</b> Please wait for the page to fully re-load once you have entered your card details. You will see a confirmation message once the transaction is complete. | |
</form> | |
"; | |
}else{ | |
$out.=" | |
<script> | |
function frm_validate(){ | |
if(!\$('global[fields][name]').value){ | |
alert('Please enter your name.'); | |
return; | |
} | |
if(isNaN(\$('global[fields][payNUM]').value) || !\$('global[fields][payNUM]').value || \$('global[fields][payNUM]').value*1<=1 || \$('global[fields][payNUM]').value*1>=3000){ | |
alert('Please enter an amount greater than 1 and less than 3000.'); | |
return; | |
}else{ | |
\$('psf').submit(); | |
} | |
} | |
</script> | |
<form id=psf action=/?Pay".($_GET[test]?"&test=1":"")." method=post> | |
<div style=margin-bottom:15px;><b>Please enter your name and the amount you want to pay:</b></div> | |
<div style=\"margin-bottom:10px;\"> | |
<div style=float:left;margin-right:10px;margin-top:6px;> | |
Your name: | |
</div> | |
<div style=float:left;width:300px;> | |
<!--:formfield:name:--> | |
</div> | |
<div style=clear:both;></div> | |
</div> | |
<div style=float:left;font-size:24pt;margin-right:10px;margin-top:6px;>£</div> | |
<!--:formfield:payNUM||0.00| onclick=this.value=''; style=text-align:right;width:100px;float:left;margin-right:20px; :--> | |
<a href=# onclick=\"frm_validate();return false;\" style=float:left;margin-top:4px; class='bb_main_content_styled_button_blue' >Next ></a> | |
".($_GET[test]?"<div style=float:left;margin-left:15px;background:yellow;color:black;padding:6px;>Test Mode</div>":"")." | |
<div style=clear:both;></div> | |
</form> | |
"; | |
} | |
return($out); | |
} |