· 7 years ago · Feb 27, 2018, 04:30 AM
1<?php
2/**
3 * This is a sample code for manual integration with senangPay
4 * It is so simple that you can do it in a single file
5 * Make sure that in senangPay Dashboard you have key in the return URL referring to this file for example http://myserver.com/senangpay_sample.php
6 */
7
8# please fill in the required info as below
9$merchant_id = '';
10$secretkey = '';
11
12
13# this part is to process data from the form that user key in, make sure that all of the info is passed so that we can process the payment
14if(isset($_POST['detail']) && isset($_POST['amount']) && isset($_POST['order_id']) && isset($_POST['name']) && isset($_POST['email']) && isset($_POST['phone']))
15{
16 # assuming all of the data passed is correct and no validation required. Preferably you will need to validate the data passed
17 $hashed_string = md5($secretkey.urldecode($_POST['detail']).urldecode($_POST['amount']).urldecode($_POST['order_id']));
18
19 # now we send the data to senangPay by using post method
20 ?>
21 <form name="order" method="post" action="https://app.senangpay.my/payment/<?php echo $merchant_id; ?>">
22 <input type="text" name="detail" value="payment_for_order_<?php echo $order_id; ?>">
23 <input type="hidden" name="amount" value="<?php echo $_POST['amount']; ?>">
24 <input type="hidden" name="order_id" value="<?php echo $_POST['order_id']; ?>">
25 <input type="hidden" name="name" value="<?php echo $_POST['name']; ?>">
26 <input type="hidden" name="email" value="<?php echo $_POST['email']; ?>">
27 <input type="hidden" name="phone" value="<?php echo $_POST['phone']; ?>">
28 <input type="hidden" name="hash" value="<?php echo $hashed_string; ?>">
29 </form>
30
31 <?php
32}
33# this part is to process the response received from senangPay, make sure we receive all required info
34else if(isset($_GET['status_id']) && isset($_GET['order_id']) && isset($_GET['msg']) && isset($_GET['transaction_id']) && isset($_GET['hash']))
35{
36 # verify that the data was not tempered, verify the hash
37 $hashed_string = md5($secretkey.urldecode($_GET['status_id']).urldecode($_GET['order_id']).urldecode($_GET['transaction_id']).urldecode($_GET['msg']));
38
39 # if hash is the same then we know the data is valid
40 if($hashed_string == urldecode($_GET['hash']))
41 {
42 # this is a simple result page showing either the payment was successful or failed. In real life you will need to process the order made by the customer
43 if(urldecode($_GET['status_id']) == '1')
44 echo 'Payment was successful with message: '.urldecode($_GET['msg']);
45 else
46 echo 'Payment failed with message: '.urldecode($_GET['msg']);
47 }
48 else
49 echo 'Hashed value is not correct';
50}
51# this part is to show the form where customer can key in their information
52else
53{
54 # by right the detail, amount and order ID must be populated by the system, in this example you can key in the value yourself
55?>
56
57 <!-- banner -->
58 <div class="banner10" id="home1">
59 <div class="container">
60 <h2>Checkout</h2>
61 </div>
62 </div>
63 <!-- //banner -->
64
65 <!-- breadcrumbs -->
66 <div class="breadcrumb_dress">
67 <div class="container">
68 <ul>
69 <li><a href="index.php?task=home"><span class="glyphicon glyphicon-home" aria-hidden="true"></span> Home</a> <i>/</i></li>
70 <li>Checkout</li>
71 </ul>
72 </div>
73 </div>
74 <!-- //breadcrumbs -->
75 <div class="checkout">
76 <div class="container">
77 <form method="post" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>">
78 <?php $order_id = mt_rand(1000000000,99999999999); ?>
79 <div class="checkout-right">
80 <table class='timetable_sub'>
81 <thead>
82 <tr>
83 <th>No.</th>
84 <th>Product Name</th>
85 <th>Quantity</th>
86 <th>Price</th>
87 <th>Remove</th>
88 </tr>
89 </thead>
90 <?php
91 $totalQuantity = 0;
92 $grandTot = "";
93 $productDesc = "";
94 $current_url = base64_encode($url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
95 $product = "<table>
96 <tr>
97 <th>No.</th>
98 <th>Product Name</th>
99 <th>Quantity</th>
100 <th>Price</th>
101 <th>Remove</th>
102 </tr>
103 ";
104
105 if(isset($_SESSION["products"]))
106 {
107 $cart_items = 0;
108 $no = 1;
109 foreach ($_SESSION["products"] as $cart_itm)
110 {
111 $product_name = $cart_itm["code"];
112 $product_price = $cart_itm["price"];
113 //$product_img = $cart_itm["product_img"];
114 $quantity = $cart_itm["qty"];
115 $totalQuantity = $totalQuantity + $quantity;
116 $subTot = $product_price * $quantity;
117 $grandTot = $grandTot + $subTot;
118 $amount = number_format($grandTot,2);
119 $productDesc .= "Product Name : $product_name ($quantity)<br>";
120
121 $product .= "
122 <tr class='rem1'>
123 <td>$no</td>
124 <td>$product_name</td>
125 <td>".$cart_itm["qty"]."</td>
126 <td>$product_price</td>
127 <td>X</td>
128 </tr>";
129
130 echo '<tr class="rem1">';
131 echo '<td class="invert">'.$no.'</td>';
132 echo '<td class="invert">'.$product_name.'</td> ';
133 echo '<td class="invert">'.$cart_itm["qty"].'</td>';
134 echo '<td class="invert">MYR '.$product_price.' / item</td>';
135 echo '<td class="invert"><a href="cart_update.php?removep='.$cart_itm["code"].'&return_url='.$current_url.'"><img src="images/close.png"></a>
136 </td> ';
137 echo '</tr>';
138
139
140
141 //echo '<input type="hidden" name="item_img['.$cart_items.']" value="'.$product_img.'" />';
142 echo '<input type="hidden" name="item_code['.$cart_items.']" value="'.$product_name.'" />';
143 echo '<type="number" min="1" style="width: 70px;" name="item_qty['.$cart_items.']" value="'.$cart_itm["qty"].'" />';
144 echo '<input type="hidden" name="item_price['.$cart_items.']" value="'.$cart_itm["price"].'" />';
145
146 $cart_items ++;
147 $no++;
148 }
149 $product .= "</table>";
150 //echo '</ul>';
151 echo '<span class="check-out-txt">';
152 echo '<h3>Your shopping cart contains: <span>'.$cart_items.' Products</span></h3>';
153 echo '</span>';
154 echo '<input type="hidden" name="totalItem" value="'.$cart_items.'" />';
155 }else{
156 echo '#Your Cart is empty';
157 }
158 ?>
159 </table>
160 </div>
161 <img src="https://app.senangpay.my/public/img/pay.png">
162 <table>
163 <tr>
164 <td colspan="2"> </td>
165 </tr>
166 <tr>
167 <td>Detail</td>
168 <td>: <input type="text" name="detail" value="payment_for_order_<?php echo $order_id; ?>" placeholder="Description of the transaction" size="30"></td>
169 </tr>
170 <tr>
171 <td>Amount</td>
172 <td>: <input type="text" name="amount" value="<?php echo $amount;?>" placeholder="Amount to pay, for example 12.20" size="30"></td>
173 </tr>
174 <tr>
175 <td>Order ID</td>
176 <td>: <input type="text" name="order_id" value="<?php echo $order_id; ?>" placeholder="Unique id to reference the transaction or order" size="30"></td>
177 </tr>
178 <tr>
179 <td>Customer Name</td>
180 <td>: <input type="text" name="name" value="" placeholder="Name of the customer" size="30"></td>
181 </tr>
182 <tr>
183 <td>Customer Email</td>
184 <td>: <input type="text" name="email" value="" placeholder="Email of the customer" size="30"></td>
185 </tr>
186 <tr>
187 <td>Customer Contact No</td>
188 <td>: <input type="text" name="phone" value="" placeholder="Contact number of customer" size="30"></td>
189 </tr>
190 <tr>
191 <td>Address</td>
192 <td>: <input type="text" name="address" value="" placeholder="Address" size="30"></td>
193 </tr>
194 <tr>
195 <td><input type="submit" value="Submit"></td>
196 </tr>
197 </table>
198 </form>
199 </div>
200 </div>
201
202<?php
203}
204?>