A zero dependency, pure Ruby utility to handle requests against the Payson payment gateway API.
- 2.6
- 2.7
- 3.0
Put this line in your Gemfile:
gem 'payson_api'
Then bundle:
$ bundle
This explains how to use this gem with the Payson Checkout v2 API.
You need to configure the gem with your own Payson credentials through the PaysonAPI::V2.configure method:
PaysonAPI::V2.configuredo |config|
config.api_user_id='XXXX'config.api_password='XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX'endPlease note that if config.api_user_id is set to 4, the client will go into test mode. Valid test access credentials could be found in their documentation.
For more detailed testing you may create your own test agent for use in the test environment. Use config.test_mode = true
account=PaysonAPI::V2::Client.get_accountrequest=PaysonAPI::V2::Requests::CreateCheckout.newrequest.merchant.checkout_uri='http://localhost/checkout'request.merchant.confirmation_uri='http://localhost/confirmation'request.merchant.notification_uri='http://localhost/notification'request.merchant.terms_uri='http://localhost/terms'request.order.currency='sek'request.order.items << PaysonAPI::V2::Requests::OrderItem.new.tapdo |item|
item.name='My product name'item.unit_price=1000item.quantity=3item.reference='product-1'endcheckout=PaysonAPI::V2::Client.create_checkout(request)# Continue by rendering the HTML from checkout.snippet.checkout=PaysonAPI::V2::Client.get_checkout(checkout_id)request=PaysonAPI::V2::Requests::UpdateCheckout.newrequest.id=checkout.idrequest.status=checkout.statusrequest.merchant.checkout_uri=checkout.merchant.checkout_urirequest.merchant.confirmation_uri=checkout.merchant.confirmation_urirequest.merchant.notification_uri=checkout.merchant.notification_urirequest.merchant.terms_uri=checkout.merchant.terms_urirequest.order.currency='eur'request.order.items << PaysonAPI::V2::Requests::OrderItem.new.tapdo |item|
item.name='My product name'item.unit_price=200item.quantity=3item.reference='product-1'endrequest.order.items << PaysonAPI::V2::Requests::OrderItem.new.tapdo |item|
item.name='Another product name'item.unit_price=600item.quantity=1item.reference='product-2'endcheckout=PaysonAPI::V2::Client.update_checkout(request)This explains how to use the Payson 1.0 API.
You need to configure the gem with your own Payson credentials through the PaysonAPI::V1.configure method:
PaysonAPI::V1.configuredo |config|
config.api_user_id='XXXX'config.api_password='XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX'endPlease note that if config.api_user_id is set to 4, the client will go into test mode. Valid test access credentials could be found in {documentation}[https://tech.payson.se/#Testing/sandbox].
For more detailed testing you may create your own test agent for use in the test environment. Use config.test_mode = true
payment=PaysonAPI::V1::Requests::Payment.newpayment.return_url='http://localhost/payson/success'payment.cancel_url='http://localhost/payson/cancel'payment.ipn_url='http://localhost/payson/ipn'payment.memo='Sample order description'payment.sender=PaysonAPI::V1::Sender.new.tapdo |s|
s.email='mycustomer@mydomain.com's.first_name='My's.last_name='Customer'endpayment.receivers=[]payment.receivers << PaysonAPI::V1::Receiver.new.tapdo |r|
r.email='me@mydomain.com'r.amount=100r.first_name='Me'r.last_name='Just me'r.primary=trueendpayment.order_items=[]payment.order_items << PaysonAPI::V1::OrderItem.new.tapdo |i|
i.description='Order item description'i.unit_price=100i.quantity=1i.tax=0i.sku='MY-ITEM-1'endresponse=PaysonAPI::V1::Client.initiate_payment(payment)ifresponse.success?# Redirect to response.forward_urlelseputsresponse.errorsendtoken='token-received-from-payment-request'payment_details=PaysonAPI::V1::Requests::PaymentDetails.new(token)response=PaysonAPI::V1::Client.get_payment_details(payment_details)ifresponse.success?# Do stuff with response objectelseputsresponse.errorsendtoken='token-received-from-payment-request'action='CANCELORDER'payment_update=PaysonAPI::V1::Requests::PaymentUpdate.new(token,action)response=PaysonAPI::V1::Client.update_payment(payment_update)ifresponse.success?# Do stuff with response objectelseputsresponse.errorsendThis example assumes the use of the Rails web framework.
classPayson < ApplicationControllerdefipn_responderrequest_body=request.body.readipn_response=PaysonAPI::V1::Responses::IPN.new(request_body)# Create a new IPN request object containing the raw response from aboveipn_request=PaysonAPI::V1::Requests::IPN.new(ipn_response.raw)validation=PaysonAPI::V1::Client.validate_ipn(ipn_request)unlessvalidation.verified?raise"Something went terribly wrong"end# Do business transactions, e.g. update the corresponding order:# order = Order.find_by_payson_token(ipn_response.token)# order.payson_status = ipn_response.status# order.save!endendNothing at the moment.
Feel free to message me on GitHub.
Copyright (c) 2021 Christopher Svensson.