Paymill Rail Integration - Locating IDs Locally

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?

+4
source share
2 answers

Proposed Changes:

  • Use the Payment model to store payment_card_token and payment_id . This will allow you to support multiple cards per client, if you need in the future.
  • Create a PaymentTransaction table and a db table. Each time a user makes a payment, store the data in the PaymentTransaction model instead of the Payment model.
  • Take a look at the structure of webhooks. I did not use it myself, but this could help with your questions about expired tokens, etc.

Also a general note about your rails code

  • Instead of User.where("user_id = ?", user_id).update_attributes just say user.update_attributes , as you have already defined the association.
  • Even better if you can move the if paymill_client_id block code if paymill_client_id to the user model.
+1
source

valid payment object - yes, you are right, the payment object is valid only for 364 days, but it has been extended for another 364 days, starting from the last transaction. Important! A token should only be used once until you receive a payment object in a successful transaction or preauth response. After that, please use paymentobject. We will change this behavior so that the token can be used only once in our version of the API version 3.

webhooks - right now there is no website for an object with an expired payment, but we are selling a token that warns you that the card will expire in the next days. But it will take several months. Before you save the expiration date. There is no problem with PCI Security to save this information.

Best, Christian

+1
source

Source: https://habr.com/ru/post/1501988/


All Articles