<commit_before># coding: utf-8
#inspired by https://github.com/spree-contrib/spree_skrill
module Spree
  class AlipayStatusController < StoreController
    #fixes Action::Controller::InvalidAuthenticityToken error on alipay_notify
    skip_before_action :verify_authenticity_token

    def alipay_done
      # alipay acount could vary in each store.
      # get order_no from query string -> get payment -> initialize Alipay -> verify ailpay callback
      order = retrieve_order params["out_trade_no"]
      alipay_payment = get_alipay_payment( order )

      if alipay_payment.payment_method.provider.verify?( request.query_parameters )
        # 担保交易的交易状态变更顺序依次是:
        #  WAIT_BUYER_PAY→WAIT_SELLER_SEND_GOODS→WAIT_BUYER_CONFIRM_GOODS→TRADE_FINISHED。
        # 即时到账的交易状态变更顺序依次是:
        #  WAIT_BUYER_PAY→TRADE_FINISHED。
        complete_order( order, request.request_parameters.merge(params) )
        if order.complete?
          #copy from spree/frontend/checkout_controller
          session[:order_id] = nil
          flash.notice = Spree.t(:order_processed_successfully)
          flash['order_completed'] = true
          redirect_to spree.order_path( order )
        else
          #Strange
          redirect_to checkout_state_path(order.state)
        end
      else
        redirect_to checkout_state_path(order.state)
      end

    end

    def alipay_notify
      order = retrieve_order params["out_trade_no"]
      alipay_payment = get_alipay_payment( order )
      if alipay_payment.payment_method.provider.verify?( request.request_parameters )
        complete_order( order, request.request_parameters.merge(params) )
        payment = order.payments.last
        payment.update_attributes({state: 'completed'}) if payment.state != 'completed'
        render text: "success"
      else
        render text: "fail"
      end

    end

    private

    def retrieve_order(order_number)
      @order = Spree::Order.find_by_number!(order_number)
    end

    def get_alipay_payment( order )
      #use payment instead of unprocessed_payments, order may be completed.
      order.payments.last
    end

    def complete_order( order, alipay_parameters )
      unless order.complete?
        alipay_payment = get_alipay_payment( order )
        # it require pending_payments to process_payments!
        order.next
        alipay_payment.update_attributes(
          {
            response_code: "#{alipay_parameters['trade_no']},#{alipay_parameters['trade_status']}",
            state: 'completed'
          })

      end
    end
  end
end
<commit_msg>force complete order & payment<commit_after># coding: utf-8
#inspired by https://github.com/spree-contrib/spree_skrill
module Spree
  class AlipayStatusController < StoreController
    #fixes Action::Controller::InvalidAuthenticityToken error on alipay_notify
    skip_before_action :verify_authenticity_token

    def alipay_done
      # alipay acount could vary in each store.
      # get order_no from query string -> get payment -> initialize Alipay -> verify ailpay callback
      order = retrieve_order params["out_trade_no"]
      alipay_payment = get_alipay_payment( order )

      if alipay_payment.payment_method.provider.verify?( request.query_parameters )
        # 担保交易的交易状态变更顺序依次是:
        #  WAIT_BUYER_PAY→WAIT_SELLER_SEND_GOODS→WAIT_BUYER_CONFIRM_GOODS→TRADE_FINISHED。
        # 即时到账的交易状态变更顺序依次是:
        #  WAIT_BUYER_PAY→TRADE_FINISHED。
        complete_order( order, request.request_parameters.merge(params) )
        if order.complete?
          #copy from spree/frontend/checkout_controller
          session[:order_id] = nil
          flash.notice = Spree.t(:order_processed_successfully)
          flash['order_completed'] = true
          redirect_to spree.order_path( order )
        else
          #Strange
          redirect_to checkout_state_path(order.state)
        end
      else
        redirect_to checkout_state_path(order.state)
      end

    end

    def alipay_notify
      order = retrieve_order params["out_trade_no"]
      alipay_payment = get_alipay_payment( order )
      if alipay_payment.payment_method.provider.verify?( request.request_parameters )
        complete_order( order, request.request_parameters.merge(params) )
        render text: "success"
      else
        render text: "fail"
      end

    end

    private

    def retrieve_order(order_number)
      @order = Spree::Order.find_by_number!(order_number)
    end

    def get_alipay_payment( order )
      #use payment instead of unprocessed_payments, order may be completed.
      order.payments.last
    end

    def complete_order( order, alipay_parameters )
      alipay_payment = get_alipay_payment( order )
      unless order.complete?
        # it require pending_payments to process_payments!
        order.next
      end
      alipay_payment.update_attributes(
        {
          response_code: "#{alipay_parameters['trade_no']},#{alipay_parameters['trade_status']}",
          state: 'completed'
        })
    end
  end
end

{'fraction_non_alphanumeric': 0.07390438247011952, 'fraction_numerical': 0.00039840637450199205, 'mean_word_length': 2.931871574001566, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 2, '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': '7661286', 'n_tokens_mistral': 1596, 'n_tokens_neox': 1566, 'n_words': 302}