-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsave-settings.php
89 lines (80 loc) · 3.03 KB
/
save-settings.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
<?php
require_once('../../../wp-load.php');
$content = trim(file_get_contents("php://input"));
$decoded = json_decode($content, true);
if (isset($decoded['client_id']) && isset($decoded['app_sceret']) && isset($decoded['currency']) && isset($decoded['default_amount']) && isset($decoded['environment'])) {
global $wpdb;
try {
if ($decoded['environment'] == 'test') {
$api_url = 'https://api-m.sandbox.paypal.com';
} else {
$api_url = 'https://api-m.paypal.com';
}
$client_id = $decoded['client_id'];
$app_sceret = $decoded['app_sceret'];
$currency = $decoded['currency'];
$default_amount = $decoded['default_amount'];
$environment = $decoded['environment'];
$sql = "CREATE TABLE " . $wpdb->prefix . "Paypal_Settings (client_id VARCHAR(100) NOT NULL, app_sceret VARCHAR(100) NOT NULL, currency VARCHAR(5) NOT NULL, default_amount FLOAT(2) NOT NULL);";
maybe_create_table($wpdb->prefix . "Paypal_Settings", $sql);
$result = $wpdb->get_results("SELECT * FROM " . $wpdb->prefix . "Paypal_Settings", ARRAY_A);
if (empty($result)) {
$result = $wpdb->insert(
$wpdb->prefix . "Paypal_Settings",
array(
'client_id' => $client_id,
'app_sceret' => $app_sceret,
'currency' => $currency,
'default_amount' => $default_amount,
'environment' => $environment,
'api_url' => $api_url
)
);
if ($result === false) {
echo json_encode(array(
'msg' => 'Message has not been sent',
'error' => $wpdb->last_error
), JSON_PRETTY_PRINT);
die();
}
} else {
$result = $wpdb->update(
$wpdb->prefix . "Paypal_Settings",
array(
'client_id' => $client_id,
'app_sceret' => $app_sceret,
'currency' => $currency,
'default_amount' => $default_amount,
'environment' => $environment,
'api_url' => $api_url
)
);
if ($result === false) {
echo json_encode(array(
'msg' => 'Message has not been sent',
'error' => $wpdb->last_error
), JSON_PRETTY_PRINT);
die();
}
}
echo json_encode(array(
'msg' => 'update'
), JSON_PRETTY_PRINT);
die();
} catch (Exception $e) {
echo json_encode(array(
'error' => 'Cannot update settings',
'msg' => $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();
}