Skip to content

Commit

Permalink
#464 enable stripe, refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
syjer committed Jun 26, 2018
1 parent 5607276 commit c6d52b4
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 105 deletions.
8 changes: 7 additions & 1 deletion src/main/java/alfio/controller/ReservationController.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public class ReservationController {
private final TicketReservationRepository ticketReservationRepository;

@RequestMapping(value = "/event/{eventName}/reservation/{reservationId}/book", method = RequestMethod.GET)
public String showPaymentPage(@PathVariable("eventName") String eventName,
public String showBookingPage(@PathVariable("eventName") String eventName,
@PathVariable("reservationId") String reservationId,
//paypal related parameters
@RequestParam(value = "paymentId", required = false) String paypalPaymentId,
Expand Down Expand Up @@ -354,6 +354,12 @@ public String showOverview(@PathVariable("eventName") String eventName, @PathVar
.map(PaymentManager.PaymentMethod::getPaymentProxy)
.collect(toList());

boolean includeStripe = !orderSummary.getFree() && activePaymentMethods.contains(PaymentProxy.STRIPE);
model.addAttribute("includeStripe", includeStripe);
if (includeStripe) {
model.addAttribute("stripe_p_key", paymentManager.getStripePublicKey(event));
}

model.addAttribute("multiplePaymentMethods" , activePaymentMethods.size() > 1 )
.addAttribute("activePaymentMethods", activePaymentMethods);

Expand Down
102 changes: 1 addition & 101 deletions src/main/webapp/resources/js/event/reservation-page.js
Original file line number Diff line number Diff line change
@@ -1,75 +1,7 @@
(function() {

'use strict';


function stripeResponseHandler(status, response) {
var $form = $('#payment-form');


//https://stripe.com/docs/api#errors codes from stripes

/*
* incorrect_number The card number is incorrect.
* invalid_number The card number is not a valid credit card number.
* invalid_expiry_month The card's expiration month is invalid.
* invalid_expiry_year The card's expiration year is invalid.
* invalid_cvc The card's security code is invalid.
* expired_card The card has expired.
* incorrect_cvc The card's security code is incorrect.
* incorrect_zip The card's zip code failed validation.
* card_declined The card was declined.
* missing There is no card on a customer that is being charged.
* processing_error An error occurred while processing the card.
* rate_limit An error occurred due to requests hitting the API too quickly.
*
*/

var errorCodeToSelectorMap = {
incorrect_number : '[data-stripe=number]',
invalid_number: '[data-stripe=number]',
invalid_expiry_month : '[data-stripe=exp-month]',
invalid_expiry_year : '[data-stripe=exp-year]',
invalid_cvc : '[data-stripe=cvc]',
expired_card : '[data-stripe]',
incorrect_cvc : '[data-stripe=cvc]',
card_declined : '[data-stripe]',
missing : '[data-stripe]',
processing_error : '[data-stripe]',
rate_limit : '[data-stripe]'
};

if (response.error) {
$(".payment-errors").removeClass('hide').empty();
$("[data-stripe]").parent().removeClass('has-error');


var attrValue = document.getElementById("stripe-key").getAttribute('data-stripe-message-'+response.error.code);

$form.find('.payment-errors').append("<p><strong>"+(attrValue || response.error.message)+"</strong></p>");
$form.find('button').prop('disabled', false);
$form.find(errorCodeToSelectorMap[response.error.code]).parent().addClass('has-error');

} else {
$(".payment-errors").addClass('hide');
// token contains id, last4, and card type
var token = response.id;
// Insert the token into the form so it gets submitted to the server
$form.append($('<input type="hidden" name="stripeToken" />').val(token));
// and re-submit
$form.get(0).submit();
}
}

var hasStripe = document.getElementById("stripe-key") != null;

if(hasStripe) {
// This identifies your website in the createToken call below
Stripe.setPublishableKey(document.getElementById("stripe-key").getAttribute('data-stripe-key'));
}




jQuery(function() {

var hiddenClasses = 'hidden-xs hidden-sm hidden-md hidden-lg';
Expand Down Expand Up @@ -133,14 +65,6 @@
// Disable the submit button to prevent repeated clicks
$form.find('button').prop('disabled', true);

var selectedPaymentMethod = $form.find('input[name=paymentMethod]');
if(hasStripe && (selectedPaymentMethod.length === 0 ||
(selectedPaymentMethod.length === 1 && selectedPaymentMethod.val() === 'STRIPE') ||
selectedPaymentMethod.filter(':checked').val() === 'STRIPE')) {
Stripe.card.createToken($form, stripeResponseHandler);
// Prevent the form from submitting with the default action
return false;
}
return true;
}

Expand Down Expand Up @@ -217,31 +141,7 @@
}
});

var paymentMethod = $('input[name=paymentMethod]');
if(paymentMethod.length > 1) {
$('#payment-method-STRIPE').find('input').removeAttr('required');
$('.payment-method-detail').hide();

paymentMethod.change(function() {
var method = $(this).attr('data-payment-method');
$('.payment-method-detail').hide();
$('#payment-method-'+method).show();
if(method === 'STRIPE') {
var inputFields = $('#payment-method-STRIPE').find('input');
inputFields.attr('required', true);
var fullName = $.trim($.trim($('#first-name').val()) + ' ' + $.trim($('#last-name').val()));
if(fullName === '') {
fullName = $.trim($('#full-name').val());
}
$('#card-name').val(fullName);
inputFields.first().focus();

} else {
$('#payment-method-STRIPE').find('input').val('').removeAttr('required');
methodSelected(method);
}
});
}

$('#first-name.autocomplete-src, #last-name.autocomplete-src').change(function() {
fillAttendeeData($('#first-name').val(), $('#last-name').val());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,9 @@ public void reservationFlowTest() throws Exception{
String reservationIdentifier = redirectResult.substring(redirectStart.length()).replace("/book", "");


// check that the payment page is shown
String reservationPage = reservationController.showPaymentPage(eventName, reservationIdentifier, null, null, null, null, null, null, null, null, null, null, null, null, null, new BindingAwareModelMap(), Locale.ENGLISH);
assertEquals("/event/reservation-page", reservationPage);
// check that the booking page is shown
String bookingPage = reservationController.showBookingPage(eventName, reservationIdentifier, null, null, null, null, null, null, null, null, null, null, null, null, null, new BindingAwareModelMap(), Locale.ENGLISH);
assertEquals("/event/reservation-page", bookingPage);
//

// pay offline
Expand Down

0 comments on commit c6d52b4

Please sign in to comment.