|
| 1 | +<?php |
| 2 | +if (!defined('ABSPATH')) { |
| 3 | + die('You are not allowed to call this page directly.'); |
| 4 | +} |
| 5 | + |
| 6 | +class GFPaystackApi |
| 7 | +{ |
| 8 | + private $plugin_name; |
| 9 | + private $public_key; |
| 10 | + private $secret_key; |
| 11 | + |
| 12 | + public function __construct($config) |
| 13 | + { |
| 14 | + $this->plugin_name = 'Paystack Addon for Gravity Forms'; |
| 15 | + $this->secret_key = $config->secret_key ?? ''; |
| 16 | + $this->public_key = $config->public_key ?? ''; |
| 17 | + } |
| 18 | + |
| 19 | + /** |
| 20 | + * Track Payment Transactions from this Plugin |
| 21 | + * |
| 22 | + * @param string $trx_ref |
| 23 | + * @return void |
| 24 | + */ |
| 25 | + public function log_transaction_success($reference) |
| 26 | + { |
| 27 | + // Send reference to logger along with plugin name and public key |
| 28 | + $params = [ |
| 29 | + 'plugin_name' => $this->plugin_name, |
| 30 | + 'public_key' => $this->public_key, |
| 31 | + 'transaction_reference' => $reference |
| 32 | + ]; |
| 33 | + |
| 34 | + $this->send_request( |
| 35 | + 'log/charge_success', |
| 36 | + $params, |
| 37 | + 'post', |
| 38 | + 'https://plugin-tracker.paystackintegrations.com/' |
| 39 | + ); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Send request to the Paystack Api |
| 44 | + * |
| 45 | + * @param string $endpoint API request path |
| 46 | + * @param array $args API request arguments |
| 47 | + * @param string $method API request method |
| 48 | + * @param string $domain API request uri |
| 49 | + * |
| 50 | + * @return object|null JSON decoded transaction object. NULL on API error. |
| 51 | + */ |
| 52 | + public function send_request( |
| 53 | + $endpoint, |
| 54 | + $args = array(), |
| 55 | + $method = 'post', |
| 56 | + $domain = 'https://api.paystack.co/' |
| 57 | + ) { |
| 58 | + $uri = "{$domain}{$endpoint}"; |
| 59 | + |
| 60 | + $arg_array = array( |
| 61 | + 'method' => strtoupper($method), |
| 62 | + 'body' => $args, |
| 63 | + 'timeout' => 15, |
| 64 | + 'headers' => $this->get_headers() |
| 65 | + ); |
| 66 | + |
| 67 | + $res = wp_remote_request($uri, $arg_array); |
| 68 | + |
| 69 | + if (is_wp_error($res)) { |
| 70 | + throw new Exception(sprintf(__('You had an HTTP error connecting to %s', 'gravityformspaystack'), $this->name)); |
| 71 | + } |
| 72 | + |
| 73 | + $body = json_decode($res['body'], true); |
| 74 | + |
| 75 | + if ($body !== null) { |
| 76 | + if (isset($json_res['error']) || $body['status'] == false) { |
| 77 | + throw new Exception("{$body['message']}"); |
| 78 | + } else { |
| 79 | + return $body; |
| 80 | + } |
| 81 | + } else { // Un-decipherable message |
| 82 | + throw new Exception(sprintf(__('There was an issue connecting with the payment processor. Try again later.', 'gravityformspaystack'), $this->name)); |
| 83 | + } |
| 84 | + |
| 85 | + return false; |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Validate Webhook Signature |
| 90 | + * |
| 91 | + * @param $input |
| 92 | + * @return boolean |
| 93 | + */ |
| 94 | + public function validate_webhook($input) |
| 95 | + { |
| 96 | + return $_SERVER['HTTP_X_PAYSTACK_SIGNATURE'] == hash_hmac('sha512', $input, $this->secret_key); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Generates the headers to pass to API request. |
| 101 | + */ |
| 102 | + public function get_headers() |
| 103 | + { |
| 104 | + return apply_filters( |
| 105 | + 'mepr_paystack_request_headers', |
| 106 | + [ |
| 107 | + 'Authorization' => "Bearer {$this->secret_key}", |
| 108 | + ] |
| 109 | + ); |
| 110 | + } |
| 111 | +} |
0 commit comments