File size: 4,269 Bytes
89a1081
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php

namespace App\Http\Controllers\PaymentMethod;  

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\PaymentMethod;
use App\Models\PaymentLog;
use App\Http\Controllers\PaymentMethod\PaymentController;
use App\Http\Controllers\PaymentMethod\PayTm\PaytmChecksum as PaytmChecksum;

class PaymentWithPaytm extends Controller
{
   public function getTransactionToken(Request $request){

    $generatedOrderID = "PYTM_BLINK_".time();
    
    $amount = "1.00";
    $callbackUrl = '';

    $paytmParams["body"] = array(
                                "requestType"   => "Payment",
                                "mid"           => $request->paytm_mid,
                                "websiteName"   => $request->paytm_website,
                                "orderId"       => $generatedOrderID,
                                "callbackUrl"   => $callbackUrl,
                                "txnAmount"     => array(
                                                        "value" => $amount,
                                                        "currency" => "INR",
                                                    ),
                                "userInfo"      => array(
                                                    "custId" => "CUST_".time(),
                                                ),
                            );

        $generateSignature = PaytmChecksum::generateSignature(json_encode($paytmParams['body'], JSON_UNESCAPED_SLASHES), $request->paytm_merchant_key);

        $paytmParams["head"] = array(
                                "signature" => $generateSignature
                            );

        $url = $request->paytm_environment."/theia/api/v1/initiateTransaction?mid=". $request->paytm_mid ."&orderId=". $generatedOrderID;

        $getcURLResponse = $this->getcURLRequest($url, $paytmParams);

        if(!empty($getcURLResponse['body']['resultInfo']['resultStatus']) && $getcURLResponse['body']['resultInfo']['resultStatus'] == 'S'){
            $result = array('success' => true, 'orderId' => $generatedOrderID, 'txnToken' => $getcURLResponse['body']['txnToken'], 'amount' => $amount, 'message' => $getcURLResponse['body']['resultInfo']['resultMsg']);
        }else{
            $result = array('success' => false, 'orderId' => $generatedOrderID, 'txnToken' => '', 'amount' => $amount, 'message' => $getcURLResponse['body']['resultInfo']['resultMsg']);
        }
        return $result;
    
    }


    public function getcURLRequest($url , $postData = array(), $headers = array("Content-Type: application/json")){

        $post_data_string = json_encode($postData, JSON_UNESCAPED_SLASHES);

        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data_string);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
        curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json")); 
        $response = curl_exec($ch);
        return json_decode($response,true);
    }

    public function getSiteURL(){
        $actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
        return str_replace('config.php', '', $actual_link);
    }

    public function transactionStatus(Request $request)
    {
        /* initialize an array */
        $paytmParams = array();

        /* body parameters */
        $paytmParams["body"] = array(
            "mid" => PAYTM_MID,
            /* Enter your order id which needs to be check status for */
            "orderId" => "YOUR_ORDERId_Here",
        );
        $checksum = PaytmChecksum::generateSignature(json_encode($paytmParams["body"], JSON_UNESCAPED_SLASHES), PAYTM_MERCHANT_KEY);

        /* head parameters */
        $paytmParams["head"] = array(
            "signature" => $checksum
        );

        /* prepare JSON string for request */
        $post_data = json_encode($paytmParams, JSON_UNESCAPED_SLASHES);

        $url = PAYTM_ENVIRONMENT."/v3/order/status";

        $getcURLResponse = getcURLRequest($url, $paytmParams);
        return $getcURLResponse;
    }

    public function ipn(Request $request)
    {
        // code...
    }
}