From 13fae70277c4580fc73d3f50f4c3bc8433f30115 Mon Sep 17 00:00:00 2001 From: Sylvain Date: Wed, 9 Jun 2021 18:48:51 +0200 Subject: [PATCH] test payzen endpoints --- app/models/footprintable.rb | 2 +- app/services/cart_service.rb | 8 +- lib/pay_zen/pci/charge.rb | 37 ++++++++ lib/tasks/fablab/chain.rake | 2 +- test/fixtures/history_values.yml | 24 +++--- test/integration/payzen_test.rb | 144 +++++++++++++++++++++++++++++++ 6 files changed, 199 insertions(+), 18 deletions(-) create mode 100644 lib/pay_zen/pci/charge.rb create mode 100644 test/integration/payzen_test.rb diff --git a/app/models/footprintable.rb b/app/models/footprintable.rb index 37ca1bd49..7f6060fda 100644 --- a/app/models/footprintable.rb +++ b/app/models/footprintable.rb @@ -34,7 +34,7 @@ class Footprintable < ApplicationRecord FootprintService.debug_footprint(self.class, self) end - protected + #protected def compute_footprint FootprintService.compute_footprint(self.class, self) diff --git a/app/services/cart_service.rb b/app/services/cart_service.rb index 0fddf562e..60bf501ce 100644 --- a/app/services/cart_service.rb +++ b/app/services/cart_service.rb @@ -16,9 +16,9 @@ class CartService items = [] cart_items[:items].each do |item| - if item.keys.first == 'subscription' + if ['subscription', :subscription].include?(item.keys.first) items.push(CartItem::Subscription.new(plan_info[:plan], @customer)) if plan_info[:new_subscription] - elsif item.keys.first == 'reservation' + elsif ['reservation', :reservation].include?(item.keys.first) items.push(reservable_from_hash(item[:reservation], plan_info)) end end @@ -66,8 +66,8 @@ class CartService def plan(cart_items) new_plan_being_bought = false - plan = if cart_items[:items].any? { |item| item.keys.first == 'subscription' } - index = cart_items[:items].index { |item| item.keys.first == 'subscription' } + plan = if cart_items[:items].any? { |item| ['subscription', :subscription].include?(item.keys.first) } + index = cart_items[:items].index { |item| ['subscription', :subscription].include?(item.keys.first) } if cart_items[:items][index][:subscription][:plan_id] new_plan_being_bought = true Plan.find(cart_items[:items][index][:subscription][:plan_id]) diff --git a/lib/pay_zen/pci/charge.rb b/lib/pay_zen/pci/charge.rb new file mode 100644 index 000000000..5e20573da --- /dev/null +++ b/lib/pay_zen/pci/charge.rb @@ -0,0 +1,37 @@ +# frozen_string_literal: true + +require 'pay_zen/client' + +# PayZen PCI endpoints +module PayZen::PCI; end + +# PCI/Charge/* endpoints of the PayZen REST API +class PayZen::PCI::Charge < 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/en-EN/rest/V4.0/api/playground/PCI/Charge/CreatePayment/ + ## + def create_payment(amount: 0, + currency: Setting.get('payzen_currency'), + order_id: nil, + form_action: 'PAYMENT', + contrib: "fab-manager #{Version.current}", + customer: nil, + device: nil, + payment_forms: nil) + post('/PCI/Charge/CreatePayment', + amount: amount, + currency: currency, + orderId: order_id, + formAction: form_action, + contrib: contrib, + customer: customer, + device: device, + paymentForms: payment_forms) + end + +end + diff --git a/lib/tasks/fablab/chain.rake b/lib/tasks/fablab/chain.rake index 72ae9955c..43bc22bd4 100644 --- a/lib/tasks/fablab/chain.rake +++ b/lib/tasks/fablab/chain.rake @@ -71,7 +71,7 @@ namespace :fablab do end def chain_history_values - HistoryValue.order(:id).all.each(&:chain_record) + HistoryValue.order(:created_at).all.each(&:chain_record) end desc 'assign all footprints to existing PaymentSchedule records' diff --git a/test/fixtures/history_values.yml b/test/fixtures/history_values.yml index f3567ac27..bb58ffe2e 100644 --- a/test/fixtures/history_values.yml +++ b/test/fixtures/history_values.yml @@ -756,55 +756,55 @@ history_value_78: history_value_79: id: 79 setting_id: 79 - value: '' + value: 69876357 created_at: '2020-04-15 14:38:40.000421' updated_at: '2021-05-31 15:00:37.503801' - footprint: 6bce8d94ae14b39a89f220d8c73f4daf44a795888ca011bf1a11da3a35eaf171 + footprint: 55d2a2354a3a12ed7bba0a3854f9f4559aed546c81040fbe35cd94d80b8bb811 invoicing_profile_id: 1 history_value_80: id: 80 setting_id: 80 - value: '' + value: testpassword_DEMOPRIVATEKEY23G4475zXZQ2UA5x7M created_at: '2020-04-15 14:38:40.000421' updated_at: '2021-05-31 15:00:37.518625' - footprint: b1b314da439b81e95281909992e79f4d118e08633607010f777d6ef40ad28d8e + footprint: a0f076a64824b3473c60dacd18892da21d14830dedd7bb959c442faa59d1313d invoicing_profile_id: 1 history_value_81: id: 81 setting_id: 81 - value: '' + value: https://api.payzen.eu created_at: '2020-04-15 14:38:40.000421' updated_at: '2021-05-31 15:00:37.535175' - footprint: 57dac418c605a111ab2a9c253704f9d9b182dfbb74068fc6596f3d20c611fb42 + footprint: 2d0d3090a7976a5b41fb4ccaa1e87e8ee61116813691280f3a2f19c574f0bea5 invoicing_profile_id: 1 history_value_82: id: 82 setting_id: 82 - value: '' + value: 69876357:testpublickey_DEMOPUBLICKEY95me92597fd28tGD4r5 created_at: '2020-04-15 14:38:40.000421' updated_at: '2021-05-31 15:00:37.551956' - footprint: 68a9d660f03f0e1408ee406ccf7df781147f3a7a2e5ac158a8d3ee4aaaca655a + footprint: 9fbe6418520003f7dc9e37c5a9a3256adc4963d7a5d2402ded929d4154d17a81 invoicing_profile_id: 1 history_value_83: id: 83 setting_id: 83 - value: '' + value: 83daf5e7b80d990f037407bab78dff9904aaf3c19 created_at: '2020-04-15 14:38:40.000421' updated_at: '2021-05-31 15:00:37.568426' - footprint: 5a984dc2686c93d0380e04c017ce8ec05d18cb3c4c0fec8d4154605986078903 + footprint: b0d7f03eff084c39cb87dbf01e3d05f217203d776b0e01ce002a55b426abfe23 invoicing_profile_id: 1 history_value_84: id: 84 setting_id: 84 - value: '' + value: EUR created_at: '2020-04-15 14:38:40.000421' updated_at: '2021-05-31 15:00:37.593645' - footprint: bdf31d00b03e511c0a2a1bf18f628462655f32834b6ee6822d1a883055a191f4 + footprint: 3545edef1b4367afd0d36742e30e35832e56c32882f3876dba76fc69211950db invoicing_profile_id: 1 history_value_85: diff --git a/test/integration/payzen_test.rb b/test/integration/payzen_test.rb new file mode 100644 index 000000000..8aae49f4d --- /dev/null +++ b/test/integration/payzen_test.rb @@ -0,0 +1,144 @@ +# frozen_string_literal: true + +require 'test_helper' + +class PayzenTest < ActionDispatch::IntegrationTest + def setup + @user = User.members.first + login_as(@user, scope: :user) + + Setting.set('payment_gateway', 'payzen') + end + + test 'create payment with payzen' do + training = Training.first + availability = training.availabilities.first + plan = Plan.find_by(group_id: @user.group.id, type: 'Plan') + + VCR.use_cassette('create_payzen_payment_token_success') do + post '/api/payzen/create_payment', + params: { + customer_id: @user.id, + cart_items: { + items: [ + { + reservation: { + reservable_id: training.id, + reservable_type: training.class.name, + slots_attributes: [ + { + start_at: availability.start_at.to_s(:iso8601), + end_at: availability.end_at.to_s(:iso8601), + availability_id: availability.id + } + ] + } + }, + { + subscription: { + plan_id: plan.id + } + } + ] + } + }.to_json, headers: default_headers + end + + # Check the response + assert_equal 200, response.status + payment = json_response(response.body) + assert_not_nil payment[:formToken] + assert_not_nil payment[:orderId] + end + + + test 'confirm payment with payzen' do + require 'pay_zen/helper' + require 'pay_zen/pci/charge' + + training = Training.first + availability = training.availabilities.first + plan = Plan.find_by(group_id: @user.group.id, type: 'Plan') + + reservations_count = Reservation.count + availabilities_count = Availability.count + invoices_count = Invoice.count + slots_count = Slot.count + + + cart_items = { + items: [ + { + reservation: { + reservable_id: training.id, + reservable_type: training.class.name, + slots_attributes: [ + { + start_at: availability.start_at.to_s(:iso8601), + end_at: availability.end_at.to_s(:iso8601), + availability_id: availability.id + } + ] + } + }, + { + subscription: { + plan_id: plan.id + } + } + ] + } + + + cs = CartService.new(@user) + cart = cs.from_hash(cart_items) + amount = cart.total[:total] + id = PayZen::Helper.generate_ref(cart_items, @user.id) + + VCR.use_cassette('confirm_payzen_payment_success') do + client = PayZen::PCI::Charge.new + result = client.create_payment(amount: amount, + order_id: id, + customer: PayZen::Helper.generate_customer(@user.id, @user.id, cart_items), + device: { + deviceType: 'BROWSER', + acceptHeader: 'text/html', + userAgent: 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101', + ip: '69.89.31.226', + javaEnabled: true, + language: 'fr-FR', + colorDepth: '32', + screenHeight: 768, + screenWidth: 1258, + timeZoneOffset: -120 + }, + payment_forms: [{ + paymentMethodType: 'CARD', + pan: '4970100000000055', + expiryMonth: 12, + expiryYear: DateTime.current.strftime('%y'), + securityCode: 123 + }]) + + assert_equal 'PAID', result['answer']['orderStatus'], 'Order is not PAID, something went wrong with PayZen' + assert_equal id, result['answer']['orderDetails']['orderId'], 'Order ID does not match, something went wrong with PayZen' + + post '/api/payzen/confirm_payment', + params: { + cart_items: cart_items, + order_id: result['answer']['orderDetails']['orderId'] + }.to_json, headers: default_headers + end + + # Check the response + assert_equal 201, response.status + invoice = json_response(response.body) + assert_equal Invoice.last.id, invoice[:id] + assert_equal amount / 100.0, invoice[:total] + + assert_equal reservations_count + 1, Reservation.count + assert_equal invoices_count + 1, Invoice.count + assert_equal slots_count + 1, Slot.count + assert_equal availabilities_count, Availability.count + end +end