Plugin Directory

Changeset 2900368


Ignore:
Timestamp:
04/17/2023 09:02:17 PM (3 years ago)
Author:
hasanayoub
Message:

Version 2.01

Location:
paylink/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • paylink/trunk/paylink.php

    r2899749 r2900368  
    1 <?php
    2 /** @noinspection DuplicatedCode */
    3 /** @noinspection PhpUnused */
    4 /** @noinspection PhpUndefinedMethodInspection */
    5 /** @noinspection PhpUndefinedConstantInspection */
    6 
    7 /*
    8 Plugin Name: Paylink
    9 Description: Use this woocommerce payment gateway plugin to enable clients of your store to pay using Paylink gateway.
    10 Version: 1.9
    11 Author: Paylink Co
    12 text-domain: paylink
    13 Plugin URI: https://paylink.sa
    14 Domain Path: /languages
    15 */
     1<?php /** @noinspection DuplicatedCode */
     2/**
     3 * Plugin Name: Paylink
     4 * Description: Use this woocommerce payment gateway plugin to enable clients of your store to pay using Paylink gateway.
     5 * Version: 2.01
     6 * Author: Paylink Co
     7 * text-domain: paylink
     8 * Plugin URI: https://paylink.sa
     9 * Domain Path: /languages
     10 */
     11
    1612add_action('plugins_loaded', 'init_PayLinkAPI', 0);
    1713
    1814function init_PayLinkAPI()
    1915{
    20     if (!class_exists('WC_Payment_Gateway')) {
    21         return;
     16    if (!defined('ABSPATH')) {
     17        exit; // Exit if accessed directly.
    2218    }
    2319
     20    // Register the Paylink payment gateway.
     21    function paylinkwc_register_gateway($gateways)
     22    {
     23        $gateways[] = 'WcPayLink';
     24        return $gateways;
     25    }
     26
     27    add_filter('woocommerce_payment_gateways', 'paylinkwc_register_gateway');
     28
     29    // Add the Paylink order meta.
     30    function paylinkwc_add_order_meta($order_id)
     31    {
     32        update_post_meta($order_id, '_paylink_transaction_no', $_GET['transactionNo']);
     33    }
     34
     35    add_action('woocommerce_thankyou', 'paylinkwc_add_order_meta');
     36
     37    // Display the Paylink order reference in the order details.
     38    function paylinkwc_display_order_reference($order)
     39    {
     40        $paylink_transaction_no = $order->get_meta('_paylink_transaction_no');
     41        if ($paylink_transaction_no) {
     42            echo '<p><strong>' . __('Paylink Transaction No', 'paylinkwc') . ':</strong> ' . $paylink_transaction_no . '</p>';
     43        }
     44    }
     45
     46    add_action('woocommerce_order_details_after_order_table', 'paylinkwc_display_order_reference');
     47
    2448    /**
    25      * @param $methods
    26      * @return array
     49     * The main class representing the PaylinkWC payment gateway.
    2750     */
    28     function add_WC_PayLink($methods)
    29     {
    30         $methods[] = 'WcPayLink';
    31         return $methods;
    32     }
    33 
    34     add_filter('woocommerce_payment_gateways', 'add_WC_PayLink');
    35 
    3651    class WcPayLink extends WC_Payment_Gateway
    3752    {
    38         private $_getInvoiceUrl;
    39         private $_addInvoiceUrl;
    40         private $_loginUrl;
    41         private $_base_server_url;
    4253        private $_token_auth;
     54        private string $_get_invoice_url;
     55        private string $_add_invoice_url;
     56        private string $_login_url;
     57        private string $_base_server_url;
     58        private string $_is_testing;
    4359
    4460        private function postError($error, $calledUrl, $method)
     
    6581        }
    6682
    67         /**
    68          * Constructor for the gateway.
    69          */
    7083        public function __construct()
    7184        {
    72             load_plugin_textdomain('paylink', false, 'paylink_wc_payment_gateway/languages');
    73 
    74             $this->id = 'paylink';
    75             $this->has_fields = false;
     85            load_plugin_textdomain('paylinkwc', false, 'paylink_wc_payment_gateway/languages');
     86
     87            $this->id = 'paylinkwc';
    7688            $this->icon = apply_filters('woocommerce_paylink_icon', 'https://paylink.sa/assets/img/paylink-logo.png');
    77             $this->order_button_text = __('Pay Using PayLink', 'paylink');
    78             $this->title = __('Paylink', 'paylink');
    79             $this->method_title = 'Paylink Payment Gateway';
     89            $this->has_fields = true;
     90            $this->method_title = __('Paylink Payment Gateway', 'paylink');
    8091            $this->method_description = __('It provides your customers with the popular payment methods in the Kingdom of Saudi Arabia, such as Mada, Visa, MasterCard, and Apple Pay', 'paylink');
    8192
    82             $this->enabled = $this->get_option('enabled');
    83             $this->description = __('Pay by Mada, VISA, Mastercard, or ApplePay', 'paylink');
    84 
    85             // Load the settings.
     93            $this->supports = [
     94                'products',
     95                'refunds'
     96            ];
     97
    8698            $this->init_form_fields();
    8799            $this->init_settings();
    88100
    89             $isTestingEnv = $this->get_option('is_testing_env');
    90             if ($isTestingEnv === 'on' || $isTestingEnv === 'yes') {
     101            $this->enabled = $this->get_option('enabled');
     102            $this->title = $this->get_option('title');
     103            $this->description = $this->get_option('description');
     104
     105            $this->_is_testing = $this->get_option('is_testing_env');
     106            if ($this->_is_testing === 'on' || $this->_is_testing === 'yes') {
    91107                $this->_base_server_url = 'https://restpilot.paylink.sa';
    92108            } else {
    93109                $this->_base_server_url = 'https://restapi.paylink.sa';
    94110            }
    95             $this->_getInvoiceUrl = $this->_base_server_url . '/api/getInvoice';
    96             $this->_addInvoiceUrl = $this->_base_server_url . '/api/addInvoice';
    97             $this->_loginUrl = $this->_base_server_url . '/api/auth';
     111            $this->_get_invoice_url = $this->_base_server_url . '/api/getInvoice';
     112            $this->_login_url = $this->_base_server_url . '/api/auth';
     113            $this->_add_invoice_url = $this->_base_server_url . '/api/addInvoice';
    98114
    99115            add_action('init', [$this, 'check_paylink_response']);
     
    102118            add_action('woocommerce_api_' . strtolower(get_class($this)), [&$this, 'check_paylink_response']);
    103119
    104             /** @noinspection PhpUndefinedConstantInspection */
    105             if (version_compare(WOOCOMMERCE_VERSION, '2.0.0', '>=')) {
    106                 /* 2.0.0 */
    107                 add_action('woocommerce_update_options_payment_gateways_' . $this->id, [&$this, 'process_admin_options']);
    108             } else {
    109                 /* 1.6.6 */
    110                 add_action('woocommerce_update_options_payment_gateways', [&$this, 'process_admin_options']);
    111             }
    112 
    113             /** @noinspection PhpUndefinedConstantInspection */
    114             if (version_compare(WOOCOMMERCE_VERSION, '2.0.0', '>=')) {
    115                 /* 2.0.0 */
    116                 add_action('woocommerce_update_options_payment_gateways_' . $this->id, [&$this, 'process_admin_options']);
    117             } else {
    118                 /* 1.6.6 */
    119                 add_action('woocommerce_update_options_payment_gateways', [&$this, 'process_admin_options']);
    120             }
    121             add_action('woocommerce_receipt_paylink', [&$this, 'receipt_page']);
    122             add_action('woocommerce_receipt_WC_paylink', [&$this, 'receipt_page']);
    123         }
    124 
    125         /**
    126          * @return void
    127          */
     120            add_action('woocommerce_update_options_payment_gateways_' . $this->id, [$this, 'process_admin_options']);
     121
     122            add_action('woocommerce_receipt_paylinkwc', [&$this, 'receipt_page']);
     123            add_action('woocommerce_receipt_WC_paylinkwc', [&$this, 'receipt_page']);
     124
     125            add_action('wp_head', [&$this, 'write_order_payment_header']);
     126        }
     127
    128128        public function process_admin_options()
    129129        {
    130130            parent::process_admin_options();
    131             if ($this->settings['is_testing_env'] == 'yes') {
     131            if ($this->settings['is_testing_env'] == 'yes' || $this->settings['is_testing_env'] == 'on') {
    132132                $this->add_error(__("<span style='color:red;font-weight: bold'>You are using the testing environment</span>. Please use the testing credentials, <b>APP ID: APP_ID_1123453311</b>, <b>Secret Key: 0662abb5-13c7-38ab-cd12-236e58f43766</b>", 'paylink'));
    133133                $this->display_errors();
     
    138138        {
    139139            $this->form_fields = [
     140                'enabled' => [
     141                    'title' => __('Enable/Disable', 'paylink'),
     142                    'type' => 'checkbox',
     143                    'label' => __('Enable Paylink payment', 'paylink'),
     144                    'default' => 'yes'
     145                ],
     146                'title' => [
     147                    'title' => __('Title', 'paylink'),
     148                    'type' => 'text',
     149                    'description' => __('This controls the title which the user sees during checkout.', 'paylink'),
     150                    'default' => __('Pay with Paylink', 'paylink'),
     151                    'desc_tip' => true
     152                ],
     153                'description' => [
     154                    'title' => __('Description', 'paylink'),
     155                    'type' => 'textarea',
     156                    'description' => __('This controls the description which the user sees during checkout.', 'paylink'),
     157                    'default' => __('Pay using Paylink', 'paylink'),
     158                    'desc_tip' => true
     159                ],
    140160                'app_id' => [
    141161                    'title' => __('APP ID', 'paylink'),
    142162                    'type' => 'text',
     163                    'description' => __('This is the APP ID assigned to you by Paylink.', 'paylink'),
     164                    'default' => '',
     165                    'desc_tip' => true,
     166                    'required' => true
    143167                ],
    144168                'secret_key' => [
    145169                    'title' => __('Secret Key', 'paylink'),
    146170                    'type' => 'text',
     171                    'description' => __('This is the API Secret key assigned to you by Paylink.', 'paylink'),
     172                    'default' => '',
     173                    'desc_tip' => true,
     174                    'required' => true
     175                ],
     176                'callback_url' => [
     177                    'title' => __('Callback URL', 'paylink'),
     178                    'type' => 'text',
     179                    'description' => __('This is the URL where Paylink will send the payment status callback.', 'paylink'),
     180//                    'default' => get_site_url() . '/wc-api/paylinkwc/',
     181                    'default' => get_site_url() . '/wc-api/' . strtolower(get_class($this)),
     182                    'desc_tip' => true,
     183                    'custom_attributes' => [
     184                        'readonly' => 'readonly'
     185                    ]
     186                ],
     187//                'payment_form' => [
     188//                    'title' => __('Payment Form', 'paylink'),
     189//                    'type' => 'select',
     190//                    'description' => __('This controls the payment form which the user sees during checkout. On-Site will display payment in the same of the merchant website. The off-site fee will redirect income to Paylink payment pages.', 'paylink'),
     191//                    'options' => [
     192//                        'onsite' => __('On Site', 'paylink'),
     193//                        'offsite' => __('Off Site', 'paylink'),
     194//                    ],
     195//                    'desc_tip' => true
     196//                ],
     197                'display_thank_you' => [
     198                    'title' => __('Display Thank you page', 'paylink'),
     199                    'type' => 'select',
     200                    'description' => __('Display a Thank you page before redirecting to the Paylink payment page.', 'paylink'),
     201                    'options' => [
     202                        'yes' => __('Yes', 'paylink'),
     203                        'no' => __('No', 'paylink'),
     204                    ],
     205                    'desc_tip' => true
    147206                ],
    148207                'is_testing_env' => [
    149208                    'title' => __('Environment', 'paylink'),
    150209                    'type' => 'select',
     210                    'description' => __('This controls the payment gateway backend, either the production or testing environment.', 'paylink'),
    151211                    'options' => [
    152212                        'yes' => __('Testing Environment', 'paylink'),
    153213                        'no' => __('Production Environment', 'paylink'),
    154214                    ],
     215                    'desc_tip' => true
    155216                ],
    156217                'fail_msg' => [
    157218                    'title' => __('Failed Payment Message', 'paylink'),
     219                    'description' => __('This controls the error message that appears to the user if the payment fails.', 'paylink'),
    158220                    'type' => 'textarea',
     221                    'desc_tip' => true
    159222                ],
    160223            ];
    161224        }
    162225
    163         /**
    164          * 2) Receipt Page be called.
    165          * @param $order_id
    166          */
    167226        public function receipt_page($order_id)
    168227        {
     228            $paymentForm = $this->get_option('payment_form');
     229//            if ($paymentForm == 'onsite') {
     230//                wp_enqueue_script('paylinkwc', 'https://paylink.sa/assets/js/paylink.js', [], '1.0.0');
     231//                echo '<p>' . __('Thank you for your order', 'paylink') . $this->get_payment_button_html($order_id) . '</p>';
     232//            } else {
    169233            $url = $this->generate_paylink_url($order_id);
    170234            if ($url) {
    171                 echo '<p>' . __('Thank you for your order, you will be redirected to Paylink payment page.', 'paylink') . '</p>';
    172                 echo "<script>setTimeout(function() {window.location.href = '" . $url . "';}, 2000);</script>";
     235                $display_thank_you = $this->get_option('display_thank_you');
     236
     237                if ($display_thank_you == 'no') {
     238                    wp_redirect($url);
     239                    exit;
     240                } else {
     241                    echo '<p>' . __('Thank you for your order, you will be redirected to Paylink payment page.', 'paylink') . '</p>';
     242                    echo "<script>setTimeout(function() {window.location.href = '" . $url . "';}, 2000);</script>";
     243                }
    173244            } else {
    174245                echo '<p>' . __('Something error. Please try again by refreshing the page', 'paylink') . '</p>';
    175246            }
    176         }
    177 
    178         /**
    179          * 3) Generate tap button link
    180          * @param $order_id
    181          * @return bool|mixed
    182          */
     247//            }
     248        }
     249
    183250        private function generate_paylink_url($order_id)
    184251        {
     
    193260        }
    194261
    195         /**
    196          * 4) Create Paylink invoice
    197          * @param $token
    198          * @param $order_id
    199          * @return bool|mixed
    200          */
    201262        private function createPaylinkInvoiceUrl($token, $order_id)
    202263        {
    203             $order = new WC_Order($order_id);
     264            $order = wc_get_order($order_id);
     265//            $callbackUrl = get_site_url() . '/wc-api/paylinkwc'; <-- this did not work.
     266
    204267            $redirect_url = $order->get_checkout_order_received_url();
    205268            if (version_compare(WOOCOMMERCE_VERSION, '2.0.0', '>=')) {
    206269                $redirect_url = add_query_arg('wc-api', strtolower(get_class($this)), $redirect_url);
    207270            }
     271
    208272            $total = $order->get_total();
    209273            $customerEmail = $order->get_billing_email();
     
    238302            ];
    239303            try {
    240                 $response = wp_safe_remote_post($this->_addInvoiceUrl, $parameter);
     304                $response = wp_safe_remote_post($this->_add_invoice_url, $parameter);
    241305                return json_decode($response['body'])->url;
    242306            } catch (Exception $exception) {
    243307                error_log(print_r($exception, true));
    244                 $this->postError(print_r($exception, true), $this->_addInvoiceUrl, 'createPaylinkInvoiceUrl');
     308                $this->postError(print_r($exception, true), $this->_add_invoice_url, 'createPaylinkInvoiceUrl');
    245309                return false;
    246310            }
    247311        }
    248312
    249         /**
    250          * @return mixed
    251          */
    252313        private function login()
    253314        {
     
    261322                    'persistToken' => true
    262323                ];
    263                 $login_response = wp_safe_remote_post($this->_loginUrl, [
     324                $login_response = wp_safe_remote_post($this->_login_url, [
    264325                    'headers' => [
    265326                        'Content-Type' => 'application/json',
     
    267328                    'body' => wp_json_encode($login_parameter),
    268329                ]);
    269                 $this->postError('logged in', $this->_loginUrl, 'login');
    270                 $this->postError(print_r($login_response, true), $this->_loginUrl, 'login');
     330                $this->postError('logged in', $this->_login_url, 'login');
     331                $this->postError(print_r($login_response, true), $this->_login_url, 'login');
    271332                return json_decode($login_response['body'])->id_token;
    272333            } catch (Exception $ex) {
    273334                error_log(print_r($ex, true));
    274                 $this->postError(print_r($ex, true), $this->_loginUrl, 'login');
     335                $this->postError(print_r($ex, true), $this->_login_url, 'login');
    275336                return false;
    276337            }
    277338        }
    278339
    279         /**
    280          * 1) After click on payment, this method will be called.
    281          * @param $order_id
    282          * @return array
    283          */
    284340        public function process_payment($order_id)
    285341        {
    286             $order = new WC_Order($order_id);
    287             /** @noinspection PhpUndefinedConstantInspection */
     342            // Get the order object
     343            $order = wc_get_order($order_id);
     344
     345            // Add custom order notes
     346            $order->add_order_note('Thank you for your order. Please complete your payment using the payment button below.');
     347
     348            // Get the payment gateway URL
     349//            $checkout_payment_url = $order->get_checkout_payment_url(true);
    288350            if (version_compare(WOOCOMMERCE_VERSION, '2.1.0', '>=')) {
    289351                /* 2.1.0 */
     
    293355                $checkout_payment_url = get_permalink(get_option('woocommerce_pay_page_id'));
    294356            }
     357            // Return the payment button HTML
    295358            return [
    296359                'result' => 'success',
     
    313376            $transaction_no = sanitize_text_field($_REQUEST['transactionNo']);
    314377
    315             $checkout_url = wc_get_checkout_url();
     378            $checkout_url = wc_get_checkout_url() . '&transactionNo=' . $transaction_no;
    316379
    317380            if (isset($order_id) && isset($transaction_no)) {
    318                 $order = new WC_Order($order_id);
     381                $order = wc_get_order($order_id);
     382
    319383                $this->_token_auth = get_post_meta($order_id, 'token_auth');
    320384                if (!$this->_token_auth) {
    321385                    $this->_token_auth = $this->login();
    322386                }
    323 
    324387                $options = [
    325388                    'headers' => [
     
    330393                    'user-agent' => '1.0',
    331394                ];
    332                 $response = wp_safe_remote_get($this->_getInvoiceUrl . '/' . $transaction_no, $options);
     395                $response = wp_safe_remote_get($this->_get_invoice_url . '/' . $transaction_no, $options);
    333396                try {
    334397                    $order_status = sanitize_text_field(json_decode($response['body'])->{'orderStatus'});
    335398                    $order_status = mb_convert_case($order_status, MB_CASE_LOWER, 'UTF-8');
     399
    336400                    // validating order status value
    337401                    if ($order_status == 'paid' || $order_status == 'completed') {
    338                         $checkout_url = $order->get_checkout_order_received_url();
     402                        $checkout_url = $order->get_checkout_order_received_url() . '&transactionNo=' . $transaction_no;
    339403                        $order->payment_complete($transaction_no);
    340404                        $woocommerce->cart->empty_cart();
     
    356420            }
    357421            wp_redirect($checkout_url);
     422            //            if (isset($_REQUEST['transactionNo'])) {
     423//                $transaction_no = $_REQUEST['transactionNo'];
     424//                $order_id = $_REQUEST['order_id'];
     425//                $status = $_REQUEST['status'];
     426//
     427//                $order = wc_get_order($order_id);
     428//
     429//                if ($order) {
     430//                    if ($status == 'completed') {
     431//                        $order->update_status('processing');
     432//                    } else {
     433//                        $order->update_status('failed');
     434//                    }
     435//
     436//                    wc_reduce_stock_levels($order_id);
     437//                    wc_delete_product_transients();
     438//
     439//                    $thankyou_page = $this->get_return_url($order);
     440//
     441//                    wc_get_template('checkout/thankyou.php', [
     442//                        'order' => $order,
     443//                        'thankyou_page' => $thankyou_page
     444//                    ]);
     445//                    exit;
     446//                }
     447//            }
     448        }
     449
     450        public function payment_fields()
     451        {
     452            if ($this->description) {
     453                echo wpautop(wptexturize($this->description));
     454            }
     455        }
     456
     457        public function write_order_payment_header()
     458        {
     459            if (!isset($_GET['key'])) {
     460                return '';
     461            }
     462            $key = $_GET['key'];
     463            $order_id = wc_get_order_id_by_order_key($key);
     464            $order = wc_get_order($order_id);
     465//            $callbackUrl = get_site_url() . '/wc-api/paylinkwc';
     466//            $callbackUrl = get_site_url() . '/wc-api/' . strtolower(get_class($this));
     467
     468            $callbackUrl = $order->get_checkout_order_received_url();
     469            if (version_compare(WOOCOMMERCE_VERSION, '2.0.0', '>=')) {
     470                $callbackUrl = add_query_arg('wc-api', strtolower(get_class($this)), $callbackUrl);
     471            }
     472
     473            $amount = $order->get_total();
     474            $orderNumber = $order_id;
     475            $currency = $order->get_currency();
     476
     477            $clientName = '';
     478            if ($order->get_billing_first_name() || $order->get_billing_last_name()) {
     479                $clientName = ($order->get_billing_first_name() ?? ' ') . ' ' . ($order->get_billing_last_name() ?? ' ');
     480            } elseif ($order->get_shipping_first_name() || $order->get_shipping_last_name()) {
     481                $clientName = ($order->get_shipping_first_name() ?? ' ') . ' ' . ($order->get_shipping_last_name() ?? ' ');
     482            }
     483
     484            $clientMobile = '';
     485            if ($order->get_billing_phone()) {
     486                $clientMobile = $order->get_billing_phone();
     487            } elseif ($order->get_shipping_phone()) {
     488                $clientMobile = $order->get_shipping_phone();
     489            }
     490
     491            $items = $order->get_items();
     492            $products = '';
     493            foreach ($items as $item) {
     494                $product_id = $item->get_product_id();
     495                $product_name = $item->get_name();
     496                $product_quantity = $item->get_quantity();
     497                $product_total = $item->get_total();
     498                $products .= "{title: '$product_id - $product_name', price: $product_total, qty: $product_quantity},";
     499            }
     500
     501            $mode = $this->_is_testing == 'on' || $this->_is_testing == 'yes' ? 'test' : 'production';
     502            $token = $this->login();
     503
     504            echo "
     505            <script>
     506            function submitPaylinkPayment() {
     507                // paylink
     508                let paylinkOrder = new Order({
     509                    callBackUrl: '$callbackUrl', // callback page URL (for example http://localhost:6655 processPayment.php) in your site to be called after payment is processed. (mandatory)
     510                    clientName: '$clientName', // the name of the buyer. (mandatory)
     511                    clientMobile: '$clientMobile', // the mobile of the buyer. (mandatory)
     512                    amount: $amount, // the total amount of the order (including VAT or discount). (mandatory). NOTE: This amount is used regardless of total amount of products listed below.
     513                    currency: '$currency',
     514                    clientEmail: '$clientName',
     515                    orderNumber: '$orderNumber', // the order number in your system. (mandatory)
     516                    products: [ // list of products (optional)
     517                        $products
     518                    ],
     519                });
     520                let payment = new PaylinkPayments({mode: '$mode', defaultLang: 'ar', backgroundColor: '#EEE'});
     521                payment.openPayment('$token', paylinkOrder);
     522            }
     523           
     524            function submitPaylinkApplePay() {
     525                // paylink
     526                let paylinkOrder = new Order({
     527                    callBackUrl: '$callbackUrl', // callback page URL (for example http://localhost:6655 processPayment.php) in your site to be called after payment is processed. (mandatory)
     528                    clientName: '$clientName', // the name of the buyer. (mandatory)
     529                    clientMobile: '$clientMobile', // the mobile of the buyer. (mandatory)
     530                    amount: $amount, // the total amount of the order (including VAT or discount). (mandatory). NOTE: This amount is used regardless of total amount of products listed below.
     531                    currency: '$currency',
     532                    clientEmail: '$clientName',
     533                    orderNumber: '$orderNumber', // the order number in your system. (mandatory)
     534                    products: [ // list of products (optional)
     535                        $products
     536                    ],
     537                });
     538                let payment = new PaylinkPayments({mode: '$mode', defaultLang: 'ar', backgroundColor: '#EEE'});
     539                payment.openApplePay('$token', paylinkOrder);
     540            }
     541        </script>
     542            ";
     543        }
     544
     545        private function get_payment_button_html($order_id)
     546        {
     547            $order = wc_get_order($order_id);
     548
     549            if ($order->get_status() == 'pending') {
     550
     551                $title = $this->get_option('title') ?? 'Pay';
     552
     553                // Build the JavaScript code to be executed on the client side
     554                $javascript_code = "<div>
     555                                        <button onclick='submitPaylinkPayment()'>$title by Bank Card</button>
     556                                        <button onclick='submitPaylinkApplePay()'>$title by ApplePay</button>
     557                                    </div>";
     558                // Return the HTML for the payment button
     559                return $javascript_code;
     560            }
    358561        }
    359562    }
     563
     564// Add the Paylink callback endpoint.
     565//    function paylinkwc_add_callback_endpoint()
     566//    {
     567//        add_rewrite_endpoint('paylinkwc', EP_ROOT);
     568//    }
     569//
     570//    add_action('init', 'paylinkwc_add_callback_endpoint');
     571// Handle the Paylink callback.
     572//    function paylinkwc_handle_callback()
     573//    {
     574//        $gateway = new PaylinkWcGateway();
     575//        $gateway->process_callback();
     576//    }
     577//    add_action('woocommerce_api_paylinkwc', 'paylinkwc_handle_callback');
    360578}
     579//    public function process_payment($order_id)
     580//    {
     581//        $order = wc_get_order($order_id);
     582//
     583//        $params = [
     584//            'app_id' => $this->get_option('app_id'),
     585//            'secret_key' => $this->get_option('secret_key'),
     586//            'amount' => $order->get_total(),
     587//            'currency' => get_woocommerce_currency(),
     588//            'order_id' => $order_id,
     589//            'callback_url' => $this->get_option('callback_url'),
     590//            'success_url' => $this->get_return_url($order),
     591//            'cancel_url' => $order->get_cancel_order_url_raw()
     592//        ];
     593//
     594//        $response = wp_remote_post('https://api.paylink.sa/api/v1/transactions', [
     595//            'method' => 'POST',
     596//            'headers' => [
     597//                'Content-Type' => 'application/json'
     598//            ],
     599//            'body' => json_encode($params),
     600//            'timeout' => 60
     601//        ]);
     602//
     603//        if (is_wp_error($response)) {
     604//            throw new Exception(__('An error occurred while processing your payment. Please try again.', 'paylinkwc'));
     605//        }
     606//
     607//        $response_body = json_decode(wp_remote_retrieve_body($response));
     608//
     609//        if ($response_body->success) {
     610//            return [
     611//                'result' => 'success',
     612//                'redirect' => $response_body->data->payment_url
     613//            ];
     614//        } else {
     615//            throw new Exception(__('An error occurred while processing your payment. Please try again.', 'paylinkwc'));
     616//        }
     617//    }
     618
  • paylink/trunk/readme.txt

    r2899749 r2900368  
    33Requires at least: 5.5.1
    44Tested up to: 6.2
    5 Stable tag: 1.9
     5Stable tag: 2.01
    66License: GPLv2 or later
    77License URI: http://www.gnu.org/licenses/gpl-2.0.html
Note: See TracChangeset for help on using the changeset viewer.