From 4fa1f4ac81951f9b373d2167413ac3401fd8d060 Mon Sep 17 00:00:00 2001 From: HombreLaser Date: Thu, 13 Apr 2023 18:14:25 -0600 Subject: AƱade servicios y validaciones de pago MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/services/payment/payment_service.rb | 42 +++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 app/services/payment/payment_service.rb (limited to 'app/services/payment/payment_service.rb') diff --git a/app/services/payment/payment_service.rb b/app/services/payment/payment_service.rb new file mode 100644 index 0000000..9f44f87 --- /dev/null +++ b/app/services/payment/payment_service.rb @@ -0,0 +1,42 @@ +# frozen_string_literal: true + +module Payment + # PaymentService + class PaymentService + attr_reader :error_messages + + def initialize(params, user_account) + @user_account = user_account + @params = params + @order = Order.new(params.except(:card_id)) + @card_id = params[:card_id] + @order = CartToOrderService.new(@user_account).call + end + + def call + payment = Payment.new(order_id: @order.id, card_id: @card_id, total:) + + @error_messages = { errors: @payment.errors.as_json } and return unless payment.save + + @order.update(payment_id: payment.id) + user_account.cart.product_carts.destroy_all + @order + end + + private + + def total + total = 0 + + @order.product_orders.joins(:product).select( + 'product_orders.*, products.bulk_price AS bulk_price, products.unitary_price AS unitary_price' + ).each do |product_order| + total += if product_order.quantity < 5 + unitary_price * product_order.quantity + else + bulk_price * product_order.quantity + end + end + end + end +end -- cgit v1.2.3