php - 如何在 Woocommerce WC_Payment_Gateway 类之外获取设置字段值?

标签 php wordpress woocommerce payment-gateway checkout

我已经成功地使用 woocommerce 创建了一个支付网关插件,但是我有一个回调文件,我需要从 WC_Payment_Gateway 类外部的结帐设置字段中检索 PRIVATE_KEY。

我正在使用 session 进行回调,但我想摆脱 session 并使用设置字段值 (PRIVATE_KEY)。我不知道如何从 WC 设置字段中提取 PRIVATE_KEY。

回调有什么作用?

回调通过 POST 从主要结帐功能获取 token 和金额,并使用 PRIVATE_KEY 和创建交易的金额向服务器发出另一个请求。

插件与https://gist.github.com/John-Henrique/1617074相同我正在使用 https://docs.woothemes.com/document/settings-api/存储设置字段。

这是我的插件_construct 功能的一部分:

我的插件.php

session_start(); // I HAVE TO GET RID OF SESSION

add_action('plugins_loaded', 'woocommerce_myplugin', 0);

function woocommerce_myplugin(){
    if (!class_exists('WC_Payment_Gateway'))
        return; // if the WC payment gateway class is not available, do nothing

    class WC_Gateway_Myplugin extends WC_Payment_Gateway{

        // Logging
        public static $log_enabled = false;
        public static $log = false;

        public function __construct(){

            $plugin_dir = plugin_dir_url(__FILE__);

            global $woocommerce;

            $this->id = 'myplugin';
            $this->icon = apply_filters('woocommerce_myplugin_icon', ''.$plugin_dir.'myplugin.png');
            $this->has_fields = true;

            // Load the settings
            $this->init_form_fields();
            $this->init_settings();

            // Define user set variables
            $this->title = $this->get_option('title');
            $this->publishable_key = $this->get_option('publishable_key');
            $this->private_key = $this->get_option('private_key');

    unset($_SESSION['private_key']);
    if($this->sandbox == "no"){ 
        $_SESSION['private_key'] = $this->private_key;
        $_SESSION['url'] = 'https://www.xxxxx.io/api/v1/charge';

    } else { 
        $_SESSION['private_key'] = $this->sb_private_key; 
        $_SESSION['url'] = 'https://sandbox.xxxx.io/api/v1/charge';
    }
}

回调.php

$url = $_SESSION['url'];
$token=$_POST["token"]; 
$amount=$_POST["amount"]*100; 

$fields = array(
    'token' => $token,
    'amount' => $amount,
    'key' => $_SESSION['private_key'], //<==== INSTEAD OF SESSION I NEED TO USE WC FUNCT TO GET THE PRIVATE_KEY FROM CHECKOUT SETTINGS.
    );

//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);

//execute post
$result = curl_exec($ch);
$http_code = curl_getinfo($ch)["http_code"];

enter image description here

如有任何帮助,我们将不胜感激。

最佳答案

在下面添加 add_action('plugins_loaded', 'woocommerce_myplugin_init', 0);

function woocommerce_myplugin_init(){

    if (!class_exists('WC_Payment_Gateway'))
        return; // if the WC payment gateway class is not available, do nothing

    class WC_Gateway_Myplugin extends WC_Payment_Gateway{
        public function __construct() {

        }

        public function init_form_fields() {
        $this->form_fields = array(
            'your_field_name'               => array(
                'title'   => __('Enable', 'woocommerce_gateway_myplugin'),
                'type'    => 'text',
                'label'   => __('Some text that describes your plugin name', 'myplugin_gateway'),
                'default' => ''
            )
        );
      }
    }

    // Do your code checking stuff here e.g. 
    $myPluginGateway = new WC_Gateway_Myplugin();

    $fieldNameVar = $myPluginGateway->get_option('your_field_name');

    if ($fieldNameVar == 'something') {
        // everything is okay so far
    } else {
        // OMG we are all going to die!
    }

}

关于php - 如何在 Woocommerce WC_Payment_Gateway 类之外获取设置字段值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37263825/

相关文章:

php - Woocommerce 运费基于特定运输类别的元素数量

PHP - 在字符串中添加指向 URL 的链接(如果不存在)

php - 无效的 JSON 需要 Swift iOS

html - 如何使wordpress div与页面顶部对齐?

Functions.php 中的 WordPress 功能和 current_user_can()

javascript - 使用 jQuery 在 WooCommerce 上增加/减少数量

php - 谷歌网站站长工具 API

php - 根据选项选择更新mysql记录

php - Yii2。如何仅为 GridView 小部件中的特定列指定宽度?

php - 如何将优惠券添加到 WooCommerce 产品页面