fork download
  1. <?php
  2. // Replace these with your actual PhonePe API credentials
  3.  
  4. $merchantId = 'PGTESTPAYUAT'; // sandbox or test merchantId
  5. $apiKey="099eb0cd-02cf-4e2a-8aca-3e6c6aff0399"; // sandbox or test APIKEY
  6. $redirectUrl = 'thank-you.php';
  7.  
  8. // Set transaction details
  9. $order_id = uniqid();
  10. $name="Tutorials Website";
  11. $email="info@tutorialswebsite.com";
  12. $mobile=9999999999;
  13. $amount = 10; // amount in INR
  14. $description = 'Payment for Product/Service';
  15.  
  16.  
  17. $paymentData = array(
  18. 'merchantId' => $merchantId,
  19. 'merchantTransactionId' => "MT7850590068188104", // test transactionID
  20. "merchantUserId"=>"MUID123",
  21. 'amount' => $amount*100,
  22. 'redirectUrl'=>$redirectUrl,
  23. 'redirectMode'=>"POST",
  24. 'callbackUrl'=>$redirectUrl,
  25. "merchantOrderId"=>$order_id,
  26. "mobileNumber"=>$mobile,
  27. "message"=>$description,
  28. "email"=>$email,
  29. "shortName"=>$name,
  30. "paymentInstrument"=> array(
  31. "type"=> "PAY_PAGE",
  32. )
  33. );
  34.  
  35.  
  36. $jsonencode = json_encode($paymentData);
  37. $payloadMain = base64_encode($jsonencode);
  38. $salt_index = 1; //key index 1
  39. $payload = $payloadMain . "/pg/v1/pay" . $apiKey;
  40. $sha256 = hash("sha256", $payload);
  41. $final_x_header = $sha256 . '###' . $salt_index;
  42. $request = json_encode(array('request'=>$payloadMain));
  43.  
  44. $curl = curl_init();
  45. CURLOPT_URL => "https://a...content-available-to-author-only...e.com/apis/pg-sandbox/pg/v1/pay",
  46. CURLOPT_RETURNTRANSFER => true,
  47. CURLOPT_ENCODING => "",
  48. CURLOPT_MAXREDIRS => 10,
  49. CURLOPT_TIMEOUT => 30,
  50. CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  51. CURLOPT_CUSTOMREQUEST => "POST",
  52. CURLOPT_POSTFIELDS => $request,
  53. CURLOPT_HTTPHEADER => [
  54. "Content-Type: application/json",
  55. "X-VERIFY: " . $final_x_header,
  56. "accept: application/json"
  57. ],
  58. ]);
  59.  
  60. $response = curl_exec($curl);
  61. $err = curl_error($curl);
  62.  
  63. curl_close($curl);
  64.  
  65. if ($err) {
  66. echo "cURL Error #:" . $err;
  67. } else {
  68. $res = json_decode($response);
  69.  
  70. if(isset($res->success) && $res->success=='1'){
  71. $paymentCode=$res->code;
  72. $paymentMsg=$res->message;
  73. $payUrl=$res->data->instrumentResponse->redirectInfo->url;
  74.  
  75. header('Location:'.$payUrl) ;
  76. }
  77. }
  78.  
  79. ?>
Success #stdin #stdout 0.03s 26440KB
stdin
Standard input is empty
stdout
cURL Error #:Could not resolve host: api-preprod.phonepe.com