1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-11-28 09:24:24 +01:00

WIP: payzen recurring payments

This commit is contained in:
Sylvain 2021-06-01 12:20:02 +02:00
parent d6a6d34105
commit 431ea28448
8 changed files with 62 additions and 7 deletions

View File

@ -4,6 +4,7 @@
class API::PayzenController < API::PaymentsController
require 'pay_zen/charge'
require 'pay_zen/order'
require 'pay_zen/transaction'
require 'pay_zen/helper'
def sdk_test
@ -58,6 +59,23 @@ class API::PayzenController < API::PaymentsController
render json: e, status: :unprocessable_entity
end
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
private
def on_payment_success(order_id, cart)

View File

@ -33,8 +33,13 @@ export default class PayzenAPI {
return res?.data;
}
static async confirm(orderId: string, cart: ShoppingCart): Promise<Invoice|PaymentSchedule> {
const res: AxiosResponse<Invoice|PaymentSchedule> = await apiClient.post('/api/payzen/confirm_payment', { cart_items: cart, order_id: orderId });
static async confirm(orderId: string, cart: ShoppingCart): Promise<Invoice> {
const res: AxiosResponse<Invoice> = await apiClient.post('/api/payzen/confirm_payment', { cart_items: cart, order_id: orderId });
return res?.data;
}
static async confirmPaymentSchedule(orderId: string, transactionUuid: string, cart: ShoppingCart): Promise<PaymentSchedule> {
const res: AxiosResponse<PaymentSchedule> = await apiClient.post('/api/payzen/confirm_payment_schedule', { cart_items: cart, order_id: orderId, transaction_uuid: transactionUuid });
return res?.data;
}
}

View File

@ -8,9 +8,11 @@ import { Loader } from '../../base/loader';
import {
CreateTokenResponse,
KryptonClient,
KryptonError,
KryptonError, PaymentTransaction,
ProcessPaymentAnswer
} from '../../../models/payzen';
import { PaymentSchedule } from '../../../models/payment-schedule';
import { Invoice } from '../../../models/invoice';
/**
* A form component to collect the credit card details and to create the payment method on Stripe.
@ -63,7 +65,7 @@ export const PayzenForm: React.FC<GatewayFormProps> = ({ onSubmit, onSuccess, on
const transaction = event.clientAnswer.transactions[0];
if (event.clientAnswer.orderStatus === 'PAID') {
PayzenAPI.confirm(event.clientAnswer.orderDetails.orderId, cart).then((confirmation) => {
confirmPayment(event, transaction).then((confirmation) => {
PayZenKR.current.removeForms().then(() => {
onSuccess(confirmation);
});
@ -77,6 +79,18 @@ export const PayzenForm: React.FC<GatewayFormProps> = ({ onSubmit, onSuccess, on
return true;
};
/**
* Confirm the payment, depending on the current type of payment (single shot or recurring)
* @param event
*/
const confirmPayment = async (event: ProcessPaymentAnswer, transaction: PaymentTransaction): Promise<Invoice|PaymentSchedule> => {
if (!paymentSchedule) {
return await PayzenAPI.confirm(event.clientAnswer.orderDetails.orderId, cart);
} else {
return await PayzenAPI.confirmPaymentSchedule(event.clientAnswer.orderDetails.orderId, transaction.uuid, cart);
}
}
/**
* Callback triggered when the PayZen form was entirely loaded and displayed
* @see https://docs.lyra.com/fr/rest/V4.0/javascript/features/reference.html#%C3%89v%C3%A9nements

View File

@ -28,7 +28,7 @@ class CartItem::Subscription < CartItem::BaseItem
end
def to_object
Subscription.new(
::Subscription.new(
plan_id: @plan.id,
statistic_profile_id: StatisticProfile.find_by(user: @customer).id
)

View File

@ -183,6 +183,7 @@ Rails.application.routes.draw do
post 'payzen/sdk_test' => 'payzen#sdk_test'
post 'payzen/create_payment' => 'payzen#create_payment'
post 'payzen/confirm_payment' => 'payzen#confirm_payment'
post 'payzen/confirm_payment_schedule' => 'payzen#confirm_payment_schedule'
post 'payzen/check_hash' => 'payzen#check_hash'
post 'payzen/create_token' => 'payzen#create_token'

View File

@ -30,7 +30,7 @@ class PayZen::Client
raise ::PayzenError unless res.is_a?(Net::HTTPSuccess)
json = JSON.parse(res.body)
raise ::PayzenError, json['answer']['errorMessage'] if json['status'] == 'ERROR'
raise ::PayzenError, json['answer'] if json['status'] == 'ERROR'
json
end

View File

@ -12,7 +12,7 @@ class PayZen::Service < Payment::Service
def create_subscription(payment_schedule, order_id)
first_item = payment_schedule.ordered_items.first
order = PayZen::Order.new.get(order_id: order_id, operation_type: 'VERIFICATION')
order = PayZen::Order.new.get(order_id, operation_type: 'VERIFICATION')
client = PayZen::Charge.new
params = {

View File

@ -0,0 +1,17 @@
# frozen_string_literal: true
require 'pay_zen/client'
# Transaction/* endpoints of the PayZen REST API
class PayZen::Transaction < PayZen::Client
def initialize(base_url: nil, username: nil, password: nil)
super(base_url: base_url, username: username, password: password)
end
##
# @see https://payzen.io/fr-FR/rest/V4.0/api/playground/Transaction/Get/
##
def get(uuid)
post('/Transaction/Get/', uuid: uuid)
end
end