One time payment
The payment adapter must accept payment in 2 methods, one time payments & recurring payments. Methods from the AccessType will be called to payment adapter with certain arguments and we expect the response in same format which is covered here.
We will call the one time class of the payment adapter where we will pass the credentials details and enviroment of the account. Therefore, payment adapter will always have the payment gateway credentials and other details in every method.
We have already created a one payment adapter gem for razorpay payment gateway. In which the accesstype is initializing the razorpay one time payment as below,
credentials = {'app_key'=>'<APP_KEY_OF_RAZORPAY>', 'secret'=> '<SECRET_KEY_OF_RAZORPAY>', 'enabled'=>true, 'webhook_secret'=>'12345a'}
accesstype_razorpay = AccesstypeRazorpay::Onetime.new(credentials: credentials, environment: '<live or sandbox>')
In the same above manner one time payment should be done for other payment gateways. Atfer initialization of the one time class, accesstype will call different instance methods like preview, after_charge, refund_payment, etc.
preview
This method will be called initially by accesstype. Many payment gateways are allowing to create order or subscription before doing the actual payment.
Arguments
- subscription_plan Hash object
- Subscription Plan Object
- subscriber Hash object
- Subscriber Object
- opts Hash object
- It's optional argument and it may contain the payment links or metadata.
Response
It is expecting payment gateway’s order id or subscription id. There would be cases where payment gateway create a direct payment without creating any order. In that case, this method should simply return nil
as response. The accesstype will understand it nil
as valid success response when payment gateway don’t have feature to create any order witohut payment.
charge?
This method will return true
if the class has charge
method. It will return false
if the class hasn’t charge
method.
Arguments
No arguments will be passed
Response
It will return true
or false
.
charge
This is not a mandatory method. However, This method needs to be created in payment adapter gem when we need to call API for making payment based on the card details. Many of the payment gateway support this type of flow where user first add the payment details and Backend will call payment api for the transaction. Razorpay payment gateway doesn’t follow this flow, but omise payment gateway do follow this flow.
Arguments
- subscription_plan Hash object
- Subscription Plan Object
- subscriber Hash object
- Subscriber Object
- subscription_attempt Hash object
- Subscription Attempt Object
- payload Hash object
- It will contain the following details `payment_type`, `amount_cents`, `amount_currency`, `payment_token`.
Response
#<AccesstypeRazorpay::PaymentResult:0x00007fa33e48afb0 @payment_type="razorpay", @success=true, @payload=nil, @payment_gateway_fee=1047, @payment_token="SAMPLE_TOKEN", @status="charged", @amount_currency=USD, @amount_cents=1000, @international=nil, @skip_invoice=nil, @metadata=nil, @external_payment_id=nil, @payment_gateway_fee_currency=nil, @external_refund_id=nil>
initiate_charge
This method will initiate the payment . This is only required if the payment cannot be completed in 2 steps ie preview and create subscription calls. This is needed in PGs like adyen where initate_charge sends payment parameters together with the input details collected from the shopper.
Arguments
- payload Hash object
- Initiate Charge Payload Object
- Subscriber Hash object
- Subscriber Object
- Subscription Plan Hash object
- Subscription Plan Object
Response
The Accesstype will expect a success response as below,
#<AccesstypeRazorpay::PaymentResult
@payment_token: 'AB78833',
@payment_gateway_fee: 1000,
@payment_gateway_fee_currency: 'EUR',
@amount_cents: 20000,
@amount_currency: 'EUR',
@metadata: 'tyuis990007888'
@status: 'Success',
@client_payload: {
"pspReference": "8815329842815468",
"resultCode": "Authorised"
} >
after_charge
This method will be called when payment is done on the payment gateway. The accesstype will send a payment token in arguments. The payment adapter will fetch the payment from payment gateway based on the payment token and send response back to the accesstype. This is method will ensure the payment token received to the accesstype is valid.
Arguments
- payment_token String
- payment token received from the payment gateway
Response
The Accesstype will expect a success response as below,
#<AccesstypeRazorpay::PaymentResult:0x00007fa33eeaf360 @payment_type="razorpay", @success=true, @payload=nil, @payment_gateway_fee=nil, @payment_token="<PAYMENT_TOKEN>", @status=nil, @amount_currency="CNY", @amount_cents=10000, @international=true, @skip_invoice=nil, @metadata=nil, @external_payment_id=nil, @payment_gateway_fee_currency=nil, @external_refund_id=nil>
capture
This method will be called when the accesstype received the success response from the after_charge
method. Once we ensure payment is made on payment gateway, we will capture that transaction on payment gateway. It is not mandatory method, some of the payment gateway don’t allow to capture the transacation. If it is not available on the payment gateway, payment adapter can skip it from the integration.
Arguments
- payment Hash object
- Payment Object
Response
The Accesstype will expect a success response as below,
#<AccesstypeRazorpay::PaymentResult:0x00007fa33e48afb0 @payment_type="razorpay", @success=true, @payload=nil, @payment_gateway_fee=1047, @payment_token=nil, @status="captured", @amount_currency=nil, @amount_cents=nil, @international=nil, @skip_invoice=nil, @metadata=nil, @external_payment_id=nil, @payment_gateway_fee_currency=nil, @external_refund_id=nil>
refund_payment
This method will be called for refund of payment. When any accesstype dashboard user request for the refund of payment, Accesstype will call this method of payment adapter. The accesstype will send invoice object in the argument which contains the external_payment_id
which requires for refund of the payment.
Arguments
- invoice Hash object
- Invoice Object
- amount Integer
- Amount which needs to be refunded to user
Response
#<AccesstypeRazorpay::PaymentResult:0x00007fa33e48afb0 @payment_type="razorpay", @success=true, @payload=nil, @payment_gateway_fee=nil, @payment_token=nil, @status=nil, @amount_currency=INR, @amount_cents=34900, @international=nil, @skip_invoice=nil, @metadata=nil, @external_payment_id=nil, @payment_gateway_fee_currency=nil, @external_refund_id=<REFUND_ID_FROM_RAZORPAY>>