Class: WechatPayment::Service
- Inherits:
-
Object
- Object
- WechatPayment::Service
- Defined in:
- app/services/wechat_payment/service.rb
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#payment_order ⇒ Object
readonly
Returns the value of attribute payment_order.
Class Method Summary collapse
-
.handle_payment_notify(notify_data) ⇒ Object
处理支付回调.
-
.handle_refund_notify(notify_data) ⇒ Object
处理退款回调.
Instance Method Summary collapse
-
#initialize(payment_order) ⇒ Service
constructor
A new instance of Service.
-
#order ⇒ Object
下单.
-
#refund(refund_fee) ⇒ Object
退款.
Constructor Details
#initialize(payment_order) ⇒ Service
Returns a new instance of Service.
6 7 8 9 |
# File 'app/services/wechat_payment/service.rb', line 6 def initialize(payment_order) @client = WechatPayment::Client.new @payment_order = payment_order end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
4 5 6 |
# File 'app/services/wechat_payment/service.rb', line 4 def client @client end |
#payment_order ⇒ Object (readonly)
Returns the value of attribute payment_order.
4 5 6 |
# File 'app/services/wechat_payment/service.rb', line 4 def payment_order @payment_order end |
Class Method Details
.handle_payment_notify(notify_data) ⇒ Object
处理支付回调
43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'app/services/wechat_payment/service.rb', line 43 def self.handle_payment_notify(notify_data) result = WechatPayment::Client.handle_payment_notify(notify_data) payment_order = WechatPayment::PaymentOrder.find_by(out_trade_no: notify_data["out_trade_no"]) if result.success? && payment_order.pending_pay? payment_order.with_lock do payment_order.payment_exec_success(result.data) end else payment_order.payment_exec_failure(result.error) end result end |
.handle_refund_notify(notify_data) ⇒ Object
处理退款回调
59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'app/services/wechat_payment/service.rb', line 59 def self.handle_refund_notify(notify_data) result = WechatPayment::Client.handle_refund_notify(notify_data) refund_order = WechatPayment::RefundOrder.find_by(out_refund_no: result.data["out_refund_no"]) if result.success? && refund_order.pending_refund? refund_order.with_lock do refund_order.refund_exec_success(result.data) end else refund_order.refund_exec_failure(result.error) end result end |
Instance Method Details
#order ⇒ Object
下单
12 13 14 15 16 17 18 19 20 21 22 |
# File 'app/services/wechat_payment/service.rb', line 12 def order order_result = client.order(payment_order.as_order_params) if order_result.success? payment_order.payment_apply_success(order_result.data) else payment_order.payment_apply_failure(order_result.error) end order_result end |
#refund(refund_fee) ⇒ Object
退款
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'app/services/wechat_payment/service.rb', line 25 def refund(refund_fee) if !payment_order.balance_enough_to_refund?(refund_fee) return WechatPayment::ServiceResult.new(message_type: :error, message: "Balance is not enough.") end refund_order = payment_order.create_refund_order(refund_fee) refund_result = client.refund(refund_order.as_refund_params) if refund_result.success? refund_order.refund_apply_success(refund_result.data) else refund_order.refund_apply_failure(refund_result.error) end refund_result end |