I am working on integrating a Rails application with a Paymill payment gateway (using the paymill-ruby gem), and wondered if anyone could give any advice on the best way to interact with the API during a standard order process. I managed to get the basics, but there are very few resources to structure the complete process.
I have a User model where I want to save the Paymill customer ID and payment ID and payment model, where I store data about each transaction (referring to the order ID). My billing model code currently looks like this:
class Payment < ActiveRecord::Base include ActiveModel::ForbiddenAttributesProtection attr_accessor :paymill_card_token, :email, :paymill_client_id, :paymill_payment_id belongs_to :order belongs_to :user validates_presence_of :order_id validates_presence_of :user_id def save_with_payment if valid? if paymill_client_id.blank? #if user hasn't paid before, create paymill client client = Paymill::Client.create email: email, description: user_id paymill_client_id = client.id # update current user with paymill client ID User.where("user_id = ?", user_id).update_attributes(:paymill_client_id => paymill_client_id) end if paymill_payment_id.blank? #if paymill_payment_id isn't present, create new payment payment = Paymill::Payment.create token: paymill_card_token, client: paymill_client_id paymill_payment_id = payment.id # update current user with paymill payment ID User.where("user_id = ?", user_id).update_attributes(:paymill_payment_id => paymill_payment_id) end transaction = Paymill::Transaction.create client: paymill_client_id, amount: "#{amount.to_s.gsub('.', '')}0", currency: 'GBP', description: "ORDER ID #{order_id}", payment: paymill_payment_id self.paymill_id = transaction.id save! end rescue Paymill::PaymillError => e logger.error "Paymill error while creating customer: #{e.message}" errors.add :base, "There was a problem with your credit card. Please try again." false end end
I read somewhere in the documentation that the paymill payment object (customerโs credit card information) is valid for only one year - is that right? And if so, how should this be decided? Is it a good idea to keep this expiration date as well as the user credit card expiration date in my user model so that I know when to ask them to re-enter their card information?
source share