From ef7dcd37d85025b676927613674d82c2258908c4 Mon Sep 17 00:00:00 2001 From: Sylvain Date: Mon, 12 Apr 2021 12:16:12 +0200 Subject: [PATCH] retreive the formToken from PZ API --- app/controllers/api/payzen_controller.rb | 7 ++++++- .../javascript/components/payment/payzen/payzen-form.tsx | 2 +- app/frontend/src/javascript/models/payzen.ts | 1 + app/views/api/payzen/create_payment.json.jbuilder | 3 ++- lib/pay_zen/helper.rb | 7 +++++++ 5 files changed, 17 insertions(+), 3 deletions(-) diff --git a/app/controllers/api/payzen_controller.rb b/app/controllers/api/payzen_controller.rb index dc56512b1..1f5fe9141 100644 --- a/app/controllers/api/payzen_controller.rb +++ b/app/controllers/api/payzen_controller.rb @@ -3,6 +3,7 @@ # API Controller for accessing PayZen API endpoints through the front-end app class API::PayzenController < API::PaymentsController require 'pay_zen/charge' + require 'pay_zen/helper' def sdk_test str = 'fab-manager' @@ -17,8 +18,12 @@ class API::PayzenController < API::PaymentsController def create_payment amount = card_amount + @id = PayZen::Helper.generate_ref(cart_items_params, params[:customer]) client = PayZen::Charge.new - @result = client.create_payment(amount: amount, customer: { reference: params[:customer][:id], email: params[:customer][:email] }) + @result = client.create_payment(amount: amount[:amount], + order_id: @id, + customer: { reference: params[:customer][:id], email: params[:customer][:email] }) + @result end end diff --git a/app/frontend/src/javascript/components/payment/payzen/payzen-form.tsx b/app/frontend/src/javascript/components/payment/payzen/payzen-form.tsx index 4db3a9434..54a4a6e62 100644 --- a/app/frontend/src/javascript/components/payment/payzen/payzen-form.tsx +++ b/app/frontend/src/javascript/components/payment/payzen/payzen-form.tsx @@ -50,8 +50,8 @@ export const PayzenForm: React.FC = ({ onSubmit, onSuccess, onE }); const submitEmbeddedPayzenForm = (): void => { - // FIXME: not working... const button: HTMLButtonElement = document.querySelector('button.kr-payment-button'); + button.setAttribute('type', 'button'); button.click() } diff --git a/app/frontend/src/javascript/models/payzen.ts b/app/frontend/src/javascript/models/payzen.ts index 7b160f225..b30b6e1d2 100644 --- a/app/frontend/src/javascript/models/payzen.ts +++ b/app/frontend/src/javascript/models/payzen.ts @@ -4,4 +4,5 @@ export interface SdkTestResponse { export interface CreatePaymentResponse { formToken: string + orderId: string } diff --git a/app/views/api/payzen/create_payment.json.jbuilder b/app/views/api/payzen/create_payment.json.jbuilder index 1ffe67cbb..39ceb1ced 100644 --- a/app/views/api/payzen/create_payment.json.jbuilder +++ b/app/views/api/payzen/create_payment.json.jbuilder @@ -1,3 +1,4 @@ # frozen_string_literal: true -json.formToken @result['answer'] +json.formToken @result['answer']['formToken'] +json.orderId @id diff --git a/lib/pay_zen/helper.rb b/lib/pay_zen/helper.rb index f67dd47cd..94527049e 100644 --- a/lib/pay_zen/helper.rb +++ b/lib/pay_zen/helper.rb @@ -16,5 +16,12 @@ class PayZen::Helper end res end + + def generate_ref(cart_items, customer) + require 'sha3' + + content = { cart_items: cart_items, customer: customer }.to_json + DateTime.current.to_s + SHA3::Digest.hexdigest(:sha256, content) + end end end