From 6c3054f8a422ccfcd9a6e873bb0b364f164f0b1b Mon Sep 17 00:00:00 2001 From: p31729568 Date: Fri, 14 Jun 2019 14:19:56 +0800 Subject: [PATCH 1/3] trainings: add pay and result page --- app/controllers/managements_controller.rb | 2 +- app/controllers/trainings_controller.rb | 50 ++++++-------- app/models/training.rb | 18 +++++ app/models/training_payinfo.rb | 9 ++- app/views/trainings/pay.html.erb | 81 ++++++++++++----------- app/views/trainings/result.html.erb | 30 ++++----- 6 files changed, 103 insertions(+), 87 deletions(-) diff --git a/app/controllers/managements_controller.rb b/app/controllers/managements_controller.rb index b2781417..39e89bd8 100644 --- a/app/controllers/managements_controller.rb +++ b/app/controllers/managements_controller.rb @@ -4151,7 +4151,7 @@ end sheet1 = book.create_worksheet :name => "sheet" blue = Spreadsheet::Format.new :color => :blue, :weight => :bold, :size => 10 sheet1.row(0).default_format = blue - sheet1.row(0).concat(["姓名", "手机号","邮箱","单位名称", "职位","专业", "学号", '授课/研究领域' "支付方式","支付状态","支付时间", "发票类型", "发票抬头","税务登记号", "发票内容", "备注", "微信支付单号", "报名时间"]) + sheet1.row(0).concat(["姓名", "手机号","邮箱","单位名称", "职位","专业", "学号", '授课/研究领域', "支付方式","支付状态","支付时间", "发票类型", "发票抬头","税务登记号", "发票内容", "备注", "微信支付单号", "报名时间"]) count_row = 1 trainings.find_each do |t| sheet1[count_row, 0] = t.name diff --git a/app/controllers/trainings_controller.rb b/app/controllers/trainings_controller.rb index e3e96f11..cd555249 100644 --- a/app/controllers/trainings_controller.rb +++ b/app/controllers/trainings_controller.rb @@ -10,7 +10,7 @@ class TrainingsController < ApplicationController before_filter :authenticate, except: [:auth, :auth_callback, :pay_callback] - before_filter :find_tag_id, only: [:show, :create, :update, :enroll, :test] + before_filter :find_tag_id, except: [:auth, :auth_callback, :pay_callback] before_filter :find_training, only: [:show, :test] @@ -44,10 +44,13 @@ class TrainingsController < ApplicationController end def pay - _pay_params - @training = current_training + if @training.blank? + redirect_to enroll_training_path(id: @tag_id) + return + end + # 防止重复支付,对于已支付过的,不应该再到这个页来 if @training.payed? redirect_to result_training_path(id: @tag_id) @@ -55,7 +58,6 @@ class TrainingsController < ApplicationController end @training.training_payinfo ||= TrainingPayinfo.new - end def pay_callback @@ -98,11 +100,9 @@ class TrainingsController < ApplicationController end def result - _pay_params @training = current_training end - def create @training = current_training || Training.new(params) @training.training_type = @training_type @@ -113,6 +113,7 @@ class TrainingsController < ApplicationController @training.save! flash[:message] = '提交成功' + redirect_to enroll_training_path(id: @tag_id) # redirect_to pay_training_path(id: @tag_id) end @@ -120,8 +121,8 @@ class TrainingsController < ApplicationController def update @training = current_training - unless @training - render_404 + if @training.blank? + redirect_to enroll_training_path(id: @tag_id) return end @@ -132,6 +133,7 @@ class TrainingsController < ApplicationController @training.save! flash[:message] = '提交成功' + redirect_to enroll_training_path(id: @tag_id) # redirect_to pay_training_path(id: @tag_id) end @@ -143,13 +145,11 @@ class TrainingsController < ApplicationController # 采用ajax调用方式,返回支付参数 def update_payinfo @training = current_training - unless @training - render_404 + if @training.blank? + redirect_to enroll_training_path(id: @tag_id) return end - _pay_params - attachment = nil if params[:image] attachment = Attachment.create!(file: params[:image], author: User.first) @@ -163,12 +163,9 @@ class TrainingsController < ApplicationController training_info = TrainingPayinfo.new(params) end - training_info.num = params[:enlistN].to_i - if training_info.num < 1 - training_info.num = 1 - end + training_info.num = params[:enlistN].to_i < 1 ? 1 : params[:enlistN].to_i - training_info.fee = (training_info.num * @pay_fee).to_i + training_info.fee = @training.registration_fee(training_info.num) training_info.attachment = attachment if attachment.present? @@ -183,7 +180,8 @@ class TrainingsController < ApplicationController training_info.save! if params[:js] == 'true' - _pay_js(training_info.fee) + Rails.logger.info("### start wechat pay => fee: #{training_info.fee}") + _pay_js(0.01 || training_info.fee) else redirect_to url = result_training_path(id: @tag_id) end @@ -218,7 +216,7 @@ class TrainingsController < ApplicationController # # 写入wechat_pay付费表 WechatPay.create!(training_id: @training.id, out_trade_no: out_trade_no) - render json: {status: 0, data: unifiedorder(out_trade_no, fee)} + render json: {status: 0, data: unifiedorder(out_trade_no, fee, @training.pay_order_title)} end end @@ -248,9 +246,6 @@ class TrainingsController < ApplicationController end private - def _pay_params - @pay_fee = Redmine::Configuration['training_fee'].to_f || 5000 - end def authenticate if Rails.env.development? @@ -279,7 +274,7 @@ class TrainingsController < ApplicationController def current_training Rails.logger.info("##########openid:#{session[:wechat_open_id]}, training_type: #{@training_type}") - Training.where(openid: session[:wechat_open_id], training_type: @training_type).first + @_current_training ||= Training.where(openid: session[:wechat_open_id], training_type: @training_type).first end def valid_training @@ -326,14 +321,10 @@ class TrainingsController < ApplicationController end - def unifiedorder(out_trade_no, fee) + def unifiedorder(out_trade_no, fee, title) @config = {} - output = Wechat.pay.unifiedorder('湖南警察学院大数据培训会-报名费', - (fee * 100).to_i, - session[:wechat_open_id], - client_ip, - out_trade_no) + output = Wechat.pay.unifiedorder(title, (fee * 100).to_i, session[:wechat_open_id], client_ip, out_trade_no) data = output.fetch("xml") if data.nil? raise "获取微信统一单错误" @@ -361,5 +352,4 @@ class TrainingsController < ApplicationController @config end - end diff --git a/app/models/training.rb b/app/models/training.rb index 7c1333be..cb7f2db7 100644 --- a/app/models/training.rb +++ b/app/models/training.rb @@ -25,4 +25,22 @@ class Training < ActiveRecord::Base research_field.present? && research_field.split(',').include?(str) end + def pay_order_title + case training_type + when 3 then '全国高校大数据和人工智能暑期师资培训会-报名费' + when 4 then '工程教育认证培训会-报名费' + end + end + + def registration_fee(num = 1) + case training_type + when 3 then + # 三人以上 8折 + num >= 3 ? 3000.0 * 0.8 * num : 3000.0 * num + when 4 then + 1200.0 * num + else + raise ArgumentError + end + end end diff --git a/app/models/training_payinfo.rb b/app/models/training_payinfo.rb index 3520c4c0..71f1c793 100644 --- a/app/models/training_payinfo.rb +++ b/app/models/training_payinfo.rb @@ -15,7 +15,12 @@ class TrainingPayinfo < ActiveRecord::Base PayType_bank = 3 #银行卡支付 def pay_type_str - pay_type==PayType_Wechat ? '微信支付' : (pay_type==PayType_alipay ? '支付宝支付' : (pay_type==PayType_bank ? '银行卡支付' : '')) + case pay_type + when PayType_Wechat then '微信支付' + when PayType_alipay then '支付宝支付' + when PayType_bank then '银行卡支付' + else '' + end end def pay_status_str @@ -30,7 +35,7 @@ class TrainingPayinfo < ActiveRecord::Base end def pay_time - WechatPay.where(:out_trade_no => self.out_trade_no).first.try(:created_at) + WechatPay.where(out_trade_no: out_trade_no).first.try(:created_at) end def wechat_wait_pay? diff --git a/app/views/trainings/pay.html.erb b/app/views/trainings/pay.html.erb index ccc4f3eb..875845f7 100644 --- a/app/views/trainings/pay.html.erb +++ b/app/views/trainings/pay.html.erb @@ -1,5 +1,6 @@
+ <%= hidden_field_tag(:tag_id, params[:id]) %>

@@ -39,7 +40,7 @@

发票内容 + value="<%= @training.training_payinfo.try(:invoice_content).presence || '研讨会' %>"/>

@@ -49,32 +50,36 @@ <%= render :partial => 'upload_img', :locals => {:training => @training} %>
-

费用明细(3人及以上8折优惠)

+

费用明细 + <% if @training.training_type == 3 %> + (3人及以上8折优惠) + <% end %> +

含会议注册费、场地费、培训费、教材资料费、餐费、专家差率费等

- - 3000 + + <%= @training.registration_fee %> - - - - - 1 - - - - + + + + + 1 + + + +

" id="offlinePaySubmitBtn"> - 返回 + 返回 提交
" id="paySubmitBtn"> - 返回 + 返回 立即支付
@@ -124,22 +129,20 @@ // // eruda.init(); - // var userInfo = { - // name: '<%= @training.name %>', - // school: '<%= @training.school %>', - // //支付方式,默认为0 - // // 1 微信支付 - // // 2 支付宝支付 - // // 3 银行卡支付 - // payType: <%#= @training.training_payinfo.pay_type.to_i %>, - // - // //发票抬头 - // //为空则代表不需要发票 - // invoiceTitle: '<%#= @training.training_payinfo.invoice_title %>', - // - // //税号 - // invoiceNo: '<%#= @training.training_payinfo.invoice_no %>' - // }; + var userInfo = { + name: '<%= @training.name %>', + school: '<%= @training.school %>', + //支付方式,默认为0 + // 1 微信支付 + // 2 支付宝支付 + // 3 银行卡支付 + payType: <%= @training.training_payinfo.pay_type.to_i %>, + //发票抬头 + //为空则代表不需要发票 + invoiceTitle: '<%= @training.training_payinfo.invoice_title %>', + //税号 + invoiceNo: '<%= @training.training_payinfo.invoice_no %>' + }; //点击弹出发票弹框 aboutBillType(userInfo); @@ -147,13 +150,13 @@ aboutPayType(); //初始化发票类型 - //InitBill(userInfo); + InitBill(userInfo); //切换发票类型 $(".billType li").on("click", function () { $(".billType li").removeClass("active"); $(this).addClass("active"); - billTypeShowFun(parseInt($(this).index())); + billTypeShowFun(parseInt($(this).index()), userInfo); }) //关闭弹窗 $(".billClose").on("click", function () { @@ -205,7 +208,7 @@ // 使用以上方式判断前端返回,微信团队郑重提示: //res.err_msg将在用户支付成功后返回ok,但并不保证它绝对可靠。 setTimeout(function () { - window.location.href = "/trainings/bigdata_hnjcxy_2019/result"; + window.location.href = "/trainings/<%= @tag_id %>/result"; }, 500); } else if (res.err_msg == 'get_brand_wcpay_request:cancel') { @@ -236,7 +239,7 @@ //加减报名人数 function minusNum(item){ var num=parseInt($("#enlistN").html()); - var money=parseFloat(3000).toFixed(2); + var money=parseFloat(<%= @training.registration_fee %>).toFixed(2); var percent=$("#percentPay"); if(num>1){ num=num-1; @@ -259,7 +262,7 @@ } function plusNum(item){ var num=parseInt($("#enlistN").html()); - var money=parseFloat(3000).toFixed(2); + var money=parseFloat(<%= @training.registration_fee %>).toFixed(2); var percent=$("#percentPay"); num=num+1; @@ -297,7 +300,7 @@ $(".billType li").eq(index).addClass("active"); billTypeShowFun(index,userInfo); } - function billTypeShowFun(index){ + function billTypeShowFun(index, userInfo){ if (parseInt(index) == 0) { $(".needWrite").removeClass("none"); $(".billUnit").attr("placeholder", "请填写公司单位名称").val(userInfo.school); @@ -353,7 +356,7 @@ //提交发票信息 - /*function submitBillInfo() { + function submitBillInfo() { var type = $(".billType li.active").html(); var unit = $(".billUnit").val(); var tax = $(".taxNumber").val(); @@ -386,7 +389,7 @@ //记录选中的发票类型 $("#billDemand").attr("status",$(".billType li.active").index()); hideNav($(".billDownNav")); - }*/ + } function InitPhoto() { var tmpl = '
  • ', diff --git a/app/views/trainings/result.html.erb b/app/views/trainings/result.html.erb index 31e845f8..c8a2ad39 100644 --- a/app/views/trainings/result.html.erb +++ b/app/views/trainings/result.html.erb @@ -10,12 +10,12 @@ <%= @training.sex == 1 ? '男' : '女' %>

    - - <%= @training.major %> + + <%= @training.school %>

    - - <%= @training.student_id %> + + <%= @training.position %>

    @@ -55,27 +55,27 @@ <% end %> <% end %> - + <% end %> +

    @@ -98,11 +98,11 @@ <% else %> <% if @training.training_payinfo.wechat_wait_pay? %>
    - <%= link_to "返回", pay_training_path(id: 'bigdata_hnjcxy_2019'), class: 'leftbtn' %> + <%= link_to "返回", pay_training_path(id: @tag_id), class: 'leftbtn' %> 立即支付
    <% elsif !@training.payed? %> - <%= link_to "返回", pay_training_path(id: 'bigdata_hnjcxy_2019'), class: 'returnBtn' %> + <%= link_to "返回", pay_training_path(id: @tag_id), class: 'returnBtn' %> <% end %> <% end %>
    @@ -144,7 +144,7 @@ // 使用以上方式判断前端返回,微信团队郑重提示: //res.err_msg将在用户支付成功后返回ok,但并不保证它绝对可靠。 setTimeout(function () { - window.location.href = "/trainings/bigdata_hnjcxy_2019/result"; + window.location.href = "/trainings/<% @tag_id %>/result"; }, 500); } else if (res.err_msg == 'get_brand_wcpay_request:cancel') { From 3ac89e3f6540be268d7ad3ac0d22701408e3d9c9 Mon Sep 17 00:00:00 2001 From: p31729568 Date: Fri, 14 Jun 2019 16:03:52 +0800 Subject: [PATCH 2/3] training: add offline pay --- app/controllers/trainings_controller.rb | 14 +-- app/views/trainings/enroll.html.erb | 124 ++++++++---------------- app/views/trainings/pay.html.erb | 30 +++--- app/views/trainings/result.html.erb | 12 ++- 4 files changed, 79 insertions(+), 101 deletions(-) diff --git a/app/controllers/trainings_controller.rb b/app/controllers/trainings_controller.rb index cd555249..0f291f4e 100644 --- a/app/controllers/trainings_controller.rb +++ b/app/controllers/trainings_controller.rb @@ -57,7 +57,11 @@ class TrainingsController < ApplicationController return end - @training.training_payinfo ||= TrainingPayinfo.new + @training.training_payinfo ||= begin + payinfo = TrainingPayinfo.new + payinfo.fee = @training.registration_fee # 默认值价格不对 + payinfo + end end def pay_callback @@ -114,8 +118,7 @@ class TrainingsController < ApplicationController flash[:message] = '提交成功' - redirect_to enroll_training_path(id: @tag_id) - # redirect_to pay_training_path(id: @tag_id) + redirect_to pay_training_path(id: @tag_id) end @@ -134,8 +137,7 @@ class TrainingsController < ApplicationController flash[:message] = '提交成功' - redirect_to enroll_training_path(id: @tag_id) - # redirect_to pay_training_path(id: @tag_id) + redirect_to pay_training_path(id: @tag_id) end @@ -163,7 +165,7 @@ class TrainingsController < ApplicationController training_info = TrainingPayinfo.new(params) end - training_info.num = params[:enlistN].to_i < 1 ? 1 : params[:enlistN].to_i + training_info.num = params[:enlistNum].to_i < 1 ? 1 : params[:enlistNum].to_i training_info.fee = @training.registration_fee(training_info.num) diff --git a/app/views/trainings/enroll.html.erb b/app/views/trainings/enroll.html.erb index 6b898691..84a88510 100644 --- a/app/views/trainings/enroll.html.erb +++ b/app/views/trainings/enroll.html.erb @@ -152,57 +152,25 @@ name="position" value="<%= @training.position %>"/> +
  • + 手机号码 + +
  • + <% if @training_type == 3 %> -

    您的授课/研究领域(最少选1项)

    -
  • - 机器学习/深度学习 - - id="check_1" class="magic-checkbox"> - - -
  • -
  • - 计算机结构与组成 - - id="check_2" class="magic-checkbox"> - - -
  • -
  • - 计算机系统 - - id="check_3" class="magic-checkbox"> - - -
  • -
  • - 计算机软件技术和应用 - - id="check_4" class="magic-checkbox"> - - -
  • -
  • - 计算机理论 - - id="check_5" class="magic-checkbox"> - - -
  • -
  • - 计算机应用与学科交叉 - - id="check_6" class="magic-checkbox"> - - -
  • -
  • - 其他 - - id="check_7" class="magic-checkbox"> - - -
  • +

    您关注的课程类型(最少选1项)

    + + <% %w(程序设计 操作系统 数据库 软件工程 云计算 大数据 人工智能 计算机网络 物联网 信息安全 其他).each_with_index do |name, index| %> +
  • + <%= name %> + + id="check_<%= index %>" class="magic-checkbox"> + + +
  • + <% end %> <% end %> @@ -214,12 +182,6 @@ -
  • - 手机号码 - -
  • @@ -258,40 +220,40 @@ //下一步 function NextStep() { - var username = $("#username").val(); - var userMajor = $("#userMajor").val(); - var userphone = $("#userphone").val(); - var userID = $("#userID").val(); + var username = $("#username").val(); + var userUnit = $("#userUnit").val(); + var userPositional = $("#userPositional").val(); + var userphone = $("#userphone").val(); - if (username == '') { - alert("请输入真实姓名"); - return; - } - if (userMajor == '') { - alert("请输入专业"); - return; - } + if (username == '') { + alert("请输入姓名"); + return; + } + if (userUnit == '') { + alert("请输入单位名称或者学校"); + return; + } - if (userID == '') { - alert("请输入学号"); - return; - } + if (userPositional == '') { + alert("请输入职称"); + return; + } + + if (userphone == '') { + alert("请输入手机号码"); + return; + } else if (!/^1\d{10}$/.test(userphone) && !/^[a-zA-Z0-9]+([.\-_\\]*[a-zA-Z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/.test(userphone)) { + alert("请输入正确的手机号码"); + return; + } <% if @training_type == 3 %> if ($("input[type='checkbox'][name='research_field[]']:checked").length == 0) { - alert('请选择授课类型/研究领域'); + alert('请选择您关注的课程类型'); return; } <% end %> - if (userphone == '') { - alert("请输入手机号码"); - return; - } else if (!/^1\d{10}$/.test(userphone) && !/^[a-zA-Z0-9]+([.\-_\\]*[a-zA-Z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/.test(userphone)) { - alert("请输入正确的手机号码"); - return; - } - $('form').submit(); } diff --git a/app/views/trainings/pay.html.erb b/app/views/trainings/pay.html.erb index 875845f7..8474233f 100644 --- a/app/views/trainings/pay.html.erb +++ b/app/views/trainings/pay.html.erb @@ -1,13 +1,16 @@
    <%= hidden_field_tag(:tag_id, params[:id]) %> -
    + +

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

    -
    mt5" id="payInfos"> + +
    @@ -57,15 +60,15 @@

    含会议注册费、场地费、培训费、教材资料费、餐费、专家差率费等

    - - - <%= @training.registration_fee %> + + + <%= @training.training_payinfo.fee || @training.registration_fee %> - 1 + <%= @training.training_payinfo.num || 1 %> @@ -73,12 +76,12 @@

    -
    " id="offlinePaySubmitBtn"> - 返回 - 提交 +
    " id="offlinePaySubmitBtn"> + 稍后支付 + 立即支付
    -
    " id="paySubmitBtn"> +
    " id="paySubmitBtn"> 返回 立即支付
    @@ -120,7 +123,7 @@
    - + @@ -136,7 +139,8 @@ // 1 微信支付 // 2 支付宝支付 // 3 银行卡支付 - payType: <%= @training.training_payinfo.pay_type.to_i %>, + // payType: <%#= @training.training_payinfo.pay_type.to_i %>, + payType: 3, //发票抬头 //为空则代表不需要发票 invoiceTitle: '<%= @training.training_payinfo.invoice_title %>', diff --git a/app/views/trainings/result.html.erb b/app/views/trainings/result.html.erb index c8a2ad39..c63a24c5 100644 --- a/app/views/trainings/result.html.erb +++ b/app/views/trainings/result.html.erb @@ -29,6 +29,14 @@
    -->
    +

    + + <%= @training.training_payinfo.num %>人 +

    +

    + + ¥<%= @training.training_payinfo.fee %> +

    <%= @training.training_payinfo.pay_status_str %> @@ -85,8 +93,10 @@ <%= render :partial => 'upload_img', :locals => {:training => @training} %> <% end %> -

    ¥<%= @training.training_payinfo.fee %><%= @training.training_payinfo.num %>人

    +
    +
    +

    @@ -77,7 +79,7 @@

    " id="offlinePaySubmitBtn"> - 稍后支付 + 稍后支付 立即支付
    @@ -235,6 +237,14 @@ } $('form').submit(); }); + $('#laterSubmitFormBtn').on('click', function () { + if($("#billDemand").attr("status")==2){ + $(".billInput").val(''); + } + $('#offline_later_pay').val('true'); + $('form').submit(); + }); + //上传头像 InitPhoto();