-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclass-gf-paystack-api.php
118 lines (101 loc) · 3.33 KB
/
class-gf-paystack-api.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
<?php
if (!defined('ABSPATH')) {
die('You are not allowed to call this page directly.');
}
class GFPaystackApi
{
public $plugin_name;
protected $public_key;
protected $secret_key;
public function __construct($config)
{
$this->plugin_name = 'Paystack Addon for Gravity Forms';
$this->secret_key = $config->secret_key ?? '';
$this->public_key = $config->public_key ?? '';
}
/**
* Track Payment Transactions from this Plugin
*
* @param string $trx_ref
* @return void
*/
public function log_transaction_success($reference)
{
// Send reference to logger along with plugin name and public key
// $params = [
// 'plugin_name' => $this->plugin_name,
// 'public_key' => $this->public_key,
// 'transaction_reference' => $reference
// ];
$params = [
'plugin_name' => 'pstk-gravityforms',
'public_key' => $this->public_key,
'transaction_reference' => $reference
];
$this->send_request(
'log/charge_success',
$params,
'post',
'https://plugin-tracker.paystackintegrations.com/'
);
}
/**
* Send request to the Paystack Api
*
* @param string $endpoint API request path
* @param array $args API request arguments
* @param string $method API request method
* @param string $domain API request uri
*
* @return object|null JSON decoded transaction object. NULL on API error.
*/
public function send_request(
$endpoint,
$args = array(),
$method = 'post',
$domain = 'https://api.paystack.co/'
) {
$uri = "{$domain}{$endpoint}";
$arg_array = array(
'method' => strtoupper($method),
'body' => $args,
'timeout' => 15,
'headers' => $this->get_headers()
);
$res = wp_remote_request($uri, $arg_array);
if (is_wp_error($res)) {
throw new Exception(sprintf(__('You had an HTTP error connecting to %s', 'gravityformspaystack'), $this->name));
}
$body = json_decode($res['body'], true);
if ($body !== null) {
error_log(__METHOD__ . '(): for ' . $uri . ' Paystack Request ' . print_r($arg_array, true) . ' Paystack Response => ' . print_r($body, true));
if (isset($body['error']) || $body['status'] == false) {
throw new Exception("{$body['message']}");
} else {
return $body;
}
} else { // Un-decipherable message
throw new Exception(sprintf(__('There was an issue connecting with the payment processor. Try again later.', 'gravityformspaystack'), $this->name));
}
return false;
}
/**
* Validate Webhook Signature
*
* @param $input
* @return boolean
*/
public function validate_webhook($input)
{
return $_SERVER['HTTP_X_PAYSTACK_SIGNATURE'] == hash_hmac('sha512', $input, $this->secret_key);
}
/**
* Generates the headers to pass to API request.
*/
public function get_headers()
{
return apply_filters('gf_paystack_request_headers', [
'Authorization' => "Bearer {$this->secret_key}"
]);
}
}