1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-11-29 10:24:20 +01:00
fab-manager/app/controllers/api/payzen_controller.rb

99 lines
3.3 KiB
Ruby
Raw Normal View History

# 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
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'
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
end
2021-04-09 17:17:58 +02:00
def create_payment
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,
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 create_token
@id = PayZen::Helper.generate_ref(params[:cart_items], params[:customer_id])
client = PayZen::Charge.new
@result = client.create_token(order_id: @id,
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
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
order = client.get(params[:order_id], operation_type: 'DEBIT')
2021-04-13 17:16:05 +02:00
cart = shopping_cart
2021-04-13 17:16:05 +02:00
if order['answer']['transactions'].first['status'] == 'PAID'
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
def on_payment_success(order_id, cart)
super(order_id, 'PayZen::Order', cart)
2021-04-13 17:16:05 +02:00
end
end