-
Notifications
You must be signed in to change notification settings - Fork 2
/
wc_knet_payment_transactions.php
89 lines (80 loc) · 3.35 KB
/
wc_knet_payment_transactions.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
<?php
defined( 'ABSPATH' ) || exit;
/**
* create transactions table
*/
if(!function_exists( "alnazer_create_transactions_db_table" )){
function alnazer_create_transactions_db_table(){
global $wpdb;
$charset_collate = $wpdb->get_charset_collate();
$table_name = $wpdb->prefix.WC_PAYMENT_KNET_TABLE;
if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) {
$sql = "CREATE TABLE $table_name (
id int(11) NOT NULL AUTO_INCREMENT,
order_id int(11) NOT NULL,
payment_id varchar(100) NOT NULL,
track_id varchar(100) NOT NULL,
amount DECIMAL(20,3) DEFAULT 0.000 NOT NULL,
tran_id varchar(100) NULL,
ref_id varchar(100) NULL,
status varchar(100) DEFAULT '".WC_PAYMENT_STATUS_FAIL."' NOT NULL,
result varchar(100) DEFAULT '".WC_PAYMENT_STATUS_NEW."' NOT NULL,
info text NULL,
created_at datetime DEFAULT CURRENT_TIMESTAMP NOT NULL,
PRIMARY KEY (id),
INDEX (id, order_id, payment_id, result)
) $charset_collate;";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql );
add_option( 'wc_knet_db_version', WC_PAYMENT_KNET_DV_VERSION);
}
}
}
/**
* create new transation record
* @param $data
* @return bool|false|int
*/
add_action("alnazer_wc_knet_create_new_transaction", "alnazer_wc_handel_knet_create_new_transaction", 10, 2);
if(!function_exists( "alnazer_wc_handel_knet_create_new_transaction" )){
function alnazer_wc_handel_knet_create_new_transaction($order,$transation_data){
global $wpdb;
$table_name = $wpdb->prefix.WC_PAYMENT_KNET_TABLE;
try {
if(!alnazer_wc_is_transaction_exist($transation_data["payment_id"])){
return $wpdb->insert(
$table_name,
[
'order_id' => $order->get_id(),
'payment_id' => $transation_data["payment_id"],
'track_id' => $transation_data["track_id"],
'tran_id' => $transation_data["tran_id"],
'ref_id' => $transation_data["ref_id"],
'status' => $transation_data["status"],
'result' => $transation_data["result"],
'amount'=>$transation_data["amount"],
'info' => json_encode($transation_data),
'created_at' => wp_date( 'Y-m-d H:i:s'),
]
);
}
return false;
}catch (Exception $e){
return false;
}
}
}
if(!function_exists( "alnazer_wc_is_transaction_exist" )){
function alnazer_wc_is_transaction_exist($payment_id){
global $wpdb;
$table_name = $wpdb->prefix.WC_PAYMENT_KNET_TABLE;
return $wpdb->get_var("SELECT `payment_id` FROM `$table_name` WHERE `payment_id`='$payment_id' ");
}
}
if(!function_exists( "alnazer_wc_get_transaction_by_order_id" )){
function alnazer_wc_get_transaction_by_order_id($order_id){
global $wpdb;
$table_name = $wpdb->prefix.WC_PAYMENT_KNET_TABLE;
return $wpdb->get_row($wpdb->prepare("SELECT * FROM `$table_name` WHERE `order_id`=%d ORDER BY `id` DESC LIMIT 1",[$order_id]));
}
}