diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..757fee3 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/.idea \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..33929c3 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# Payhubix Gateway + +Integrates Payhubix payment gateway with Easy Digital Downloads. diff --git a/edd-payhubix.php b/edd-payhubix.php new file mode 100644 index 0000000..20f6105 --- /dev/null +++ b/edd-payhubix.php @@ -0,0 +1,273 @@ +init_hooks(); + } + + private function init_hooks() { + add_filter('edd_payment_gateways', array($this, 'register_gateway')); + add_filter('edd_settings_sections_gateways', array($this, 'add_gateway_section')); + add_filter('edd_settings_gateways', array($this, 'add_gateway_settings')); + add_action('edd_gateway_payhubix', array($this, 'process_payment')); + add_action('edd_payhubix_cc_form', array($this, 'payment_form')); + add_action('init', array($this, 'listen_for_payhubix_callback')); + } + + public function register_gateway($gateways) { + $gateways['payhubix'] = array( + 'admin_label' => 'Payhubix', + 'checkout_label' => __('Pay with Payhubix', 'payhubix-edd-gateway') + ); + return $gateways; + } + + public function add_gateway_section($sections) { + $sections['payhubix'] = __('Payhubix', 'payhubix-edd-gateway'); + return $sections; + } + + public function add_gateway_settings($settings) { + $payhubix_settings = array( + array( + 'id' => 'payhubix_settings', + 'name' => '' . __('Payhubix Settings', 'payhubix-edd-gateway') . '', + 'type' => 'header', + ), + array( + 'id' => 'payhubix_api_key', + 'name' => __('Payhubix API Key', 'payhubix-edd-gateway'), + 'type' => 'textarea', + 'desc' => __('Enter your Payhubix API Key', 'payhubix-edd-gateway') + ), + array( + 'id' => 'payhubix_shop_id', + 'name' => __('Payhubix Shop ID', 'payhubix-edd-gateway'), + 'type' => 'text', + 'desc' => __('Enter your Payhubix Shop ID', 'payhubix-edd-gateway') + ), + array( + 'id' => 'payhubix_time_for_payment', + 'name' => __('Time for Payment', 'payhubix-edd-gateway'), + 'type' => 'select', + 'options' => array( + '00:15' => '15 minutes', + '00:30' => '30 minutes', + '01:00' => '1 hour', + '02:00' => '2 hours', + '03:00' => '3 hours', + ), + 'desc' => __('Select the time allowed for payment', 'payhubix-edd-gateway') + ) + ); + + if (current_filter() === 'edd_settings_gateways') { + $settings = array_merge($settings, $payhubix_settings); + } + + return $settings; + } + + public function payment_form() { + return; + } + + public function process_payment($purchase_data) { + $this->api_key = edd_get_option('payhubix_api_key'); + $this->shop_id = edd_get_option('payhubix_shop_id'); + $this->time_for_payment = edd_get_option('payhubix_time_for_payment', '02:00'); + + if (empty($this->api_key) || empty($this->shop_id)) { + edd_set_error('payhubix_config_error', __('Payhubix gateway is not configured correctly.', 'payhubix-edd-gateway')); + edd_send_back_to_checkout(); + return; + } + + $payment_data = array( + 'price' => $purchase_data['price'], + 'date' => $purchase_data['date'], + 'user_email' => $purchase_data['user_email'], + 'purchase_key' => $purchase_data['purchase_key'], + 'currency' => edd_get_currency(), + 'downloads' => $purchase_data['downloads'], + 'user_info' => $purchase_data['user_info'], + 'cart_details' => $purchase_data['cart_details'], + 'gateway' => 'payhubix', + 'status' => 'pending' + ); + + $payment_id = edd_insert_payment($payment_data); + + $response = $this->call_payhubix_api($payment_id, $purchase_data); + + if (!isset($response['error']) || $response['error'] === false) { + edd_update_payment_meta($payment_id, '_payhubix_invoice_id', $response['message']['link']); + + wp_redirect($response['message']['invoice_url']); + exit; + } else { + edd_set_error('payhubix_payment_error', $response['message']); + edd_send_back_to_checkout(); + return; + } + } + + /** + * Make API call to Payhubix + */ + private function call_payhubix_api($payment_id, $purchase_data) { + $url = 'https://api.payhubix.com/v1/payment/shops/' . $this->shop_id . '/invoices/'; + + $data = [ + 'currency_amount' => (float) $purchase_data['price'], + 'currency_symbol' => edd_get_currency(), + 'customer_email' => $purchase_data['user_email'], + 'time_for_payment' => $this->time_for_payment, + 'currencies' => [], + 'order_id' => $payment_id, + 'order_description'=> 'Digital Download Purchase', + 'callback_url' => add_query_arg(['edd-listener' => 'payhubix', 'order_id' => $payment_id], home_url('index.php')), + ]; + + $args = [ + 'body' => json_encode($data), + 'headers' => [ + 'Content-Type' => 'application/json', + 'X-Api-key' => $this->api_key, + ], + 'timeout' => 30, + ]; + + $response = wp_remote_post($url, $args); + + if (is_wp_error($response)) { + return [ + 'error' => true, + 'message' => $response->get_error_message() + ]; + } + + $body = wp_remote_retrieve_body($response); + $decoded_body = json_decode($body, true); + + if (!is_array($decoded_body) || isset($decoded_body['error']) && $decoded_body['error']) { + return [ + 'error' => true, + 'message' => 'Invalid response from Payhubix' + ]; + } + + return $decoded_body; + } + + public function listen_for_payhubix_callback() { + + if (!isset($_GET['edd-listener']) || $_GET['edd-listener'] !== 'payhubix') { + return; + } + + $payment_id = $_GET['order_id']; + + if (!$payment_id) { + error_log('Payhubix callback: Payment not found for key ' . $_GET['key']); + wp_redirect(edd_get_checkout_uri()); + exit; + } + + $invoice_id = edd_get_payment_meta($payment_id, '_payhubix_invoice_id', true); + + $data = $this->check_payment_status($invoice_id); + + if (isset($data['error']) && !$data['error']) { + $invoice_data = $data['message']; + + if ($invoice_id != $invoice_data['link']) { + error_log('Payhubix callback: Invoice ID mismatch for payment ' . $payment_id); + wp_redirect(edd_get_checkout_uri()); + exit; + } + + switch ($invoice_data['status']) { + case 'Paid': + edd_update_payment_status($payment_id, 'complete'); + edd_insert_payment_note($payment_id, 'Payment successfully processed by Payhubix.'); + break; + + case 'Canceled': + edd_update_payment_status($payment_id, 'cancelled'); + edd_insert_payment_note($payment_id, 'Payment was canceled by the customer or Payhubix.'); + break; + + case 'PartiallyExpired': + case 'Expired': + edd_update_payment_status($payment_id, 'failed'); + edd_insert_payment_note($payment_id, 'Payment expired or partially expired.'); + break; + + default: + edd_update_payment_status($payment_id, 'pending'); + edd_insert_payment_note($payment_id, 'Payment status: ' . $invoice_data['status']); + break; + } + + wp_redirect(edd_get_success_page_uri()); + exit; + } else { + error_log('Payhubix callback error: ' . json_encode($data)); + wp_redirect(edd_get_checkout_uri()); + exit; + } + } + + private function check_payment_status($invoice_id) { + $url = 'https://api.payhubix.com/v1/payment/invoices/' . $invoice_id; + + $args = [ + 'headers' => [ + 'Content-Type' => 'application/json', + 'X-Api-key' => edd_get_option('payhubix_api_key'), + ], + ]; + + $response = wp_remote_get($url, $args); + + if (is_wp_error($response)) { + return [ + 'error' => true, + 'message' => $response->get_error_message() + ]; + } + + $body = wp_remote_retrieve_body($response); + $data = json_decode($body, true); + + return $data; + } +} + +function payhubix_edd_init() { + new Payhubix_EDD_Gateway(); +} +add_action('plugins_loaded', 'payhubix_edd_init'); +?> diff --git a/readme.txt b/readme.txt new file mode 100644 index 0000000..0c7ec05 --- /dev/null +++ b/readme.txt @@ -0,0 +1,35 @@ +=== Easy Digital Downloads Payhubix Gateway === +Contributors: Payhubix TM, Mohammad Bina +Donate link: https://payhubix.com/ +Tags: easy-digital-downloads, payhubix +Requires at least: 6.5 or higher +Tested up to: 6.7.1 +Stable tag: 1.0.0 +Requires PHP: 7.4 or higher +License: GPLv2 or later +License URI: http://www.gnu.org/licenses/gpl-2.0.html + +== Description == +**Easy Digital Downloads Payhubix Gateway** Integrates Payhubix payment gateway with Easy Digital Downloads. + +== Features == + * Compatible with latest Easy Digital Downloads and older versions + * Simple and user-friendly settings panel + * Ability to set the invoice expiration time + * Display payment gateway errors + + + + +== Installation == +1. Upload the ‘edd-payhubix’ folder to the path ‘/wp-content/plugins/’. +2. Activate the plugin through the ‘Plugins’ menu in WordPress. +3. You can configure the plugin settings through the Easy Digital Downloads settings / Payment section. + + + +== Change log == + += 1.0.0 = +Initial release +