Custom – UPI Collect

  • Validate VPA
  • Custom – UPI Collect

Validate VPA

Used to check if the given VPA is valid or not.

Parameters

ParameterTypeMandatoryDescription
vpastrYesThe Virtual Payment Address (VPA) for the payment.

For Python SDK Version <= 0.0.3, the imports should be:
from phonepe.sdk.pg.payments

For Python SDK Version > 0.0.3, the imports should be:
from phonepe.sdk.pg.payments.v1

Example – Valid VPA

from phonepe.sdk.pg.payments.v1.payment_client import PhonePePaymentClient
from phonepe.sdk.pg.env import Env

merchant_id = "<YOUR_MERCHANT_ID>"
salt_key = "<YOUR_SALT_KEY>"
salt_index = 1 # insert your salt index
env = Env.UAT
should_publish_events = True
phonepe_client = PhonePePaymentClient(merchant_id, salt_key, salt_index, env, should_publish_events)

vpa_to_verify = "valid_vap@ybl"
response = phonepe_client.validate_vpa(vpa_to_verify)
vpa_linked_name = response.data.name

Example – Invalid VPA (Raises exception)

from phonepe.sdk.pg.common.exceptions import PhonePeException
from phonepe.sdk.pg.payments.v1.payment_client import PhonePePaymentClient
from phonepe.sdk.pg.env import Env

merchant_id = "<YOUR_MERCHANT_ID>"
salt_key = "<YOUR_SALT_KEY>"
salt_index = 1 # insert your salt index
env = Env.UAT
should_publish_events = True
phonepe_client = PhonePePaymentClient(merchant_id, salt_key, salt_index, env, should_publish_events)

try:
    response = phonepe_client.validate_vpa('bad_vpa@upi')
except PhonePeException as exception:  
    print(f"The given VPA: {exception.phonepe_response.vpa} is invalid")

Returns

The function returns a VpaValidateResponse object with the following properties:

ParameterTypeDescription
successbooleanSuccess/failure of the request processing
codestrResponse code explaining the reason for the status
messagestrMessage providing more information about the code
dataVpaValidateResponseInformation about the VPA

VpaValidateResponse Properties

PropertyTypeDescription
vpastrThe VPA sent in the request.
namestrThe name linked to the VPA.

Custom – UPI Collect

This method is used to initiate a UPI Collect via the PhonePe PG Custom Checkout

Parameters

ParameterTypeMandatoryDescription
merchant_transaction_idstrYesThe unique transaction ID assigned by the merchant.
Note:
– merchantTransactionId length should be less than 35 characters
– No Special characters allowed except underscore “_” and hyphen “-“
amountintYesThe amount of the payment. In paise. Minimum 100 i.e. 1 rupee
vpastrYesThe Virtual Payment Address (VPA) for the payment.
merchant_user_idstrNoThe ID assigned to the user by the merchant.
Note:
– merchantUserId length should be less than 36 characters
– No Special characters allowed except underscore “_” and hyphen “-“
redirect_urlstrNoThe UI URL to redirect the user after a successful payment.
redirect_modestrNoThe mode of redirection after payment completion.
callback_urlstrNoThe S2S callback URL to which status notifications will be sent.
callback_modestrNoThe mode of callback handling.
merchant_order_idstrNoThe ID of the order assigned by the merchant.
device_osstrNoThe operating system of the device used for the payment. Possible values: ANDROID or IOS.

Example (Custom – UPI Collect)

import uuid
from phonepe.sdk.pg.payments.v1.models.request.pg_pay_request import PgPayRequest
from phonepe.sdk.pg.payments.v1.payment_client import PhonePePaymentClient
from phonepe.sdk.pg.env import Env

merchant_id = "<YOUR_MERCHANT_ID>"
salt_key = "<YOUR_SALT_KEY>"
salt_index = 1 # insert your salt index
env = Env.UAT
should_publish_events = True
phonepe_client = PhonePePaymentClient(merchant_id, salt_key, salt_index, env, should_publish_events)

unique_transcation_id = str(uuid.uuid4())[:-2]
amount = 100
merchant_user_id = "YOUR_USER_ID"
s2s_callback_url = "https://www.merchant.com/callback"
merchant_order_id = "<YOUR_ORDER_ID>"
device_os = "ANDROID"
redirect_url = "https://www.merchant.com/success"
cancel_redirect_url = "https://www.merchant.com/cancel"
callback_mode = "POST"
validated_vpa = "valid_vpa@ybl" #Insert a valid vpa  

upi_collect_request_data = PgPayRequest.upi_collect_pay_request_builder(merchant_transaction_id=unique_transcation_id,
                                                                        amount=amount,
                                                                        vpa=validated_vpa,
                                                                        merchant_user_id=merchant_user_id,
                                                                        callback_url=s2s_callback_url,
                                                                        merchant_order_id=merchant_order_id,
                                                                        device_os=device_os,
                                                                        redirect_url=redirect_url,
                                                                        cancel_redirect_url=cancel_redirect_url,
                                                                        callback_mode=callback_mode)
upi_collect_response = phonepe_client.pay(upi_collect_request_data)

Returns

The pay function returns a PgPayResponse object. With UpiCollectInstrumentResponse object in data.