Permalink
Browse files

refactor alipay config. #401

  • Loading branch information...
1 parent 2825690 commit 15541d6d2d3f5202de0d9a6af4baa098dc3cfad2 @saberma saberma committed Feb 5, 2012
@@ -1,9 +1,30 @@
App.Models.Payment = Backbone.Model.extend
name: 'payment'
+ url: ->
+ if @id
+ "/admin/payments/#{@id}"
+ else
+ "/admin/payments"
validate: (attrs) ->
- if attrs.name == ""
- "方式不能为空"
+ errors = {}
+ errors["方式"] = "不能为空." if attrs.name is ''
+ if attrs.payment_type_id is 1 # 支付宝
+ errors["合作者身份ID"] = "不能为空." if attrs.partner is ''
+ errors["帐号"] = "不能为空." if attrs.account is ''
+ errors["交易安全校验码"] = "不能为空." if !attrs.id? and attrs.key is '' # 新增时才校验
+
+ if _(errors).size() is 0
+ return
+ else
+ messages = _(errors).map (value, key) -> "#{key} #{value}"
+ error_msg messages.join(' ')
+ errors
+
+ toJSON : ->
+ @unset 'service_name', silent: true
+ @unset 'id', silent: true
+ attrs = this.wrappedAttributes()
App.Collections.Payments = Backbone.Collection.extend
model: App.Models.Payment
@@ -0,0 +1,69 @@
+App.Views.Payment.Alipay = Backbone.View.extend
+ el: '#payment_alipay'
+
+ events:
+ 'change .gateway-id' : 'select'
+ 'click .payment_edit' : 'edit' # 编辑
+ 'submit form' : 'save' # 保存
+ "click .cancel" : "cancel" # 取消
+ "click .destroy" : "destroy" # 删除
+
+ initialize: ->
+ @render()
+
+ render: ->
+ configed = @model.get('id')?
+ @$('.activate_payment_provider strong').html "支付宝#{@model.get('service_name')}服务"
+ @$('.payment_select').toggle !configed
+ @$('.activate_payment_provider').toggle configed
+ @$('.account_payment_provider').hide()
+
+ select: -> # 显示表单
+ selected = @$('.gateway-id').val() is '1'
+ @edit() if selected
+ @$('.account_payment_provider').toggle(selected)
+
+ edit: -> # 编辑
+ self = this
+ template = Handlebars.compile $('#payment-alipay-form-item').html()
+ @$('.payment-gateway-form').html template @model.attributes
+ @$('.account_payment_provider form select').each -> $(this).val self.model.get($(this).attr('name')) # 显示下拉框
+ @$('.account_payment_provider').show()
+ @$('.activate_payment_provider').hide()
+ false
+
+ save: ->
+ self = this
+ @model.save {
+ partner: self.$("input[name='partner']").val(),
+ account: self.$("input[name='account']").val(),
+ key: self.$("input[name='key']").val(),
+ service: self.$("select[name='service']").val(),
+ payment_type_id: 1,
+ },
+ success: (model, resp) ->
+ msg '修改成功!'
+ self.render()
+ false
+
+ cancel: ->
+ @$('.account_payment_provider').hide()
+ if @model.get('id')? # 编辑时
+ @$('.activate_payment_provider').show()
+ @$('.payment_select').hide()
+ else # 新增时
+ @$('.gateway-id').val 0
+ false
+
+ destroy: ->
+ self = this
+ if confirm '您确定要删除吗'
+ @model.destroy
+ success: (model, response) ->
+ self.$('.payment_select').show()
+ self.$('.account_payment_provider').hide()
+ self.$('.activate_payment_provider').hide()
+ msg '删除成功!'
+ model.clear()
+ model.id = null
+ return false
@@ -1,44 +1,31 @@
App.Views.Payment.Index = Backbone.View.extend
el: '#main'
+
events:
'change #select_custom_payment_method' : 'select_method'
'click #checkout' : 'change_checkout_config'
'submit form#new_custom_payment' : 'save'
initialize: ->
+ new App.Views.Payment.Alipay model: App.payment_alipay # 支付宝
self = this
@collection.view = this
_.bindAll this, 'render'
@collection.bind 'add',(model) ->
new App.Views.Payment.Show model: model
- this.render()
+ @render()
$('#shop_customer_accounts_required').click ->
$('#customer-accounts-required').show()
$('#shop_customer_accounts_optional,#shop_customer_accounts_').each ->
$(this).click ->
$('#customer-accounts-required').hide()
- $('#alipay-gateway-id').change ->
- $('#account_payment_provider').toggle()
- $('#alipay_edit').click ->
- $('#account_payment_provider').toggle()
- $('#activate_payment_provider').hide()
- $('#cancel').click ->
- $('#account_payment_provider').toggle()
- $('#activate_payment_provider').toggle()
- $('#alipay-gateway-id option:eq(0)').attr 'selected', true
- false
-
$('#cancel_custom_payment_form').click ->
$('#account_manual_payment_gateway').hide()
$('#select_custom_payment_method option:eq(0)').attr 'selected', true
false
- if $('.field-with-errors input').size() > 0
- $('#account_payment_provider').show()
- $('#alipay-gateway-id option:eq(1)').attr 'selected', true
-
render: ->
_(@collection.models).each (model) ->
new App.Views.Payment.Show model: model
@@ -36,3 +36,13 @@ input.placeholder_text, textarea.placeholder_text, ::-webkit-input-placeholder {
position: absolute;
cursor: default;
z-index: 4000 !important }
+
+/* 支付页面 */
+.gateway-select {
+ input {
+ vertical-align: middle;
+ margin-left: 15px; }
+ label {
+ font-weight: normal;
+ color: #777;
+ font-size: 12px; } }
@@ -4,8 +4,11 @@ class Admin::PaymentsController < Admin::AppController
prepend_before_filter :authenticate_user!
expose(:shop){ current_user.shop }
expose(:payments){ shop.payments }
- expose(:payment_alipay){ shop.payments.where(:payment_type_id => 1).first || shop.payments.new}
+ expose(:payment_alipay){ shop.payments.where(payment_type_id: 1).first }
+ expose(:payment_alipay_json){ payment_alipay.to_json(except: [:created_at, :updated_at], methods: [:service_name]) }
+ expose(:payment_tenpay){ shop.payments.where(payment_type_id: 2).first }
expose(:payment)
+ expose(:payment_json){ payment.to_json(except: [:created_at, :updated_at], methods: [:service_name]) }
expose(:policy_types){ KeyValues::PolicyType.all }
expose(:policies){ shop.policies }
expose(:all_custom_types){ KeyValues::Payment::Custom.all.map{|c|[c.name,c.name]}}
@@ -23,38 +26,17 @@ def index
end
def create
- #处理支付宝部分
- if params[:payment][:payment_type_id]
- payment_alipay.attributes = params[:payment]
- if payment_alipay.save
- redirect_to payments_path, notice: notice_msg
- else
- render action: 'index'
- end
- #处理普通支付部分
- else
- payment.attributes = params[:payment]
- payment.save
- render json: payment
- end
+ payment.save
+ render json: payment_json
end
def update
- if payment.save
- respond_to do |format|
- format.html { redirect_to payments_path, notice: notice_msg }
- format.json { render json: payment }
- end
- else
- render action: 'index'
- end
+ payment.save
+ render json: payment_json
end
def destroy
payment.destroy
- respond_to do |format|
- format.js { render js: "window.location='#{payments_path}';msg('删除成功!')" }
- format.json { render json: payment }
- end
+ render json: nil
end
end
@@ -0,0 +1,65 @@
+#payment_alipay.payment-service-provider
+ %table
+ %tr
+ %td.fl(style="padding:20px 10px 20px 20px;width:225px")
+ =link_to image_tag('admin/payments/alipay.png'), 'https://b.alipay.com/order/productSign.htm?action=newsign&productId=2011011904422299', title: '申请', target: '_blank'
+ %td
+ %table.desc
+ %tr
+ %td.r 支付方式:
+ %td.meta
+ %strong 在线支付-支付宝
+ %tr
+ %td.r 描述:
+ %td.meta==#{link_to '支付宝', 'http://www.alipay.com'}致力于为中国电子商务提供各种安全、方便、个性化的在线支付解决方案。
+ %dl.cl.sst(style="padding:0px; margin-bottom: 0; border-top: 1px solid #ddd")
+ %dd.payment_select.hide(style="margin:0; padding-bottom: 10px")
+ %p.gateway-select(style="padding-top:10px; padding-left:10px")
+ %select.gateway-id(name="enabled_gateway")
+ %option(value="0") 不使用支付宝
+ %option(value="1") 使用支付宝
+ %dd.hide.activate_payment_provider(style="padding: 10px 10px 5px 10px")
+ .fr
+ %a.destroy(href="#")=image_tag 'admin/icons/trash.gif'
+ 当前正在使用:
+ %strong 支付宝即时到帐服务
+ %span.warn=link_to w('edit'), '#', class: :payment_edit
+ %dd.account_payment_provider.nsb.alipay-properties(style="display:none;border-top: 1px solid #ddd;")
+ %form(action="#" onsubmit="return false")
+ .group-fields
+ %table
+ %tr
+ %td.payment-gateway-form(style="vertical-align: top; padding: 20px 20px 20px 0px; width: 180px")
+ %td.provider-info
+ %div
+ %p.note
+ %strong
+ 请提供您
+ %a(href="http://www.alipay.com" target="gateway") 支付宝
+ 账户的合作者身份ID(partner)、交易安全校验码(key)以及账户,若没有,则请您登陆到
+ %a(href="https://b.alipay.com/order/productDetail.htm?productId=2011060800327555" target="gateway") 支付宝商家服务
+ 进行申请
+ / .group-fields
+ .group-actions
+ %input.btn(name="commit" type="submit" value="保存")
+ =w('or')
+ =link_to w('cancel'), '#', class: 'cancel'
+ / .group-actions
+
+%script(id="payment-alipay-form-item" type="text/x-handlebars-template")
+ .sb
+ %label(for="payment_alipay_partner") 合作者身份ID
+ %p.credential
+ %input#payment_alipay_partner(autocomplete="off" name="partner" size="30" type="text" value="{{partner}}")
+ .sb
+ %label(for="payment_alipay_account") 帐号
+ %p.credential
+ %input#payment_alipay_account(autocomplete="off" name="account" size="30" type="text" value="{{account}}")
+ .sb
+ %label(for="payment_alipay_key") 交易安全校验码
+ %p.credential
+ %input#payment_alipay_key(autocomplete="off" name="key" size="30" type="password" value="{{key}}")
+ .sb
+ %label(for="payment_alipay_service") 接口类型
+ %p.credential
+ %select#payment_alipay_service(name="service")=options_for_select service_types
Oops, something went wrong.

0 comments on commit 15541d6

Please sign in to comment.