Add transaction origin in descriptiopn

This commit is contained in:
Gaetan Craig-Riou
2026-03-06 13:13:54 +11:00
parent e7a2b7ea48
commit 6a99d2a3c8
6 changed files with 10 additions and 34 deletions

View File

@@ -6,13 +6,11 @@ module Api
def create
authorize! :create, CustomerAccountTransaction
# We only allow using the api customer credit payment method
default_params = {
currency: CurrentConfig.get(:currency), payment_method_id:, created_by: current_api_user
currency: CurrentConfig.get(:currency), created_by: current_api_user
}
transaction = CustomerAccountTransaction.new(
default_params.merge(customer_account_transaction_params)
)
parameters = default_params.merge(customer_account_transaction_params).merge(description: )
transaction = CustomerAccountTransaction.new(parameters)
if transaction.save
render json: Api::V1::CustomerAccountTransactionSerializer.new(transaction),
@@ -28,8 +26,8 @@ module Api
params.require(:customer_account_transaction).permit(:customer_id, :amount, :description)
end
def payment_method_id
Spree::PaymentMethod.api_customer_credit&.id
def description
I18n.t(".api_customer_credit", description: params[:description])
end
end
end

View File

@@ -11,7 +11,6 @@ class CustomerAccountTransactionSchema < JsonApiSchema
customer_id: { type: :integer, example: 10 },
amount: { type: :decimal, example: 10.50 },
currency: { type: :string, example: "AUD" },
payment_method_id: { type: :integer, example: 100 },
description: { type: :string, nullable: true, example: "Payment processed by POS" },
balance: { type: :decimal, example: 10.50 },
}
@@ -22,10 +21,10 @@ class CustomerAccountTransactionSchema < JsonApiSchema
end
def self.writable_attributes
attributes.except(:id, :balance, :payment_method_id, :currency)
attributes.except(:id, :balance, :currency)
end
def self.relationships
[:customer, :payment_method]
[:customer]
end
end

View File

@@ -3,7 +3,7 @@
module Api
module V1
class CustomerAccountTransactionSerializer < Api::V1::BaseSerializer
attributes :id, :customer_id, :payment_method_id, :amount, :currency, :description, :balance
attributes :id, :customer_id, :amount, :currency, :description, :balance
end
end
end

View File

@@ -5195,6 +5195,7 @@ en:
invisible_captcha:
sentence_for_humans: "Please leave empty"
timestamp_error_message: "Please try again after 5 seconds."
api_customer_credit: "API credit: %{description}"
api_payment_method:
name: API customer credit
description: Used to credit customer via customer account transactions endpoint

View File

@@ -4,7 +4,6 @@ require "swagger_helper"
RSpec.describe "CustomerAccountTransactions", swagger_doc: "v1.yaml", feature: :api_v1 do
let!(:enterprise) { create(:enterprise) }
let!(:payment_method) { create(:api_customer_credit_payment_method) }
let(:customer) { create(:customer) }
before do
@@ -36,10 +35,9 @@ RSpec.describe "CustomerAccountTransactions", swagger_doc: "v1.yaml", feature: :
run_test! do
expect(json_response[:data][:attributes]).to include(
customer_id: customer.id,
payment_method_id: payment_method.id,
amount: "10.25",
currency: "AUD",
description: "Payment processed by POS",
description: "API credit: Payment processed by POS",
balance: "10.25",
)

View File

@@ -310,9 +310,6 @@ components:
currency:
type: string
example: AUD
payment_method_id:
type: integer
example: 100
description:
type: string
nullable: true
@@ -325,7 +322,6 @@ components:
- customer_id
- amount
- currency
- payment_method_id
- description
- balance
relationships:
@@ -347,22 +343,6 @@ components:
properties:
related:
type: string
payment_method:
type: object
properties:
data:
type: object
properties:
id:
type: string
type:
type: string
example: payment_method
links:
type: object
properties:
related:
type: string
meta:
type: object
links: