china_pay 0.0.1

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.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in china_pay.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Devin Zhang
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,77 @@
1
+ # ChinaPay
2
+
3
+ A simple payment library for china payment gateways.
4
+
5
+ ## 安装方法
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'china_pay'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install china_pay
18
+
19
+ ## 使用指南
20
+
21
+ ### 支付宝 即时到账交易接口
22
+
23
+ 此接口的官方服务名称为:create_direct_pay_by_user
24
+
25
+ 接口集成示例代码:
26
+
27
+ class PaymentsController < ApplicationController
28
+ def pay
29
+ @partner_id = '2088123456' # 支付宝合作者身份 ID,以 2088 开头
30
+ @key = 'SECURE_KEY' # 安全校验码
31
+
32
+ @merchant = ChinaPay::Alipay::Merchant.new(@partner_id, @key)
33
+
34
+ @order_id = 'KC201301300001D' # 商家内部唯一订单编号
35
+ @subject = 'iPhone 5 16G 黑色 x 1' # 订单标题
36
+ @description = '感谢您购买 iPhone 5 !' # 订单内容
37
+
38
+ @order = @merchant.create_order(@order_id, @subject, @description)
39
+
40
+ @seller_email = 'seller@company.com' # 卖家支付宝帐号
41
+ @total_fee = 0.01 # 订单总额
42
+
43
+ @direct_pay = @order.seller_email(@seller_email).total_fee(@total_fee).direct_pay
44
+
45
+ # 交易成功同步返回地址
46
+ @return_url = 'http://company.com/payments/success'
47
+ @direct_pay.after_payment_redirect_url(@return_url)
48
+
49
+ # 交易状态变更异步通知地址
50
+ @notify_url = 'http://company.com/payments/notify'
51
+ @direct_pay.notification_callback_url(@notify_url)
52
+
53
+ redirect_to @direct_pay.gateway_api_url
54
+ end
55
+ end
56
+
57
+ 或者,用一行代码搞定:
58
+
59
+ class PaymentsController < ApplicationController
60
+ def pay
61
+ redirect_to ChinaPay::Alipay::Merchant.new('2088123456', 'SECURE_KEY')
62
+ .create_order('KC201301300001D', 'iPhone 5 16G 黑色 x 1', '感谢您购买 iPhone 5 !')
63
+ .seller_email('seller@company.com').total_fee(0.01)
64
+ .direct_pay
65
+ .after_payment_redirect_url('http://company.com/payments/success')
66
+ .notification_callback_url('http://company.com/payments/notify')
67
+ .gateway_api_url
68
+ end
69
+ end
70
+
71
+ ## Contributing
72
+
73
+ 1. Fork it
74
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
75
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
76
+ 4. Push to the branch (`git push origin my-new-feature`)
77
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/china_pay.gemspec ADDED
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'china_pay/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "china_pay"
8
+ gem.version = ChinaPay::VERSION
9
+ gem.authors = ["Devin Zhang"]
10
+ gem.email = ["daqing1986@gmail.com"]
11
+ gem.description = %q{This gem can help you integrate Alipay, Tenpay and 99bill to your application.}
12
+ gem.summary = %q{A simple payment library for china payment gateways}
13
+ gem.homepage = "https://github.com/daqing/china_pay"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+ end
@@ -0,0 +1,163 @@
1
+ # encoding: utf-8
2
+ require 'digest'
3
+
4
+ module ChinaPay
5
+ module Alipay
6
+ module Product
7
+ class Base
8
+ DEFAULT_CHARSET = 'utf-8'
9
+ SIGN_TYPE_MD5 = 'MD5'
10
+
11
+ GATEWAY_URL = 'https://mapi.alipay.com/gateway.do'
12
+
13
+ ATTR_REQUIRED = [:service, :partner, :_input_charset,
14
+ :sign_type, :sign, :notify_url, :return_url,
15
+ :out_trade_no, :subject, :payment_type, :seller_email
16
+ ]
17
+
18
+ end
19
+
20
+ class DirectPay < Base
21
+ NAME = '即时到账'
22
+ SERVICE_LABEL = :create_direct_pay_by_user
23
+
24
+ def initialize(order)
25
+ @order = order
26
+
27
+ @params = {}
28
+ @extra_params = {}
29
+ @extended_params = {}
30
+ end
31
+
32
+ def notification_callback_url(url)
33
+ @params[:notify_url] = url
34
+ self
35
+ end
36
+
37
+ def after_payment_redirect_url(url)
38
+ @params[:return_url] = url
39
+ self
40
+ end
41
+
42
+ def extra_params(params)
43
+ @extra_params = params
44
+ self
45
+ end
46
+
47
+ def gateway_api_url
48
+ secure_signature = create_signature
49
+ request_params = sign_params.merge(
50
+ :sign_type => SIGN_TYPE_MD5,
51
+ :sign => secure_signature
52
+ )
53
+
54
+ lost_attributes = ATTR_REQUIRED - request_params.keys
55
+ if lost_attributes.any?
56
+ raise "the following keys are lost: #{lost_attributes.inspect}"
57
+ end
58
+
59
+ uri = URI(GATEWAY_URL)
60
+ uri.query = URI.encode_www_form(request_params.sort)
61
+
62
+ uri.to_s
63
+ end
64
+
65
+ # 公用业务扩展参数
66
+ #
67
+ # 用于特定业务信息的传递
68
+ #
69
+ # NOTE: 需要单独签约才能生效
70
+ def extended_params(params)
71
+ @extended_params = params
72
+ self
73
+ end
74
+
75
+ # 出错通知异步调用 URL
76
+ #
77
+ # NOTE: 需要联系技术支持才能开通
78
+ def error_callback_url(url)
79
+ @params[:error_notify_url] = url
80
+ self
81
+ end
82
+
83
+ private
84
+ def sign_params
85
+ params = @order.attributes.merge(@params)
86
+ params[:service] = SERVICE_LABEL
87
+ params[:partner] = @order.merchant.partner
88
+ params[:_input_charset] = DEFAULT_CHARSET
89
+ @sign_params ||= params
90
+ end
91
+
92
+ def create_signature
93
+ sequence = sign_params.sort.map {|k, v| "#{k}=#{v}"}.join('&')
94
+ Digest::MD5.hexdigest(sequence + @order.merchant.key)
95
+ end
96
+ end
97
+ end
98
+
99
+ class Order
100
+ PAYMENT_TYPE_BUYING = 1
101
+ PAYMENT_TYPE_DONATION = 4
102
+
103
+ attr_accessor :merchant
104
+ attr_reader :attributes
105
+
106
+ def initialize(order_id, subject, description)
107
+ @attributes = {
108
+ :out_trade_no => order_id,
109
+ :subject => subject,
110
+ :body => description }
111
+
112
+ @attributes[:payment_type] = PAYMENT_TYPE_BUYING
113
+ end
114
+
115
+ def seller_email(email)
116
+ @attributes[:seller_email] = email
117
+ self
118
+ end
119
+
120
+ def buyer_email(email)
121
+ @attributes[:buyer_email] = email
122
+ self
123
+ end
124
+
125
+ def total_fee(fee)
126
+ @attributes[:total_fee] = fee
127
+
128
+ self
129
+ end
130
+
131
+ def product_url(url)
132
+ @attributes[:show_url] = url
133
+ self
134
+ end
135
+
136
+ def as_donation
137
+ @attributes[:payment_type] = PAYMENT_TYPE_DONATION
138
+ self
139
+ end
140
+
141
+ def direct_pay
142
+ Product::DirectPay.new(self)
143
+ end
144
+
145
+ end
146
+
147
+ class Merchant
148
+ attr_accessor :partner, :key
149
+
150
+ def initialize(partner, key)
151
+ @partner = partner
152
+ @key = key
153
+ end
154
+
155
+ def create_order(order_id, subject, description)
156
+ order = Order.new(order_id, subject, description)
157
+ order.merchant = self
158
+ order
159
+ end
160
+ end
161
+ end
162
+ end
163
+
@@ -0,0 +1,3 @@
1
+ module ChinaPay
2
+ VERSION = "0.0.1"
3
+ end
data/lib/china_pay.rb ADDED
@@ -0,0 +1,6 @@
1
+ require "china_pay/version"
2
+ require "china_pay/alipay"
3
+
4
+ module ChinaPay
5
+ # Your code goes here...
6
+ end
metadata ADDED
@@ -0,0 +1,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: china_pay
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Devin Zhang
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-30 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: This gem can help you integrate Alipay, Tenpay and 99bill to your application.
15
+ email:
16
+ - daqing1986@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - Gemfile
23
+ - LICENSE.txt
24
+ - README.md
25
+ - Rakefile
26
+ - china_pay.gemspec
27
+ - lib/china_pay.rb
28
+ - lib/china_pay/alipay.rb
29
+ - lib/china_pay/version.rb
30
+ homepage: https://github.com/daqing/china_pay
31
+ licenses: []
32
+ post_install_message:
33
+ rdoc_options: []
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ! '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ required_rubygems_version: !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ requirements: []
49
+ rubyforge_project:
50
+ rubygems_version: 1.8.24
51
+ signing_key:
52
+ specification_version: 3
53
+ summary: A simple payment library for china payment gateways
54
+ test_files: []