Skip to content

Commit

Permalink
Introduce a templating system
Browse files Browse the repository at this point in the history
  • Loading branch information
mgiulio committed Sep 3, 2014
1 parent 1befb43 commit e2dbee6
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 138 deletions.
120 changes: 120 additions & 0 deletions assets/js/striper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
var initStriper = function() {
jQuery(function($) {
var $form = $('form.checkout,form#order_review');

// Add additional information to be passed to Stripe
var stripeMap = {

billing_address_1: 'address_line1',
billing_address_2: 'address_line2',
billing_city: 'address_city',
billing_country: 'address_country',
billing_state: 'address_state',
billing_postcode: 'address_zip',
}
var card_name = '';
$('form.checkout').find('input[id*=billing_],select[id*=billing_]').each(function(idx,el){
var mapped = stripeMap[el.id];
if (mapped)
{
$(el).attr('data-stripe',mapped);

}
if(el.id == 'billing_first_name' || el.id == 'billing_last_name')
{
// If the billing first and last name fields were pre-populated (if the user was logged in)
// the fields will have values
var billingFirstName = $('#billing_first_name').val();
var billingLastName = $('#billing_last_name').val();

// Set the card name from the pre-populated fields
card_name = $('#billing_first_name').val() + ' ' + $('#billing_last_name').val();


// If the first name is changed
$('#billing_first_name').blur(function () {

// update the first name
billingFirstName = $(this).val();

// update the card name with the new first name
card_name = billingFirstName + " " + billingLastName;

// Update the hidden Stripe card name input with the new card name
$('#stripeCardName').attr('value', card_name);
});


// If the last name is changed
$('#billing_last_name').blur(function () {

// update the last name
billingLastName = $('#billing_last_name').val();

// update the card name with the new last name
card_name = billingFirstName + " " + billingLastName;

// Update the hidden Stripe card name input with the new card name
$('#stripeCardName').attr('value', card_name);
});
}
});
if (!$('#stripeCardName').length)
{
$('<input id="stripeCardName" class="input-text" type="hidden" data-stripe="name" value="'+card_name+'"/>').appendTo($form);
}

var stripeResponseHandler = function(status, response) {

if (response.error) {

// Show the errors on the form
$form.find('.payment-errors').text(response.error.message);
// Unblock the form to re-enter data
$form.unblock();

} else {
// Append the Token
$form.append($('<input type="hidden" name="stripeToken" />').val(response.id));

//Re-Submit
$form.submit();

}
};

$('body').on('click', '#place_order,form#order_review input:submit', function(){
// Make sure there's not an old token on the form
Stripe.setPublishableKey($('#stripe_pub_key').data('publishablekey'));
Stripe.createToken($form, stripeResponseHandler);
return false;
});


$('body').on('click', '#place_order,form.checkout input:submit', function(){
// Make sure there's not an old token on the form
$('form.checkout').find('[name=stripeToken]').remove()
})


// Bind to the checkout_place_order event to add the token
$('form.checkout').bind('#place_order,checkout_place_order_Striper', function(e){

if($('input[name=payment_method]:checked').val() != 'Striper'){
return true;
}

$form.find('.payment-errors').html('');
$form.block({message: null,overlayCSS: {background: "#fff url(" + woocommerce_params.ajax_loader_url + ") no-repeat center",backgroundSize: "16px 16px",opacity: .6}});

// Pass if we have a token
if( $form.find('[name=stripeToken]').length)
return true;

Stripe.setPublishableKey($('#stripe_pub_key').data('publishablekey'));
Stripe.createToken($form, stripeResponseHandler)
// Prevent the form from submitting with the default action
return false;
});
});
};
35 changes: 34 additions & 1 deletion stripe_gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@

class Striper extends WC_Payment_Gateway
{
private $version = '0.28';
private $path;
private $url;


protected $GATEWAY_NAME = "Striper";
protected $usesandboxapi = true;
protected $order = null;
Expand All @@ -22,6 +27,8 @@ class Striper extends WC_Payment_Gateway

public function __construct()
{
$this->setup_paths_and_urls();

$this->id = 'Striper';
$this->has_fields = true;

Expand Down Expand Up @@ -136,7 +143,15 @@ public function admin_options()

public function payment_fields()
{
include_once('templates/payment.php');
$this->get_template('payment', array('publishable_key' => $this->publishable_key));

wp_enqueue_script(
'striper',
$this->url['assets'] . 'js/striper.js',
array('jquery'),
$this->version,
true
);
}

protected function send_to_stripe()
Expand Down Expand Up @@ -274,6 +289,24 @@ protected function getRequestData()
}
return false;
}

private function setup_paths_and_urls() {
$this->path['plugin_file'] = __FILE__;
$this->path['plugin_dir'] = untrailingslashit(plugin_dir_path(__FILE__));
//$this->path['includes'] = $this->path['plugin_dir'] . 'includes/';

$this->url['plugin_dir'] = plugin_dir_url(__FILE__);
$this->url['assets'] = $this->url['plugin_dir'] . 'assets/';
}

private function get_template($template_name, $args = array()) {
wc_get_template(
"$template_name.php",
$args,
'striper',
$this->path['plugin_dir'] . 'templates/'
);
}

}

Expand Down
138 changes: 1 addition & 137 deletions templates/payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/
?>

<div id="stripe_pub_key" class="hidden" style="display:none" data-publishablekey="<?php echo $this->publishable_key; ?>"> </div>
<div id="stripe_pub_key" class="hidden" style="display:none" data-publishablekey="<?php echo esc_attr($publishable_key;) ?>"> </div>
<div class="clear"></div>
<span class='payment-errors required'></span>
<p class="form-row striper-card-number">
Expand Down Expand Up @@ -53,139 +53,3 @@
<input class="input-text" type="text" maxlength="4" data-stripe="cvc" value="" style="border-radius:6px"/>
</p>
<div class="clear"></div>

<script>

var initStriper = function(){
jQuery(function($) {
var $form = $('form.checkout,form#order_review');

// Add additional information to be passed to Stripe
var stripeMap = {

billing_address_1: 'address_line1',
billing_address_2: 'address_line2',
billing_city: 'address_city',
billing_country: 'address_country',
billing_state: 'address_state',
billing_postcode: 'address_zip',
}
var card_name = '';
$('form.checkout').find('input[id*=billing_],select[id*=billing_]').each(function(idx,el){
var mapped = stripeMap[el.id];
if (mapped)
{
$(el).attr('data-stripe',mapped);

}
if(el.id == 'billing_first_name' || el.id == 'billing_last_name')
{
// If the billing first and last name fields were pre-populated (if the user was logged in)
// the fields will have values
var billingFirstName = $('#billing_first_name').val();
var billingLastName = $('#billing_last_name').val();

// Set the card name from the pre-populated fields
card_name = $('#billing_first_name').val() + ' ' + $('#billing_last_name').val();


// If the first name is changed
$('#billing_first_name').blur(function () {

// update the first name
billingFirstName = $(this).val();

// update the card name with the new first name
card_name = billingFirstName + " " + billingLastName;

// Update the hidden Stripe card name input with the new card name
$('#stripeCardName').attr('value', card_name);
});


// If the last name is changed
$('#billing_last_name').blur(function () {

// update the last name
billingLastName = $('#billing_last_name').val();

// update the card name with the new last name
card_name = billingFirstName + " " + billingLastName;

// Update the hidden Stripe card name input with the new card name
$('#stripeCardName').attr('value', card_name);
});
}
});
if (!$('#stripeCardName').length)
{
$('<input id="stripeCardName" class="input-text" type="hidden" data-stripe="name" value="'+card_name+'"/>').appendTo($form);
}

var stripeResponseHandler = function(status, response) {

if (response.error) {

// Show the errors on the form
$form.find('.payment-errors').text(response.error.message);
// Unblock the form to re-enter data
$form.unblock();

} else {
// Append the Token
$form.append($('<input type="hidden" name="stripeToken" />').val(response.id));

//Re-Submit
$form.submit();

}
};

$('body').on('click', '#place_order,form#order_review input:submit', function(){
// Make sure there's not an old token on the form
Stripe.setPublishableKey($('#stripe_pub_key').data('publishablekey'));
Stripe.createToken($form, stripeResponseHandler);
return false;
});


$('body').on('click', '#place_order,form.checkout input:submit', function(){
// Make sure there's not an old token on the form
$('form.checkout').find('[name=stripeToken]').remove()
})


// Bind to the checkout_place_order event to add the token
$('form.checkout').bind('#place_order,checkout_place_order_Striper', function(e){

if($('input[name=payment_method]:checked').val() != 'Striper'){
return true;
}

$form.find('.payment-errors').html('');
$form.block({message: null,overlayCSS: {background: "#fff url(" + woocommerce_params.ajax_loader_url + ") no-repeat center",backgroundSize: "16px 16px",opacity: .6}});

// Pass if we have a token
if( $form.find('[name=stripeToken]').length)
return true;

Stripe.setPublishableKey($('#stripe_pub_key').data('publishablekey'));
Stripe.createToken($form, stripeResponseHandler)
// Prevent the form from submitting with the default action
return false;
});
});
};

if(typeof jQuery=='undefined')
{
var headTag = document.getElementsByTagName("head")[0];
var jqTag = document.createElement('script');
jqTag.type = 'text/javascript';
jqTag.src = 'https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js';
jqTag.onload = initStriper;
headTag.appendChild(jqTag);
} else {
initStriper()
}
</script>

0 comments on commit e2dbee6

Please sign in to comment.