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-06-04 18:26:20 +02:00
|
|
|
require 'pay_zen/token'
|
2021-06-01 12:20:02 +02:00
|
|
|
require 'pay_zen/transaction'
|
2021-04-12 12:16:12 +02:00
|
|
|
require 'pay_zen/helper'
|
2021-10-22 15:12:25 +02:00
|
|
|
require 'pay_zen/service'
|
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-10-18 12:14:51 +02:00
|
|
|
@result = client.create_payment(amount: PayZen::Service.new.payzen_amount(amount[:amount]),
|
2021-04-12 12:16:12 +02:00
|
|
|
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]))
|
2021-06-04 18:26:20 +02:00
|
|
|
rescue PayzenError => e
|
|
|
|
render json: e, status: :unprocessable_entity
|
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]))
|
2021-06-04 18:26:20 +02:00
|
|
|
rescue PayzenError => e
|
|
|
|
render json: e, status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_token
|
|
|
|
schedule = PaymentSchedule.find(params[:payment_schedule_id])
|
|
|
|
token = schedule.gateway_payment_mean
|
|
|
|
@id = schedule.gateway_order.id
|
|
|
|
@result = PayZen::Token.new.update(token.id,
|
|
|
|
PayZen::Helper.generate_customer(schedule.user.id, current_user.id, schedule.to_cart),
|
|
|
|
order_id: @id)
|
|
|
|
rescue PayzenError => e
|
|
|
|
render json: e, status: :unprocessable_entity
|
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
|
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-28 17:34:20 +02:00
|
|
|
render on_payment_success(params[:order_id], cart)
|
|
|
|
else
|
|
|
|
render json: order['answer'], status: :unprocessable_entity
|
2021-04-13 17:16:05 +02:00
|
|
|
end
|
|
|
|
rescue StandardError => e
|
|
|
|
render json: e, status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
|
2021-06-01 12:20:02 +02:00
|
|
|
def confirm_payment_schedule
|
|
|
|
render(json: { error: 'Bad gateway or online payment is disabled' }, status: :bad_gateway) and return unless PayZen::Helper.enabled?
|
|
|
|
|
|
|
|
client = PayZen::Transaction.new
|
|
|
|
transaction = client.get(params[:transaction_uuid])
|
|
|
|
|
|
|
|
cart = shopping_cart
|
|
|
|
|
|
|
|
if transaction['answer']['status'] == 'PAID'
|
|
|
|
render on_payment_success(params[:order_id], cart)
|
|
|
|
else
|
|
|
|
render json: transaction['answer'], status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
rescue StandardError => e
|
|
|
|
render json: e, status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
|
2021-04-13 17:16:05 +02:00
|
|
|
private
|
|
|
|
|
2021-05-28 17:34:20 +02:00
|
|
|
def on_payment_success(order_id, cart)
|
2021-05-21 18:25:18 +02:00
|
|
|
super(order_id, 'PayZen::Order', cart)
|
2021-04-13 17:16:05 +02:00
|
|
|
end
|
2021-04-01 18:20:26 +02:00
|
|
|
end
|