-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathMeprPaystackAPI.php
129 lines (114 loc) · 4.22 KB
/
MeprPaystackAPI.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
<?php
if (!defined('ABSPATH')) {
die('You are not allowed to call this page directly.');
}
class MeprPaystackAPI
{
public $plugin_name;
protected $public_key;
protected $secret_key;
public function __construct($settings)
{
$this->plugin_name = 'Memberpress Paystack Gateway Addon';
$this->secret_key = isset($settings->secret_key) ? $settings->secret_key : '';
$this->public_key = isset($settings->public_key) ? $settings->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,
'transaction_reference' => $reference,
'public_key' => $this->public_key
];
$this->send_request(
'log/charge_success',
$params,
'post',
'https://plugin-tracker.paystackintegrations.com/',
false
);
}
/**
* 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
* @param boolean $blocking Block response for irrelevant requests
* @param boolean $idempotency_key
* @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/',
$blocking = true,
$idempotency_key = false
) {
$mepr_options = MeprOptions::fetch();
$uri = "{$domain}{$endpoint}";
$args = MeprHooks::apply_filters('mepr_paystack_request_args', $args);
$arg_array = array(
'method' => strtoupper($method),
'body' => $args,
'timeout' => 15,
'blocking' => $blocking,
'sslverify' => $mepr_options->sslverify,
'headers' => $this->get_headers()
);
if (false !== $idempotency_key) {
$arg_array['headers']['Idempotency-Key'] = $idempotency_key;
}
$arg_array = MeprHooks::apply_filters('mepr_paystack_request', $arg_array);
// $uid = uniqid();
// $this->email_status("###{$uid} Paystack Call to {$uri} API Key: {$this->settings->secret_key}\n" . MeprUtils::object_to_string($arg_array, true) . "\n", $this->settings->debug);
$resp = wp_remote_request($uri, $arg_array);
// If we're not blocking then the response is irrelevant
// So we'll just return true.
if ($blocking == false)
return true;
if (is_wp_error($resp)) {
throw new MeprHttpException(sprintf(__('You had an HTTP error connecting to %s', 'memberpress'), $this->name));
} else {
if (null !== ($json_res = json_decode($resp['body'], true))) {
//$this->email_status("###{$uid} Paystack Response from {$uri}\n" . MeprUtils::object_to_string($json_res, true) . "\n", $this->settings->debug);
if (isset($json_res['error']) || $json_res['status'] == false)
throw new MeprRemoteException("{$json_res['message']}");
else
return $json_res;
} else // Un-decipherable message
throw new MeprRemoteException(sprintf(__('There was an issue with the payment processor. Try again later.', 'memberpress'), $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(
'mepr_paystack_request_headers',
[
'Authorization' => "Bearer {$this->secret_key}",
]
);
}
}