Skip to content

Instantly share code, notes, and snippets.

@snmmaurya
Last active February 16, 2024 10:21
Show Gist options
  • Save snmmaurya/7b1a690dca9f6a1385371e2249a6abd5 to your computer and use it in GitHub Desktop.
Save snmmaurya/7b1a690dca9f6a1385371e2249a6abd5 to your computer and use it in GitHub Desktop.
PhonePe Ruby on Rails Implementation
@api_key = '099eb0cd-02cf-4e2a-8aca-3e6c6aff0399'
@merchant_id = 'PGTESTPAYUAT'
@key_index = 1
# Step 2
@payment_params = {
'merchantId' => @merchant_id,
'merchantTransactionId' => "MT7850590068188104",
"merchantUserId"=>"MUID123",
'amount' => 1000, # Amount in paisa (10 INR)
'redirectUrl'=>"https://f092-2401-4900-5ad1-f30a-55a1-7293-2caa-8d2f.ngrok-free.app/callbacks/v1/confirmations/phonepe_confirmations",
'redirectMode'=>"POST",
'callbackUrl'=>"https://f092-2401-4900-5ad1-f30a-55a1-7293-2caa-8d2f.ngrok-free.app/callbacks/v1/confirmations/phonepe_confirmations",
"merchantOrderId"=> "12345",
"mobileNumber"=>"7317715517",
"message"=>"Order description",
"email"=>"test@snmmaurya.com",
"shortName"=>"Test",
"paymentInstrument"=> {
"type"=> "PAY_PAGE",
}
}
# Step 3
@base64_encoaded_payload = Base64.strict_encode64(JSON.pretty_generate(@payment_params))
# step 4
@combine_payload = "#{@base64_encoaded_payload}/pg/v1/pay#{@api_key}"
@checksum = Digest::SHA256.hexdigest(@combine_payload)+"###1"
# Step 5
response = HTTP.headers("accept" => "application/json", "Content-Type" => "application/json", "X-VERIFY" => @checksum).post("https://api-preprod.phonepe.com/apis/pg-sandbox/pg/v1/pay", json: {request: @base64_encoaded_payload})
@json_body = JSON.parse response.body
use url to redirect for payment in the above response
callback confirmations validations -
base64_encoaded_payload = params["response"]
combine_payload = "#{base64_encoaded_payload}#{Pgs::PhonepeService::API_KEY}"
checksum = Digest::SHA256.hexdigest(combine_payload)+"####{Pgs::PhonepeService::KEY_INDEX}"
request.headers["X-VERIFY"] == checksum
response = JSON.parse Base64.decode64 base64_encoaded_payload
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment