more acct requirements

This commit is contained in:
24c02 2025-12-18 15:08:50 -05:00
parent 97a1267f95
commit e5132db647
7 changed files with 68 additions and 48 deletions

View file

@ -101,9 +101,16 @@ class Letter::BatchesController < BaseBatchesController
redirect_to process_letter_batch_path(@batch), alert: "Please select a valid payment account when using indicia"
return
end
end
hcb_payment_account = current_user.hcb_payment_accounts.find_by(id: letter_batch_params[:hcb_payment_account_id])
hcb_payment_account = current_user.hcb_payment_accounts.find_by(id: letter_batch_params[:hcb_payment_account_id])
if hcb_payment_account.nil?
redirect_to process_letter_batch_path(@batch), alert: "Please select an HCB payment account to purchase indicia"
return
end
else
hcb_payment_account = nil
end
begin
@batch.process!(

View file

@ -30,6 +30,7 @@ class Letter::InstantQueuesController < Letter::QueuesController
:template,
:postage_type,
:usps_payment_account_id,
:hcb_payment_account_id,
:include_qr_code,
:letter_mailing_date,
tags: [],

View file

@ -187,22 +187,16 @@ class LettersController < ApplicationController
hcb_payment_account = current_user.hcb_payment_accounts.find_by(id: params[:hcb_payment_account_id])
if hcb_payment_account.present?
service = HCB::IndiciumPurchaseService.new(indicium: indicium, hcb_payment_account: hcb_payment_account)
if service.call
redirect_to @letter, notice: "Indicia purchased successfully (charged to #{hcb_payment_account.organization_name})."
else
redirect_to @letter, alert: service.errors.join(", ")
end
elsif current_user.can_use_indicia?
begin
indicium.buy!
redirect_to @letter, notice: "Indicia purchased successfully."
rescue => e
redirect_to @letter, alert: "Failed to purchase indicia: #{e.message}"
end
else
if hcb_payment_account.blank?
redirect_to @letter, alert: "You must select an HCB payment account to purchase indicia."
return
end
service = HCB::IndiciumPurchaseService.new(indicium: indicium, hcb_payment_account: hcb_payment_account)
if service.call
redirect_to @letter, notice: "Indicia purchased successfully (charged to #{hcb_payment_account.organization_name})."
else
redirect_to @letter, alert: service.errors.join(", ")
end
end

View file

@ -155,30 +155,17 @@ class Letter::Batch < Batch
end
# Purchase indicia for all letters in the batch using a single payment token
# If hcb_payment_account is provided, creates a single disbursement for the whole batch
def purchase_batch_indicia(usps_payment_account, hcb_payment_account: nil)
if hcb_payment_account.present?
service = HCB::BatchPurchaseService.new(
batch: self,
hcb_payment_account: hcb_payment_account,
usps_payment_account: usps_payment_account,
)
unless service.call
raise StandardError, service.errors.join(", ")
end
else
payment_token = usps_payment_account.create_payment_token
# Requires hcb_payment_account to create a disbursement for the whole batch
def purchase_batch_indicia(usps_payment_account, hcb_payment_account:)
raise ArgumentError, "HCB payment account is required to purchase indicia" if hcb_payment_account.nil?
letters.includes(:address).each do |letter|
next unless letter.postage_type == "indicia" && letter.usps_indicium.nil?
indicium = USPS::Indicium.new(
letter: letter,
payment_account: usps_payment_account,
mailing_date: letter_mailing_date,
)
indicium.buy!(payment_token)
end
service = HCB::BatchPurchaseService.new(
batch: self,
hcb_payment_account: hcb_payment_account,
usps_payment_account: usps_payment_account,
)
unless service.call
raise StandardError, service.errors.join(", ")
end
end

View file

@ -48,6 +48,7 @@ class Letter::InstantQueue < Letter::Queue
validates :template, presence: true
validates :postage_type, presence: true, inclusion: { in: %w[indicia stamps international_origin] }
validates :usps_payment_account_id, presence: true, if: :indicia?
validates :hcb_payment_account_id, presence: true, if: :indicia?
validates :letter_mailing_date, presence: true, if: :indicia?
# Associations
@ -96,14 +97,10 @@ class Letter::InstantQueue < Letter::Queue
)
Rails.logger.info("Created indicium #{indicium.id} for letter #{letter.id}")
if hcb_payment_account.present?
Rails.logger.info("Using HCB payment account #{hcb_payment_account.id} for letter #{letter.id}")
service = HCB::IndiciumPurchaseService.new(indicium: indicium, hcb_payment_account: hcb_payment_account)
unless service.call
raise "HCB payment failed: #{service.errors.join(', ')}"
end
else
indicium.buy!
Rails.logger.info("Using HCB payment account #{hcb_payment_account.id} for letter #{letter.id}")
service = HCB::IndiciumPurchaseService.new(indicium: indicium, hcb_payment_account: hcb_payment_account)
unless service.call
raise "HCB payment failed: #{service.errors.join(', ')}"
end
Rails.logger.info("Successfully bought indicium for letter #{letter.id}")

View file

@ -113,6 +113,21 @@
{ selected: USPS::PaymentAccount.first&.id },
{ class: "form-select" } %>
</div>
<div class="form-group hcb-payment-account-field" style="display: none;">
<%= f.label :hcb_payment_account_id, "Pay with HCB Organization", class: "form-label" %>
<% if current_user.hcb_payment_accounts.any? %>
<%= f.collection_select :hcb_payment_account_id,
current_user.hcb_payment_accounts,
:id,
:organization_name,
{ prompt: "Select an organization" },
{ class: "form-select", required: true } %>
<% else %>
<p class="text-sm text-gray-600">
<%= link_to "Connect your HCB account", new_back_office_hcb_oauth_connection_path %> to purchase indicia.
</p>
<% end %>
</div>
<div class="form-group">
<%= f.label :template_cycle, "Template Cycle", class: "form-label" %>
<%= f.hidden_field :template_cycle, id: "selected_template", value: params[:template] %>
@ -143,10 +158,12 @@
const usPostageSelect = document.querySelector('#batch_us_postage_type');
const intlPostageSelect = document.querySelector('#batch_intl_postage_type');
const paymentAccountField = document.querySelector('.payment-account-field');
const hcbPaymentAccountField = document.querySelector('.hcb-payment-account-field');
function updatePaymentAccountVisibility() {
const showPaymentAccount = usPostageSelect.value === 'indicia' || intlPostageSelect.value === 'indicia';
paymentAccountField.style.display = showPaymentAccount ? 'block' : 'none';
hcbPaymentAccountField.style.display = showPaymentAccount ? 'block' : 'none';
}
function updatePostageCosts() {

View file

@ -88,6 +88,21 @@
{ selected: letter_queue.usps_payment_account_id },
{ class: "form-control" } %>
</div>
<div class="form-group" id="hcbPaymentAccountField" style="display: <%= letter_queue.postage_type == 'indicia' ? 'block' : 'none' %>">
<%= form.label :hcb_payment_account_id, "Pay with HCB Organization", class: "form-label" %>
<% if current_user.hcb_payment_accounts.any? %>
<%= form.collection_select :hcb_payment_account_id,
current_user.hcb_payment_accounts,
:id,
:organization_name,
{ selected: letter_queue.hcb_payment_account_id, prompt: "Select an organization" },
{ class: "form-control", required: true } %>
<% else %>
<p class="text-sm text-gray-600">
<%= link_to "Connect your HCB account", new_back_office_hcb_oauth_connection_path %> to use indicia.
</p>
<% end %>
</div>
<div class="form-group">
<%= form.label :include_qr_code, "Include QR Code", class: "form-label" %>
<%= form.check_box :include_qr_code, class: "form-checkbox", checked: letter_queue.include_qr_code.nil? ? true : letter_queue.include_qr_code %>
@ -117,7 +132,9 @@
function togglePaymentAccount(select) {
const paymentAccountField = document.getElementById('paymentAccountField');
const hcbPaymentAccountField = document.getElementById('hcbPaymentAccountField');
paymentAccountField.style.display = select.value === "indicia" ? "block" : "none";
hcbPaymentAccountField.style.display = select.value === "indicia" ? "block" : "none";
}
toggleQueueType(document.getElementById('letter_queue_type'));