File: //home/awaldron/_domains/pennhillscdc.org/charge.php
<?php
// pk_live_CRAKkT6PtnPl7KJxFmxTb1v700LRYEK7cv
// sk_live_c3i18JqclWrUrGRanHaqH0Zg00GgfLWz1u
// pk_test_uAnC1YeCudsoqqGVMmbrrwMx00kwe7jCEF
// sk_test_hg6NW1r7Sv2LLpZ5WvsW8XT7000mfzaS3d
error_reporting( E_ALL );
//***** SET UP OUR VARIABLES ********************************************************
$pricePerBook = 100;
$shippingCostPerBook = 17;
$intlShippingPerBook = 45;
$taxPct = 0.06;
function trim_value(&$value) {
$value = trim($value); // this removes whitespace and related characters from the beginning and end of the string
}
array_filter($_POST, 'trim_value');
$postfilter = // set up the filters to be used with the trimmed post array
array(
'occRazQty' => array('filter' => FILTER_SANITIZE_STRING), // removes tags. formatting code is encoded -- add nl2br() when displaying
'blaHolQty' => array('filter' => FILTER_SANITIZE_STRING), // removes tags. formatting code is encoded -- add nl2br() when displaying
'name' => array('filter' => FILTER_SANITIZE_STRING), // removes tags. formatting code is encoded -- add nl2br() when displaying
'address' => array('filter' => FILTER_SANITIZE_STRING), // removes tags. formatting code is encoded -- add nl2br() when displaying
'city' => array('filter' => FILTER_SANITIZE_STRING), // removes tags. formatting code is encoded -- add nl2br() when displaying
'state' => array('filter' => FILTER_SANITIZE_STRING), // we are using this in the url
'country' => array('filter' => FILTER_SANITIZE_STRING), // we are using this in the url
'zip' => array('filter' => FILTER_SANITIZE_STRING), // we are using this in the url
'email' => array('filter' => FILTER_SANITIZE_EMAIL) // we are using this in the url
);
$post = filter_var_array($_POST, $postfilter); // must be referenced via a variable which is now an array that takes the place of $_POST[]
$post['order_id'] = time();
//***** SET UP THE STRIPE CHARGE ********************************************************
require 'stripe/init.php';
$token = $_POST['stripeToken'];
\Stripe\Stripe::setApiKey('sk_test_hg6NW1r7Sv2LLpZ5WvsW8XT7000mfzaS3d');
$post['qty'] = $post['occRazQty'] + $post['blaHolQty'];
$amount = $post['qty'] * $pricePerBook;
if ($post['state'] == 'Pennsylvania') {
$amount += $post['qty'] * $pricePerBook * $taxPct;
}
if ($post['country'] == 'United States') {
$amount += $post['qty'] * $shippingCostPerBook;
} else {
$amount += $post['qty'] * $intlShippingPerBook;
}
$success = false;
try {
$charge = \Stripe\Charge::create(
array(
'amount' => ceil($amount*100),
'currency' => 'usd',
'source' => $token,
'metadata' => $post
)
);
$success = true;
} catch(Stripe_CardError $e) {
$error = $e->getMessage();
} catch (Stripe_InvalidRequestError $e) {
// Invalid parameters were supplied to Stripe's API
$error = $e->getMessage();
} catch (Stripe_AuthenticationError $e) {
// Authentication with Stripe's API failed
$error = $e->getMessage();
} catch (Stripe_ApiConnectionError $e) {
// Network communication with Stripe failed
$error = $e->getMessage();
} catch (Stripe_Error $e) {
// Display a very generic error to the user, and maybe send
// yourself an email
$error = $e->getMessage();
} catch (Exception $e) {
// Something else happened, completely unrelated to Stripe
$error = $e->getMessage();
}
//***** PRINT OUT THE CONFIRMATION ********************************************************
include('_head.php');
include('_pagehead.php');
$out = '';
if ($success) {
$chargeId = $charge->id;
// DO STUFF
$out .= '<h1 style="margin:60px 0 20px;padding-bottom:20px;">Order Successful</h1>';
$out .= '<p style="font-size:18px;"><b>Your Order ID is '.$post['order_id'].'</b>';
$out .= '<p style="font-size:18px;">$'.number_format($amount,2).' has been charged to your credit card.<br>Please contact us should you have any questions regarding your order.</p>';
$out .= '<p style="font-size:18px;"><a href="/">Back to Home Page</a></p>';
} else {
$out .= '<h1 style="margin:60px 0 20px;padding-bottom:20px;">Order Failed</h1>';
$out .= '<p style="font-size:18px;"><b>There was a problem processing your order.</b></p>';
$out .= '<p style="font-size:18px;">Error: '.$error.'</p>';
$out .= '<p style="font-size:18px;"><a href="/">Go back and try your order again.</a></p>';
}
//echo '<pre>', print_r($charge), '</pre>'."\n";
$out .= '<br/><br/><br/><br/>';
?>
<section id="page-splash">
<div class="container">
<div class="row">
<div class="col-sm-12" style="text-align: center;">
<?php echo $out; ?>
</div>
</div>
</div>
</section>
<?php
include('_footer.php');
//***** SEND OUT SOME EMAILS ********************************************************
if ($success) {
$to = $post['email'];
$subject = 'Order Confirmation - Books by Gary Lyon Otto';
$headers = "From: glotto@comcast.net" . "\r\n";
$headers .= "Reply-To: glotto@comcast.net" . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= 'X-Mailer: PHP/' . phpversion();
$html = '
<h1>Thank you for your order!</h1>
<table>
<tr>
<td valign="top">
<table>
<tr><td valign="top">Order Number: </td><td>'.$post['order_id'].'</td></tr>
<tr><td valign="top">Quantity: </td><td>'.$post['qty'].'</td></tr>
<tr><td valign="top">Order Total: </td><td>$'.number_format($amount,2).'</td></tr>
<tr><td valign="top">Order Date:</td><td>'.date('F j, Y').' </td></tr>
</table>
</td>
<td valign="top">
<b>Shipping Address:</b>
<br>'.$post['name'].'
<br>'.$post['address'].'
<br>'.$post['city'].', '.$post['state'].' '.$post['zip'].'
</td>
</tr>
</table>
<p><b>You Ordered:</b>
<br>( '.$post['occRazQty'].' ) Occam\'s Razor
<br>( '.$post['blaHolQty'].' ) Black Holes: A New Paradigm
</p>
<br><br>
<p>Thank you for your order!
Your order is currently being processed. Shipments occur once per week so please allow up to 10 days for delivery.</p>
<p>If you have any questions, please reply this email and we will answer them as quickly
as possible.</p>
<p>Thanks!<br>Gary Lyon Otto</p>
</body></html>';
mail($to,$subject,$html,$headers,'-fbooks@garylyonotto.com');
mail('gary.l.otto@morganstanley.com, glotto@comcast.net, alan@waldroninteractive.com, tom@hrpictures.com, garylyonotto@gmail.com','NEW ORDER!', $html, $headers,'-fbooks@garylyonotto.com');
}
/*
<html><body>
<table>
<tr><td valign="top"><img src="https://glo.alanwaldron.com/img/book-cover-3.png" width="225" height="349"></td>
<td valign="top"> <br>
</td></tr></table>
Array
(
[qty] => 1
[name] => Alan Waldron
[address] => 342 Iris Dr
[city] => Penn Hills
[country] => United States
[state] => Pennsylvania
[zip] => 15235
[email] => awwebiz@gmail.com
[stripeToken] => tok_1DeYyeFCFywognkzX4h51soo
)
1
Stripe\Charge Object
(
[id] => ch_1DeYzjFCFywognkzkszbSE8X
[object] => charge
[amount] => 11600
[amount_refunded] => 0
[application] =>
[application_fee] =>
[balance_transaction] => txn_1DeYzjFCFywognkz8gFmFo5E
[captured] => 1
[created] => 1544149391
[currency] => usd
[customer] =>
[description] =>
[destination] =>
[dispute] =>
[failure_code] =>
[failure_message] =>
[fraud_details] => Array
(
)
[invoice] =>
[livemode] =>
[metadata] => Stripe\StripeObject Object
(
[qty] => 1
[name] => Alan Waldron
[address] => 342 Iris Dr
[city] => Penn Hills
[state] => Pennsylvania
[country] => United States
[zip] => 15235
[email] => awwebiz@gmail.com
)
[on_behalf_of] =>
[order] =>
[outcome] => Stripe\StripeObject Object
(
[network_status] => approved_by_network
[reason] =>
[risk_level] => normal
[risk_score] => 51
[seller_message] => Payment complete.
[type] => authorized
)
[paid] => 1
[payment_intent] =>
[receipt_email] =>
[receipt_number] =>
[refunded] =>
[refunds] => Stripe\Collection Object
(
[object] => list
[data] => Array
(
)
[has_more] =>
[total_count] => 0
[url] => /v1/charges/ch_1DeYzjFCFywognkzkszbSE8X/refunds
)
[review] =>
[shipping] =>
[source] => Stripe\Card Object
(
[id] => card_1DeYyeFCFywognkz2bRkojgf
[object] => card
[address_city] =>
[address_country] =>
[address_line1] =>
[address_line1_check] =>
[address_line2] =>
[address_state] =>
[address_zip] =>
[address_zip_check] =>
[brand] => Visa
[country] => US
[customer] =>
[cvc_check] =>
[dynamic_last4] =>
[exp_month] => 5
[exp_year] => 2021
[fingerprint] => 5ZpM3Q5N4nyt2AVx
[funding] => unknown
[last4] => 1111
[metadata] => Stripe\StripeObject Object
(
)
[name] =>
[tokenization_method] =>
)
[source_transfer] =>
[statement_descriptor] =>
[status] => succeeded
[transfer_group] =>
)
*/