<commit_before>require_dependency 'lib/payatron_4000'

module TradoStripeModule

    class Striper

        # Creates a charge for the Stripe API; receives a response and in turn creates the relevant transaction records,
        # sends a confirmation email and redirects the user.
        #
        # @param order [Object]
        # @param session [Object
        def self.complete order, session, ip_address
            order.transfer(order.cart)
            charge = Stripe::Charge.create(
                amount: Store::Price.new(price: order.gross_amount, tax_type: 'net').singularize,
                currency: Store.settings.currency_code,
                customer: order.stripe_customer_id,
                description: "Order ID ##{order.id} | #{order.billing_address.full_name} | #{order.email}",
                metadata: Hash[ *order.order_items.collect { |item| [ "order_item_#{item.id}", "#{item.product.name} - #{item.sku.full_sku}" ] }.flatten ],
                statement_descriptor: Store.settings.stripe_statement_descriptor
            )
            if charge.paid
                TradoStripeModule::Striper.successful(charge, order)
                Payatron4000.destroy_cart(session)
                Payatron4000.decommission_order(order)
                order.reload
                Mailatron4000::Orders.confirmation_email(order)
                return Rails.application.routes.url_helpers.success_order_url(order, host: Trado::Application.config.action_mailer.default_url_options[:host])
            end
        rescue Stripe::CardError => e
            body = e.json_body
            err  = body[:error]
            TradoStripeModule::Striper.failed(err, order)
            order.reload
            Mailatron4000::Orders.confirmation_email(order)
            return Rails.application.routes.url_helpers.failed_order_url(order, host: Trado::Application.config.action_mailer.default_url_options[:host])
        end

        # Upon successfully completing an order with a Stripe payment option a new transaction record is created, stock is updated for the relevant SKU
        #
        # @param charge [Object]
        # @param order [Object]
        def self.successful charge, order
            Transaction.new(  :fee                      => charge.application_fee,  
                              :order_id                 => order.id, 
                              :payment_status           => 'completed', 
                              :transaction_type         => 'Credit', 
                              :tax_amount               => order.tax_amount, 
                              :stripe_charge_id         => charge.id, 
                              :payment_type             => 'stripe',
                              :net_amount               => order.net_amount,
                              :gross_amount             => order.gross_amount
            ).save(validate: false)
            Payatron4000.update_stock(order)
            Payatron4000.increment_product_order_count(order.products)
        end

        
        # When an order has failed to complete, a new transaction record is created with a logged status reason
        #
        # @param error [Object]
        # @param order [Object]
        def self.failed error, order
            Transaction.new(  :fee                        => 0, 
                              :gross_amount               => order.gross_amount, 
                              :order_id                   => order.id, 
                              :payment_status             => 'failed', 
                              :transaction_type           => 'Credit', 
                              :tax_amount                 => order.tax_amount, 
                              :stripe_charge_id           => nil, 
                              :payment_type               => 'stripe',
                              :net_amount                 => order.net_amount,
                              :status_reason              => error[:message],
                              :error_code                 => error[:code].to_i
            ).save(validate: false)
            Payatron4000.increment_product_order_count(order.products)
        end
    end
end<commit_msg>Set up new method for storing the order id in cache temporarily and fixed bug with redirect urls.<commit_after>require_dependency 'lib/payatron_4000'

module TradoStripeModule

    class Striper

        # Creates a charge for the Stripe API; receives a response and in turn creates the relevant transaction records,
        # sends a confirmation email and redirects the user.
        #
        # @param order [Object]
        # @param session [Object
        def self.complete order, session, ip_address
            order.transfer(order.cart)
            charge = Stripe::Charge.create(
                amount: Store::Price.new(price: order.gross_amount, tax_type: 'net').singularize,
                currency: Store.settings.currency_code,
                customer: order.stripe_customer_id,
                description: "Order ID ##{order.id} | #{order.billing_address.full_name} | #{order.email}",
                metadata: Hash[ *order.order_items.collect { |item| [ "order_item_#{item.id}", "#{item.product.name} - #{item.sku.full_sku}" ] }.flatten ],
                statement_descriptor: Store.settings.stripe_statement_descriptor
            )
            if charge.paid
                TradoStripeModule::Striper.successful(charge, order)
                Payatron4000.destroy_cart(session)
                Payatron4000.decommission_order(order)
                order.reload
                Mailatron4000::Orders.confirmation_email(order)
                return Rails.application.routes.url_helpers.success_orders_url(host: Trado::Application.config.action_mailer.default_url_options[:host])
            end
        rescue Stripe::CardError => e
            body = e.json_body
            err  = body[:error]
            TradoStripeModule::Striper.failed(err, order)
            order.reload
            Mailatron4000::Orders.confirmation_email(order)
            return Rails.application.routes.url_helpers.failed_orders_url(host: Trado::Application.config.action_mailer.default_url_options[:host])
        end

        # Upon successfully completing an order with a Stripe payment option a new transaction record is created, stock is updated for the relevant SKU
        #
        # @param charge [Object]
        # @param order [Object]
        def self.successful charge, order
            Transaction.new(  :fee                      => charge.application_fee,  
                              :order_id                 => order.id, 
                              :payment_status           => 'completed', 
                              :transaction_type         => 'Credit', 
                              :tax_amount               => order.tax_amount, 
                              :stripe_charge_id         => charge.id, 
                              :payment_type             => 'stripe',
                              :net_amount               => order.net_amount,
                              :gross_amount             => order.gross_amount
            ).save(validate: false)
            Payatron4000.update_stock(order)
            Payatron4000.increment_product_order_count(order.products)
            Payatron4000.set_order_id_session(order.id, 'success')
        end

        
        # When an order has failed to complete, a new transaction record is created with a logged status reason
        #
        # @param error [Object]
        # @param order [Object]
        def self.failed error, order
            Transaction.new(  :fee                        => 0, 
                              :gross_amount               => order.gross_amount, 
                              :order_id                   => order.id, 
                              :payment_status             => 'failed', 
                              :transaction_type           => 'Credit', 
                              :tax_amount                 => order.tax_amount, 
                              :stripe_charge_id           => nil, 
                              :payment_type               => 'stripe',
                              :net_amount                 => order.net_amount,
                              :status_reason              => error[:message],
                              :error_code                 => error[:code].to_i
            ).save(validate: false)
            Payatron4000.increment_product_order_count(order.products)
            Payatron4000.set_order_id_session(order.id, 'failed')
        end
    end
end
{'fraction_non_alphanumeric': 0.08011527377521614, 'fraction_numerical': 0.008530259365994237, 'mean_word_length': 1.4377634166900815, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5609642', 'n_tokens_mistral': 2202, 'n_tokens_neox': 2074, 'n_words': 446}