diff --git a/app/assets/stylesheets/registrar/registrar.sass b/app/assets/stylesheets/registrar/registrar.sass index 9f046237b..601abf5ec 100644 --- a/app/assets/stylesheets/registrar/registrar.sass +++ b/app/assets/stylesheets/registrar/registrar.sass @@ -32,3 +32,12 @@ h1, h2, h3, h4 .semifooter padding: 42px 0 80px 0 + + + +.payment-form + text-align: center + input[type="submit"] + cursor: pointer + top: 50% + position: absolute \ No newline at end of file diff --git a/app/controllers/registrar/payments_controller.rb b/app/controllers/registrar/payments_controller.rb index 368dc7cb9..330ffaa46 100644 --- a/app/controllers/registrar/payments_controller.rb +++ b/app/controllers/registrar/payments_controller.rb @@ -1,26 +1,25 @@ class Registrar::PaymentsController < RegistrarController + protect_from_forgery except: :back + skip_authorization_check # actually anyone can pay, no problems at all skip_before_action :authenticate_user!, :check_ip, only: [:back] before_action :check_bank # to handle existing model we should - # get invoice_id and then get reference_number + # get invoice_id and then get number # build BankTransaction without connection with right reference number # do not connect transaction and invoice def pay invoice = Invoice.find(params[:invoice_id]) - - render text: "You are trying to pay with #{params[:bank]} for #{invoice.reference_no}" + @bank_link = BankLink::Request.new(params[:bank], invoice, self) + @bank_link.make_transaction end - def cancel - - end # connect invoice and transaction # both back and IPN def back - + @bank_link = BankLink::Response.new(params[:bank], params) end private diff --git a/app/models/bank_link.rb b/app/models/bank_link.rb new file mode 100644 index 000000000..4e478770a --- /dev/null +++ b/app/models/bank_link.rb @@ -0,0 +1,83 @@ +class BankLink + module Base + def prepend_size(value) + value = (value || "").to_s.strip + string = "" + string << sprintf("%03i", value.size) + string << value + end + end + + class Request + include Base + include ActionView::Helpers::NumberHelper + + # need controller here in order to handle random ports and domains + # I don't want to do it but has to + attr_accessor :type, :invoice, :controller + def initialize(type, invoice, controller) + @type, @invoice, @controller = type, invoice, controller + end + + def url + ENV["payments_#{type}_url"] + end + + def fields + @fields ||= (hash = {} + hash["VK_SERVICE"] = "1012" + hash["VK_VERSION"] = "008" + hash["VK_SND_ID"] = ENV["payments_#{type}_seller_account"] + hash["VK_STAMP"] = invoice.number + hash["VK_AMOUNT"] = number_with_precision(invoice.sum_cache, :precision => 2, :separator => ".") + hash["VK_CURR"] = invoice.currency + hash["VK_REF"] = "" + hash["VK_MSG"] = "Order nr. #{invoice.number}" + hash["VK_RETURN"] = controller.registrar_return_payment_with_url(type) + hash["VK_CANCEL"] = controller.registrar_return_payment_with_url(type) + hash["VK_DATETIME"] = Time.now.strftime("%Y-%m-%dT%H:%M:%S%z") + hash["VK_MAC"] = calc_mac(hash) + hash["VK_ENCODING"] = "UTF-8" + hash["VK_LANG"] = "ENG" + hash) + end + + def calc_mac(fields) + pars = %w(VK_SERVICE VK_VERSION VK_SND_ID VK_STAMP VK_AMOUNT VK_CURR VK_REF VK_MSG VK_RETURN VK_CANCEL VK_DATETIME) + data = pars.map{|e| prepend_size(fields[e]) }.join + + sign(data) + end + + def make_transaction + transaction = BankTransaction.where(description: fields["VK_MSG"]).first_or_initialize( + reference_no: invoice.reference_no, + currency: invoice.currency, + ) + + transaction.save! + end + + private + def sign(data) + private_key = OpenSSL::PKey::RSA.new(File.read(ENV["payments_#{type}_seller_private"])) + + signed_data = private_key.sign(OpenSSL::Digest::SHA1.new, data) + signed_data = Base64.encode64(signed_data).gsub(/\n|\r/, '') + signed_data + end + end + + + + + class Response + attr_accessor :type, :params + def initialize(type, params) + @type, @params = type, params + end + def bank_public_key + OpenSSL::X509::Certificate.new(certificate).public_key + end + end +end \ No newline at end of file diff --git a/app/views/registrar/payments/pay.html.haml b/app/views/registrar/payments/pay.html.haml new file mode 100644 index 000000000..62f5fb87a --- /dev/null +++ b/app/views/registrar/payments/pay.html.haml @@ -0,0 +1,10 @@ +.payment-form + = form_tag @bank_link.url, method: :post do + - @bank_link.fields.each do |k, v| + = hidden_field_tag k, v + = submit_tag "Mine maksma" + + +:coffeescript + $(document).ready -> + $('.payment-form form').submit() \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 06ff248fe..f5b81e8b5 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -92,9 +92,10 @@ Rails.application.routes.draw do end end - get 'pay/:bank' => 'payments#pay', as: 'payment_with' - get 'pay/:bank/cancel' => 'payments#cancel',as: 'cancel_payment_with' - get 'pay/:bank/return' => 'payments#back', as: 'return_payment_with' + + get 'pay/return/:bank' => 'payments#back', as: 'return_payment_with' + post 'pay/return/:bank' => 'payments#back' + get 'pay/go/:bank' => 'payments#pay', as: 'payment_with' end # REGISTRANT ROUTES