Skip to content

Commit

Permalink
tracks pagination + webhooks fix, trach purchase
Browse files Browse the repository at this point in the history
  • Loading branch information
michelson committed Aug 13, 2023
1 parent c0d230b commit 586d27b
Show file tree
Hide file tree
Showing 11 changed files with 91 additions and 58 deletions.
30 changes: 21 additions & 9 deletions app/controllers/track_purchases_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@ def create
# When a customer purchases a ticket for an event:
customer = current_user

@purchase = current_user.purchases.new(purchasable: @track)
price = @track.price
price_param = build_params[:price].to_f
if @track.name_your_price? && price_param && price_param > @track.price
price = price_param
end

@purchase = current_user.purchases.new(purchasable: @track, price: price )

@purchase.virtual_purchased = [
VirtualPurchasedItem.new({resource: @track, quantity: 1})
Expand All @@ -39,7 +45,7 @@ def handle_stripe_session
{
"quantity" => 1,
"price_data" => {
"unit_amount" => ((@track.price * v) * 100).to_i,
"unit_amount" => ((@purchase.price * v) * 100).to_i,
"currency" => "USD",
"product_data" => {
"name" => @track.title,
Expand All @@ -53,15 +59,19 @@ def handle_stripe_session

fee_amount = 3

payment_intent_data = {}

payment_intent_data = {
application_fee_amount: fee_amount
# "transfer_data"=> %{
# "destination"=> c.uid
# }
} if account

@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
# }
},
payment_intent_data: payment_intent_data,
customer_email: current_user.email,
mode: "payment",
success_url: success_track_track_purchase_url(@track, @purchase), # Replace with your success URL
Expand Down Expand Up @@ -96,6 +106,8 @@ def failure
end

def build_params
params.require(:payment).permit(:include_message, :optional_message)
params.require(:payment).permit(
:include_message, :optional_message, :price
)
end
end
4 changes: 2 additions & 2 deletions app/controllers/tracks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class TracksController < ApplicationController
before_action :authenticate_user!, except: [:index, :show ]

def index
@tracks = Track.published.page(params[:page]).per(10)
@tracks = Track.published.order("id desc").page(params[:page]).per(12)
end

def new
Expand Down Expand Up @@ -52,7 +52,7 @@ def update

def show
@track = Track.friendly.find(params[:id])

@supporters = []
set_meta_tags(
# title: @track.title,
# description: @track.description,
Expand Down
7 changes: 6 additions & 1 deletion app/controllers/webhooks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,13 @@ def handle_stripe
payload = request.body.read
sig_header = request.env['HTTP_STRIPE_SIGNATURE']
event = nil
json_event = JSON.parse(payload)
begin
event = Stripe::Webhook.construct_event(payload, sig_header, ENV['STRIPE_SIGNING_SECRET'])
if json_event["account"]
event = Stripe::Webhook.construct_event(payload, sig_header, ENV['STRIPE_SIGNING_SECRET'])
else
event = Stripe::Webhook.construct_event(payload, sig_header, ENV['STRIPE_SIGNING_SECRET_ACC'])
end
rescue JSON::ParserError => e
# Invalid payload
render json: { error: { message: e.message }}, status: :bad_request
Expand Down
33 changes: 21 additions & 12 deletions app/views/shared/_music_purchase.html.erb
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@
<div id="xx-5">
<div class="text-2xl font-bold space-x-2">
<div class="text-xl font-bold space-x-2">
<%= link_to new_track_track_purchase_path(@track),
"data-turbo-frame": "modal",
class: "hover:underline dark:border-white border-black rounded-lg border-2 p-2 bg-green-500 hover:bg-green-500/90" do %>
Comprar álbum digital
<%= t("tracks.buy_digital_music")%>
<% end %>
<span> <%= resource.price %> <span class="text-gray-700 dark:text-gray-300"> USD </span>
<span> <%= number_to_currency(resource.price) %> <span class="text-gray-700 dark:text-gray-300"> USD </span>
</span>
<span class="text-gray-700 dark:text-gray-300"> o más </span>

<% if resource.name_your_price? %>
<span class="text-gray-700 dark:text-gray-300"> (<%= t("tracks.or_more") %>) </span>
<% end %>
</div>
</div>

<div class="sm:text-xl text-sm container mx-auto my-4 flex flex-col space-y-4">
<h3 class="font-bold font-medium">Supporters</h3>
<div class="-space-x-4">
<a href="/pueblonuevo" data-phx-link="redirect" data-phx-link-state="push">
<img class="relative z-30 inline object-cover w-10 h-10 border-2 border-white rounded-full" src="/active_storage/representations/redirect/SFMyNTY.eyJleHBpcmVzX2luIjpudWxsLCJtZXNzYWdlIjoiNTgxIiwicHVycG9zZSI6bnVsbH0.lSA7cpcMmQhKxah4FF8lgkeoOUcKYIRR8IwJs7Dbxhc/SFMyNTY.eyJleHBpcmVzX2luIjpudWxsLCJtZXNzYWdlIjoie1wiZm9ybWF0XCI6XCJqcGdcIixcInJlc2l6ZV90b19maWxsXCI6XCIyMDB4MjAwXCJ9IiwicHVycG9zZSI6bnVsbH0.6eK0-2F79Tbc3XOJhTLe4owI_7qgf-ay0_FT3Jn-tg0/Foto-Mika-Martini-destacada[1].jpg">
<h3 class="hidden mt-6 text-gray-900 dark:text-gray-100 text-sm font-medium"> pueblonuevo </h3>
</a>
<% if @supporters.any? %>
<div class="sm:text-xl text-sm container mx-auto my-4 flex flex-col space-y-4">
<h3 class="font-bold font-medium"><%= t("tracks.supporters") %></h3>
<div class="-space-x-4">
<% @supporters.each do |supporter| %>
<%= link_to user_path(supporter.username) do %>
<%= image_tag supporter.cover_url(:small), class: "relative z-30 inline object-cover w-10 h-10 border-2 border-white rounded-full" %>
<h3 class="hidden mt-6 text-gray-900 dark:text-gray-100 text-sm font-medium">
<%= supporter.username %>
</h3>
<% end %>
<% end %>
</div>
</div>
</div>
<% end %>
2 changes: 1 addition & 1 deletion app/views/track_purchases/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
<div id="purchase-form">
<%= form_for @payment, url: track_track_purchases_path(@track) do |form| %>


<span class="text-xl py-4">
<%= t("payments.buy_digital_track") %>
</span>
Expand All @@ -21,6 +20,7 @@
$
</span>
<%= form.text_field( :price,
label: false,
class: "block w-64 flex-1- rounded-none rounded-r-md border-gray-300 dark:border-gray-800 dark:bg-gray-800 focus:border-brand-500 focus:ring-brand-500 sm:text-sm",
placeholder: t("payments.name_your_price", num: number_to_currency(@track.price, precision: 2) )
) %>
Expand Down
6 changes: 3 additions & 3 deletions app/views/track_purchases/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
Order #<%= @purchase.id %>
</h1>

<a href="#" class="hidden text-sm font-medium text-indigo-600 hover:text-indigo-500 sm:block">
<a href="#" class="hidden text-sm font-medium text-indigo-600 hover:text-indigo-500 sm:block-">
View invoice
<span aria-hidden="true"> &rarr;</span>
</a>
Expand All @@ -20,7 +20,7 @@
<%= l @purchase.created_at, format: :long %>
</time>
</p>
<a href="#" class="text-sm font-medium text-indigo-600 hover:text-indigo-500 sm:hidden">
<a href="#" class="hidden text-sm font-medium text-indigo-600 hover:text-indigo-500 sm:hidden-">
View invoice
<span aria-hidden="true"> &rarr;</span>
</a>
Expand Down Expand Up @@ -48,7 +48,7 @@
<p class="mt-2 text-sm font-medium text-gray-900"><%= number_to_currency item.purchased_item.price %></p>
<p class="mt-3 text-sm text-gray-500">
<%= item.purchased_item.id %>
<%= link_to "Downloadss", item.purchased_item.zip.url %>
<%= link_to "Downloads", item.purchased_item.zip.url %>

</p>
</div>
Expand Down
24 changes: 20 additions & 4 deletions app/views/tracks/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,26 @@
class="max-w-2xl mx-auto pt-12-- pb-16 px-4 sm:pt-16-- sm:pb-24 sm:px-6 lg:max-w-7xl lg:px-8">
<h2 id="products-heading" class="sr-only">Pistas</h2>

<div class="grid grid-cols-1 gap-y-10 sm:grid-cols-2 gap-x-6 lg:grid-cols-5 xl:grid-cols-6 xl:gap-x-8" id="infinite-scroll" phx-hook="InfiniteScroll" phx-update="append" data-page="1" phx-target="1" data-total-pages="6" data-phx-component="1">

<% @tracks.each do |track| %>
<%= render "tracks/track_cell", track: track %>
<%= turbo_frame_tag "conversation-list-#{@tracks.current_page}" do %>
<div class="grid grid-cols-1 gap-y-10 sm:grid-cols-2 gap-x-6 lg:grid-cols-5 xl:grid-cols-6 xl:gap-x-8" id="infinite-scroll">

<% @tracks.each do |track| %>
<%= render "tracks/track_cell", track: track %>
<% end %>
</div>

<% if @tracks.next_page.present? %>
<%= turbo_frame_tag "conversation-list-#{@tracks.next_page}", loading: :lazy,
src: tracks_path(page: @tracks.next_page) do %>
<div class="">
<%= t('loading') %>
</div>
<% end %>
<% end %>
</div>
<% end %>



</section>
31 changes: 6 additions & 25 deletions app/views/tracks/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@
</h1>
</div>
<!--
<div class="mt-6 flex items-center justify-stretch space-x-2 sm:flex-row sm:space-y-0 sm:space-x-4">
<div>
<%= link_to "#", class: "button" do %>
Expand All @@ -157,39 +157,20 @@
<% end %>
</div>
<div>
<%= link_to "#", class: "button" do %>
<svg xmlns="http://www.w3.org/2000/svg" class="h-4 w-4" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
<path stroke-linecap="round" stroke-linejoin="round" d="M4.318 6.318a4.5 4.5 0 000 6.364L12 20.364l7.682-7.682a4.5 4.5 0 00-6.364-6.364L12 7.636l-1.318-1.318a4.5 4.5 0 00-6.364 0z"></path>
</svg>
<span class="flex space-x-1">
<span>1</span>
<span class="hidden sm:block">Me gusta</span>
</span>
<% end %>
</div>
<%= render "likes/like_button", track: @track, button_class: "button" %>
<div>
<%= link_to "#", class: "button" do %>
<svg xmlns="http://www.w3.org/2000/svg" class="h-4 w-4" viewBox="0 0 20 20" fill="currentColor">
<path fill-rule="evenodd" d="M4 2a1 1 0 011 1v2.101a7.002 7.002 0 0111.601 2.566 1 1 0 11-1.885.666A5.002 5.002 0 005.999 7H9a1 1 0 010 2H4a1 1 0 01-1-1V3a1 1 0 011-1zm.008 9.057a1 1 0 011.276.61A5.002 5.002 0 0014.001 13H11a1 1 0 110-2h5a1 1 0 011 1v5a1 1 0 11-2 0v-2.101a7.002 7.002 0 01-11.601-2.566 1 1 0 01.61-1.276z" clip-rule="evenodd"></path>
</svg>
<span class="hidden sm:block">Repostar</span>
<% end %>
</div>
<%= render "reposts/repost_button", track: @track, button_class: "button" %>
<div>
<%= link_to edit_track_path(@track), class: "button" do %>
<svg xmlns="http://www.w3.org/2000/svg" class="h-4 w-4" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
<path stroke-linecap="round" stroke-linejoin="round" d="M15.232 5.232l3.536 3.536m-2.036-5.036a2.5 2.5 0 113.536 3.536L6.5 21.036H3v-3.572L16.732 3.732z"></path>
</svg>
<span>Editar</span>
<span><%= t("edit") %></span>
<% end %>
</div>
</div>
-->
</div>
</div>
Expand Down Expand Up @@ -268,7 +249,7 @@
<div class="pb-4">
<h2 id="activity-title" class="text-lg font-medium text-gray-900 dark:text-gray-100">
Comentarios
<%= t("comments") %>
</h2>
</div>
Expand Down
4 changes: 4 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ en:
by: "By %{name}"
see_everything: See Everything
edit: Edit
loading: Loading...

menu:
music: Music
Expand Down Expand Up @@ -84,6 +85,9 @@ en:
go_to: Go to your track
share: Share your new track
link: Link
buy_digital_music: Buy digital music
or_more: or more
supporters: Supporters

home:
artists: Artists you should know
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20230813191411_add_price_to_purchase.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddPriceToPurchase < ActiveRecord::Migration[7.0]
def change
add_column :purchases, :price, :decimal
end
end
3 changes: 2 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 586d27b

Please sign in to comment.