0

我正在开发一个需要使用 ipn 的应用程序,但它似乎效果不佳。

我正在尝试在成功操作中获取通知,并在贝宝沙箱中指定了正确的 url。

def success
        topup = current_user.topups.last
        logger.debug "topup -------->"
        logger.debug topup.amount.to_i
        # raise request
        details = EXPRESS_GATEWAY.details_for(topup.express_token)
        logger.debug "details ------->"
        logger.debug details.payer_id
        # raise params[:payer_id]
        response = EXPRESS_GATEWAY.purchase(topup.price_in_cents,{
          :ip               => request.remote_ip,
          :token            => topup.express_token,
          :payer_id         => details.payer_id
        })

        logger.debug "Response starts here --------->"
        logger.debug response

        if response.success?
            amount = topup.amount.to_i
            current_user.credits = current_user.credits.to_i +  amount
            current_user.save!
            flash[:success] = "Thank you for the top up"
            # @account_history = current_user.account_histories.build
            # @account_history.update_attribute(:type => "Top Up", :details => "", :amount => amount, :user_id => current_user.id)
            redirect_to current_user


            notify = Paypal::Notification.new request.raw_post

            logger.info "Notifying --------->"
            logger.info notify
            logger.info notify.status
            logger.info "Notifying 2 --------->"
            logger.info notify.acknowledge

            logger.debug notify.status

            if notify.acknowledge
                logger.debug "Notifying --------->"
                logger.debug notify.mc_gross
                logger.debug notify.txn_id
            end

        else
            redirect_to root_url
        end

    end

notify.acknowledge 不返回任何内容(为空白)

4

1 回答 1

0

在敲了很长时间之后,我能够解决这个问题。发布我的解决方案以防万一有人陷入类似情况:-

一件重要的事情是 IPN 将发布数据发送到 IPN 侦听器,因此请确保您为此定义了一个路由。

我也有一个before_filter :authenticate_user!,因此我得到了 html 代码 401。必须在过滤器之前跳过通知操作。

def pay_with_account
        topup = Topup.find(params[:id])
        response = EXPRESS_GATEWAY.setup_purchase(topup.price_in_cents,{
          :ip                => request.remote_ip,
          :currency_code     => 'GBP',
          :return_url        => topups_success_url(topup),
          :notify_url        => topups_notify_url(topup),
          :cancel_return_url => topups_cancel_url(topup),
          # :allow_guest_checkout => true,
          :items => [{:name => "Topup", :quantity => 1,:description => "Top up my account", :amount => topup.price_in_cents}]
        })
        topup.update_attribute :express_token, response.token
        redirect_to EXPRESS_GATEWAY.redirect_url_for(response.token)
    end

这是返回网址:-

def success
        topup = current_user.topups.last
        details = EXPRESS_GATEWAY.details_for(topup.express_token)
        response = EXPRESS_GATEWAY.purchase(topup.price_in_cents,{
          :ip               => request.remote_ip,
          :currency_code    => 'GBP',
          :token            => topup.express_token,
          :payer_id         => details.payer_id
        })

        if response.success?
            amount = topup.amount.to_i
            current_user.credits = current_user.credits.to_i +  amount
            current_user.save!
            redirect_to user_payments_path(current_user)
            flash[:success] = "Your transaction was successfully completed"
        else
            flash[:error] = "Your transaction could not be compelted"
            redirect_to user_payments_path(current_user)
        end

    end

这是通知网址:-

def notify
        notify = Paypal::Notification.new(request.raw_post) 

        if notify.acknowledge
            @account_history = topup.user.account_histories.build
            @account_history.update_attributes(:payment_type => "Top up",:status => notify.status, :amount => notify.gross)
            if params[:payer_status] == "verified"
                @account_history.update_attributes(:details => "Pay Pal #{@account_history.id}")
            elsif params[:payer_status] == "unverified"
                @account_history.update_attributes(:details => "Credit Card #{@account_history.id}")
            end
            @account_history.save
        end

         render :nothing => true
    end

路线:-

  get "topups/pay_with_account"
  get "topups/pay_without_account"
  get "topups/success"
  get "topups/cancel"
  get "topups/notify"
  post "topups/notify
于 2012-08-29T19:28:34.410 回答