EasyPing
EasyPing is an out of the box Ping++ Ruby SDK. Once installed, you're ready to set up a minimal configuration and get started using EasyPing.
Note
It's released on RubyGems.
If something doesn't work, feel free to report a bug or start an issue.
Installation
Add this line to your application's Gemfile:
gem 'easy_ping'
And then execute:
$ bundle
Or install it yourself as:
$ gem install easy_ping
If you prefer to use the latest code, you can build from source:
gem build easy_ping.gemspec
gem install easy_ping-<VERSION>.gem
Configuration
Write these lines of code to your configuration file, for example, easy_ping.rb
.
EasyPing.configure do |config|
config.app_id = 'app_Wzrrb9DW1GaLmbjn'
config.api_key = 'sk_test_Dq54mDyHufz9nrPeH8Hm50G8'
config.channel = :alipay
end
And put it under config/initiailizers
directory of your Rails project.
Or require this file manually by yourself.
Usage
EasyPing::Charge.create(order_number, amount, subject, body, options={}) -> charge object
EasyPing::Charge.create(options) -> charge object
EasyPing::Charge.create
EasyPing.charge
charge = EasyPing::Charge.create 'order_number_1', 100, 'apple', 'one delicous big apple'
charge = EasyPing.charge({
order_number: 'order_number_2',
amount: 100,
subject: 'apple',
body: 'one delicous big apple',
app_id: 'app_Wzrrb9DW1GaLmbjn',
metadata: { color: 'red'},
})
charge.raw
charge.values
charge.heasers
charge.status
charge.live?
charge.amount
charge.livemode
charge.refunded
EasyPing::Charge.find(charge_id) -> charge object
EasyPing::Charge.find(charge_id: charge_id) -> charge object
EasyPing::Charge.find
EasyPing.find
EasyPing.find_charge
charge = EasyPing::Charge.find 'ch_8OG4WDTe10q1q5G8aL8aDSmH'
charge = EasyPing.find charge_id: 'ch_8OG4WDTe10q1q5G8aL8aDSmH'
EasyPing::Charge.all(options={}) -> charge object list
EasyPing::Charge.all
EasyPing.all
EasyPing.all_charges
EasyPing.get_charge_list
charges = EasyPing::Charge.all
charges = EasyPing.all {
limit: 10,
offset: 'ch_8OG4WDTe10q1q5G8aL8aDSmH',
paid: true,
refunded: false
}
charges.get_next_page(options={}) -> charge object list
charges.get_next_page
charges.get_next_page!
charges.get_prev_page
charges.get_prev_page!
new_charges = charges.get_next_page
charges.get_prev_page!(limit: 5)
charges.has_more?
charges.url
charges.each {|ch| puts ch.id }
EasyPing::Refund.create(description, charge_id) -> refund object
EasyPing::Refund.create(amount, description, charge_id) -> refund object
EasyPing::Refund.create(options) -> refund object
EasyPing::Refund.create
EasyPing.refund
EasyPing::Refund.create 'refund description', 'ch_0ijQi5LKqT5sEiOePOKWb1mF'
EasyPing.refund({
amount: 50,
description: 'refund description',
charge_id: 'ch_0ijQi5LKqT5sEiOePOKWb1mF'
})
charge.refund(amount description) -> refund object
charge.refund(description) -> refund object
charge.refund(options) -> refund object
charge = EasyPing::Charge.find 'ch_0ijQi5LKqT5sEiOePOKWb1mF'
refund = charge.refund 10, 'refund description'
refund.raw
refund.values
refund.heasers
refund.status
refund.live?
refund.amount
refund.description
EasyPing::Refund.find(charge_id, refund_id) -> refund object
EasyPing::Refund.find(options) -> refund object
EasyPing::Refund.find
EasyPing.find
EasyPing.find_refund
refund = EasyPing::Refund.find 'ch_0ijQi5LKqT5sEiOePOKWb1mF', 're_TmbvDKHiXLCSG0mnj9jnDyjA'
refund = EasyPing.find_refund charge_id: 'ch_0ijQi5LKqT5sEiOePOKWb1mF', refund_id: 're_TmbvDKHiXLCSG0mnj9jnDyjA'
EasyPing::Refund.all(charge_id, options={}) -> refund object list
EasyPing::Refund.all(options) -> refund object list
EasyPing::Refund.all
EasyPing.all_refund
EasyPing.all_refunds
EasyPing.get_refund_list
refund_list = EasyPing::Refund.all 'ch_0ijQi5LKqT5sEiOePOKWb1mF', { limit: 5 }
refund_list = EasyPing.all_refund charge_id: 'ch_0ijQi5LKqT5sEiOePOKWb1mF'
charge.all_refund(options={}) -> refund object list
charge.all_refund
charge.all_refunds
charge.get_refund_list
charge = EasyPing::Charge.find 'ch_0ijQi5LKqT5sEiOePOKWb1mF'
refund_list = charge.all_refund limit: 5
refund_list.has_more?
refund_list.url
refund_list.each {|re| puts re.id }
Retrieve charge or refund object from async notification is easy.
EasyPing will automatically detect whether response object is charge
or refund.
EasyPing.from_notification(params) -> charge/refund object
EasyPing::Charge.from_notification(params)
EasyPing::Refund.from_notification(params)
charge = EasyPing::Charge.from_notification(params)
refund = EasyPing::Refund.from_notification(params)
Advanced Usage
ping = EasyPing.new({
app_id: 'app_Wzrrb9DW1GaLmbjn',
api_key: 'sk_test_Dq54mDyHufz9nrPeH8Hm50G8',
channel: :wx
})
ping.charge 'order_number_3' 100, 'apple', 'one delicous big apple'
config = EasyPing.config
config.api_key
config.app_id
config.to_options
EasyPing.config
ping = EasyPing.new({
app_id: 'app_Wzrrb9DW1GaLmbjn',
api_key: 'sk_test_Dq54mDyHufz9nrPeH8Hm50G8',
})
ping.config
["alipay", "wx", "upmp", "alipay_wap", "upmp_wap"]
Error Handling
If fail to create or retrieve charge/refund, an error will be raised.
begin
charge = EasyPing::Charge.find 'ch_0ijQi5LKqT5sEiOePOKWb1mF'
rescue EasyPing::APIError => e
puts e.message
puts e.status
puts e.type
puts e.param
rescue EasyPing::Error => e
puts e.message
rescue Exception => boom
puts "something wrong with your code, #{boom.message}"
puts boom.backtrace.join("\n")
end
Others
For Ping++ API information, please visit https://pingplusplus.com/document/api