Skip to content

Instantly share code, notes, and snippets.

@leroyrosales
Created December 14, 2021 16:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leroyrosales/822c0b7af41ee3dd51fa24e4c569a5d2 to your computer and use it in GitHub Desktop.
Save leroyrosales/822c0b7af41ee3dd51fa24e4c569a5d2 to your computer and use it in GitHub Desktop.
<?php
require '../vendor/autoload.php';
// Is this actually an AJAX request
if ( isset( $_SERVER['HTTP_X_REQUESTED_WITH'] ) && ! empty( $_SERVER['HTTP_X_REQUESTED_WITH'] ) && strtolower( $_SERVER['HTTP_X_REQUESTED_WITH'] ) == 'xmlhttprequest' ) {
exit;
}
// Let's us use WP functions
require_once $_SERVER['DOCUMENT_ROOT'] . '/wp-load.php';
// Get secret key from site options
$secret_key = get_field( 'secret_key', 'option' );
\Stripe\Stripe::setApiKey( $secret_key );
// header( 'Content-Type: application/json' );
// Sanitize POST Array
$POST = filter_var_array($_POST, FILTER_SANITIZE_STRING);
$fname = isset($_POST["fname"]) ? sanitize_text_field($_POST["fname"]) : '';
$lname = isset($_POST["lname"]) ? sanitize_text_field($_POST["lname"]) : '';
$email = isset($_POST["email"]) ? sanitize_text_field($_POST["email"]) : '';
// Calculate promo amount
$promo_exists = false;
$amount = 49900; //$499.00
// Get promo from form
$promo_code = isset($_POST["promo"]) ? sanitize_text_field($_POST["promo"]) : '';
$description = "Artificial Intelligence Solutions: An Introductory View from a Human-Machine Perspective";
$site_domain = get_site_url();
// Create Customer In Stripe
$customer = \Stripe\Customer::create(array(
"email" => $email,
"name" => $fname . ' ' . $lname,
"description" => $description,
"metadata" => [
"first_name" => $fname,
"last_name" => $lname,
],
));
$checkout_session = \Stripe\Checkout\Session::create(array(
'line_items' => array(
array(
'amount' => $amount,
'quantity' => 1,
'currency' => 'usd',
'name' => $description,
),
),
'mode' => 'payment',
"metadata" => [
"first_name" => $fname,
"last_name" => $lname,
],
'allow_promotion_codes' => true,
'success_url' => $site_domain . '/thank-you',
'cancel_url' => $site_domain . '/register',
));
header( 'HTTP/1.1 303 See Other' );
header( 'Location: ' . $checkout_session->url );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment