From e10fd0b02099b95d9ca6819923ef66878c145697 Mon Sep 17 00:00:00 2001 From: Gaetan Craig-Riou Date: Tue, 3 Feb 2026 13:44:48 +1100 Subject: [PATCH] Add api payment method It will be used to credit customer via the customer account transaction API --- config/locales/en.yml | 3 ++ ..._add_api_customer_credit_payment_method.rb | 33 +++++++++++++++++++ db/schema.rb | 2 +- .../sample_data/payment_method_factory.rb | 12 +++++++ 4 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20260129230557_add_api_customer_credit_payment_method.rb diff --git a/config/locales/en.yml b/config/locales/en.yml index 0c0c376a7e..aa59623a5e 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -5170,3 +5170,6 @@ en: invisible_captcha: sentence_for_humans: "Please leave empty" timestamp_error_message: "Please try again after 5 seconds." + api_payment_method: + name: API customer credit + description: Used to credit customer via customer account transactions endpoint diff --git a/db/migrate/20260129230557_add_api_customer_credit_payment_method.rb b/db/migrate/20260129230557_add_api_customer_credit_payment_method.rb new file mode 100644 index 0000000000..063784b815 --- /dev/null +++ b/db/migrate/20260129230557_add_api_customer_credit_payment_method.rb @@ -0,0 +1,33 @@ +# frozen_string_literal: true + +class AddApiCustomerCreditPaymentMethod < ActiveRecord::Migration[7.1] + def up + # Create payment method + execute(<<-SQL.squish + INSERT INTO spree_payment_methods ( type, name, description, environment, active, display_on, created_at, updated_at) + VALUES ('Spree::PaymentMethod::Check', 'api_payment_method.name', 'api_payment_method.description', '#{Rails.env}', true, 'back_end', NOW(), NOW()) + SQL + ) + + # Link to existing hub + execute(<<-SQL.squish + INSERT INTO distributors_payment_methods (distributor_id, payment_method_id, created_at, updated_at) + SELECT id, (SELECT id FROM spree_payment_methods WHERE name = 'api_payment_method.name') as payment_method_id, NOW() as created_at, NOW() as updated_at + FROM enterprises WHERE sells != 'none' + SQL + ) + end + + def down + execute(<<-SQL.squish + DELETE FROM distributors_payment_methods + WHERE payment_method_id = (SELECT id FROM spree_payment_methods WHERE name = 'api_payment_method.name') + SQL + ) + + execute(<<-SQL.squish + DELETE FROM spree_payment_methods WHERE name = 'api_payment_method.name' + SQL + ) + end +end diff --git a/db/schema.rb b/db/schema.rb index be1f5da401..7169deb667 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.1].define(version: 2026_01_19_024509) do +ActiveRecord::Schema[7.1].define(version: 2026_01_29_230557) do # These are extensions that must be enabled in order to support this database enable_extension "pg_stat_statements" enable_extension "plpgsql" diff --git a/lib/tasks/sample_data/payment_method_factory.rb b/lib/tasks/sample_data/payment_method_factory.rb index 30644cd289..8e8e08eec6 100644 --- a/lib/tasks/sample_data/payment_method_factory.rb +++ b/lib/tasks/sample_data/payment_method_factory.rb @@ -14,6 +14,7 @@ module SampleData distributors.each do |enterprise| create_payment_methods(enterprise) end + create_api_method(distributors) end private @@ -45,5 +46,16 @@ module SampleData payment_method.calculator = calculator payment_method.save! end + + def create_api_method(enterprises) + log "Creating api payment method" + Spree::PaymentMethod::Check.create!( + name: "api_payment_method.name", + description: "api_payment_method.description", + environment: Rails.env, + distributors: enterprises, + display_on: "back_end" + ) + end end end