<commit_before>require "active_merchant"
module ShiftCommerce
  module UiPaymentGateway
    DEFAULT_DESCRIPTION = "THE DEFAULT DESCRIPTION - TO BE CHANGED"
    class PaypalExpressEngine
      def initialize(cart:, gateway_class: ::ActiveMerchant::Billing::PaypalExpressGateway, config: Config.instance, request:, success_url:, cancel_url:)
        self.cart = cart
        self.request = request
        self.gateway = gateway_class.new({login: config.paypal_login, password: <PASSWORD>, signature: config.paypal_signature})
        self.success_url = success_url
        self.cancel_url = cancel_url
      end
      # @param [Order] cart The cart that the payment is for
      # @return [String] The URL to redirect the user to
      def setup_payment(cart:)
        shipping_address = cart.shipping_address
        items = cart.line_items.map do |li|
          {
            name: li.title,
            number: "Number",
            quantity: li.unit_quantity,
            amount: convert_amount(li.total / li.unit_quantity),
            description: li.title,
            url: "http://www.google.com"
          }
        end
        response = gateway.setup_purchase convert_amount(cart.total),
                                          ip: request.remote_ip,
                                          return_url: success_url,
                                          cancel_return_url: cancel_url,
                                          currency: ::ShiftCommerce::UiPaymentGateway::DEFAULT_CURRENCY,
                                          address_override: true,
                                          shipping_address: {
                                            name: shipping_address.name,
                                            address1: shipping_address.address_line_1,
                                            address2: shipping_address.address_line_2,
                                            city: shipping_address.city,
                                            state: shipping_address.state,
                                            country: shipping_address.country,
                                            zip: shipping_address.postcode

                                          },
                                          description: DEFAULT_DESCRIPTION,
                                          items: items
        if response.success?
          gateway.redirect_url_for(response.token)
        else
          raise "An error occured communicating with paypal " # @TODO Find out where to get the message from and add it
        end
      end

      # Processes the token - i.e. takes the payment and returns the authorisation code
      # @param [String] token The token to use (comes from params[:token] in the controller)
      # @param [Object] cart The cart for which we are taking payment for
      # @param [Object] payer_id The payer id (comes from params["PayerID"] in the controller)
      # @return [Hash] The authorization id and token to be stored by the application (auth_id: [String], token: [String])
      # @raise [ShiftCommerce::UiPaymentGateway::Exceptions::PaymentNotAccepted] Raised if paypal errors for any reason - the response is available as an attribute on the exception
      def process_token(token:, cart:, payer_id:)
        response = gateway.purchase(convert_amount(cart.total), token: token, payer_id: payer_id, currency: ::ShiftCommerce::UiPaymentGateway::DEFAULT_CURRENCY)
        raise Exceptions::PaymentNotAccepted.new(response) unless response.success?
        return { auth_id: response.authorization, token: response.token }
      end

      private

      def convert_amount(amount)
        (amount * 100).round
      end

      attr_accessor :cart, :gateway, :request, :success_url, :cancel_url
    end
  end
end<commit_msg>Improved error reporting on paypal failure<commit_after>require "active_merchant"
module ShiftCommerce
  module UiPaymentGateway
    DEFAULT_DESCRIPTION = "THE DEFAULT DESCRIPTION - TO BE CHANGED"
    class PaypalExpressEngine
      def initialize(cart:, gateway_class: ::ActiveMerchant::Billing::PaypalExpressGateway, config: Config.instance, request:, success_url:, cancel_url:)
        self.cart = cart
        self.request = request
        self.gateway = gateway_class.new({login: config.paypal_login, password: <PASSWORD>, signature: config.paypal_signature})
        self.success_url = success_url
        self.cancel_url = cancel_url
      end
      # @param [Order] cart The cart that the payment is for
      # @return [String] The URL to redirect the user to
      def setup_payment(cart:)
        shipping_address = cart.shipping_address
        items = cart.line_items.map do |li|
          {
            name: li.title,
            number: "Number",
            quantity: li.unit_quantity,
            amount: convert_amount(li.total / li.unit_quantity),
            description: li.title,
            url: "http://www.google.com"
          }
        end
        response = gateway.setup_purchase convert_amount(cart.total),
                                          ip: request.remote_ip,
                                          return_url: success_url,
                                          cancel_return_url: cancel_url,
                                          currency: ::ShiftCommerce::UiPaymentGateway::DEFAULT_CURRENCY,
                                          address_override: true,
                                          shipping_address: {
                                            name: shipping_address.name,
                                            address1: shipping_address.address_line_1,
                                            address2: shipping_address.address_line_2,
                                            city: shipping_address.city,
                                            state: shipping_address.state,
                                            country: shipping_address.country,
                                            zip: shipping_address.postcode

                                          },
                                          description: DEFAULT_DESCRIPTION,
                                          items: items
        if response.success?
          gateway.redirect_url_for(response.token)
        else
          raise "An error occured communicating with paypal #{response.message} \n\n#{response.params.to_json}" # @TODO Find out where to get the message from and add it
        end
      end

      # Processes the token - i.e. takes the payment and returns the authorisation code
      # @param [String] token The token to use (comes from params[:token] in the controller)
      # @param [Object] cart The cart for which we are taking payment for
      # @param [Object] payer_id The payer id (comes from params["PayerID"] in the controller)
      # @return [Hash] The authorization id and token to be stored by the application (auth_id: [String], token: [String])
      # @raise [ShiftCommerce::UiPaymentGateway::Exceptions::PaymentNotAccepted] Raised if paypal errors for any reason - the response is available as an attribute on the exception
      def process_token(token:, cart:, payer_id:)
        response = gateway.purchase(convert_amount(cart.total), token: token, payer_id: payer_id, currency: ::ShiftCommerce::UiPaymentGateway::DEFAULT_CURRENCY)
        raise Exceptions::PaymentNotAccepted.new(response) unless response.success?
        return { auth_id: response.authorization, token: response.token }
      end

      private

      def convert_amount(amount)
        (amount * 100).round
      end

      attr_accessor :cart, :gateway, :request, :success_url, :cancel_url
    end
  end
end
{'fraction_non_alphanumeric': 0.0705684319564936, 'fraction_numerical': 0.0018127670594328628, 'mean_word_length': 1.7625178826895564, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 62, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4155179', 'n_tokens_mistral': 1808, 'n_tokens_neox': 1773, 'n_words': 526}