1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-12-03 14:24:23 +01:00
fab-manager/app/services/payments/payzen_service.rb

39 lines
1.4 KiB
Ruby
Raw Normal View History

2022-08-25 08:52:17 +02:00
# frozen_string_literal: true
# Provides methods for pay cart by PayZen
class Payments::PayzenService
require 'pay_zen/helper'
require 'pay_zen/order'
require 'pay_zen/charge'
require 'pay_zen/service'
include Payments::PaymentConcern
2022-08-26 20:10:21 +02:00
def payment(order, coupon_code)
amount = debit_amount(order, coupon_code)
2022-08-25 08:52:17 +02:00
raise Cart::ZeroPriceError if amount.zero?
2022-08-25 08:52:17 +02:00
id = PayZen::Helper.generate_ref(order, order.statistic_profile.user.id)
client = PayZen::Charge.new
result = client.create_payment(amount: PayZen::Service.new.payzen_amount(amount),
order_id: id,
customer: PayZen::Helper.generate_customer(order.statistic_profile.user.id,
order.statistic_profile.user.id, order))
{ order: order, payment: { formToken: result['answer']['formToken'], orderId: id } }
end
2022-08-26 20:10:21 +02:00
def confirm_payment(order, coupon_code, payment_id)
2022-08-25 08:52:17 +02:00
client = PayZen::Order.new
payzen_order = client.get(payment_id, operation_type: 'DEBIT')
if payzen_order['answer']['transactions'].any? { |transaction| transaction['status'] == 'PAID' }
2022-08-26 20:10:21 +02:00
o = payment_success(order, coupon_code, 'card', payment_id, 'PayZen::Order')
2022-08-25 08:52:17 +02:00
{ order: o }
else
2022-09-14 19:54:24 +02:00
order.update(state: 'payment_failed')
{ order: order, payment: { error: { statusText: payzen_order['answer'] } } }
2022-08-25 08:52:17 +02:00
end
end
end