Custom – Token

  • RSA Encrypt Data
  • Custom – Token

RSA Encrypt Data

This method is used to RSA Encrypt the data with the provided public key.

Parameters

ParameterTypeMandatoryDescription
datastrYesThe data to encrypt, generally it is the card number, token or the card cvv.
public_keystrYesThe public key provided by PhonePe.

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

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 – RSA Encrypt Data

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)

sensitive_data = "<CARD_CVV>"
public_key = "<YOUR_PUBLIC_KEY>"

encrypted_data = phonepe_client.encrypt_data(data=sensitive_data, public_key=public_key)

Returns

The encrypted data.

Custom – Token

This method is used to initiate Token pay requests 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
auth_modestrYesThe authentication mode for the card payment. Generally 3DS is used.
encrypted_cvvstrYesThe encrypted CVV of the card.
cryptogramstrYesThe cryptogram value associated with the token.
encrypted_tokenstrYesThe encrypted token value.
encryption_key_idintYesThe ID of the encryption key used for encrypting the card details.
expiry_monthstrYesThe expiration month of the card.
expiry_yearstrYesThe expiration year of the card.
pan_suffixstrYesThe last four digits (suffix) of the card PAN.
card_holder_namestrYesThe name of the cardholder
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.
address_line1strNoThe first line of the cardholder’s billing address.
address_line2strNoThe second line of the cardholder’s billing address.
address_citystrNoThe city of the cardholder’s billing address.
address_statestrNoThe state of the cardholder’s billing address.
address_zipstrNoThe ZIP or postal code of the cardholder’s billing address.
address_countrystrNoThe country of the cardholder’s billing address.

Example (Custom – Token)

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_transaction_id = str(uuid.uuid4())[:-2]
s2s_callback_url = "https://www.merchant.com/callback"
amount = 100
id_assigned_to_user_by_merchant = "<YOUR_USER_ID>"

auth_mode = "3DS"
encrypted_cvv = "<YOUR_CVV_ENCRYPTED>"  # can use payment_client.encrypt function for this
cryptogram = "<YOUR_CRYPTOGRAM>"
encrypted_token = "<YOUR_TOKEN_ENCRYPTED>"  # can use payment_client.encrypt function for this
key_id_used_to_encrypt_data = -1  # replace with actual key-id
expiry_month = "<EXPIRY_MONTH>"
expiry_year = "<EXPIRY_YEAR>"
pan_suffix = "<YOUR_LAST_4_DIGITS_OF_TOKENIZED_CARD>"
card_holder_name = "<NAME_OF_TOKENIZED_CARD_HOLDER>"
token_payment_request = PgPayRequest.token_pay_request_builder(merchant_transaction_id=unique_transaction_id,
                                                               amount=amount,
                                                               auth_mode=auth_mode,
                                                               encrypted_cvv=encrypted_cvv,
                                                               cryptogram=cryptogram,
                                                               encrypted_token=encrypted_token,
                                                               encryption_key_id=key_id_used_to_encrypt_data,
                                                               expiry_month=expiry_month,
                                                               expiry_year=expiry_year,
                                                               pan_suffix=pan_suffix,
                                                               card_holder_name=card_holder_name)
token_response = phonepe_client.pay(token_payment_request)
url = token_response.data.instrument_response.redirect_info.url

Returns

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