Skip to content

Commit

Permalink
Merge pull request #2 from rauversion/event-payments
Browse files Browse the repository at this point in the history
Event payments
  • Loading branch information
michelson committed Aug 24, 2023
2 parents 308e826 + d0d7bad commit 1990f69
Show file tree
Hide file tree
Showing 19 changed files with 157 additions and 89 deletions.
134 changes: 71 additions & 63 deletions app/controllers/event_purchases_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,12 @@ def create
VirtualPurchasedItem.new({resource: aa, quantity: 1})
end

# handle_stripe_session
handle_tbk_session
case @event.payment_gateway
when "stripe" then handle_stripe_session
when "transbank" then handle_tbk_session
else
raise "No payment gateway available for this event"
end

#########

Expand Down Expand Up @@ -78,46 +82,50 @@ def handle_stripe_session

ActiveRecord::Base.transaction do
@purchase.store_items
@purchase.save

line_items = @purchase.purchased_items.group(:purchased_item_id).count.map do |k, v|
ticket = EventTicket.find(k)
{
"quantity" => v,
"price_data" => {
"unit_amount" => ((ticket.price * v) * 100).to_i,
"currency" => ticket.event.ticket_currency,
"product_data" => {
"name" => ticket.title,
"description" => "#{ticket.short_description} \r for event: #{ticket.event.title}"
if @purchase.save
line_items = @purchase.purchased_items.group(:purchased_item_id).count.map do |k, v|
ticket = EventTicket.find(k)
{
"quantity" => v,
"price_data" => {
"unit_amount" => ((ticket.price * v) * 100).to_i,
"currency" => ticket.event.ticket_currency,
"product_data" => {
"name" => ticket.title,
"description" => "#{ticket.short_description} \r for event: #{ticket.event.title}"
}
}
}
}
end
end

puts line_items


total = line_items.map{|o| o["price_data"]["unit_amount"] }.sum

@session = Stripe::Checkout::Session.create(
payment_method_types: ["card"],
line_items: line_items,
payment_intent_data: {
application_fee_amount: @purchase.calculate_fee(total, @event.ticket_currency).to_i
# "transfer_data"=> %{
# "destination"=> c.uid
# }
},
customer_email: current_user.email,
mode: "payment",
success_url: success_event_event_purchase_url(@event, @purchase), # Replace with your success URL
cancel_url: failure_event_event_purchase_url(@event, @purchase) # Replace with your cancel URL
)

@purchase.update(
checkout_type: "stripe",
checkout_id: @session["id"]
)

@payment_url = @session["url"]

puts line_items

@session = Stripe::Checkout::Session.create(
payment_method_types: ["card"],
line_items: line_items,
payment_intent_data: {
application_fee_amount: fee_amount
# "transfer_data"=> %{
# "destination"=> c.uid
# }
},
customer_email: current_user.email,
mode: "payment",
success_url: success_event_event_purchase_url(@event, @purchase), # Replace with your success URL
cancel_url: failure_event_event_purchase_url(@event, @purchase) # Replace with your cancel URL
)

@purchase.update(
checkout_type: "stripe",
checkout_id: @session["id"]
)

@payment_url = @session["url"]
end
end
end

Expand All @@ -136,36 +144,36 @@ def handle_tbk_session

ActiveRecord::Base.transaction do
@purchase.store_items
@purchase.save

# cancel_url: failure_event_event_purchase_url(@event, @purchase)
if @purchase.save
# cancel_url: failure_event_event_purchase_url(@event, @purchase)
@details = [
{
"amount" => "1000",
"commerce_code" => ::Transbank::Common::IntegrationCommerceCodes::WEBPAY_PLUS_MALL_CHILD1,
"buy_order" => "childBuyOrder1_#{rand(1000)}"
},
{
"amount" => "2000",
"commerce_code" => ::Transbank::Common::IntegrationCommerceCodes::WEBPAY_PLUS_MALL_CHILD2,
"buy_order" => "childBuyOrder2_#{rand(1000)}"
}
]

@details = [
{
"amount" => "1000",
"commerce_code" => ::Transbank::Common::IntegrationCommerceCodes::WEBPAY_PLUS_MALL_CHILD1,
"buy_order" => "childBuyOrder1_#{rand(1000)}"
},
{
"amount" => "2000",
"commerce_code" => ::Transbank::Common::IntegrationCommerceCodes::WEBPAY_PLUS_MALL_CHILD2,
"buy_order" => "childBuyOrder2_#{rand(1000)}"
}
]
@buy_order = "buyOrder_#{rand(1000)}"
@session_id = "sessionId_#{rand(1000)}"

@buy_order = "buyOrder_#{rand(1000)}"
@session_id = "sessionId_#{rand(1000)}"
@purchase.update(
checkout_type: "tbk",
checkout_id: @session_id
)

@purchase.update(
checkout_type: "tbk",
checkout_id: @session_id
)
@return_url = success_event_event_purchase_url(@event, @purchase, provider: "tbk", enc: @purchase.signed_id)

@return_url = success_event_event_purchase_url(@event, @purchase, provider: "tbk", enc: @purchase.signed_id)
@resp = @tx.create(@buy_order, @session_id, @return_url, @details)

@resp = @tx.create(@buy_order, @session_id, @return_url, @details)
@payment_url = "#{@resp["url"]}?token_ws=#{@resp["token"]}"

@payment_url = "#{@resp["url"]}?token_ws=#{@resp["token"]}"
end
end
end
end
2 changes: 1 addition & 1 deletion app/javascript/controllers/google_maps.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function getAddressParts(obj) {
export default class extends Controller {
static targets = ["field", "map", "latitude", "longitude", "country", "city", "province"]

connect() {
initialize() {
if (typeof (google) != "undefined"){
this.initializeMap()
}
Expand Down
12 changes: 11 additions & 1 deletion app/models/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ class Event < ApplicationRecord
has_many :event_tickets
has_many :purchases, as: :purchasable
has_many :purchased_items, through: :purchases
has_many :paid_purchased_items, through: :purchases, class_name: "PurchasedItem", source: :purchased_items
has_many :paid_purchased_items, -> {
where(state: "paid")
}, through: :purchases, class_name: "PurchasedItem", source: :purchased_items
has_many :purchased_event_tickets, -> {
where(purchased_items: {purchased_item_type: "EventTicket"})
}, through: :purchased_items, source: :purchased_item, source_type: "EventTicket"
Expand Down Expand Up @@ -123,4 +125,12 @@ def sales_count
def tickets_sold
paid_purchased_items.size
end

def has_transbank?
self.user.tbk_commerce_code.present?
end

def has_stripe?
self.user.oauth_credentials.where(provider: "stripe_connect").present?
end
end
30 changes: 29 additions & 1 deletion app/models/purchase.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@ class Purchase < ApplicationRecord

validate :validate_purchased_items

include AASM

aasm column: :state do
state :pending, initial: true
state :paid

event :confirm do
transitions from: :pending, to: :paid
end
end

def virtual_purchased
@virtual_purchased || []
end
Expand Down Expand Up @@ -38,7 +49,7 @@ def validate_ticket(ticket, purchased_item)
end

# Check if the ticket quantity is sufficient for purchase
if purchased_item.quantity <= ticket.qty
if purchased_item.quantity >= ticket.qty
errors.add(:base, "Insufficient quantity for Ticket ID #{ticket.id}. Available: #{ticket.qty}. Requested: #{purchased_item.quantity}.")
end

Expand Down Expand Up @@ -80,4 +91,21 @@ def notify_purchase
def is_downloadable?
state == "paid" or state == "free_access"
end

def calculate_fee(total, ccy = "usd")
t = total.to_f * app_fee() / 100.0

case ccy
when "usd"
t
when "clp"
t.round
else
t
end
end

def app_fee
ENV['PLATFORM_EVENTS_FEE'].to_i
end
end
4 changes: 2 additions & 2 deletions app/views/event_hosts/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<div class="space-y-2">
<% if form.object.new_record? %>
<%= form.text_field :email, hint: t("event_hosts.email_hint") %>
<%= form.submit t("Save"), class: "inline-flex items-center px-4 py-2 border border-transparent text-sm font-medium rounded-md shadow-sm text-white bg-brand-600 hover:bg-brand-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-brand-500" %>
<%= form.submit t("save"), class: "inline-flex items-center px-4 py-2 border border-transparent text-sm font-medium rounded-md shadow-sm text-white bg-brand-600 hover:bg-brand-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-brand-500" %>
<% else %>

Expand Down Expand Up @@ -40,7 +40,7 @@
</div>

<div class="sm:col-span-6 flex justify-end space-x-2">
<%= form.submit gettext("Save"), class: "inline-flex items-center px-4 py-2 border border-transparent text-sm font-medium rounded-md shadow-sm text-white bg-brand-600 hover:bg-brand-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-brand-500" %>
<%= form.submit t("save"), class: "inline-flex items-center px-4 py-2 border border-transparent text-sm font-medium rounded-md shadow-sm text-white bg-brand-600 hover:bg-brand-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-brand-500" %>
</div>
</div>
<% end %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/event_purchases/create.turbo_stream.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<%= turbo_stream.update "modal" do %>
<%= render "shared/modal" do %>
<% # @purchase.errors.as_json %>
<%= @purchase.errors.as_json %>
<%= render "form", purchase: @purchase %>
<% end %>
<% end %>
8 changes: 7 additions & 1 deletion app/views/event_purchases/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
</a>
</div>



<p class="text-sm text-gray-600">Order placed
<time datetime="2021-03-22" class="font-medium text-gray-900">
<%= l @purchase.created_at, format: :long %>
Expand All @@ -38,7 +40,11 @@
<div class="px-4 py-6 sm:px-6 lg:grid lg:grid-cols-12 lg:gap-x-8 lg:p-8">
<div class="sm:flex lg:col-span-7">
<div class="aspect-h-1 aspect-w-1 w-full flex-shrink-0 overflow-hidden rounded-lg sm:aspect-none sm:h-40 sm:w-40">
<%= image_tag item.qr , class: "h-full w-full object-cover object-center sm:h-full sm:w-full" %>
<% if item.paid? %>
<%= image_tag item.qr , class: "h-full w-full object-cover object-center sm:h-full sm:w-full" %>
<% else %>
<%= item.state %>
<% end %>
</div>

<div class="mt-6 sm:ml-6 sm:mt-0">
Expand Down
8 changes: 5 additions & 3 deletions app/views/event_tickets/_qr_view.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
<div class="flex flex-col">
<div class="flex items-center space-x-2">
<div class="flex self-auto items-center justify-center rounded-full bg-green-100 dark:bg-green-900">
<div class="text-green-600 dark:text-green-400 flex items-center justify-center">
<%= image_tag @purchased_item.qr %>
</div>
<% if @purchased_item.paid? %>
<div class="text-green-600 dark:text-green-400 flex items-center justify-center">
<%= image_tag @purchased_item.qr %>
</div>
<% end %>
</div>

<div class="flex flex-col">
Expand Down
7 changes: 5 additions & 2 deletions app/views/events/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -109,16 +109,19 @@
<div class="sm:col-span-6 space-y-3 flex flex-col justify-between">
<div class="flex items-center justify-between space-x-2">
<div class="sm:w-1/2 flex flex-col justify-start space-y-2">
<%= f.label :payment_gateway %>
<%= f.select( :payment_gateway, [
["none", "none"],
#{key: "stripe", value: "stripe", disabled: has_stripe?(@event.user_id)},
#{key: "transbank", value: "transbank", disabled: has_transbank?(@current_user)} ]
["stripe", "stripe", {disabled: !@event.has_stripe?} ],
["transbank", "transbank", {disabled: !@event.has_transbank?} ]
],
hint: gettext("para transbank usaremos tu codigo de comercio de los ajustes")
) %>

</div>
<div class="sm:w-1/2 flex flex-col justify-start space-y-2">

<%= f.label :ticket_currency %>
<%= f.select( :ticket_currency, [
["CLP", "clp"],
["USD", "usd"],
Expand Down
4 changes: 2 additions & 2 deletions app/views/events/_overview_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
<h2 class="text-3xl font-bold tracking-tight text-gray-900 dark:text-gray-100 sm:text-4xl">
<%= gettext("Event overview") %>
</h2>
<p class="mt-3 text-xl text-gray-500 dark:text-gray-300 dark:text-gray-300 dark:text-gray-300 sm:mt-4">
<!--<p class="mt-3 text-xl text-gray-500 dark:text-gray-300 dark:text-gray-300 dark:text-gray-300 sm:mt-4">
Lorem ipsum dolor, sit amet consectetur adipisicing elit. Repellendus repellat laudantium.
</p>
</p>-->
</div>
</div>
<div class="mt-10 bg-white dark:bg-black pb-12 sm:pb-16">
Expand Down
2 changes: 1 addition & 1 deletion app/views/events/_schedule_fields.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
</div>

<div class="my-3" data-nested-form-target="links">
<%= link_to "Add new ticke", "#",
<%= link_to "Add new schedule", "#",
class: "inline-flex items-center px-3 py-1.5 border border-transparent text-xs font-medium rounded-full shadow-sm text-white bg-brand-600 hover:bg-brand-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-brand-500",
data: { action: "click->nested-form#add_association" } %>
</div>
Expand Down
2 changes: 1 addition & 1 deletion app/views/events/_tickets_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
</div>

<div class="my-3" data-nested-form-target="links">
<%= link_to "Add new ticke", "#",
<%= link_to "Add new ticket", "#",
class: "inline-flex items-center px-3 py-1.5 border border-transparent text-xs font-medium rounded-full shadow-sm text-white bg-brand-600 hover:bg-brand-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-brand-500",
data: { action: "click->nested-form#add_association" } %>
</div>
Expand Down
4 changes: 2 additions & 2 deletions app/views/purchases/tickets.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
<div class="min-w-0 flex-1 px-4 md:grid md:grid-cols-2 md:gap-4">
<div>
<p class="truncate text-sm font-medium text-brand-600">
<%= "ticket.event_ticket.event.title" %>
<%= purchase.purchasable.title %>
</p>
<p class="space-x-2 mt-2 flex items-center text-sm text-gray-500 dark:text-gray-200">
<svg
Expand All @@ -112,7 +112,7 @@
/>
</svg>

<span class="truncate"><%= "ticket.user.email" %></span>
<span class="truncate"><%= purchase.user.email %></span>
</p>
</div>
<div class="hidden md:block">
Expand Down
3 changes: 2 additions & 1 deletion config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ en:
likes: Likes
sure: Are you sure?
more: More
save: Save

menu:
music: Music
Expand Down Expand Up @@ -73,7 +74,7 @@ en:
edit: Edit Event
create: Create Event

published: Your event is published
published: Published Events
unpublished: Your event has not been published
unpublish: Unpublish Event
publish: Publish Event
Expand Down
Loading

0 comments on commit 1990f69

Please sign in to comment.