From 65c91f890a1df521e4494fe968d87e4c69699e9b Mon Sep 17 00:00:00 2001 From: p31729568 Date: Wed, 19 Jun 2019 09:18:19 +0800 Subject: [PATCH] open wechat pay method --- app/controllers/trainings_controller.rb | 37 +++++++--------------- app/models/training.rb | 2 +- app/views/trainings/pay.html.erb | 24 +++++++------- app/views/trainings/result.html.erb | 6 ++-- app/views/trainings/trainingsInfo.html.erb | 7 +--- 5 files changed, 30 insertions(+), 46 deletions(-) diff --git a/app/controllers/trainings_controller.rb b/app/controllers/trainings_controller.rb index d97cd53b..8650a5a7 100644 --- a/app/controllers/trainings_controller.rb +++ b/app/controllers/trainings_controller.rb @@ -13,16 +13,6 @@ class TrainingsController < ApplicationController before_filter :check_training_type, only: [:enroll] before_filter :check_current_training, only: [:show, :update, :pay, :pay_js, :update_payinfo, :result] - # TODO: 测试完成后删除 - ALLOW_OPENID_LIST = [ - 'oSrqE52fgGMooW3B1K4mZ0Le5G9I' - ] - before_filter :check_openid, only: [:update_payinfo] - def check_openid - return if ALLOW_OPENID_LIST.include?(session[:wechat_open_id]) - render plain: '暂未开放' - end - layout 'base_trainings' def show @@ -33,18 +23,22 @@ class TrainingsController < ApplicationController return end - # TODO: 测试完成后删除 - @test_wxpay = ALLOW_OPENID_LIST.include?(session[:wechat_open_id]) - render 'trainingsInfo' end def enroll @training = current_training || Training.new - if @training.training_payinfo.present? && params[:disable_redirect].blank? - redirect_to result_training_path(id: friendly_id) - return + if params[:disable_redirect].blank? + if @training.training_payinfo.present? + redirect_to result_training_path(id: friendly_id) + return + end + + unless @training.new_record? + redirect_to training_path(id: friendly_id) + return + end end end @@ -56,9 +50,6 @@ class TrainingsController < ApplicationController @training.training_payinfo.fee = @training.registration_fee @training.training_payinfo.pay_type = params[:pay_type].presence || 3 end - - # TODO: 测试完成后删除 - @test_wxpay = ALLOW_OPENID_LIST.include?(session[:wechat_open_id]) end def pay_callback @@ -164,13 +155,8 @@ class TrainingsController < ApplicationController training_info.save! - # TODO: 测试完成后删除 - @test_wxpay = ALLOW_OPENID_LIST.include?(session[:wechat_open_id]) - if params[:js] == 'true' && training_info.not_payed? - Rails.logger.info("### start wechat pay => fee: #{training_info.fee}") - # TODO: 测试完成后删除 - _pay_js(@test_wxpay ? 0.01 : training_info.fee) + _pay_js(training_info.fee) else redirect_to result_training_path(id: friendly_id) end @@ -199,6 +185,7 @@ class TrainingsController < ApplicationController #js获取支付参数 def _pay_js(fee) + Rails.logger.info("### start wechat pay => fee: #{fee}") @training = current_training js_function_call do out_trade_no = Wechat.pay.gen_trade_no diff --git a/app/models/training.rb b/app/models/training.rb index cb7f2db7..58de07ab 100644 --- a/app/models/training.rb +++ b/app/models/training.rb @@ -38,7 +38,7 @@ class Training < ActiveRecord::Base # 三人以上 8折 num >= 3 ? 3000.0 * 0.8 * num : 3000.0 * num when 4 then - 1200.0 * num + 700.0 * num else raise ArgumentError end diff --git a/app/views/trainings/pay.html.erb b/app/views/trainings/pay.html.erb index 903876cf..9aebe19f 100644 --- a/app/views/trainings/pay.html.erb +++ b/app/views/trainings/pay.html.erb @@ -2,18 +2,10 @@
<%= hidden_field_tag(:friendly_id, @friendly_id) %> - <% if @test_wxpay %> -
- <% else %> -
- <% end %> +
>

- <% if @test_wxpay %> - <%= @training.training_payinfo.pay_type.to_i == 3 ? "线下支付" : "微信支付" %> - <% else %> - 线下支付 - <% end %> + <%= @training.training_payinfo.pay_type.to_i == 3 ? "线下支付" : "微信支付" %>

mt5" id="payInfos"> @@ -107,7 +99,11 @@
" id="paySubmitBtn"> 返回 - 立即支付 + <% if @training.training_payinfo.not_payed? %> + 立即支付 + <% else %> + 提交 + <% end %>
@@ -266,6 +262,12 @@ $('form').submit(); }); + // 微信支付后修改内容提交 + $("#submitWxpayFormBtn").on('click', function () { + if(!checkBillInfo()){ return; } + $('form').submit(); + }); + //上传头像 InitPhoto(); diff --git a/app/views/trainings/result.html.erb b/app/views/trainings/result.html.erb index d43da5f0..c71758ab 100644 --- a/app/views/trainings/result.html.erb +++ b/app/views/trainings/result.html.erb @@ -121,7 +121,7 @@ // eruda.init(); function wechatPay() { $.ajax({ - url: '<%= pay_js_trainings_path %>', + url: '<%= pay_js_trainings_path(friendly_id: @friendly_id) %>', type: "POST", data: '{}' } @@ -147,7 +147,7 @@ // 使用以上方式判断前端返回,微信团队郑重提示: //res.err_msg将在用户支付成功后返回ok,但并不保证它绝对可靠。 setTimeout(function () { - window.location.href = "/trainings/<% @tag_id %>/result"; + window.location.href = "/trainings/<% @friendly_id %>/result"; }, 500); } else if (res.err_msg == 'get_brand_wcpay_request:cancel') { @@ -189,7 +189,7 @@ var data = new FormData(); data.append('image', files[0]); - data.append('tag_id', '<%= @tag_id %>'); + data.append('friendly_id', '<%= @friendly_id %>'); $.ajax({ url: '<%= update_picture_trainings_path %>', diff --git a/app/views/trainings/trainingsInfo.html.erb b/app/views/trainings/trainingsInfo.html.erb index e71b9c07..964455bc 100644 --- a/app/views/trainings/trainingsInfo.html.erb +++ b/app/views/trainings/trainingsInfo.html.erb @@ -41,12 +41,7 @@ <% if @training.training_payinfo.blank? || @training.training_payinfo.not_payed? %>
<%= link_to '转账支付', pay_training_path(id: @friendly_id, pay_type: 3), class: 'leftbtn' %> - - <% if @test_wxpay %> - <%= link_to '微信支付', pay_training_path(id: @friendly_id, pay_type: 1), class: 'rightbtn' %> - <% else %> - 微信支付 - <% end %> + <%= link_to '微信支付', pay_training_path(id: @friendly_id, pay_type: 1), class: 'rightbtn' %>
<% else %>