mirror of
https://github.com/LaCasemate/fab-manager.git
synced 2025-01-18 07:52:23 +01:00
check product is valid and in stock before payment
This commit is contained in:
parent
193c21a583
commit
8a8ce607b7
5
app/exceptions/cart/zero_price_error.rb
Normal file
5
app/exceptions/cart/zero_price_error.rb
Normal file
@ -0,0 +1,5 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
# Raised when order amount = 0
|
||||
class Cart::ZeroPriceError < StandardError
|
||||
end
|
@ -5,10 +5,10 @@ class Order < ApplicationRecord
|
||||
belongs_to :statistic_profile
|
||||
has_many :order_items, dependent: :destroy
|
||||
|
||||
ALL_STATES = %w[cart].freeze
|
||||
ALL_STATES = %w[cart in_progress ready canceled return].freeze
|
||||
enum state: ALL_STATES.zip(ALL_STATES).to_h
|
||||
|
||||
PAYMENT_STATES = %w[paid failed].freeze
|
||||
PAYMENT_STATES = %w[paid failed refunded].freeze
|
||||
enum payment_state: PAYMENT_STATES.zip(PAYMENT_STATES).to_h
|
||||
|
||||
validates :token, :state, presence: true
|
||||
|
@ -6,6 +6,10 @@ class Checkout::PaymentService
|
||||
require 'stripe/helper'
|
||||
|
||||
def payment(order, operator, payment_id = '')
|
||||
raise Cart::OutStockError unless Orders::OrderService.new.in_stock?(order, 'external')
|
||||
|
||||
raise Cart::InactiveProductError unless Orders::OrderService.new.all_products_is_active?
|
||||
|
||||
if operator.member?
|
||||
if Stripe::Helper.enabled?
|
||||
Payments::StripeService.new.payment(order, payment_id)
|
||||
|
23
app/services/orders/in_stock_service.rb
Normal file
23
app/services/orders/in_stock_service.rb
Normal file
@ -0,0 +1,23 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
# Provides methods for Check if the product is in stock
|
||||
class Cart::SetQuantityService
|
||||
def call(order)
|
||||
|
||||
return order if quantity.to_i.zero?
|
||||
|
||||
raise Cart::OutStockError if quantity.to_i > orderable.stock['external']
|
||||
|
||||
item = order.order_items.find_by(orderable: orderable)
|
||||
|
||||
raise ActiveRecord::RecordNotFound if item.nil?
|
||||
|
||||
different_quantity = quantity.to_i - item.quantity
|
||||
order.amount += (orderable.amount * different_quantity)
|
||||
ActiveRecord::Base.transaction do
|
||||
item.update(quantity: quantity.to_i)
|
||||
order.save
|
||||
end
|
||||
order.reload
|
||||
end
|
||||
end
|
18
app/services/orders/order_service.rb
Normal file
18
app/services/orders/order_service.rb
Normal file
@ -0,0 +1,18 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
# Provides methods for Order
|
||||
class Orders::OrderService
|
||||
def in_stock?(order, stock_type = 'external')
|
||||
order.order_items.each do |item|
|
||||
return false if item.orderable.stock[stock_type] < item.quantity
|
||||
end
|
||||
true
|
||||
end
|
||||
|
||||
def all_products_is_active?(order)
|
||||
order.order_items.each do |item|
|
||||
return false unless item.orderable.is_active
|
||||
end
|
||||
true
|
||||
end
|
||||
end
|
@ -18,7 +18,7 @@ module Payments::PaymentConcern
|
||||
def payment_success(order)
|
||||
ActiveRecord::Base.transaction do
|
||||
WalletService.debit_user_wallet(order, order.statistic_profile.user)
|
||||
order.update(payment_state: 'paid')
|
||||
order.update(state: 'in_progress', payment_state: 'paid')
|
||||
order.order_items.each do |item|
|
||||
ProductService.update_stock(item.orderable, 'external', 'sold', -item.quantity)
|
||||
end
|
||||
|
@ -11,6 +11,8 @@ class Payments::PayzenService
|
||||
def payment(order)
|
||||
amount = debit_amount(order)
|
||||
|
||||
raise Cart::ZeroPriceError if amount.zero?
|
||||
|
||||
id = PayZen::Helper.generate_ref(order, order.statistic_profile.user.id)
|
||||
|
||||
client = PayZen::Charge.new
|
||||
|
@ -7,6 +7,9 @@ class Payments::StripeService
|
||||
|
||||
def payment(order, payment_id)
|
||||
amount = debit_amount(order)
|
||||
|
||||
raise Cart::ZeroPriceError if amount.zero?
|
||||
|
||||
# Create the PaymentIntent
|
||||
intent = Stripe::PaymentIntent.create(
|
||||
{
|
||||
|
Loading…
x
Reference in New Issue
Block a user