Skip to content

Commit

Permalink
close #232 link account api
Browse files Browse the repository at this point in the history
  • Loading branch information
panhachom committed Dec 18, 2024
1 parent 782bc95 commit 7720d2d
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module Spree
module Api
module V2
module Storefront
class AccountOnFilesController < BaseController
def create
end
end
end
end
end
end
5 changes: 5 additions & 0 deletions app/models/spree/linked_account.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module Spree
class LinkedAccount < Base
belongs_to :user, class_name: 'Spree::User'
end
end
9 changes: 9 additions & 0 deletions app/models/vpago/user_decorator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Vpago
module UserDecorator
def self.prepended(base)
base.has_one :linked_account, dependent: :destroy, class_name: 'Spree::LinkedAccount'
end
end
end

Spree::Product.prepend(Vpago::UserDecorator)
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
get :payment_redirect
patch :request_update_payment
end
resources :account_on_files, only: [:create]
end
end
end
Expand Down
9 changes: 9 additions & 0 deletions db/migrate/20241218022922_create_spree_linked_account.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class CreateSpreeLinkedAccount < ActiveRecord::Migration[7.0]
def change
create_table :spree_linked_accounts do |t|
t.references :user, null: false, foreign_key: { to_table: :spree_users }
t.jsonb :response, default: {}
t.timestamps
end
end
end
54 changes: 54 additions & 0 deletions lib/vpago/payway_v2/account_linker.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
require 'faraday'

module Vpago
module PaywayV2
class AccountLinker
def call
context.response = response
end

def response
conn = Faraday::Connection.new do |faraday|
faraday.request :url_encoded
end

data = {
request_time: req_time,
merchant_id: merchant_id,
return_params: nil,
return_url: return_url,
hash: hash
}
conn.post(link_account_url, data)
end

def link_account_url
"#{host}#{ENV.fetch('PAYWAY_V2_LINK_ACCCOUNT_PATH')}"
end

def merchant_id
ENV.fetch('MERCHANT_ID')
end

def req_time
Time.current.strftime('%Y%m%d%H%M%S')
end

def return_url
preferred_return_url = ENV.fetch('PAYWAY_V2_LINK_ACCOUNT_CALLBACK_URL', nil)
return nil if preferred_return_url.blank?

Base64.encode64(preferred_return_url).delete("\n")
end

def hash
hash_data = "#{merchant_id}#{req_time}"
Base64.strict_encode64(OpenSSL::HMAC.digest(OpenSSL::Digest.new('sha512'), api_key, hash_data))
end

def api_key
ENV.fetch('API_KEY')
end
end
end
end

0 comments on commit 7720d2d

Please sign in to comment.