-
Notifications
You must be signed in to change notification settings - Fork 2
/
sslcommerz.php
143 lines (128 loc) · 5.77 KB
/
sslcommerz.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
141
<?php
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
JLoader::import( 'adapter.payment.payment' );
require VIKSSLCOMMERZ_DIR . DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR . 'SslCommerzNotification.php';
class AbstractSslCommerzPayment extends JPayment {
public function __construct( $alias, $order, $params = array() ) {
parent::__construct( $alias, $order, $params );
}
protected function buildAdminParameters(): array {
$logo_img = VIKSSLCOMMERZ_URI . 'sslcommerz.png';
return [
'logo' => [
'label' => __( '', 'vikbooking' ),
'type' => 'custom',
'html' => '<img src="' . $logo_img . '"/>'
],
'sandbox' => [
'label' => __( 'Test Mode', 'vikbooking' ),
'type' => 'select',
'options' => [ 'Yes', 'No' ],
],
'store_id' => [
'label' => __( 'Store ID', 'vikbooking' ),
'type' => 'text',
],
'store_passwd' => [
'label' => __( 'Store Password', 'vikbooking' ),
'type' => 'text',
],
];
}
protected function doRefund( JPaymentStatus &$status ) {
parent::doRefund( $status ); // TODO: Change the autogenerated stub
}
protected function beginTransaction() {
$sslcommerz_url = $this->getParam( 'sandbox' ) === 'Yes' ? "https://sandbox.sslcommerz.com" : "https://securepay.sslcommerz.com";
$details = $this->get( 'details' );
$customer_values = $this->prepareCustomerData( $details['custdata'] );
$post_data = [];
//Integration Required Parameters
$post_data['store_id'] = $this->getParam( 'store_id' );
$post_data['store_passwd'] = $this->getParam( 'store_passwd' );
$post_data['total_amount'] = $this->get( 'total_to_pay' );
$post_data['currency'] = $this->get( 'transaction_currency' );
$post_data['tran_id'] = $this->get( 'sid' ) . "-" . $this->get( 'ts' );
$post_data['success_url'] = JUri::getInstance( $this->get( 'notify_url' ) );
$post_data['fail_url'] = JUri::getInstance( $this->get( 'notify_url' ) );
$post_data['cancel_url'] = JUri::getInstance( $this->get( 'notify_url' ) );
//Customer Information
$post_data['cus_name'] = $customer_values['Name'] . ' ' . $customer_values['Last Name'];
$post_data['cus_email'] = $this->get( 'customer_email' );
$post_data['cus_add1'] = $customer_values['Address'];
$post_data['cus_city'] = $customer_values['City'];
$post_data['cus_country'] = $customer_values['Country'];
$post_data['cus_phone'] = $customer_values['Phone'] ?? '';
$post_data['cus_postcode'] = $customer_values['Zip Code'] ?? '';
//Shipment Information
$post_data['shipping_method'] = 'No';
$post_data['num_of_item'] = $details['roomsnum'];
//Product Information
$post_data['product_name'] = $this->order->rooms_name;
$post_data['product_category'] = 'ecommerce';
$post_data['product_profile'] = 'general';
$sslCommerzN = new SslCommerzNotification( $post_data['store_id'], $post_data['store_passwd'], $sslcommerz_url );
$sslCommerzN->makePayment( $post_data, 'hosted' );
}
private function prepareCustomerData( $customer_data ): array {
$customer_data_parts = explode( "\n", $customer_data );
$customer_values = array();
if ( str_contains( $customer_data_parts[0], ':' ) && str_contains( $customer_data_parts[1], ':' ) ) {
foreach ( $customer_data_parts as $custdet ) {
if ( $custdet === '' ) {
continue;
}
$customer_det_parts = explode( ':', $custdet );
if ( count( $customer_det_parts ) >= 2 ) {
$key = $customer_det_parts[0];
unset( $customer_det_parts[0] );
$customer_values[ $key ] = trim( implode( ':', $customer_det_parts ) );
}
}
}
return $customer_values;
}
protected function validateTransaction( JPaymentStatus &$status ): bool {
$sslcommerz_url = $this->getParam( 'sandbox' ) === 'Yes' ? "https://sandbox.sslcommerz.com" : "https://securepay.sslcommerz.com";
$sslv = new SslCommerzNotification( $this->getParam( 'store_id' ), $this->getParam( 'store_passwd' ), $sslcommerz_url );
$tran_id = $_POST['tran_id'];
$bank_tran_id = $_POST['bank_tran_id'];
$amount = $_POST['amount'];
$currency = $_POST['currency'];
$_POST['connect_from_localhost'] = false;
if ($_POST['status']=='VALID'){
$validated = $sslv->orderValidate( $tran_id, $amount, $currency, $_POST );
if ( $validated ) {
$status->verified();
$status->paid( $amount );
$status->setData( 'TransactionId', $tran_id );
$status->setData( 'merchantOrderId', $bank_tran_id );
$status->appendLog( "Successful payment" );
} else {
$status->setData( 'TransactionId', $tran_id );
$status->setData( 'merchantOrderId', $bank_tran_id );
$status->appendLog( "Failure payment" );
$status->appendLog( ( "Failure Reason " . ( isset( $_POST['failedreason'] ) ) ) ? $_POST['failedreason'] : $_POST['error'] );
}
}
$status->appendLog( 'STATUS:- ' . $_POST['status'] );
$status->appendLog( "Transaction ID:- " . $tran_id );
$status->appendLog( "Transaction Time:- " . $_POST['tran_date'] );
$status->appendLog( "Payment Method:- " . $_POST['card_issuer'] );
$status->appendLog( "Bank Transaction ID:- " . $_POST['bank_tran_id'] );
$status->appendLog( "Amount:- " . $_POST['amount'] . ' ' . $_POST['currency'] );
return true;
}
protected function complete( $res = 0 ) {
$app = JFactory::getApplication();
if ( $res ) {
$url = $this->get( 'return_url' );
$app->enqueueMessage( __( 'Thank you! Payment successfully received.', 'viksslcommerz' ) );
} else {
$url = $this->get( 'error_url' );
$app->enqueueMessage( __( 'It was not possible to verify the payment. Please, try again.', 'viksslcommerz' ) );
}
JFactory::getApplication()->redirect( $url );
exit;
}
}