-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBillDeskPayment.php
140 lines (122 loc) · 5.24 KB
/
BillDeskPayment.php
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php
namespace common\components;
use Yii;
use yii\base\Component;
use yii\helpers\Url;
class BillDeskPayment extends Component {
public $PAYMENT_URL = 'https://pgi.billdesk.com/pgidsk/PGIMerchantPayment';
public $MERCHANT_ID,$CHECKSUM_KEY,$SECRET_KEY,$CURRENCY_TYPE,$REDIRECT_ACTION;
/*
* to control the iniatial data
*/
public function __construct($config=[]) {
parent::__construct($config);
$this->MERCHANT_ID = $this->MERCHANT_ID;
$this->CHECKSUM_KEY = $this->CHECKSUM_KEY;
$this->SECRET_KEY = $this->SECRET_KEY;
$this->CURRENCY_TYPE = $this->CURRENCY_TYPE;
$this->REDIRECT_ACTION = Url::to($this->REDIRECT_ACTION,true);
}
/*
* To intitate the payment
* amount and other additional params as array (amount is mandatory)
* amount is number type, params is array
*/
public function initiatePayment($amount, $options=[]) {
$params['merchantId'] = $this->MERCHANT_ID;
$params['transaction_id'] = $this->generateRandomString();
$params['naData1'] = (isset($options['naData1'])) ? $options['naData1'] : 'NA';
$params['amount'] = ($amount) ? $amount : '';
$params['naData2'] = (isset($options['naData2'])) ? $options['naData2'] : 'NA';
$params['naData3'] = (isset($options['naData3'])) ? $options['naData3'] : 'NA';
$params['naData4'] = (isset($options['naData4'])) ? $options['naData4'] : 'NA';
$params['currencyType'] = $this->CURRENCY_TYPE;
$params['naData5'] = (isset($options['naData5'])) ? $options['naData5'] : 'NA';
$params['typeField'] = 'R';
$params['securityKey'] = $this->SECRET_KEY;
$params['naData7'] = (isset($options['naData7'])) ? $options['naData7'] : 'NA';
$params['naData8'] = (isset($options['naData8'])) ? $options['naData8'] : 'NA';
$params['typeField1'] = 'F';
$params['additionalInfo1'] = (isset($options['additionalInfo1'])) ? $options['additionalInfo1'] : 'NA';
$params['additionalInfo2'] = (isset($options['additionalInfo2'])) ? $options['additionalInfo2'] : 'NA';
$params['additionalInfo3'] = (isset($options['additionalInfo3'])) ? $options['additionalInfo3'] : 'NA';
$params['additionalInfo4'] = (isset($options['additionalInfo4'])) ? $options['additionalInfo4'] : 'NA';
$params['additionalInfo5'] = (isset($options['additionalInfo5'])) ? $options['additionalInfo5'] : 'NA';
$params['additionalInfo6'] = (isset($options['additionalInfo6'])) ? $options['additionalInfo6'] : 'NA';
$params['additionalInfo7'] = (isset($options['additionalInfo7'])) ? $options['additionalInfo7'] : 'NA';
$params['redirectAction'] = $this->REDIRECT_ACTION;
$convertedData = implode('|',$params);
$encryptedData = $this->encryptString($convertedData);
$params['checksumValue'] = $encryptedData;
$msg = implode('|',$params);
$data = ['msg'=>$msg];
$this->curlPost($this->PAYMENT_URL,$data);
}
/*
* To execute the given url with params as post data
*/
public function curlPost($url, $params=false){
$html = "<form method = 'post' action = '$url' id = 'frm1'>";
foreach($params as $param => $vl) {
$html .= "<input type = 'hidden' name ='$param' value ='$vl' />";
}
$html .= "</form>
<script>
document.getElementById('frm1').submit();
</script>
";
$fields = [];
echo $html;exit;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($params));
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
$response = curl_exec ($ch);
if (curl_errno($ch)) {
$error_msg = curl_error($ch);
//echo 'error-'.json_encode($error_msg);exit;
}
curl_close ($ch);
}
/*
* To generate random string
*/
public function generateRandomString($length = 8) {
$characters = '0123456789ABCDEFGHIJKLM';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString.time();
}
/*
* to encrypt the given string with sha256 method
*/
public function encryptString($str){
return strtoupper(hash_hmac('sha256',$str,$this->CHECKSUM_KEY, false));
}
/*
* To check the transaction process is done or not after the transaction
*/
public function extractParams($msg=false){
$result = false;
if($msg){
$code = substr(strrchr($msg, "|"), 1); //Last check sum value
$newStr =str_replace("|".$code,"",$msg);
$splitdata = strtoupper(hash_hmac('sha256',$newStr,$this->CHECKSUM_KEY, false));
return $splitdata;
/* if($splitdata==$code && isset($splitdata[14]) && $splitdata[14] == "0300"){
$result = true;
}else{
$result = false;
}*/
}
return $result;
}
}
?>