wechat_payment 1.0.0 → 2.0.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bf5c9c5546859b4b7429bede75ad056c5042121091257514caf1b66aa91f4b05
4
- data.tar.gz: 2d259a01e005dd91ce315a89ae9138c26dcab767151d39af0167c876d1cabae3
3
+ metadata.gz: 3ccc823e0ae167b60ffa78ba54e98c559c95c0019167a3c2e5033db6718f741b
4
+ data.tar.gz: 68e1de8aa02ac29ebc7d6012173dfdaa0162c493dbcb08f36e55db7909ef38b3
5
5
  SHA512:
6
- metadata.gz: 15742a17bffe3310201b9c20320ac96e7f19573c84a69002ed8b5852c0f5e6b423fc39610a26a048edee75f1cd6ad9fa5506719c84020bc89d85103029f2b47a
7
- data.tar.gz: f9decf10bd53c243d2e1e11b8bc0430599b288ba9db8c3fd6a7e35a5a93898014de23043a0d769947cd558c901680499492d95e3c0bb0b1a3f7a25023484e164
6
+ metadata.gz: 6dc4007ff8cbdd1cc8d347ed4d9193e43cddcf683192dae186ecf2b1f4511ce288acf40cffa6f10c66268382fb140f65d984ed9539a27388d6c258022f9c8b3b
7
+ data.tar.gz: 7655b449f9e31b5e481b174648ee6019753607addceb45ee0e4e848ce0d3aec11da6ddf1007694c80a51a005c817a3c927f910b3c95310830209ed1e05201d52
data/README.md CHANGED
@@ -3,7 +3,7 @@
3
3
 
4
4
 
5
5
  ## Convention
6
- 在使用该 Engine 之前,需要先建立`用户模型`,`商品模型`,`用户商品关联模型`,用户表需要有 open_id 字段,
6
+ 在使用该 Engine 之前,需要先建立`用户模型`,`商品模型`,`用户商品关联模型`,用户表需要有 openid 字段,
7
7
  商品表需要有 price 和 name 字段。假设`商品模型`为`Product`,则用户和商品之间的关联模型为`UserProduct`,
8
8
  关联模型属于`user`和`product`。
9
9
 
@@ -17,8 +17,8 @@
17
17
 
18
18
  3. 初始化
19
19
  ```bash
20
- # rails g wechat_payment:install [GoodsModel] [UserModel]
21
- $ rails g wechat_payment:install Product User
20
+ # rails g wechat_payment:install [GoodsModel] [UserGoodRelationModel] [UserModel]
21
+ $ rails g wechat_payment:install Product UserProduct User
22
22
  $ rails db:migrate
23
23
  ```
24
24
 
@@ -227,4 +227,4 @@ TODO 待补充
227
227
  Contribution directions go here.
228
228
 
229
229
  ## License
230
- The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
230
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,9 @@
1
+
2
+ class PaymentOrdersController < ApplicationController
3
+ def index
4
+ @payment_orders = WechatPayment::PaymentOrder.by_state(params[:state])
5
+
6
+ render json: @payment_orders
7
+ end
8
+
9
+ end
@@ -8,6 +8,10 @@ module WechatPayment
8
8
  before_create :gen_out_trade_no
9
9
  belongs_to :goods, polymorphic: true
10
10
 
11
+ scope :by_state, ->(state)do
12
+ where(state: state) if state.present?
13
+ end
14
+
11
15
  enum state: {
12
16
  paid: "paid",
13
17
  pending: "pending",
@@ -17,7 +21,7 @@ module WechatPayment
17
21
 
18
22
  # 将部分用户信息保存至订单
19
23
  def set_customer_info
20
- self.open_id = customer.open_id if open_id.blank?
24
+ self.openid = customer.openid if openid.blank?
21
25
  self.spbill_create_ip = customer.spbill_create_ip
22
26
  end
23
27
 
@@ -50,7 +54,7 @@ module WechatPayment
50
54
  spbill_create_ip: spbill_create_ip,
51
55
  total_fee: total_fee,
52
56
  body: body,
53
- openid: open_id
57
+ openid: openid
54
58
  }
55
59
  end
56
60
 
@@ -69,9 +73,16 @@ module WechatPayment
69
73
  # "orderId": 17
70
74
  # }
71
75
  def pay
76
+ if payment_params.present?
77
+ return ServiceResult.new(success: true, data: payment_params, message: "获取支付参数成功")
78
+ end
79
+
72
80
  order_result = WechatPayment::Service.new(self).order
73
81
  if order_result.success?
74
82
  payload = WechatPayment::Client.gen_js_pay_payload(order_result.data).merge(orderId: id).with_indifferent_access
83
+
84
+ update(payment_params: payload)
85
+
75
86
  WechatPayment::ServiceResult.new(success: true, data: payload)
76
87
  else
77
88
  order_result
@@ -199,7 +210,8 @@ module WechatPayment
199
210
  update(
200
211
  state: :paid,
201
212
  transaction_id: result["transaction_id"],
202
- paid_at: Time.current
213
+ paid_at: Time.current,
214
+ payment_params: nil
203
215
  )
204
216
 
205
217
  if goods.respond_to? :payment_exec_success
@@ -1,7 +1,7 @@
1
1
  class CreateWechatPaymentPaymentOrders < ActiveRecord::Migration[6.1]
2
2
  def change
3
3
  create_table :wechat_payment_payment_orders do |t|
4
- t.string :open_id
4
+ t.string :openid
5
5
  t.string :out_trade_no
6
6
  t.references :goods, polymorphic: true, null: false
7
7
  t.references :customer, polymorphic: true, null: false
@@ -14,9 +14,10 @@ class CreateWechatPaymentPaymentOrders < ActiveRecord::Migration[6.1]
14
14
  t.string :state
15
15
  t.datetime :paid_at
16
16
  t.datetime :refunded_at
17
+ t.jsonb :payment_params
17
18
 
18
19
  t.timestamps
19
20
  end
20
- add_index :wechat_payment_payment_orders, :open_id
21
+ add_index :wechat_payment_payment_orders, :openid
21
22
  end
22
23
  end
@@ -25,8 +25,7 @@ module WechatPayment
25
25
  # 售出
26
26
  # @param [User] user
27
27
  # @return [WechatPaymentOrder]
28
- def sell_to(user)
29
-
28
+ def sell_to(user, with_info = {})
30
29
  persist_goods_data = {}.tap do |h|
31
30
  self.class.persist_goods_attrs.each do |attr|
32
31
  h[attr] = send(attr)
@@ -36,7 +35,8 @@ module WechatPayment
36
35
  user_goods = self.class.user_goods_model.constantize.create(
37
36
  self.class.goods_ref_field => self,
38
37
  self.class.user_ref_field => user,
39
- **persist_goods_data
38
+ **with_info,
39
+ **persist_goods_data,
40
40
  )
41
41
 
42
42
  user_goods.payment_orders.create(
@@ -14,8 +14,8 @@ module WechatPayment
14
14
  end
15
15
  end
16
16
 
17
- def buy(goods)
18
- goods.sell_to(me)
17
+ def buy(goods, with_info = {})
18
+ goods.sell_to(me, with_info)
19
19
  end
20
20
  end
21
21
  end
@@ -1,3 +1,3 @@
1
1
  module WechatPayment
2
- VERSION = '1.0.0'
2
+ VERSION = '2.0.3'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wechat_payment
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 2.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - ian
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-01-15 00:00:00.000000000 Z
11
+ date: 2022-01-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -66,6 +66,7 @@ files:
66
66
  - app/assets/stylesheets/wechat_payment/application.css
67
67
  - app/controllers/wechat_payment/application_controller.rb
68
68
  - app/controllers/wechat_payment/callback_controller.rb
69
+ - app/controllers/wechat_payment/payment_orders_controller.rb
69
70
  - app/helpers/wechat_payment/application_helper.rb
70
71
  - app/jobs/wechat_payment/application_job.rb
71
72
  - app/mailers/wechat_payment/application_mailer.rb