Skip to content

Instantly share code, notes, and snippets.

@michaelbaudino
Last active December 16, 2015 12:38
Show Gist options
  • Save michaelbaudino/5435478 to your computer and use it in GitHub Desktop.
Save michaelbaudino/5435478 to your computer and use it in GitHub Desktop.
Implémentation Paymill (code du slide #27 de mon talk à Lyon.rb le 10/04/2013 : http://michaelbaudino.github.io/presentation-paiements).
# Initialize Paymill public API key
window.PAYMILL_PUBLIC_KEY = '8a8394c13bb2c160013bb7f6af8a1c85'
# Retrieve Paymill javascript snippet (the "bridge")
$.getScript('https://bridge.paymill.com')
# This should be called when the user validates the HTML form
paymill.createToken
number: $('#card-number').val() # required, without spaces and hyphens
exp_month: $('#card-expiry-month').val() # required
exp_year: $('#card-expiry-year').val() # required, four digits e.g. "2016"
cvc: $('#card-cvc').val() # required
amount_int: 4200 # required, integer e.g. "15" for 0.15 EUR
currency: 'EUR' # required, ISO 4217 e.g. "EUR" or "GBP"
, (error, result) ->
if error
alert 'error.apierror'
else
$.ajax(
type: 'POST'
url: '/pay/'
dataType: 'json'
data:
token: result.token
success: (data) ->
alert 'Paiement réussi'
error: (data) ->
alert 'Paiement échoué'
)
# This gist is using the paymill gem : https://github.com/dkd/paymill-ruby
# This is the real controller
def process_payment
respond_to do |format|
format.html { not_implemented__use(:json) }
format.json begin
response = Paymill::Transaction.create(
amount: 4200,
currency: 'EUR',
token: params[:token]
)
if response.status == 'closed' or response.status == :closed
render :json => response, :status => :ok
else
raise Exception.new('Payment refused')
end
rescue Exception => e
render :json => {:error => e.to_s}, :status => :unauthorized
end
end
end
<label for="card-number">Numéro de carte</label>
<input id="card-number" type="text" maxlength="16" />
<label for="card-expiry-month">Mois d'expiration</label>
<input id="card-expiry-month" type="text" placeholder="MM" maxlength="2" />
<label for="card-expiry-year">Année d'expiration</label>
<input id="card-expiry-year" type="text" placeholder="AAAA" maxlength="4" />
<label for="card-cvc">Numéro de sécurité</label>
<input id="card-cvc" type="text" maxlength="3" />
# Configure Paymill by retrieving your private API key in an environment variable
Paymill::api_key = ENV['PAYMILL_API_KEY']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment