Add Payments::WebhookPayload to manage payload data

It includes test data so any change in the payload should not affect
the test webhook enpoint functionality
This commit is contained in:
Gaetan Craig-Riou
2025-12-03 17:06:45 +11:00
parent f6a7225c47
commit d7505bcef4
4 changed files with 183 additions and 52 deletions

View File

@@ -25,40 +25,9 @@ class WebhookEndpointsController < BaseController
redirect_to redirect_path
end
def test # rubocop:disable Metrics/MethodLength
def test
at = Time.zone.now
test_payload = {
payment: {
updated_at: at,
amount: 0.00,
state: "completed"
},
enterprise: {
abn: "65797115831",
acn: "",
name: "TEST Enterprise",
address: {
address1: "1 testing street",
address2: "",
city: "TestCity",
zipcode: "1234"
}
},
order: {
total: 0.00,
currency: "AUD",
line_items: [
{
quantity: 1,
price: 20.00,
tax_category_name: "VAT",
product_name: "Test product",
name_to_display: "",
unit_to_display: "1kg"
}
]
}
}
test_payload = Payments::WebhookPayload.test_data.to_hash
WebhookDeliveryJob.perform_later(@webhook_endpoint.url, "payment.completed", test_payload, at:)

View File

@@ -0,0 +1,84 @@
# frozen_string_literal: true
module Payments
class WebhookPayload
def initialize(payment:, order:, enterprise:)
@payment = payment
@order = order
@enterprise = enterprise
end
def to_hash
{
payment: @payment.slice(:updated_at, :amount, :state),
enterprise: @enterprise.slice(:abn, :acn, :name)
.merge(address: @enterprise.address.slice(:address1, :address2, :city, :zipcode)),
order: @order.slice(:total, :currency).merge(line_items: line_items)
}.with_indifferent_access
end
def self.test_data
new(payment: test_payment, order: test_order, enterprise: test_enterprise)
end
def self.test_payment
{
updated_at: Time.zone.now,
amount: 0.00,
state: "completed"
}
end
def self.test_order
order = Spree::Order.new(
total: 0.00,
currency: "AUD",
)
tax_category = Spree::TaxCategory.new(name: "VAT")
product = Spree::Product.new(name: "Test product")
Spree::Variant.new(product:, display_name: "")
order.line_items << Spree::LineItem.new(
quantity: 1,
price: 20.00,
tax_category:,
product:,
unit_presentation: "1kg"
)
order
end
def self.test_enterprise
enterprise = Enterprise.new(
abn: "65797115831",
acn: "",
name: "TEST Enterprise",
)
enterprise.address = Spree::Address.new(
address1: "1 testing street",
address2: "",
city: "TestCity",
zipcode: "1234"
)
enterprise
end
private_class_method :test_payment, :test_order, :test_enterprise
private
def line_items
@order.line_items.map do |li|
li.slice(:quantity, :price)
.merge(
tax_category_name: li.tax_category&.name,
product_name: li.product.name,
name_to_display: li.display_name,
unit_to_display: li.unit_presentation
)
end
end
end
end

View File

@@ -7,26 +7,9 @@ module Payments
class WebhookService
def self.create_webhook_job(payment:, event:, at:)
order = payment.order
enterprise = order.distributor
payload = WebhookPayload.new(payment:, order:, enterprise: order.distributor).to_hash
line_items = order.line_items.map do |li|
li.slice(:quantity, :price)
.merge(
tax_category_name: li.tax_category&.name,
product_name: li.product.name,
name_to_display: li.display_name,
unit_to_display: li.unit_presentation
)
end
payload = {
payment: payment.slice(:updated_at, :amount, :state),
enterprise: enterprise.slice(:abn, :acn, :name)
.merge(address: enterprise.address.slice(:address1, :address2, :city, :zipcode)),
order: order.slice(:total, :currency).merge(line_items: line_items)
}
coordinator = order.order_cycle.coordinator
coordinator = payment.order.order_cycle.coordinator
webhook_urls(coordinator).each do |url|
WebhookDeliveryJob.perform_later(url, event, payload, at:)
end

View File

@@ -0,0 +1,95 @@
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Payments::WebhookPayload do
describe "#to_hash" do
let(:order) { create(:completed_order_with_totals, order_cycle: ) }
let(:order_cycle) { create(:simple_order_cycle) }
let(:payment) { create(:payment, :completed, amount: order.total, order:) }
let(:tax_category) { create(:tax_category) }
subject { described_class.new(payment:, order:, enterprise: order.distributor) }
it "returns a formated hash" do
order.line_items.update_all(tax_category_id: tax_category.id)
enterprise = order.distributor
line_items = order.line_items.map do |li|
{
quantity: li.quantity,
price: li.price,
tax_category_name: li.tax_category&.name,
product_name: li.product.name,
name_to_display: li.display_name,
unit_to_display: li.unit_presentation
}
end
payload = {
payment: {
updated_at: payment.updated_at,
amount: payment.amount,
state: payment.state
},
enterprise: {
abn: enterprise.abn,
acn: enterprise.acn,
name: enterprise.name,
address: {
address1: enterprise.address.address1,
address2: enterprise.address.address2,
city: enterprise.address.city,
zipcode: enterprise.address.zipcode
}
},
order: {
total: order.total,
currency: order.currency,
line_items: line_items
}
}.with_indifferent_access
expect(subject.to_hash).to eq(payload)
end
end
describe ".test_data" do
it "returns a hash with test data" do
test_payload = {
payment: {
updated_at: kind_of(Time),
amount: 0.00,
state: "completed"
},
enterprise: {
abn: "65797115831",
acn: "",
name: "TEST Enterprise",
address: {
address1: "1 testing street",
address2: "",
city: "TestCity",
zipcode: "1234"
}
},
order: {
total: 0.00,
currency: "AUD",
line_items: [
{
quantity: 1,
price: 20.00.to_d,
tax_category_name: "VAT",
product_name: "Test product",
name_to_display: nil,
unit_to_display: "1kg"
}
]
}
}.with_indifferent_access
expect(described_class.test_data.to_hash).to match(test_payload)
end
end
end