Ruby wrapper for PayU REST API.
Add this line to your application's Gemfile:
gem 'payu_api'
And then execute:
$ bundle
Or install it yourself as:
$ gem install payu_api
Get OAuth token:
response = PayuAPI.authorize(pos_id: '300046', key: '098f6b...', sandbox: true)
response.success?
# => true
response.auth_token
# => "c8d4b7..."
response.expires_in
# => 43199
Create client:
client = PayuAPI::Client.new(pos_id: '300046', auth_token: 'c8d4b7...', sandbox: true)
Creating a new order:
response = client.create_order(
continueUrl: 'https://your.eshop.com/orders/5kr-120',
customerIp: '127.0.0.1',
description: 'RTV market',
currencyCode: 'PLN',
totalAmount: 15000,
extOrderId: '5kr-120',
buyer: {
email: '[email protected]',
firstName: 'John',
lastName: 'Doe',
language: 'en'
},
products: [
{
name: 'Wireless Mouse for Laptop',
unitPrice: 15000,
quantity: 1
}
]
)
response.success?
# => true
response.order_id
# => "H9LL64F37H160126GUEST000P01"
response.redirect_uri
# => "https://secure.payu.com/pl/standard/co/summary?sessionId=..."
With error:
response.success?
# => false
response.error?
# => true
response.error_code
# => "UNAUTHORIZED_REQUEST"
response.error_message
# => "No privileges to perform the request"
Order capture:
response = client.capture(order_id: 'H9LL64F37H160126GUEST000P01')
response.success?
# => true
Order cancellation:
response = client.cancel(order_id: 'H9LL64F37H160126GUEST000P01')
response.success?
# => true
Refund:
response = client.refund(
order_id: 'H9LL64F37H160126GUEST000P01',
params: {
description: 'Refund',
currencyCode: 'PLN',
amount: 1000
}
)
response.success?
# => true
response.refund
# {
# refundId: "H9LL64F37H160126GUEST000P02",
# amount: 1000,
# currencyCode: "PLN",
# description: "Refund",
# creationDateTime: "2016-08-05T15:42:05.241+02:00",
# status: "PENDING",
# statusDateTime: "2016-08-05T15:42:05.241+02:00"
# }
Get order info:
response = client.get_order(order_id: 'H9LL64F37H160126GUEST000P01')
response.success?
# => true
response.order
# {
# :orderId=>"H9LL64F37H160126GUEST000P01",
# :orderCreateDate=>"2016-08-05T15:42:05.241+02:00",
# :customerIp=>"127.0.0.1",
# :merchantPosId=>"145227",
# :description=>"RTV market",
# :currencyCode=>"PLN",
# :totalAmount=>"15000",
# :status=>"NEW",
# :products=>[
# {
# :name=>"Wireless Mouse for Laptop",
# :unitPrice=>"15000",
# :quantity=>"1"
# }
# ]
# }
Verify notification signature:
PayuAPI.signature_valid?(
body: request.raw_post,
headers: request.headers,
second_key: '17d24f...'
)
# => true
Retrieve payment methods which are enabled for pos:
response = client.get_paymethods
response.success?
# => true
response.pay_by_links
# => [
# {
# :value=>"m",
# :brandImageUrl=>"https://static.payu.com/images/mobile/logos/pbl_m.png",
# :name=>"mTransfer",
# :status=>"ENABLED"
# },
# {
# :value=>"m",
# :brandImageUrl=>"https://static.payu.com/images/mobile/logos/pbl_o.png",
# :name=>"Płacę z Bankiem Pekao S.A.",
# :status=>"ENABLED"
# },
# ]
Similarly you can use response.pex_tokens
to get pexTokens
and response.card_tokens
to get cardTokens
. In case of no results available, the result will be empty array.
response.pex_tokens
# => []
Bug reports and pull requests are welcome on GitHub at https://github.com/busfor/payu_api. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
The gem is available as open source under the terms of the MIT License.