/
iframe.php
87 lines (67 loc) · 2.76 KB
/
iframe.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
<?php
require_once('helper/pesapalV30Helper.php');
include_once('top.php');
// temporary key starage... you can store this on db
// KenyanTest Consumer/secret keys.. you can replace this with your own set for live testing
// TEST CREDENTIAL LINK: https://developer.pesapal.com/api3-demo-keys.txt
$consumer_key = "qkio1BGGYAXTu2JOfm7XSXNruoZsrqEW";
$consumer_secret = "osGQ364R49cXKeOYSpaOnT++rHs=";
$api = 'live';
if(isset($_POST['keys'])){
$api = 'demo';
}else{
$api = 'live';
}
// echo $api;
// Pesapal helper class
$pesapalV30Helper = new pesapalV30Helper($api);
// Step 1 Authentication
$access = $pesapalV30Helper->getAccessToken($consumer_key, $consumer_secret);
// print_r($access);
$access_token = $access->token;
// step 2 IPN URL Registration Endpoint
// $IPN_id = "7f462d26-018b-4a36-a04d-dff5624d41a0";
$callback_url = "http://localhost/sample_api3/redirect.php";
$IPN_respose = $pesapalV30Helper->getNotificationId($access_token, $callback_url);
// This notification_id uniquely identifies the endpoint Pesapal will send alerts to whenever a payment status changes for each transaction processed via API 3.0
$IPN_id = $IPN_respose->ipn_id;
// echo $IPN_id;
//get form details
if(!$_POST['reference']){
$ref = str_repeat('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',5);
$_POST['reference'] = substr(str_shuffle($ref),0,10);
}
$order = array();
$order['id'] = $_POST['reference'];;
$order['currency'] = $_POST['currency'];
$amount = str_replace(',','',$_POST['amount']); // remove thousands seperator if included
$order['amount'] = number_format($amount, 2); //format amount to 2 decimal places
$order['description'] = $_POST['description'];
$order['callback_url'] = $callback_url; //URL user to be redirected to after payment
$order['notification_id'] = $IPN_id; // //unique transaction id, generated by merchant.
$order['language'] = 'EN';
$order['terms_and_conditions_id'] = '';
$order['phone_number'] = preg_replace("/[^0-9]/", "", str_replace(' ', '', $_POST['phone_number'])); //Optional if we have email
$order['email_address'] = $_POST['email']; //Optional if we have phone
$order['country_code'] = 'KE'; //ISO codes (2 digits)
$order['first_name'] = $_POST['first_name'];
$order['middle_name'] = '';
$order['last_name'] = $_POST['last_name'];
$order['line_1'] = 'Nairobi';
$order['line_2'] = 'Riverside';
$order['city'] = 'Nairobi';
$order['state'] = '';
$order['postal_code'] = '12345';
$order['zip_code'] = '';
// var_dump($order);
// var_dump($_POST);
// STEP 3 post the order request to pesapal
$data = $pesapalV30Helper->getMerchertOrderURL($order, $access_token);
// var_dump($data);
// Iframe source link
$iframe_src = '';
if($data->redirect_url){
$iframe_src = $data->redirect_url;
}
// The actual iframe page
include('./pay.php');