PHP code example of paytm / payment
1. Go to this page and download the library: Download paytm/payment library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
paytm / payment example snippets
// Paytm configuration settings...
'Paytm' => [
'merchantKey' => env('MERCHANT_KEY',''),
'merchantId' => env('MERCHANT_ID',''),
'enviroment' => env('PAYTM_ENVIRONMENT',''),
'callbackUrl' => env('PAYTM_CALLBACK_URL',''),
],
// src/Controller/PaytmController.php
namespace App\Controller;
use Cake\Controller\Controller;
use Cake\ORM\TableRegistry;
use paytm\payment\Paytm;
class PaytmController extends Controller
{
// display a form for payment
public function initiate()
{
$this->render('paytm');
}
/* Make payment */
public function payment(){
if ($this->request->is('post')) {
$postData = $this->request->getData();
$amount = 1; //Amount to be paid
$order_id = time();
$enviroment = env('PAYTM_ENVIRONMENT');
$userData = [
"mid" => env('PAYTM_MERCHANT_ID'), //mandatory
"websiteName" => (strpos($enviroment, "stage") == true) ? "WEBSTAGING" : "DEFAULT", //mandatory
"callbackUrl" => env('PAYTM_CALLBACK_URL'), //mandatory
'name' => $postData['name'], // Name of user
'mobile' => $postData['mobile'], //Mobile number of user
'email' => $postData['email'], //Email of user
'txnAmount' => $amount, //mandatory
'orderId' => $order_id //mandatory
];
$Paytm = new Paytm();
$txntoken = $Paytm->initiatePayment($userData);
if(!empty($txntoken)){
$data = array('success'=> true,'txnToken' => $txntoken, 'txnAmount' => $amount, 'orderId' =>$order_id );
$jsonData = json_encode($data);
}else{
//echo json_encode(array('success'=> false,'txnToken' => '','data'=>$res));
$data = array('success'=> false,'txnToken' => '', 'txnAmount' => '', 'orderId' => '' );
$jsonData = json_encode($data);
}
$this->response = $this->response->withType('application/json');
// Set the JSON data as the response body
$this->response = $this->response->withStringBody($jsonData);
// Return the response
return $this->response;
}
}
/*Paytm Callback */
public function paymentCallback()
{
if(!empty($_POST['CHECKSUMHASH'])){
$post_checksum = $_POST['CHECKSUMHASH'];
}else{
$post_checksum = "";
}
if(! empty($_POST) && isset($_POST['STATUS'])){
// Access the POST data from the request
$callbackData = $this->request->getData();
$Paytm = new Paytm();
$requestData = $Paytm->verifyPaymentResponse($_POST);
if(isset($requestData['body']) && $requestData['body']['resultInfo']['resultStatus'] == 'TXN_SUCCESS'){
$msg = "Thank you for your order. Your transaction has been successful.";
$this->set(compact('msg'));
$this->render('success');
}else{
$msg = "Thank You. However, the transaction has been Failed For Reason: " . $requestData['body']['resultInfo']['resultMsg'];
$this->set(compact('msg'));
$this->render('error');
}
}
}
}
<!-- File: templates/Paytm/paytm.php -->
<?= $this->Html->css('https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css')
### Write code in src/Application.php