Wie bekomme ich Einstellungsfeldwerte außerhalb der Klasse Woocommerce WC_Payment_Gateway?

Lesezeit: 3 Minuten

Benutzer-Avatar
Alex Seifi

Ich habe erfolgreich ein Zahlungs-Gateway-Plugin mit Woocommerce erstellt, aber ich habe eine Callback-Datei und ich muss den PRIVATE_KEY aus den Checkout-Einstellungsfeldern von außerhalb der WC_Payment_Gateway-Klasse abrufen.

Ich verwende die Sitzung, um den Rückruf zu tätigen, aber ich möchte die Sitzung loswerden und den Wert des Einstellungsfelds (PRIVATE_KEY) verwenden. Ich weiß nicht, wie ich PRIVATE_KEY aus WC-Einstellungsfeldern ziehen kann.

Was bewirkt ein Rückruf?

Der Rückruf erhält das Token und den Betrag von der Hauptkassenfunktion per POST und stellt eine weitere Anfrage an den Server mit dem PRIVATE_KEY zusammen mit dem Betrag, um die Transaktion zu erstellen.

Das Plugin ist das gleiche wie https://gist.github.com/John-Henrique/1617074 und ich benutze https://docs.woothemes.com/document/settings-api/ um die Einstellfelder zu speichern.

Hier ist ein Teil meiner Plugin-_construct-Funktion:

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

Geben Sie hier die Bildbeschreibung ein

Jede Hilfe wäre dankbar.

  • Session war eine temporäre Lösung, um zu zeigen, wie es funktioniert. Einer der Hauptgründe für die Rückrufanforderung ist die Sicherheit. wir dürfen den privaten Schlüssel von myplugin.php weder mit dem Post noch mit der Session weitergeben. Ich muss nur den Wert der Einstellungsfelder von WC außerhalb der Zahlungs-Gateway-Klasse abrufen.

    – Alex Seifi

    17. Mai 2016 um 2:06 Uhr


Fügen Sie es unter add_action(‘plugins_loaded’, ‘woocommerce_myplugin_init’, 0) hinzu;

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!
    }

}

Sie können Woocommerces eigene Instanz Ihres Gateways abrufen, um den Wert des Einstellungsfelds zu erhalten.

$your_gateway = WC()->payment_gateways->payment_gateways()['your_gateway->id'];
$your_gateway->your_setting;

1066470cookie-checkWie bekomme ich Einstellungsfeldwerte außerhalb der Klasse Woocommerce WC_Payment_Gateway?

This website is using cookies to improve the user-friendliness. You agree by using the website further.

Privacy policy