Skip to content
This repository has been archived by the owner on Oct 15, 2020. It is now read-only.

Commit

Permalink
First commit of first 4 steps
Browse files Browse the repository at this point in the history
  • Loading branch information
Saikat Chakrabarti committed Nov 11, 2012
0 parents commit c289cb2
Show file tree
Hide file tree
Showing 22 changed files with 367 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
*.DS_Store
3 changes: 3 additions & 0 deletions .gitmodules
@@ -0,0 +1,3 @@
[submodule "stripe"]
path = stripe
url = https://github.com/stripe/stripe-php
5 changes: 5 additions & 0 deletions README.md
@@ -0,0 +1,5 @@
# Wilde Things - A study in PHP

Wilde Things is a website on which you can buy quotes Oscar Wilde. This is a PHP demo that demonstrates integrating Stripe, originally done for a General Assembly livestream tutorial on November 14, 2012.

The demo is 7 steps and goes from setting up a simple site for accepting one-time payments to a subscription-based service complete with webhook support.
Empty file added Step1/README.md
Empty file.
52 changes: 52 additions & 0 deletions Step1/index.php
@@ -0,0 +1,52 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>Wilde Things</title>
</head>
<body>
<?php
require_once('../stripe/lib/Stripe.php');
$stripe = array(
'secret_key' => 'sk_test_COES306BuVznThOZkMr8ibAa',
'publishable_key' => 'pk_test_fRASYtf5gVfnrjeNS6Z0aqLW'
);
Stripe::setApiKey($stripe['secret_key']);

if ($_POST) {
$charge = Stripe_Charge::create(array(
'card' => $_POST['stripeToken'],
'amount' => 53500,
'currency' => 'usd'
));

$wildeQuotes = array(
"A little sincerity is a dangerous thing, and a great deal of it is absolutely fatal.",
"Always forgive your enemies; nothing annoys them so much.",
"America is the only country that went from barbarism to decadence without civilization in between.",
"I think that God in creating Man somewhat overestimated his ability.",
"I am not young enough to know everything.",
"Fashion is a form of ugliness so intolerable that we have to alter it every six months.",
"Most modern calendars mar the sweet simplicity of our lives by reminding us that each day that passes is the anniversary of some perfectly uninteresting event.",
"Scandal is gossip made tedious by morality."
);

echo "<h1>Here's your quote!</h1>";
echo "<h2>".$wildeQuotes[array_rand($wildeQuotes)]."</h2>";
}
else
{ ?>
<h2>Wilde Things</h2>
<h3>Purchase a quote by Oscar Wilde today! Only $535! Limited supply and going fast, buy now!!</h3>

<form action="index.php" method="post">
<script src="https://button.stripe.com/v1/button.js" class="stripe-button"
data-key="<?php echo $stripe['publishable_key']; ?>"
data-amount=53500
data-description="One Wilde quote"
data-label="Buy"></script>
</form>
<?php
}
?>
</body>
</html>
66 changes: 66 additions & 0 deletions Step2/index.php
@@ -0,0 +1,66 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>Wilde Things</title>
</head>
<body>
<?php
require_once('../stripe/lib/Stripe.php');
$stripe = array(
'secret_key' => 'sk_test_COES306BuVznThOZkMr8ibAa',
'publishable_key' => 'pk_test_fRASYtf5gVfnrjeNS6Z0aqLW'
);
Stripe::setApiKey($stripe['secret_key']);

if ($_POST) {
$error = NULL;
try {
if (!isset($_POST['stripeToken']))
throw new Exception("The Stripe Token was not generated correctly");
$charge = Stripe_Charge::create(array(
'card' => $_POST['stripeToken'],
'amount' => 53500,
'currency' => 'usd'
));
}
catch (Exception $e) {
$error = $e->getMessage();
}

if ($error == NULL) {
$wildeQuotes = array(
"A little sincerity is a dangerous thing, and a great deal of it is absolutely fatal.",
"Always forgive your enemies; nothing annoys them so much.",
"America is the only country that went from barbarism to decadence without civilization in between.",
"I think that God in creating Man somewhat overestimated his ability.",
"I am not young enough to know everything.",
"Fashion is a form of ugliness so intolerable that we have to alter it every six months.",
"Most modern calendars mar the sweet simplicity of our lives by reminding us that each day that passes is the anniversary of some perfectly uninteresting event.",
"Scandal is gossip made tedious by morality."
);

echo "<h1>Here's your quote!</h1>";
echo "<h2>".$wildeQuotes[array_rand($wildeQuotes)]."</h2>";
}
else {
echo "<div class=\"error\">".$error."</div>";
}
}

if (!$_POST || $error)
{ ?>
<h2>Wilde Things</h2>
<h3>Purchase a quote by Oscar Wilde today! Only $535! Limited supply and going fast, buy now!!</h3>

<form action="index.php" method="post">
<script src="https://button.stripe.com/v1/button.js" class="stripe-button"
data-key="<?php echo $stripe['publishable_key']; ?>"
data-amount=53500
data-description="One Wilde quote"
data-label="Buy"></script>
</form>
<?php
}
?>
</body>
</html>
40 changes: 40 additions & 0 deletions Step3/charge.php
@@ -0,0 +1,40 @@
<?php
require_once('./header.php');

if ($_POST) {
$error = NULL;
try {
if (!isset($_POST['stripeToken']))
throw new Exception("The Stripe Token was not generated correctly");
$charge = Stripe_Charge::create(array(
'card' => $_POST['stripeToken'],
'amount' => 53500,
'currency' => 'usd'
));
}
catch (Exception $e) {
$error = $e->getMessage();
}

if ($error == NULL) {
$wildeQuotes = array(
"A little sincerity is a dangerous thing, and a great deal of it is absolutely fatal.",
"Always forgive your enemies; nothing annoys them so much.",
"America is the only country that went from barbarism to decadence without civilization in between.",
"I think that God in creating Man somewhat overestimated his ability.",
"I am not young enough to know everything.",
"Fashion is a form of ugliness so intolerable that we have to alter it every six months.",
"Most modern calendars mar the sweet simplicity of our lives by reminding us that each day that passes is the anniversary of some perfectly uninteresting event.",
"Scandal is gossip made tedious by morality."
);

echo "<h1>Here's your quote!</h1>";
echo "<h2>".$wildeQuotes[array_rand($wildeQuotes)]."</h2>";
}
else {
echo "<div class=\"error\">".$error."</div>";
require_once('./payment_form.php');
}
}
require_once('./footer.php');
?>
8 changes: 8 additions & 0 deletions Step3/config.php
@@ -0,0 +1,8 @@
<?php
require_once('../stripe/lib/Stripe.php');
$stripe = array(
'secret_key' => 'sk_test_COES306BuVznThOZkMr8ibAa',
'publishable_key' => 'pk_test_fRASYtf5gVfnrjeNS6Z0aqLW'
);
Stripe::setApiKey($stripe['secret_key']);
?>
2 changes: 2 additions & 0 deletions Step3/footer.php
@@ -0,0 +1,2 @@
</body>
</html>
8 changes: 8 additions & 0 deletions Step3/header.php
@@ -0,0 +1,8 @@
<?php require_once('./config.php'); ?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Wilde Things</title>
</head>
<body>
<h2>Wilde Things</h2>
5 changes: 5 additions & 0 deletions Step3/index.php
@@ -0,0 +1,5 @@
<?php
require_once('./header.php');
require_once('./payment_form.php');
require_once('./footer.php');
?>
8 changes: 8 additions & 0 deletions Step3/payment_form.php
@@ -0,0 +1,8 @@
<form action="charge.php" method="post">
<h3>Purchase a quote by Oscar Wilde today! Only $535! Limited supply and going fast, buy now!!</h3>
<script src="https://button.stripe.com/v1/button.js" class="stripe-button"
data-key="<?php echo $stripe['publishable_key']; ?>"
data-amount=53500
data-description="One Wilde quote"
data-label="Buy"></script>
</form>
65 changes: 65 additions & 0 deletions Step4/charge.php
@@ -0,0 +1,65 @@
<?php
require_once('./header.php');
require_once('./db.php');

if ($_POST) {
$error = NULL;
try {
if (isset($_POST['customer_id'])) {
$charge = Stripe_Charge::create(array(
'customer' => $_POST['customer_id'],
'amount' => 53500,
'currency' => 'usd',
'description' => 'Single quote purchase after login'));
}
else if (isset($_POST['stripeToken'])) {
// Simple uniqueness check on the email address
$existing_customer = get_customer($_POST['email']);

if ($existing_customer) {
throw new Exception("That e-mail address already exists");
}

$customer = Stripe_Customer::create(array(
'card' => $_POST['stripeToken'],
'email' => $_POST['email']
));

create_customer($_POST['email'], $_POST['password'], $customer->id);

$charge = Stripe_Charge::create(array(
'customer' => $customer->id,
'amount' => 53500,
'currency' => 'usd',
'description' => 'Single quote purchase'));
}
else {
throw new Exception("The Stripe Token or customer was not generated correctly");
}
}
catch (Exception $e) {
$error = $e->getMessage();
}

if ($error == NULL) {
$wildeQuotes = array(
"A little sincerity is a dangerous thing, and a great deal of it is absolutely fatal.",
"Always forgive your enemies; nothing annoys them so much.",
"America is the only country that went from barbarism to decadence without civilization in between.",
"I think that God in creating Man somewhat overestimated his ability.",
"I am not young enough to know everything.",
"Fashion is a form of ugliness so intolerable that we have to alter it every six months.",
"Most modern calendars mar the sweet simplicity of our lives by reminding us that each day that passes is the anniversary of some perfectly uninteresting event.",
"Scandal is gossip made tedious by morality."
);

echo "<h1>Here's your quote!</h1>";
echo "<h2>".$wildeQuotes[array_rand($wildeQuotes)]."</h2>";
}
else {
echo "<div class=\"error\">".$error."</div>";
require_once('./payment_form.php');
}
}
require_once('./footer.php');
?>
8 changes: 8 additions & 0 deletions Step4/config.php
@@ -0,0 +1,8 @@
<?php
require_once('../stripe/lib/Stripe.php');
$stripe = array(
'secret_key' => 'sk_test_COES306BuVznThOZkMr8ibAa',
'publishable_key' => 'pk_test_fRASYtf5gVfnrjeNS6Z0aqLW'
);
Stripe::setApiKey($stripe['secret_key']);
?>
39 changes: 39 additions & 0 deletions Step4/db.php
@@ -0,0 +1,39 @@
<?php
// We are using a file instead of a DB for convenience -- in a real application, we'd be using a database and not delimiting our entries with strings like this (which is pretty fragile -- for example, e-mails can actually contain semicolons and commas inside quotation marks, which would break our little scheme here).

function get_customer($email_address)
{
$customers = get_all_customers();
foreach($customers as &$customer) {
if ($customer['email'] == $email_address)
return $customer;
}
return NULL;
}

function get_all_customers()
{
$customers = array();
$db_contents = file_get_contents("/public/customers.txt");
$existing_customers = explode(';', $db_contents);
foreach($existing_customers as &$customer) {
$customer_info = explode(',', $customer);
array_push($customers, array(
'email' => $customer_info[0],
'password' => $customer_info[1],
'stripe_id' => $customer_info[2]
));
}
return $customers;
}

function create_customer($email_address, $password, $stripe_id)
{
// Encode password with Blowfish algorithm
$db_entry = $email_address.','.crypt($password).','.$stripe_id.';';

// Make sure /public is writeable by the Apache process and readable (If this is too hard, feel free to just make a /public directory temporarily and run "sudo chmod 777 /public" for the sake of this demo)
$db = fopen('/public/customers.txt', 'a');
fwrite($db, $db_entry);
}
?>
2 changes: 2 additions & 0 deletions Step4/footer.php
@@ -0,0 +1,2 @@
</body>
</html>
8 changes: 8 additions & 0 deletions Step4/header.php
@@ -0,0 +1,8 @@
<?php require_once('./config.php'); ?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Wilde Things</title>
</head>
<body>
<h2>Wilde Things</h2>
5 changes: 5 additions & 0 deletions Step4/index.php
@@ -0,0 +1,5 @@
<?php
require_once('./header.php');
require_once('./payment_form.php');
require_once('./footer.php');
?>
23 changes: 23 additions & 0 deletions Step4/login.php
@@ -0,0 +1,23 @@
<?php
require_once('./header.php');
require_once('./db.php');

if ($_POST) {
$existing_customer = get_customer($_POST['email']);

if ($existing_customer && crypt($_POST['password'], $existing_customer['password']) == $existing_customer['password']) {
$stripe_customer = Stripe_Customer::retrieve($existing_customer['stripe_id']);
?>

<form action="charge.php" method="POST">
Would you like to pay with your card ending in <?php echo $stripe_customer->active_card['last4'] ?>?
<input type="hidden" name="customer_id" value="<?php echo $stripe_customer->id ?>" />
<input type="submit" name="submit" value="Yes!" />
</form>
<?php
}
else {
echo "Your email address or password is incorrect.";
require_once('./login_form.php');
}
}
6 changes: 6 additions & 0 deletions Step4/login_form.php
@@ -0,0 +1,6 @@
<form action="login.php" method="post">
<h3>Log in to make a purchase with the card you have on file!</h3>
<input type="text" name="email" placeholder="E-mail address" />
<input type="password" name="password" placeholder="Password" />
<input type="submit" name="login" value="Login" />
</form>

1 comment on commit c289cb2

@alzoub
Copy link

@alzoub alzoub commented on c289cb2 Jan 13, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Downloaded and installed Composer, installed Stripe library with it, I've copied Step2, entered my Stripe keys, and accessed the index.php file.

Got blank page. Looking at the source, it turned out that anything after didn't display.
I'm not a professional developer, but usually, I manage to make other's script work and write my own working code.
Here I do need help. The files were installed by Composer above public_html (vendor, composer.json, composer.lock, composer.phar)

Please sign in to comment.