InsertTicket now fires transaction events

This commit is contained in:
Eric Schultz 2021-05-13 13:50:55 -05:00 committed by Eric Schultz
parent a5ad78b824
commit 225232f62f
3 changed files with 55 additions and 6 deletions

View file

@ -52,6 +52,7 @@ class TicketPurchase < ApplicationRecord
def publish_created def publish_created
Houdini.event_publisher.announce(:ticket_purchase_created, to_event('ticket_purchase.created', :event, :nonprofit, :supporter, :trx, :event_discount, :ticket_to_legacy_tickets).attributes!) Houdini.event_publisher.announce(:ticket_purchase_created, to_event('ticket_purchase.created', :event, :nonprofit, :supporter, :trx, :event_discount, :ticket_to_legacy_tickets).attributes!)
Houdini.event_publisher.announce(:trx_assignment_created, to_event('trx_assignment.created', :event, :nonprofit, :supporter, :trx, :event_discount, :ticket_to_legacy_tickets).attributes!)
end end
private private

View file

@ -49,7 +49,8 @@ module InsertTickets
result = {} result = {}
trx = entities[:supporter_id].transactions.create(amount: gross_amount) trx = entities[:supporter_id].transactions.build(amount: gross_amount)
subtrx = nil
if gross_amount > 0 if gross_amount > 0
# Create offsite payment for tickets # Create offsite payment for tickets
if data[:kind] == 'offsite' if data[:kind] == 'offsite'
@ -62,6 +63,14 @@ module InsertTickets
# create payment and offsite payment # create payment and offsite payment
result['payment'] = create_payment(entities, gross_amount) result['payment'] = create_payment(entities, gross_amount)
result['offsite_payment'] = create_offsite_payment(entities, gross_amount, data, result['payment']) result['offsite_payment'] = create_offsite_payment(entities, gross_amount, data, result['payment'])
subtrx = trx.build_subtransaction(
subtransactable: OfflineTransaction.new(amount: gross_amount),
subtransaction_payments:[
SubtransactionPayment.new(
paymentable: OfflineTransactionCharge.new(payment: Payment.find(result['payment']['id'])))
],
created: data['date']
);
# Create charge for tickets # Create charge for tickets
elsif data['kind'] == 'charge' || !data['kind'] elsif data['kind'] == 'charge' || !data['kind']
@ -85,13 +94,22 @@ module InsertTickets
)) ))
if result['charge']['status'] == 'failed' if result['charge']['status'] == 'failed'
raise ChargeError, result['charge']['failure_message'] raise ChargeError, result['charge']['failure_message']
else
subtrx = trx.build_subtransaction(
subtransactable: StripeTransaction.new(amount: gross_amount),
subtransaction_payments:[
SubtransactionPayment.new(
paymentable: StripeCharge.new(payment: Payment.find(result['payment']['id'])))
],
created: Time.current
);
end end
else else
raise ParamValidation::ValidationError.new("Ticket costs money but you didn't pay.", key: :kind) raise ParamValidation::ValidationError.new("Ticket costs money but you didn't pay.", key: :kind)
end end
end end
ticket_purchase = trx.ticket_purchases.create(event: entities[:event_id]) ticket_purchase = trx.ticket_purchases.build(event: entities[:event_id])
# Generate the bid ids # Generate the bid ids
data['tickets'] = generate_bid_ids(entities[:event_id].id, tl_entities) data['tickets'] = generate_bid_ids(entities[:event_id].id, tl_entities)
@ -99,7 +117,7 @@ module InsertTickets
result['tickets'].each do |legacy_ticket| result['tickets'].each do |legacy_ticket|
legacy_ticket.quantity.times do legacy_ticket.quantity.times do
ticket_purchase.ticket_to_legacy_tickets.create(ticket: legacy_ticket) ticket_purchase.ticket_to_legacy_tickets.build(ticket: legacy_ticket)
end end
end end
@ -108,7 +126,13 @@ module InsertTickets
ticket_ids = result['tickets'].map(&:id) ticket_ids = result['tickets'].map(&:id)
charge_id = result['charge'] ? result['charge'].id : nil charge_id = result['charge'] ? result['charge'].id : nil
trx.save!
ticket_purchase.save!
if (subtrx)
subtrx.save!
subtrx.subtransaction_payments.each{|stp| stp.publish_created}
subtrx.publish_created
end
ticket_purchase.ticket_to_legacy_tickets.each{|i| i.publish_created} ticket_purchase.ticket_to_legacy_tickets.each{|i| i.publish_created}
ticket_purchase.publish_created ticket_purchase.publish_created
trx.publish_created trx.publish_created

View file

@ -5,6 +5,9 @@
require 'rails_helper' require 'rails_helper'
describe InsertTickets do describe InsertTickets do
before do
Houdini.payment_providers.stripe.connect = true
end
include_context :shared_rd_donation_value_context include_context :shared_rd_donation_value_context
# @param [Object] data # @param [Object] data
@ -289,8 +292,12 @@ describe InsertTickets do
expect(Houdini.event_publisher).to receive(:announce).with(:ticket_level_created, any_args).ordered expect(Houdini.event_publisher).to receive(:announce).with(:ticket_level_created, any_args).ordered
expect(Houdini.event_publisher).to receive(:announce).with(:supporter_created, anything) expect(Houdini.event_publisher).to receive(:announce).with(:supporter_created, anything)
expect(Houdini.event_publisher).to receive(:announce).with(:supporter_address_created, anything) expect(Houdini.event_publisher).to receive(:announce).with(:supporter_address_created, anything)
expect(Houdini.event_publisher).to receive(:announce).with(:offline_transaction_charge_created, any_args).ordered
expect(Houdini.event_publisher).to receive(:announce).with(:payment_created, any_args).ordered
expect(Houdini.event_publisher).to receive(:announce).with(:offline_transaction_created, any_args).ordered
expect(Houdini.event_publisher).to receive(:announce).with(:ticket_created, anything).once.ordered expect(Houdini.event_publisher).to receive(:announce).with(:ticket_created, anything).once.ordered
expect(Houdini.event_publisher).to receive(:announce).with(:ticket_purchase_created, any_args).ordered expect(Houdini.event_publisher).to receive(:announce).with(:ticket_purchase_created, any_args).ordered
expect(Houdini.event_publisher).to receive(:announce).with(:trx_assignment_created, any_args).ordered
expect(Houdini.event_publisher).to receive(:announce).with(:transaction_created, any_args).ordered expect(Houdini.event_publisher).to receive(:announce).with(:transaction_created, any_args).ordered
result = InsertTickets.create(tickets: [{ quantity: 1, ticket_level_id: ticket_level.id }], nonprofit_id: nonprofit.id, supporter_id: supporter.id, token: source_token.token, event_id: event.id, kind: 'offsite', offsite_payment: { kind: 'check', check_number: 'fake_checknumber' }, current_user: user) result = InsertTickets.create(tickets: [{ quantity: 1, ticket_level_id: ticket_level.id }], nonprofit_id: nonprofit.id, supporter_id: supporter.id, token: source_token.token, event_id: event.id, kind: 'offsite', offsite_payment: { kind: 'check', check_number: 'fake_checknumber' }, current_user: user)
@ -373,12 +380,29 @@ describe InsertTickets do
end end
def success(other_elements = {}) def success(other_elements = {})
expect(Houdini.event_publisher).to receive(:announce).with(:supporter_created, anything)
expect(Houdini.event_publisher).to receive(:announce).with(:supporter_address_created, anything)
nonprofit.stripe_account_id = Stripe::Account.create['id'] nonprofit.stripe_account_id = Stripe::Account.create['id']
nonprofit.save! nonprofit.save!
card.stripe_customer_id = 'some other id' card.stripe_customer_id = 'some other id'
card.save! card.save!
success_expectations success_expectations
expect(Houdini.event_publisher).to receive(:announce).with(:ticket_level_created, any_args).twice
expect(Houdini.event_publisher).to receive(:announce).with(:event_discount_created, any_args)
expect(Houdini.event_publisher).to receive(:announce).with(:stripe_transaction_charge_created, any_args).ordered
expect(Houdini.event_publisher).to receive(:announce).with(:payment_created, any_args).ordered
expect(Houdini.event_publisher).to receive(:announce).with(:stripe_transaction_created, any_args).ordered
## there are three tickets so have them be announced three times
expect(Houdini.event_publisher).to receive(:announce).with(:ticket_created, anything).ordered
expect(Houdini.event_publisher).to receive(:announce).with(:ticket_created, anything).ordered
expect(Houdini.event_publisher).to receive(:announce).with(:ticket_created, anything).ordered
expect(Houdini.event_publisher).to receive(:announce).with(:ticket_purchase_created, any_args).ordered
expect(Houdini.event_publisher).to receive(:announce).with(:trx_assignment_created, any_args).ordered
expect(Houdini.event_publisher).to receive(:announce).with(:transaction_created, any_args).ordered
expect(InsertCharge).to receive(:with_stripe).with( expect(InsertCharge).to receive(:with_stripe).with(
kind: 'Ticket', kind: 'Ticket',
towards: event.name, towards: event.name,