<commit_before># encoding: utf-8
require 'digest'
require 'uri'
require 'net/http'

module AlipayDualfun
  GATEWAY_URL = 'https://mapi.alipay.com/gateway.do'

  module Product
    class Base
      DEFAULT_CHARSET = 'utf-8'
      SIGN_TYPE_MD5 = 'MD5'

      # all the 'required` field from offical SDK pdf
      ATTR_REQUIRED = [:service, :partner, :_input_charset,
        :sign_type, :sign, :notify_url, :return_url,
        :out_trade_no, :subject, :payment_type, :seller_email,
        :logistics_type, :logistics_fee, :logistics_payment,
        :price, :quantity
      ]

    end

    class DualfunPay < Base
      NAME = '双功能收款'
      SERVICE_LABEL = :trade_create_by_buyer

      def initialize(order)
        @order = order

        @params = {}
      end

      def notification_callback_url(url)
        @params[:notify_url] = url
        self
      end

      def after_payment_redirect_url(url)
        @params[:return_url] = url
        self
      end

      def gateway_api_url
        secure_signature = create_signature
        request_params = sign_params.merge(
          :sign_type => SIGN_TYPE_MD5,
          :sign => secure_signature
        )

        lost_attributes = ATTR_REQUIRED - request_params.keys
        if lost_attributes.any?
          raise "the following keys are lost: #{lost_attributes.inspect}"
        end

        uri = URI(GATEWAY_URL)
        uri.query = URI.encode_www_form(request_params.sort)

        uri.to_s
      end

      # 出错通知异步调用 URL
      #
      # NOTE: 需要联系技术支持才能开通
      def error_callback_url(url)
        @params[:error_notify_url] = url
        self
      end

      private
      def sign_params
        params = @order.attributes.merge(@params)
        params[:service] = SERVICE_LABEL
        params[:partner] = @order.merchant.partner
        params[:_input_charset] = DEFAULT_CHARSET
        @sign_params ||= params
      end

      def create_signature
        sequence = sign_params.sort.map {|k, v| "#{k}=#{v}"}.join('&')
        Digest::MD5.hexdigest(sequence + @order.merchant.key)
      end
    end
  end

  class Order
    PAYMENT_TYPE_BUYING = 1
    PAYMENT_TYPE_DONATION = 4

    attr_accessor :merchant
    attr_reader :attributes

    def initialize(order_id, subject, description)
      @attributes = {
        :out_trade_no => order_id,
        :subject => subject,
        :body => description }

        @attributes[:payment_type] = PAYMENT_TYPE_BUYING
    end

    def seller_email(email)
      @attributes[:seller_email] = email
      self
    end

    def logistics_and_price_settings
      @attributes[:logistics_type] = 'POST'
      @attributes[:logistics_payment] = 'BUYER_PAY'
      @attributes[:logistics_fee] = '10'
      @attributes[:price] = '0.1'
      @attributes[:quantity] = '2'
      self
    end

    def buyer_email(email)
      @attributes[:buyer_email] = email
      self
    end

    def total_fee(fee)
      @attributes[:total_fee] = fee

      self
    end

    def product_url(url)
      @attributes[:show_url] = url
      self
    end

    def as_donation
      @attributes[:payment_type] = PAYMENT_TYPE_DONATION
      self
    end

    def dualfun_pay
      Product::DualfunPay.new(self)
    end
  end

  class Merchant
    attr_accessor :partner, :key

    def initialize(partner, key)
      @partner = partner
      @key = key
    end

    def create_order(order_id, subject, description)
      order = Order.new(order_id, subject, description)
      order.merchant = self
      order
    end
  end

  class Notification
    def initialize(partner, notification_id)<commit_msg>dualpay does not support payment_type other than 1<commit_after># encoding: utf-8
require 'digest'
require 'uri'
require 'net/http'

module AlipayDualfun
  GATEWAY_URL = 'https://mapi.alipay.com/gateway.do'

  module Product
    class Base
      DEFAULT_CHARSET = 'utf-8'
      SIGN_TYPE_MD5 = 'MD5'

      # all the 'required` field from offical SDK pdf
      ATTR_REQUIRED = [:service, :partner, :_input_charset,
        :sign_type, :sign, :notify_url, :return_url,
        :out_trade_no, :subject, :payment_type, :seller_email,
        :logistics_type, :logistics_fee, :logistics_payment,
        :price, :quantity
      ]
    end

    class DualfunPay < Base
      NAME = '双功能收款'
      SERVICE_LABEL = :trade_create_by_buyer

      def initialize(order)
        @order = order
        @params = {}
      end

      def notification_callback_url(url)
        @params[:notify_url] = url
        self
      end

      def after_payment_redirect_url(url)
        @params[:return_url] = url
        self
      end

      def gateway_api_url
        secure_signature = create_signature
        request_params = sign_params.merge(
          :sign_type => SIGN_TYPE_MD5,
          :sign => secure_signature
        )

        lost_attributes = ATTR_REQUIRED - request_params.keys
        if lost_attributes.any?
          raise "the following keys are lost: #{lost_attributes.inspect}"
        end

        uri = URI(GATEWAY_URL)
        uri.query = URI.encode_www_form(request_params.sort)

        uri.to_s
      end

      # 出错通知异步调用 URL
      #
      # NOTE: 需要联系技术支持才能开通
      def error_callback_url(url)
        @params[:error_notify_url] = url
        self
      end

      private

      def sign_params
        params = @order.attributes.merge(@params)
        params[:service] = SERVICE_LABEL
        params[:partner] = @order.merchant.partner
        params[:_input_charset] = DEFAULT_CHARSET
        @sign_params ||= params
      end

      def create_signature
        sequence = sign_params.sort.map {|k, v| "#{k}=#{v}"}.join('&')
        Digest::MD5.hexdigest(sequence + @order.merchant.key)
      end
    end
  end

  class Order
    PAYMENT_TYPE_BUYING = 1

    attr_accessor :merchant
    attr_reader :attributes

    def initialize(order_id, subject, description)
      @attributes = {
        :out_trade_no => order_id,
        :subject => subject,
        :body => description }

        @attributes[:payment_type] = PAYMENT_TYPE_BUYING
    end

    def seller_email(email)
      @attributes[:seller_email] = email
      self
    end

    def logistics_and_price_settings
      @attributes[:logistics_type] = 'POST'
      @attributes[:logistics_payment] = 'BUYER_PAY'
      @attributes[:logistics_fee] = '10'
      @attributes[:price] = '0.1'
      @attributes[:quantity] = '2'
      self
    end

    def buyer_email(email)
      @attributes[:buyer_email] = email
      self
    end

    def total_fee(fee)
      @attributes[:total_fee] = fee

      self
    end

    def product_url(url)
      @attributes[:show_url] = url
      self
    end

    def dualfun_pay
      Product::DualfunPay.new(self)
    end
  end

  class Merchant
    attr_accessor :partner, :key

    def initialize(partner, key)
      @partner = partner
      @key = key
    end

    def create_order(order_id, subject, description)
      order = Order.new(order_id, subject, description)
      order.merchant = self
      order
    end
  end

  class Notification
    def initialize(partner, notification_id)
{'fraction_non_alphanumeric': 0.09089625970359916, 'fraction_numerical': 0.003669724770642202, 'mean_word_length': 2.3519394512772, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '5356603', 'n_tokens_mistral': 2361, 'n_tokens_neox': 2284, 'n_words': 480}