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

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

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

回调有什么作用?

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

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

myplugin.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';

}

}

callback.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"];

6a759f160f920c889d5e15e8ba2ed324.png

任何帮助将不胜感激.

解决方法:

将其添加到

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!

}

}

标签:checkout,woocommerce,payment-gateway,wordpress,php

来源: https://codeday.me/bug/20191118/2029780.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值