Skip to content

Commit

Permalink
Added mollie as payment provider
Browse files Browse the repository at this point in the history
  • Loading branch information
feliciaan committed Aug 17, 2017
1 parent bfa6600 commit 463648a
Show file tree
Hide file tree
Showing 20 changed files with 169 additions and 12 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Expand Up @@ -133,6 +133,8 @@ gem 'twitter-typeahead-rails'
# Enum support with prefixes
gem 'simple_enum'

gem 'mollie-api-ruby'

group :development do
gem 'sqlite3'

Expand Down
4 changes: 3 additions & 1 deletion Gemfile.lock
Expand Up @@ -180,6 +180,7 @@ GEM
mimemagic (0.3.0)
mini_portile2 (2.1.0)
minitest (5.10.1)
mollie-api-ruby (2.2.0)
multi_json (1.12.1)
multi_xml (0.6.0)
multipart-post (2.0.0)
Expand Down Expand Up @@ -393,6 +394,7 @@ DEPENDENCIES
jbuilder (~> 1.2)
jc-validates_timeliness
jquery-rails
mollie-api-ruby
mysql2
newrelic_rpm
omniauth-oauth2 (~> 1.3.1)
Expand Down Expand Up @@ -424,4 +426,4 @@ DEPENDENCIES
will_paginate-bootstrap

BUNDLED WITH
1.13.7
1.15.0
24 changes: 24 additions & 0 deletions app/controllers/payment_webhook_controller.rb
@@ -0,0 +1,24 @@
require 'mollie/api/client'

class PaymentWebhookController < ApplicationController
skip_before_filter :verify_authenticity_token, :only => [:mollie]

def mollie
mollie = Mollie::API::Client.new(Rails.application.secrets.mollie_api_key)
payment_id = params[:id]

payment = mollie.payments.get payment_id
registration = Registration.find_by_payment_id payment_id

if payment.paid?
registration.paid = payment.amount
RegistrationMailer.ticket(registration).deliver_now
registration.save!
elsif ['cancelled', 'expired', 'failed'].include? payment.status
RegistrationMailer.payment_failed(registration, registration.event).deliver_now

registration.destroy!
end
render :nothing => true, :status => 200, :content_type => 'text/html'
end
end
28 changes: 26 additions & 2 deletions app/controllers/registrations_controller.rb
@@ -1,3 +1,5 @@
require 'mollie/api/client'

class RegistrationsController < ApplicationController

before_action :authenticate_user!, only: [:index, :destroy, :resend, :update, :email, :upload]
Expand Down Expand Up @@ -71,7 +73,7 @@ def basic
authorize! :register, requested_access_level

# Make the registration
@registration = @event.registrations.new params.require(:registration).permit(:title, :email, :job_function, :firstname, :lastname, :student_number, :comment, :phone_number, :has_plus_one, :plus_one_title, :plus_one_firstname, :plus_one_lastname, :club_id)
@registration = @event.registrations.new params.require(:registration).permit(:title, :email, :job_function, :firstname, :lastname, :student_number, :comment, :phone_number, :has_plus_one, :plus_one_title, :plus_one_firstname, :plus_one_lastname, :club_id, :payment_method)
@registration.access_levels << requested_access_level
@registration.price = requested_access_level.price
@registration.paid = 0
Expand Down Expand Up @@ -103,7 +105,14 @@ def basic
if @registration.is_paid
RegistrationMailer.ticket(@registration).deliver_now
else
RegistrationMailer.confirm_registration(@registration).deliver_now
if @registration.payment_method == 'mollie'
payment = create_mollie_payment
redirect_to payment.payment_url
flash[:success] = t('flash.succes')
return
else
RegistrationMailer.confirm_registration(@registration).deliver_now
end
end

flash[:success] = t('flash.succes') # or further payment information."
Expand Down Expand Up @@ -216,4 +225,19 @@ def upload

end

def create_mollie_payment
mollie = Mollie::API::Client.new(Rails.application.secrets.mollie_api_key)

payment = mollie.payments.create(
amount: @registration.price,
description: 'Ticket: ' + @registration.event.name,
redirect_url: url_for(@registration.event),
webhook_url: url_for(controller: :payment_webhook, action: 'mollie'),
metadata: { registration_id: @registration.id}
)
@registration.payment_id=payment.id
@registration.save!
payment
end

end
2 changes: 2 additions & 0 deletions app/helpers/payment_webhook_helper.rb
@@ -0,0 +1,2 @@
module PaymentWebhookHelper
end
7 changes: 7 additions & 0 deletions app/mailers/registration_mailer.rb
Expand Up @@ -32,4 +32,11 @@ def confirm_cancel(registration, event)

mail to: "#{registration.lastname} #{registration.firstname} <#{registration.email}>", subject: t('mailers.registration.subjects.cancel', :event => registration.event.name)
end

def payment_failed(registration, event)
@registration = registration
@event = event

mail to: "#{registration.lastname} #{registration.firstname} <#{registration.email}>", subject: t('mailers.registration.subjects.failed', :event => registration.event.name)
end
end
11 changes: 10 additions & 1 deletion app/models/event.rb
Expand Up @@ -31,7 +31,7 @@
# comment_title :string
# show_telephone_disclaimer :boolean default(FALSE)
# allow_plus_one :boolean
# can_add_club :bool
# can_add_club :boolean
#

class Event < ActiveRecord::Base
Expand Down Expand Up @@ -82,6 +82,15 @@ def prettify_bank_number
self.bank_number = IBANTools::IBAN.new(self.bank_number).prettify if bank_number_changed?
end

def payment_methods
['wiretransfer', 'mollie']
end

def self.payment_methods_scope
'event.payment_methods'
end


def generate_xls
self.export_status = 'generating'
self.save
Expand Down
4 changes: 3 additions & 1 deletion app/models/registration.rb
Expand Up @@ -25,7 +25,9 @@
# plus_one_title :string
# plus_one_firstname :string
# plus_one_lastname :string
# club :Club
# club_id :integer
# payment_method :string
# payment_id :string
#

class Registration < ActiveRecord::Base
Expand Down
51 changes: 51 additions & 0 deletions app/views/registration_mailer/payment_failed.erb
@@ -0,0 +1,51 @@
<!DOCTYPE html>
<html>
<head>
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
</head>
<body>
<p>Dear <%= @registration.name %>,</p>

<p>You received this mail because the payment for this ticket failed:</p>

<table>
<tbody>
<tr>
<td><strong>Event</strong></td>
<td><%= @event.name %></td>
</tr>
<tr>
<td><strong>Organisation</strong></td>
<td><%= @event.club.name %></td>
</tr>
<tr>
<td><strong>Location</strong></td>
<td><%= @event.location %></td>
</tr>
<tr>
<td><strong>Start date</strong></td>
<td><%= nice_time @event.start_date %></td>
</tr>
<tr>
<td><strong>End date</strong></td>
<td><%= nice_time @event.end_date %></td>
</tr>
<tr>
<td><strong>Website</strong></td>
<td><a href="<%= @event.website %>"><%= @event.website %></a></td>
</tr>
</tbody>
</table>

<p>If you have any problems, you can contact us via mail: <%= mail_to @event.contact_email, @event.contact_email %></p>

<p>It's possible to create a new ticket on our website.</p>

<p>Kind regards,<br /><%= @event.club.name %></p>

<% unless @event.signature.blank? %>
<%= simple_format @event.signature %>
<% end %>

</body>
</html>
4 changes: 4 additions & 0 deletions app/views/registrations/_basic.html.erb
Expand Up @@ -54,6 +54,10 @@

<% end %>
<%end%>
<% if true %>
<%= form_radio_field f, :payment_method, @registration.payment_method || 'wiretransfer', @registration.event.payment_methods, true, true, Event.payment_methods_scope %>
<br/>
<% end %>
<%= f.submit t('event.register', default: 'Register'), class: 'btn btn-primary' %>
<% end %>
Expand Down
7 changes: 6 additions & 1 deletion config/locales/nl.yml
@@ -1,6 +1,7 @@
---
nl:
plus_one_title: Partner aanspreking
payment_method: Betalingswijze
activerecord:
attributes:
registration:
Expand All @@ -19,6 +20,7 @@ nl:
plus_one_firstname: Partner voornaam
plus_one_lastname: Partner achternaam
club_id: Vereniging
payment_method: Betalingswijze
datagrid:
table:
order:
Expand All @@ -27,6 +29,9 @@ nl:
dutch: Nederlands
english: English
event:
payment_methods:
mollie: bankkaarten (Mollie)
wiretransfer: overschrijving
barcode_number: Barcode nummer
cancel:
button: Schrijf mij nu uit!
Expand All @@ -46,7 +51,7 @@ nl:
registration:
club:
info: Het Stichtingsbal is een initiatief voor en door studentenverenigingen. U schenkt €1 per ticket aan de vereniging die u hieronder selecteert. Heeft u geen vereniging van uw voorkeur kiest u gewoon ‘Universiteit Gent’.
payments: Betalingen kunnen enkel via bank overschrijvingen gebeuren, <strong>niet via bankkaarten</strong>.
payments: Betalingen kunnen via bank overschrijvingen alsook bankkaarten gebeuren.
flash:
succes: Registratie succesvol. Uw ticket zou nu in uw mailbox toegekomen moeten zijn.
forms:
Expand Down
2 changes: 2 additions & 0 deletions config/routes.rb
@@ -1,4 +1,6 @@
Isengard::Application.routes.draw do
post 'payment_webhook/mollie'

devise_for :partners
devise_for :users, controllers: {
omniauth_callbacks: 'omniauth_callback',
Expand Down
4 changes: 3 additions & 1 deletion config/secrets.yml
Expand Up @@ -6,14 +6,15 @@ development:
enrollment_key: '#development#'
omniauth_client_id: 'tomtest'
omniauth_client_secret: 'blargh'

mollie_api_key: 'test_PVHVd2FTH5y4UrkFj3ppn2rb8CKGzR'

test:
secret_key_base: 24f28e9f092ff3849df2fcfaf5a0a56a18f098f314a369ea619171a0d47eaea9f0458c25572c52b4cc8c4823d75e2ea5f6477dfcc1e7b5ea46d4ca2be22cfb84
fk_auth_url: 'http://fkgent.be/api_isengard_v2.php'
fk_auth_salt: '#development#'
fk_auth_key: '#development#'
enrollment_key: '#development#'
mollie_api_key: 'test_PVHVd2FTH5y4UrkFj3ppn2rb8CKGzR'

production:
secret_key_base: a27fcd9aa2eabd14030493d6b5a1521ba09c9d23a1dd90b3c9d6914b8d226d1df8b46af3005f73a29c3ef1acd86de3669fbe08605d9ce8da73856c734f8a0e36
Expand All @@ -23,3 +24,4 @@ production:
enrollment_key: '#development#'
omniauth_client_id: ''
omniauth_client_secret: ''
mollie_api_key: 'test_PVHVd2FTH5y4UrkFj3ppn2rb8CKGzR'
@@ -0,0 +1,6 @@
class AddPaymentMethodToRegistration < ActiveRecord::Migration
def change
add_column :registrations, :payment_method, :string
add_column :registrations, :payment_id, :string
end
end
4 changes: 3 additions & 1 deletion db/schema.rb
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20170523185122) do
ActiveRecord::Schema.define(version: 20170817125038) do

create_table "access_levels", force: :cascade do |t|
t.string "name"
Expand Down Expand Up @@ -193,6 +193,8 @@
t.string "plus_one_firstname"
t.string "plus_one_lastname"
t.integer "club_id"
t.string "payment_method"
t.string "payment_id"
end

add_index "registrations", ["event_id"], name: "index_registrations_on_event_id"
Expand Down
9 changes: 9 additions & 0 deletions test/controllers/payment_webhook_controller_test.rb
@@ -0,0 +1,9 @@
require 'test_helper'

class PaymentWebhookControllerTest < ActionController::TestCase
test "should get mollie" do
get :mollie
assert_response :success
end

end
2 changes: 1 addition & 1 deletion test/fixtures/events.yml
Expand Up @@ -31,7 +31,7 @@
# comment_title :string
# show_telephone_disclaimer :boolean default(FALSE)
# allow_plus_one :boolean
# can_add_club :bool
# can_add_club :boolean
#

# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html
Expand Down
4 changes: 3 additions & 1 deletion test/fixtures/registrations.yml
Expand Up @@ -25,7 +25,9 @@
# plus_one_title :string
# plus_one_firstname :string
# plus_one_lastname :string
# club :Club
# club_id :integer
# payment_method :string
# payment_id :string
#

# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html
Expand Down
2 changes: 1 addition & 1 deletion test/models/event_test.rb
Expand Up @@ -31,7 +31,7 @@
# comment_title :string
# show_telephone_disclaimer :boolean default(FALSE)
# allow_plus_one :boolean
# can_add_club :bool
# can_add_club :boolean
#

require 'test_helper'
Expand Down
4 changes: 3 additions & 1 deletion test/models/registration_test.rb
Expand Up @@ -25,7 +25,9 @@
# plus_one_title :string
# plus_one_firstname :string
# plus_one_lastname :string
# club :Club
# club_id :integer
# payment_method :string
# payment_id :string
#

require 'test_helper'
Expand Down

0 comments on commit 463648a

Please sign in to comment.