Skip to content

Instantly share code, notes, and snippets.

@pipethedev
Last active December 16, 2019 21:11
Show Gist options
  • Save pipethedev/5191fc385089f3eaf8ced7d7d4da5e37 to your computer and use it in GitHub Desktop.
Save pipethedev/5191fc385089f3eaf8ced7d7d4da5e37 to your computer and use it in GitHub Desktop.
<?php
//requires
//Stripe PHP library
require_once __DIR__ .'/vendor/autoload.php';
//Database Connectivity
require_once __DIR__. '/db.php';
//Brad Traversy Pdo library
require_once __DIR__. '/pdo_db.php';
require_once __DIR__. '/customer.php';
require_once __DIR__. '/transaction.php';
//PHPMailer for reciepts
require_once __DIR__. "/mailer/PHPMailer.php";
require_once __DIR__. "/mailer/SMTP.php";
require_once __DIR__."/mailer/Exception.php";
\Stripe\Stripe::setApiKey('_____STRIPE API KEY____');
//Sanitization
$POST = filter_var_array($_POST, FILTER_SANITIZE_STRING);
$fullname = trim($POST['name']);
$email = trim($POST['email']);
$phone = trim($POST['phone']);
$title = "Ruby On Rails Package";
$amount = "5000" //this is in cents which is equivalent to $50
//collecting data as an array
$customer = \Stripe\Customer::create(array(
"email" => $email,
"source" => $token
));
//charge customer
$charge = \Stripe\Charge::create(array(
"amount" =>$amount,
"currency" => "USD", //UK
"description" =>$title,
"customer" => $customer->id,
));
//customer data which will go to data base
$customerData = [
'id' => $charge->customer,
'name' =>$name,
'phone' =>$phone,
'product' =>$title,
'email' => $email,
];
//instantiate customer
$customer = new Customer();
//adding customer to database
$customer->addCustomer($customerData);
//transaction data which will go to the database and also relate stripe history
$transactionData = [
'id' => $charge->id,
'name' =>$name,
'customer_id' =>$charge->customer,
'product' =>$charge->description,
'amount' => $charge->amount,
'currency' => $charge->currency,
'status' => $charge->status, //check if transaction was failed or successfull
];
//instantiate transaction
$transaction = new Transaction();
//adding transaction to database
$transaction->addTransaction($transactionData);
//send reciepts if successfull transaction
$mail = new PHPMailer\PHPMailer\PHPMailer();
$mail->IsSMTP(); // enable SMTP
$mail->FromName = "My Company";
//$mail->SMTPDebug = 2; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail
$mail->Host = "smtp.gmail.com";
$mail->Port = 465; // or 587
$mail->IsHTML(true);
$mail->Username = "mymail@gmail.com";
$mail->Password = "Password";
$mail->SetFrom("mymail@gmail.com");
$mail->Subject = "Reciept From My Company";
$mail->Body = "
<div id='first' style='display:block;'>
<div style='background:#4834d4; color:white; height:5rem; width:100%; ' >
<h2 style='padding:20px 20px 20px; '>Purchase Receipt <p>from My Company</p></h2>
</div>
<div class='container'>
<h1 style='text-align: center; font-weight: bold; font-family: 'Calibri', sans-serif; '>Receipt</h1>
<h2 style='text-align: center; font-family: 'Quicksand', sans-serif; font-weight: lighter;' >Thanks for the order</h2>
<div class='container-card' style='width: 90%; height: auto; -webkit-box-shadow: -1px 3px 23px 0px rgba(0,0,0,0.2);
-moz-box-shadow: -1px 3px 23px 0px rgba(0,0,0,0.2);
box-shadow: -1px 3px 23px 0px rgba(0,0,0,0.2); padding-right:15px;padding-left:15px;margin-right:auto;margin-left:auto; font-family: 'Nunito', sans-serif; border-radius: 16px; ' >
<div class='time-id'>
<br>
<p style='>Purchased: ".date("Y/m/d")." <span style='float: right;'><b>ID: ".$charge->id."</b></span> </p>
</div>
<h5 style='text-transform: uppercase;'>Payment Summary</h5>
<hr style='border:0.5px solid #bdc3c7; '>
<p><b>".$title."Ticket</b> <span style='float: right;'><b>$".$amount."</b></span></p>
<br>
<hr style='border:0.5px solid #bdc3c7; '>
<p><b>TOTAL: </b> <span style='float: right;'><b>$".$amount."</b></span></p>
<p>
<div class='footer' style='padding-bottom: 3rem;'>
<br>
<p><b>Need assistance ? Talk to us?</b></p>
<p style='font-size: 14px; '>We will do anything we can make sure you love your experience with us</p>
<p style='font-size: 14px;'>Call us :<b> <a href='tel:+#' style='text-decoration: unset;'>+123 456 7890</a></b></p>
</div>
</div>
</div>
";
$mail->AddAddress($email);
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
header('Location: success.php?tid='.$charge->id.'&product='.$charge->description.'&title='.$title);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment