-
Notifications
You must be signed in to change notification settings - Fork 0
/
submit-request.php
122 lines (110 loc) · 4.22 KB
/
submit-request.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
<?php
$path = preg_replace('/wp-content(?!.*wp-content).*/', '', __DIR__);
require_once($path . 'wp-load.php');
require_once 'config.php';
$content = trim(file_get_contents("php://input"));
$decoded = json_decode($content, true);
if (isset($decoded['paypal_email']) && isset($decoded['email_subject']) && isset($decoded['email_body']) && isset($decoded['amount'])) {
global $wpdb;
$to = $decoded['paypal_email'];
$subject = $decoded['email_subject'];
$body = $decoded['email_body'];
$amount = $decoded['amount'];
$request_id = uniqid();
$request_url = "https://www.bestarhost.com/payment-request/?request_id=" . $request_id;
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: ' . MAIL_FROM . "\r\n";
$result = $wpdb->get_results('SELECT * FROM ' . $wpdb->prefix . 'Paypal_Settings LIMIT 1', ARRAY_A);
if (empty($result)) {
$currency = PAYPAL_DEFAULT_CURRENCY;
$body = str_replace("[amount]", $amount, $body);
$body = str_replace("[currency]", $currency, $body);
$body = str_replace("[request_url]", $request_url, $body);
try {
$result = $wpdb->insert(
$wpdb->prefix . "Paypal_Request",
array(
'request_id' => $request_id,
'email' => $to,
'currency' => $currency,
'amount' => $amount,
'request_url' => $request_url,
'status' => 'pending',
)
);
if (mail($to, $subject, $body, $headers)) {
echo json_encode(array(
'msg' => 'Message has been sent'
), JSON_PRETTY_PRINT);
die();
} else {
echo json_encode(array(
'msg' => 'Message has not been sent',
'error' => error_get_last()['message']
), JSON_PRETTY_PRINT);
die();
}
} catch (Exception $e) {
echo json_encode(array(
'msg' => 'Message has not been sent',
'error' => $e->getMessage()
));
die();
}
} else {
foreach ($result as $row) {
$currency = $row['currency'];
$body = str_replace("[amount]", $amount, $body);
$body = str_replace("[currency]", $currency, $body);
$body = str_replace("[request_url]", $request_url, $body);
try {
$result = $wpdb->insert(
$wpdb->prefix . "Paypal_Request",
array(
'request_id' => $request_id,
'email' => $to,
'currency' => $currency,
'amount' => $amount,
'request_url' => $request_url,
'status' => 'pending',
)
);
if ($result === false) {
echo json_encode(array(
'msg' => 'Message has not been sent',
'error' => $wpdb->last_error
), JSON_PRETTY_PRINT);
die();
}
if (mail($to, $subject, $body, $headers)) {
echo json_encode(array(
'msg' => 'Message has been sent'
), JSON_PRETTY_PRINT);
die();
} else {
echo json_encode(array(
'msg' => 'Message has not been sent',
'error' => error_get_last()['message']
), JSON_PRETTY_PRINT);
die();
}
} catch (Exception $e) {
echo json_encode(array(
'msg' => 'Message has not been sent',
'error' => $e->getMessage()
), JSON_PRETTY_PRINT);
die();
}
}
}
} else {
echo json_encode(
array(
'msg' => 'Cannot updated request status',
'error' => 'Missing needed parameter(s)'
),
JSON_PRETTY_PRINT
);
die();
}