Last active
May 19, 2017 21:55
-
-
Save elhardoum/8cffce0c62f7c554ca0d57247e78fff2 to your computer and use it in GitHub Desktop.
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
<?php | |
// prevent direct access, only index.php can include this file | |
defined ( 'STRIPE_SECRET_KEY' ) || exit ( 'Direct access not allowed' . PHP_EOL ); | |
use \Stripe\Stripe; | |
use \Stripe\Customer; | |
use \Stripe\Charge; | |
Stripe::setApiKey(STRIPE_SECRET_KEY); | |
$token = isset($_POST['stripeToken']) ? $_POST['stripeToken'] : null; | |
unset($_POST); | |
if ( !$token ) { | |
return; // just in case | |
} | |
try { | |
$customer = Customer::create(array( | |
'email' => USER_EMAIL, | |
'source' => $token | |
)); | |
} catch ( \Exception $e ) { | |
$error []= "Error registering your payment request. Please try again or later!"; | |
return; | |
} | |
try { | |
$charge = Charge::create(array( | |
'customer' => $customer->id, | |
'amount' => STRIPE_PRICE, | |
'currency' => CURRENCY_CODE | |
)); | |
} catch ( Exception $e ) { | |
error_log( sprintf('ERROR: Stripe failed for user %s @%d (ip:%s)', USER_EMAIL, time(), $_SERVER['REMOTE_ADDR']) ); | |
error_log( print_r( $e, true ) ); | |
$error []= "Error verifying your payment request. Please try again or later if you are sure no payment has been made yet"; | |
return; | |
} | |
// Now we're dealing with a verified payment! Let's upgrade the user! | |
// a success notice | |
$success []= "Your payment has been verified successfully! We're upgrading your membership."; | |
if ( function_exists('upgrade_user') ) { | |
$upgraded = upgrade_user(USER_EMAIL); | |
if ( !$upgraded ) { | |
$error []= "We could not upgrade your membership. Please sit tight as we do it manually"; | |
error_log( sprintf('Upgrade user %s failed, do it manually ASAP!!', USER_EMAIL) ); | |
} | |
} |
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
<?php | |
error_reporting(E_ALL); | |
ini_set('display_errors', 1); | |
require __DIR__ . '/vendor/autoload.php'; | |
// API public key | |
define ( 'STRIPE_PUBLIC_KEY', 'pk_test_6pRNASCoBOKtIshFeQd4XMUh' ); | |
// API secret key | |
define ( 'STRIPE_SECRET_KEY', 'sk_test_BQokikJOvBiI2HlWgH4olfQ2' ); | |
// stripe amount | |
define ( 'STRIPE_PRICE', 100 ); // that's $1 | |
// amount currency | |
define ( 'CURRENCY_CODE', 'USD' ); | |
// current user email | |
define ( 'USER_EMAIL', '[email protected]' ); | |
$success = $error = array(); | |
// if the form was submitted (i.e payment request sent) | |
if ( isset($_GET['charge']) && $_POST ) { | |
require __DIR__ . '/charge.php'; | |
} | |
// start output | |
?> | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>My Amazing Membership Site</title> | |
</head> | |
<body style="background: #ececec; display: table; margin: 0 auto; padding-top: 5vw"> | |
<form action="index.php?charge=1" method="POST"> | |
<?php if ( $success ) : ?> | |
<ul style="background: #d3f1d3; display: block; padding: 1em; border: 1px solid #ddd; border-radius: 3px;"> | |
<li><?php echo implode ( '</li><li>', $success ); ?></li> | |
</ul> | |
<?php endif; ?> | |
<?php if ( $error ) : ?> | |
<ul style="background: #fec8b7; display: block; padding: 1em; border: 1px solid #ddd; border-radius: 3px;"> | |
<li><?php echo implode ( '</li><li>', $error ); ?></li> | |
</ul> | |
<?php endif; ?> | |
<h3>Pay membership with credit/debit card</h3> | |
<p>This is a one-time payment. You will be prompted to enter your card details securly.</p> | |
<script | |
src="https://checkout.stripe.com/checkout.js" class="stripe-button" | |
data-key="<?php echo STRIPE_PUBLIC_KEY; ?>" | |
data-email="<?php echo USER_EMAIL; ?>" | |
data-amount="<?php echo STRIPE_PRICE; ?>" | |
data-name="My Amazing Site" | |
data-description="Premium Membership / For Life!" | |
data-image="http://lorempixel.com/150/100/cats/" | |
data-currency="<?php echo CURRENCY_CODE; ?>" | |
data-locale="auto"> | |
</script> | |
</form> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment