2021-04-01 18:20:26 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# API Controller for accessing PayZen API endpoints through the front-end app
|
2021-04-09 17:17:58 +02:00
|
|
|
class API::PayzenController < API::PaymentsController
|
2021-04-01 18:20:26 +02:00
|
|
|
require 'pay_zen/charge'
|
2021-04-13 17:16:05 +02:00
|
|
|
require 'pay_zen/order'
|
2021-04-12 12:16:12 +02:00
|
|
|
require 'pay_zen/helper'
|
2021-04-01 18:20:26 +02:00
|
|
|
|
|
|
|
def sdk_test
|
|
|
|
str = 'fab-manager'
|
|
|
|
|
|
|
|
client = PayZen::Charge.new(base_url: params[:base_url], username: params[:username], password: params[:password])
|
|
|
|
res = client.sdk_test(str)
|
|
|
|
|
2021-04-02 17:16:27 +02:00
|
|
|
@status = (res['answer']['value'] == str)
|
|
|
|
rescue SocketError
|
|
|
|
@status = false
|
2021-04-01 18:20:26 +02:00
|
|
|
end
|
2021-04-09 17:17:58 +02:00
|
|
|
|
|
|
|
def create_payment
|
2021-05-19 18:12:52 +02:00
|
|
|
cart = shopping_cart
|
|
|
|
amount = debit_amount(cart)
|
|
|
|
@id = PayZen::Helper.generate_ref(params[:cart_items], params[:customer_id])
|
2021-04-09 17:17:58 +02:00
|
|
|
|
|
|
|
client = PayZen::Charge.new
|
2021-04-12 12:16:12 +02:00
|
|
|
@result = client.create_payment(amount: amount[:amount],
|
|
|
|
order_id: @id,
|
2021-04-27 17:18:20 +02:00
|
|
|
customer: PayZen::Helper.generate_customer(params[:customer_id], current_user.id, params[:cart_items]))
|
|
|
|
error_handling
|
2021-04-22 19:24:08 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def create_token
|
2021-05-19 18:12:52 +02:00
|
|
|
@id = PayZen::Helper.generate_ref(params[:cart_items], params[:customer_id])
|
2021-04-22 19:24:08 +02:00
|
|
|
client = PayZen::Charge.new
|
|
|
|
@result = client.create_token(order_id: @id,
|
2021-04-27 17:18:20 +02:00
|
|
|
customer: PayZen::Helper.generate_customer(params[:customer_id], current_user.id, params[:cart_items]))
|
|
|
|
error_handling
|
2021-04-09 17:17:58 +02:00
|
|
|
end
|
2021-04-13 17:16:05 +02:00
|
|
|
|
2021-04-14 17:56:04 +02:00
|
|
|
def check_hash
|
|
|
|
@result = PayZen::Helper.check_hash(params[:algorithm], params[:hash_key], params[:hash], params[:data])
|
|
|
|
end
|
|
|
|
|
2021-04-13 17:16:05 +02:00
|
|
|
def confirm_payment
|
|
|
|
render(json: { error: 'Bad gateway or online payment is disabled' }, status: :bad_gateway) and return unless PayZen::Helper.enabled?
|
|
|
|
|
|
|
|
client = PayZen::Order.new
|
2021-04-14 17:56:04 +02:00
|
|
|
order = client.get(params[:order_id], operation_type: 'DEBIT')
|
2021-04-13 17:16:05 +02:00
|
|
|
|
2021-05-19 18:12:52 +02:00
|
|
|
cart = shopping_cart
|
|
|
|
amount = debit_amount(cart)
|
2021-04-13 17:16:05 +02:00
|
|
|
|
2021-04-14 17:56:04 +02:00
|
|
|
if order['answer']['transactions'].first['status'] == 'PAID'
|
2021-05-19 18:12:52 +02:00
|
|
|
if cart.reservation
|
|
|
|
res = on_reservation_success(params[:order_id], amount[:details], cart)
|
|
|
|
elsif cart.subscription
|
|
|
|
res = on_subscription_success(params[:order_id], amount[:details], cart)
|
2021-04-13 17:16:05 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-04-14 17:56:04 +02:00
|
|
|
render res
|
2021-04-13 17:16:05 +02:00
|
|
|
rescue StandardError => e
|
|
|
|
render json: e, status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2021-05-19 18:12:52 +02:00
|
|
|
def on_reservation_success(order_id, details, cart)
|
|
|
|
super(order_id, 'PayZen::Order', details, cart)
|
2021-04-13 17:16:05 +02:00
|
|
|
end
|
|
|
|
|
2021-05-19 18:12:52 +02:00
|
|
|
def on_subscription_success(order_id, details, cart)
|
|
|
|
super(order_id, 'PayZen::Order', details, cart)
|
2021-04-13 17:16:05 +02:00
|
|
|
end
|
2021-04-27 17:18:20 +02:00
|
|
|
|
|
|
|
def error_handling
|
|
|
|
return unless @result['status'] == 'ERROR'
|
|
|
|
|
|
|
|
render json: { error: @result['answer']['detailedErrorMessage'] || @result['answer']['errorMessage'] }, status: :unprocessable_entity
|
|
|
|
end
|
2021-04-01 18:20:26 +02:00
|
|
|
end
|