spree_alipay 1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: fd273af5fd6e08e188a44d019daac32b90b7a9bf
4
+ data.tar.gz: d29c3da8caba4fb3271ee4b55eefe897c37709de
5
+ SHA512:
6
+ metadata.gz: 393ca0096bc7f8b6e6257d976bbb5a28d4d662b78d05e2840422732470943278ac7f10421b90f100543eef23563c7eafc7276caa02ba7fa38949f16cad25df1d
7
+ data.tar.gz: 7a605a1b950cfebb1d579fe12f32a977dfe89e36e8ecfb364ad0fa190445d6d0766545f4ed113ea5aac8633c8722b6367de0af640af27d3770d0db3230d6e4e6
data/README.md ADDED
@@ -0,0 +1 @@
1
+ # spree_alipay
@@ -0,0 +1,17 @@
1
+ Spree::CheckoutController.class_eval do
2
+
3
+ before_filter :redirect_for_alipay, :only => :update
4
+
5
+ private
6
+
7
+ def redirect_for_alipay
8
+ return unless params[:state] == "payment"
9
+ if params[:order][:payments_attributes].present?
10
+ @payment_method = Spree::PaymentMethod.find(params[:order][:payments_attributes].first[:payment_method_id])
11
+ end
12
+ if @payment_method && @payment_method.kind_of?(Spree::PaymentMethod::Alipay)
13
+ # @order.update_attributes(object_params)
14
+ redirect_to gateway_alipay_path(:gateway_id => @payment_method.id, :id => @order.number)
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,79 @@
1
+ require 'base64'
2
+ require 'digest/md5'
3
+ require 'spree_alipay/rc4'
4
+
5
+ module Spree
6
+ class Gateway::AlipayController < Spree::StoreController
7
+ helper 'spree/orders'
8
+ skip_before_filter :verify_authenticity_token, :only => [:comeback]
9
+
10
+ # Show form Alipay for payment
11
+ def show
12
+ load_order
13
+ @order.payments.destroy_all
14
+ @hash = Digest::MD5.hexdigest([@gateway.preferred_secret_key, @gateway.preferred_account_id,
15
+ @order.total.to_s, @order.number, [gateway_alipay_comeback_url(@order),'DR={DR}'].join('?'), @gateway.preferred_mode].join('|'))
16
+ payment = @order.payments.create!(:amount => 0, :payment_method_id => @gateway.id)
17
+ if @order.blank? || @gateway.blank?
18
+ flash[:error] = Spree.t(:invalid_arguments)
19
+ redirect_to :back
20
+ else
21
+ @bill_address, @ship_address = @order.bill_address, (@order.ship_address || @order.bill_address)
22
+ render :action => :show
23
+ end
24
+ end
25
+
26
+
27
+
28
+ def comeback
29
+ load_order
30
+ @data = params[:RespCode]
31
+ if @data.present? && @data == "00"
32
+
33
+ alipay_payment_success
34
+
35
+ redirect_to order_url(@order, {:checkout_complete => true, :token => @order.token}), :notice => Spree.t(:payment_success)
36
+ else
37
+ flash[:error] = Spree.t(:alipay_payment_response_error, {:error_message => @data["ResponseMessage"]})
38
+ redirect_to (@order.blank? ? root_url : edit_order_url(@order, {:token => @order.token}))
39
+ end
40
+
41
+ end
42
+
43
+
44
+
45
+
46
+ private
47
+ def load_order
48
+ @order = current_order || Spree::Order.find_by_number(params[:id])
49
+ @gateway = params[:gateway_id].blank? ? @order.payments.last.payment_method : @order.available_payment_methods.find{|x| x.id == params[:gateway_id].to_i }
50
+ end
51
+
52
+
53
+
54
+ # save the payment record and complete the order
55
+ def alipay_payment_success
56
+ source = Spree::Alipayinfo.create(:first_name => @order.bill_address.firstname, :last_name => @order.bill_address.lastname, :transaction_id => @data["TransactionID"], :payment_id => @data["PaymentID"], :amount => @data["Amount"], :order_id => @order.id)
57
+
58
+ alipay_payment_method = Spree::PaymentMethod::Alipay.last
59
+ payment = @order.payments.where(:payment_method_id => alipay_payment_method.id).first
60
+ payment = @order.payments.create!(:amount => 0, :payment_method_id => alipay_payment_method.id) if payment.blank?
61
+ payment.source = source
62
+ payment.amount = source.amount
63
+ payment.response_code = @data["ResponseCode"]
64
+ payment.avs_response = @data["ResponseMessage"]
65
+ payment.save
66
+ payment.complete!
67
+
68
+ @order.reload
69
+ @order.next
70
+ @order.state = 'complete'
71
+ @order.save
72
+
73
+ session[:order_id] = nil
74
+
75
+ @order.finalize!
76
+ end
77
+
78
+ end
79
+ end
@@ -0,0 +1,35 @@
1
+ module Spree
2
+ class Alipayinfo < ActiveRecord::Base
3
+ # attr_accessible :first_name, :last_name, :transaction_id, :payment_id, :amount, :order_id
4
+
5
+ belongs_to :order, :class_name => 'Spree::Order'
6
+
7
+ NECESSARY = %w(Mode PaymentID DateCreated MerchantRefNo Amount TransactionID ResponseCode ResponseMessage).freeze
8
+
9
+ def actions
10
+ %w(mark_as_captured void)
11
+ end
12
+
13
+ # Indicates whether it's possible to capture the payment
14
+ def can_mark_as_captured?(payment)
15
+ ['checkout', 'pending'].include?(payment.state)
16
+ end
17
+
18
+ # Indicates whether it's possible to void the payment.
19
+ def can_void?(payment)
20
+ payment.state != 'void'
21
+ end
22
+
23
+ def mark_as_captured(payment)
24
+ payment.update_attribute(:state, 'pending') if payment.state == 'checkout'
25
+ payment.complete
26
+ true
27
+ end
28
+
29
+ def void(payment)
30
+ payment.update_attribute(:state, 'pending') if payment.state == 'checkout'
31
+ payment.void
32
+ true
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,14 @@
1
+ module Spree
2
+ class PaymentMethod::Alipay < PaymentMethod
3
+
4
+ preference :account_id, :string
5
+ preference :url, :string, :default => "https://pay.veritrans-link.com/epayment/payment"
6
+ preference :secret_key, :string
7
+ preference :mode, :string
8
+ preference :currency_code, :string
9
+
10
+ def payment_profiles_supported?
11
+ false
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,44 @@
1
+ module SpreeAlipay
2
+ class RC4
3
+
4
+ def initialize(str)
5
+ begin
6
+ raise SyntaxError, "RC4: Key supplied is blank" if str.eql?('')
7
+
8
+ @q1, @q2 = 0, 0
9
+ @key = []
10
+ str.each_byte {|elem| @key << elem} while @key.size < 256
11
+ @key.slice!(256..@key.size-1) if @key.size >= 256
12
+ @s = (0..255).to_a
13
+ j = 0
14
+ 0.upto(255) do |i|
15
+ j = (j + @s[i] + @key[i] )%256
16
+ @s[i], @s[j] = @s[j], @s[i]
17
+ end
18
+ end
19
+ end
20
+
21
+ def encrypt!(text)
22
+ process text
23
+ end
24
+
25
+ def encrypt(text)
26
+ process text.dup
27
+ end
28
+
29
+ alias_method :decrypt, :encrypt
30
+
31
+ private
32
+
33
+ def process(text)
34
+ text.unpack("C*").map { |c| c ^ round }.pack("C*")
35
+ end
36
+
37
+ def round
38
+ @q1 = (@q1 + 1)%256
39
+ @q2 = (@q2 + @s[@q1])%256
40
+ @s[@q1], @s[@q2] = @s[@q2], @s[@q1]
41
+ @s[(@s[@q1]+@s[@q2])%256]
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,29 @@
1
+ <table class="index">
2
+ <tr>
3
+ <th><%= "#{t('spree.date')}/#{t('spree.time')}" %></th>
4
+ <th><%= t("amount") %></th>
5
+ <th><%= t("payment_method") %></th>
6
+ <th> Transaction ID </th>
7
+ <th></th>
8
+ </tr>
9
+ <% payments.each do |payment| %>
10
+ <tr>
11
+ <td><%= payment.created_at.to_s(:date_time24) %></td>
12
+ <td><%= number_to_currency(payment.amount) %></td>
13
+ <td><%= payment_method_name payment %></td>
14
+ <td><%= payment.source.TransactionId %></td>
15
+
16
+ <td>
17
+ <% unless payment_method_name(payment) == 'Alipay' %>
18
+ <%= link_to t('show'), admin_order_payment_path(@order, payment) %>
19
+ <% payment.actions.each do |action| %>
20
+ <%= link_to t(action), fire_admin_order_payment_path(@order, payment, :e => action), :method => :put, :confirm => t('are_you_sure') %>
21
+ <% end %>
22
+ <%# if payment.actions.empty? and !payment.finalized? %>
23
+ <%#= link_to t(:finalize), finalize_admin_order_payment_path(@order, payment), :method => :put, :confirm => t('are_you_sure') %>
24
+ <%# end %>
25
+ <% end %>
26
+ </td>
27
+ </tr>
28
+ <% end %>
29
+ </table>
@@ -0,0 +1,29 @@
1
+ <table class="index">
2
+ <tr>
3
+ <th><%= "#{t('spree.date')}/#{t('spree.time')}" %></th>
4
+ <th><%= t("amount") %></th>
5
+ <th><%= t("payment_method") %></th>
6
+ <th> Name </th>
7
+ <th> TransactionId </th>
8
+ <th> PaymentId </th>
9
+ <th> </th>
10
+ </tr>
11
+ <tr>
12
+ <td><%= payment.created_at.to_s(:date_time24) %></td>
13
+ <td><%= number_to_currency(payment.amount) %></td>
14
+ <td><%= payment_method_name payment %></td>
15
+ <td><%= payment.source.first_name+" "+payment.source.last_name %></td>
16
+ <td><%= payment.source.TransactionId %></td>
17
+ <td><%= payment.source.PaymentId %></td>
18
+ <td>
19
+ <% unless payment_method_name(payment) == 'Alipay' %>
20
+ <% payment.actions.each do |action| %>
21
+ <%= link_to t(action), fire_admin_order_payment_path(@order, payment, :e => action), :method => :put, :confirm => t('are_you_sure') %>
22
+ <% end %>
23
+ <%# if payment.actions.empty? and !payment.finalized? %>
24
+ <%#= link_to t(:finalize), finalize_admin_order_payment_path(@order, payment), :method => :put, :confirm => t('are_you_sure') %>
25
+ <%# end %>
26
+ <% end %>
27
+ </td>
28
+ </tr>
29
+ </table>
@@ -0,0 +1,2 @@
1
+ <%= t('alipay_pay_instructions') %>
2
+
@@ -0,0 +1,32 @@
1
+ <h3 class="heading"> <%= image_tag "spinner.gif"%> <%= Spree.t(:redirecting_for_payment) %></h3>
2
+
3
+ <fieldset id="order_details" class="pg-redirect-od">
4
+ <div class="clear"></div>
5
+ <p style='text-transform:uppercase;font-weight:bold; margin:0px 0px 10px 10px;'>
6
+ <%= Spree.t(:order_number) %>
7
+ <%= @order.number %>
8
+ (<%= link_to "&larr; #{Spree.t(:back_to_payment_options)}".html_safe, checkout_state_path(:payment) %>)</p>
9
+ <%= render :partial => 'spree/shared/order_details', :locals => {:order => @order} -%>
10
+ </fieldset>
11
+
12
+ <%if @gateway.name == 'UnionPay'%>
13
+ <% finalurl = @gateway.preferred_url+ "?acqID=99020344&backURL=http://baidu.haishang.co/?wc-api=WC_Alipay&charSet=UTF-8&frontURL=http://baidu.haishang.co/?wc-api=WC_Alipay&merID=800039253992057&merReserve=PRUC&orderAmount=#{@order.total.to_s}&orderCurrency=USD&orderNum=#{@order.number.to_s}&paymentSchema=UP&signType=MD5&transTime=#{Time.now.strftime('%Y%m%d%H%M%S')}&transType=PURC&version=VER000000002&signature="%>
14
+ <% finalurl1 = "acqID=99020344&backURL=http://baidu.haishang.co/?wc-api=WC_Alipay&charSet=UTF-8&frontURL=http://baidu.haishang.co/?wc-api=WC_Alipay&merID=800039253992057&merReserve=PRUC&orderAmount=#{@order.total.to_s}&orderCurrency=USD&orderNum=#{@order.number.to_s}&paymentSchema=UP&signType=MD5&transTime=#{Time.now.strftime('%Y%m%d%H%M%S')}&transType=PURC&version=VER0000000024575c66b0bf74040bc8e721174828301"%>
15
+ <%else%>
16
+ <% finalurl = @gateway.preferred_url+ "?acqID=99020344&backURL=http://baidu.haishang.co/?wc-api=WC_Alipay&charSet=UTF-8&frontURL=http://baidu.haishang.co/?wc-api=WC_Alipay&merID=800039253992057&merReserve=PRUC&orderAmount=#{@order.total.to_s}&orderCurrency=USD&orderNum=#{@order.number.to_s}&paymentSchema=AP&signType=MD5&transTime=#{Time.now.strftime('%Y%m%d%H%M%S')}&transType=PURC&version=VER000000002&signature="%>
17
+ <% finalurl1 = "acqID=99020344&backURL=http://baidu.haishang.co/?wc-api=WC_Alipay&charSet=UTF-8&frontURL=http://baidu.haishang.co/?wc-api=WC_Alipay&merID=800039253992057&merReserve=PRUC&orderAmount=#{@order.total.to_s}&orderCurrency=USD&orderNum=#{@order.number.to_s}&paymentSchema=AP&signType=MD5&transTime=#{Time.now.strftime('%Y%m%d%H%M%S')}&transType=PURC&version=VER0000000024575c66b0bf74040bc8e721174828301"%>
18
+ <%end%>
19
+ <%sign = Digest::MD5.hexdigest(finalurl1)%>
20
+
21
+ <% finalurl= finalurl+sign%>
22
+
23
+ <a id="alipay_form" href="<%=finalurl%>" class="" title="" style="display:none;"></a>
24
+
25
+ <script type="text/javascript">
26
+ $(document).ready(function() {
27
+ setTimeout(function(){
28
+ document.getElementById("alipay_form").click();
29
+ }, 5000)
30
+ });
31
+ </script>
32
+
@@ -0,0 +1,20 @@
1
+ require 'spree_core'
2
+
3
+ module SpreeAlipay
4
+ class Engine < Rails::Engine
5
+
6
+ config.autoload_paths += %W(#{config.root}/lib)
7
+
8
+ def self.activate
9
+ Dir.glob(File.join(File.dirname(__FILE__), "../app/**/*_decorator*.rb")) do |c|
10
+ Rails.env.production? ? require(c) : load(c)
11
+ end
12
+ end
13
+
14
+ initializer "spree.register.payment_methods" do |app|
15
+ app.config.spree.payment_methods += [Spree::PaymentMethod::Alipay]
16
+ end
17
+
18
+ config.to_prepare &method(:activate).to_proc
19
+ end
20
+ end
@@ -0,0 +1,3 @@
1
+ #class SpreeAlipayHooks < Spree::ThemeSupport::HookListener
2
+ # custom hooks go here
3
+ #end
@@ -0,0 +1,25 @@
1
+ namespace :spree_alipay do
2
+ desc "Copies all migrations and assets (NOTE: This will be obsolete with Rails 3.1)"
3
+ task :install do
4
+ Rake::Task['spree_alipay:install:migrations'].invoke
5
+ Rake::Task['spree_alipay:install:assets'].invoke
6
+ end
7
+
8
+ namespace :install do
9
+ desc "Copies all migrations (NOTE: This will be obsolete with Rails 3.1)"
10
+ task :migrations do
11
+ source = File.join(File.dirname(__FILE__), '..', '..', 'db')
12
+ destination = File.join(Rails.root, 'db')
13
+ Spree::Core::FileUtilz.mirror_files(source, destination)
14
+ end
15
+
16
+ desc "Copies all assets (NOTE: This will be obsolete with Rails 3.1)"
17
+ task :assets do
18
+ source = File.join(File.dirname(__FILE__), '..', '..', 'public')
19
+ destination = File.join(Rails.root, 'public')
20
+ puts "INFO: Mirroring assets from #{source} to #{destination}"
21
+ Spree::Core::FileUtilz.mirror_files(source, destination)
22
+ end
23
+ end
24
+
25
+ end
@@ -0,0 +1 @@
1
+ # add custom rake tasks here
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: spree_alipay
3
+ version: !ruby/object:Gem::Version
4
+ version: '1.0'
5
+ platform: ruby
6
+ authors:
7
+ - krypt2005
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-03-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: spree_core
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ description:
28
+ email:
29
+ - krypt2005@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - README.md
35
+ - app/controllers/spree/checkout_controller_decorator.rb
36
+ - app/controllers/spree/gateway/alipay_controller.rb
37
+ - app/models/spree/alipayinfo.rb
38
+ - app/models/spree/payment_method/alipay.rb
39
+ - app/models/spree_alipay/rc4.rb
40
+ - app/views/admin/payments/_list.html.erb
41
+ - app/views/admin/payments/source_views/_alipay.html.erb
42
+ - app/views/checkout/payment/_alipay.html.erb
43
+ - app/views/spree/gateway/alipay/show.html.erb
44
+ - lib/spree_alipay.rb
45
+ - lib/spree_alipay_hooks.rb
46
+ - lib/tasks/install.rake
47
+ - lib/tasks/spree_alipay.rake
48
+ homepage: http://vasilec.blogspot.de/
49
+ licenses: []
50
+ metadata: {}
51
+ post_install_message:
52
+ rdoc_options: []
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: 2.0.0
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ requirements:
66
+ - none
67
+ rubyforge_project:
68
+ rubygems_version: 2.2.2
69
+ signing_key:
70
+ specification_version: 4
71
+ summary: Add gem summary here
72
+ test_files: []