Showing with 120 additions and 6 deletions.
  1. +31 −2 mu-plugins/jquery.org/stripe.php
  2. +89 −4 themes/jquery/js/main.js
@@ -17,6 +17,28 @@ public static function init() {
) );
}

public static function coupon() {
require_once( 'lib/Stripe.php' );
Stripe::setApiKey( STRIPE_SECRET );

// Verify required data
if ( empty( $_REQUEST[ 'coupon' ] ) ) {
self::fail( 'Please enter a coupon code.');
}

try {
$coupon = Stripe_Coupon::retrieve( $_REQUEST[ 'coupon' ] );
} catch( Exception $e ) {
self::fail( 'Invalid coupon id' );
}

echo json_encode(array(
'percent_off' => $coupon->percent_off,
'amount_off' => $coupon->amount_off
));
exit();
}

public static function charge() {
require_once( 'lib/Stripe.php' );
Stripe::setApiKey( STRIPE_SECRET );
@@ -45,9 +67,14 @@ public static function charge() {
'card' => $token,
'description' => stripcslashes($name)
) );
$charge = $customer->updateSubscription( array(
$subscription = array(
'plan' => $_REQUEST['planId']
) );
);
if ( !empty( $_REQUEST['coupon'] ) ) {
$subscription['coupon'] = $_REQUEST['coupon'];
}
var_dump( $subscription );
$charge = $customer->updateSubscription( $subscription );

// Create or update WordPress user
$user = get_user_by( 'email', $email );
@@ -91,3 +118,5 @@ public static function fail( $message ) {
add_action( 'init', array( 'StripeForm', 'init' ) );
add_action( 'wp_ajax_nopriv_stripe_charge', array( 'StripeForm', 'charge' ) );
add_action( 'wp_ajax_stripe_charge', array( 'StripeForm', 'charge' ) );
add_action( 'wp_ajax_nopriv_stripe_coupon', array( 'StripeForm', 'coupon' ) );
add_action( 'wp_ajax_stripe_coupon', array( 'StripeForm', 'coupon' ) );
@@ -7,7 +7,7 @@ $(function() {
if ( typeof this.select === "function" ) {
this.select();
}
});
});
});


@@ -91,6 +91,89 @@ $(function() {
gifts.not( gift ).slideUp();
});

// Coupon codes
var couponDiscount = 0;
$( "[name='coupon']" ).each(function() {

var input = $( this ),
payButtons = input.closest( "form" ).find( ".pay" );

// Hide input by default, replace with a link to show the input
input.hide();
$( "<a href='#'>" )
.text( "Have a coupon code?" )
.on( "click", function( event ) {
event.preventDefault();
$( this ).hide();
input.show().focus();
})
.insertAfter( input );

// TODO: loading indicator
// Verify the coupon code on blur
input.on( "blur", function() {
var couponId = input.val();

// Disable pay buttons while we're verifying the coupon
payButtons.prop( "disabled", true );

// Verify the coupon
$.ajax({
url: StripeForm.url,
dataType: "json",
data: {
action: "stripe_coupon",
coupon: couponId
}
})
.done(function( coupon ) {

// Adjust payment buttons
payButtons.each(function() {
var amount,
button = $( this );

// Hide quarterly buttons
if ( button.text().match( /Quarterly/ ) ) {
button.hide();
}

// Adjust annual buttons
if ( button.text().match( /Annual/ ) ) {
amount = parseInt( button.data( "amount" ), 10 ) / 100;
if ( coupon.percent_off ) {
couponDiscount = amount * (coupon.percent_off / 100);
} else if ( coupon.amount_off ) {
couponDiscount = coupon.amount_off;
}

amount = amount - couponDiscount;
button.text( "Annual: $" + amount );
}
});
})
.fail(function() {
couponDiscount = 0;

// TODO: notify user
// Show all payment buttons with standard values
payButtons.each(function() {
var button = $( this ).show();

if ( button.text().match( /Annual/ ) ) {
amount = parseInt( button.data( "amount" ), 10 ) / 100;
button.text( "Annual: $" + amount );
}
});
})
.always(function() {

// Re-enable the pay buttons
payButtons.prop( "disabled", false );
});
});
});

function processMembership( data ) {
$.ajax({
url: StripeForm.url,
@@ -113,6 +196,7 @@ $(function() {
lastName = $.trim( form.find( "[name=last-name]" ).val() ),
email = $.trim( form.find( "[name=email]" ).val() ),
address = $.trim( form.find( "[name=address]" ).val() ),
coupon = $.trim( form.find( "[name=coupon]" ).val().toLowerCase() ),
gifts = form.find( "select" ),
errors = form.find( ".errors" ).empty().hide(),
valid = true;
@@ -150,15 +234,16 @@ $(function() {
name: button.data("name"),
description: button.data("description"),
panelLabel: button.data("panel-label"),
amount: button.data("amount"),
amount: parseInt( button.data("amount"), 10 ) - (couponDiscount * 100),
token: function( stripeData ) {
var data = {
token: stripeData.id,
planId: button.data( "plan-id" ),
firstName: firstName,
lastName: lastName,
email: email,
address: address
address: address,
coupon: coupon
};
gifts.each(function() {
data[ this.name ] = this.value;
@@ -231,7 +316,7 @@ $(function() {
var $siteMenu = $("#menu-top").tinyNav(),
$siteNav = $siteMenu.next();

// In order for Chosen to work as we'd like,
// In order for Chosen to work as we'd like,
// we have to insert the placeholder attribute, an empty option, and select it before instantiation
$siteNav.attr("data-placeholder", "Navigate...").prepend("<option></option>").val("").chosen();