PHP Setup

A PHP library for integrating with PhonePe APIs.

Installation

A minimum of PHP 8.0 or later is required for using this client.

Mandatory Step

Go to your project’s root directory where your composer.json file is located and add below repository details.

"repositories": [ { "type": "package", "package": [ { "dist": { "type": "zip", "url": "https://phonepe.mycloudrepo.io/public/repositories/phonepe-pg-php-sdk/phonepe-pg-php-sdk.zip" }, "name": "phonepe/phonepe-pg-php-sdk", "version": "1.0.0", "autoload": { "classmap": ["/"] } } ] } ]

Go to your project root directory where your composer.json file is located and execute the below command.

composer require --prefer-source phonepe/phonepe-pg-php-sdk

Please note that you will have to require the vendor/autoloader.php in order to autoload all the required classes.

Onboarding

To get started, you will need three details. Reach out to the Integration team.

$merchantId = “merchantId”;  
$saltKey = “saltKey”;  
$saltIndex = “saltIndex”;

Quick start

To create an instance of the PhonePePaymentClient class, you need to provide the following parameters:

const MERCHANTID = "<sample-mid>"; const SALTKEY = "<sample-salt-key>"; const SALTINDEX = "<sample-salt-index>"; const $env=Env::UAT; const SHOULDPUBLISHEVENTS=true; $phonePePaymentsClient = new PhonePePaymentClient(MERCHANTID, SALTKEY, SALTINDEX, $env,SHOULDPUBLISHEVENTS);
ParameterTypeMandatoryDescription
MERCHANTIDMixedYesUnique merchant ID provided by PhonePe.

SALTKEYMixedYesSalt key for secure communication with PhonePe.
SALTINDEXMixedYesSalt index for secure communication with PhonePe.
envMixedYesEnvironment for the PhonePeClient: Env.PRODUCTION (production), Env.UAT (testing).
SHOULDPUBLISHEVENTSBooleanNoFlag to enable event publishing to PhonePe. Set to False to disable.

Initiate a transaction via the Pay Page

To initiate payment we need to build the request using the PgPayRequestBuilder class. To initiate transactions with PayPage Instrument we use the static method buildPayPageInstrument from InstrumentBuilder class and pass it in the PgPayRequestBuilder. You can initiate the transaction using the pay function.

$merchantTransactionId = "<TestMerchantTransactionId>"; $request = PgPayRequestBuilder::builder() ->mobileNumber("xxxxxxxxx") ->callbackUrl("https://webhook.in/test/status") ->merchantId(MERCHANTID) ->merchantUserId("<merchantUserId>") ->amount(<amountInPaise>) ->merchantTransactionId($merchantTransactionId) ->redirectUrl("https://webhook.in/test/redirect") ->redirectMode("REDIRECT") ->paymentInstrument(InstrumentBuilder::buildPayPageInstrument()) ->build(); $response = $phonePePaymentsClient->pay($request); $PagPageUrl = $response->getInstrumentResponse()->getRedirectInfo()→getUrl()

Checking the validity of the callback

Let’s now verify the validity of the callback received from PhonePe on the merchant endpoint, passed as callbackUrl while initiating the payment. You need to pass two things to the verifyCallback function:

  1. The x_verify property in the headers of the callback response obtained from PhonePe.
  2. The response body received from PhonePe.sValid = $phonepeClient->verifyCallback($response, $xVerify);
$xVerify = "a005532637c6a6e4a4b08ebc6f1144384353305a9cd253d995067964427cd0bb###1"; $response = '{ "response":"eyJzdWNjZXNzIjpmYWxzZSwiY29kZSI6IlBBWU1FTlRfRVJST1IiLCJtZXNzYWdlIjoiUGF5bWVudCBGYWlsZWQiLCJkYXRhIjp7Im1lcmNoYW50SWQiOiJtZXJjaGFudElkIiwibWVyY2hhbnRUcmFuc2FjdGlvbklkIjoibWVyY2hhbnRUcmFuc2FjdGlvbklkIiwidHJhbnNhY3Rpb25JZCI6IkZUWDIzMDYwMTE1NDMxOTU3MTYzMjM5IiwiYW1vdW50IjoxMDAsInN0YXRlIjoiRkFJTEVEIiwicmVzcG9uc2VDb2RlIjoiUkVRVUVTVF9ERUNMSU5FX0JZX1JFUVVFU1RFRSIsInBheW1lbnRJbnN0cnVtZW50IjpudWxsfX0=" }'; $isValid = $phonepeClient->verifyCallback($response, $xVerify);

If the callback signature is verified, the value of the $isValid variable will be true.

Check Status of a transaction

Let’s see the details for the transaction after the payment is completed via UPI using the statusCheck function.

$checkStatus = $phonePePaymentsClient->statusCheck("<merchantTransactionId>");

Refund of a transaction

You can refund a PhonePe transaction using the refund function. To initiate refund we need to build the request using the PgRefundRequestBuilder class.

$pgRefundRequest = PgRefundRequestBuilder::builder() ->originalTransactionId("<originalMerchantTransactionId>") ->merchantId(MERCHANTID) ->merchantTransactionId("<merchantTransactionId>") ->callbackUrl("https://webhook.in/test/status") ->amount(<amountInPaise>) ->build(); $response = $phonePePaymentsClient->refund($pgRefundRequest);

Dealing with failed transaction

If you want to verify the status of the transaction that failed.

$merchantTransactionId="<merchantTransactionId>"; $checkStatus = $phonePePaymentsClient->statusCheck("<merchantTransactionId>"); $checkStatus->getResponseCode(); $checkStatus->getState(); $checkStatus→getTransactionId();