0

some help please.

I am trying to build a Woocommerce Payment Plugin with MyCred being the payment processor on another website. The mycred remote api is well configured, and I wrote the payment method already, which is posted below, but I keep getting internal server error. Any help will be highly appreciated, please.

<?php

class joseWallet_WC extends WC_Payment_Gateway {

    function __construct() {

        // global ID
        $this->id = 'josewallet_wc';

        // Show Title
        $this->method_title = __( 'JoseWallet Payment', 'josewallet_wc' );

        // Show Description
        $this->method_description = __( 'Accept payment using JoseWallet', 'josewallet-wc' );

        // Vertical tab title
        $this->title = $this->get_option( 'title' );

        // Description I don't really know
        $this->description = $this->get_option( 'description' );

        // The Icon
        $this->icon = null;

        // Has Checkout fields
        $this->has_fields = true;

        // setting defines
        $this->init_form_fields();

        // load time variable setting
        $this->init_settings();
        
        // Turn these settings into variables we can use
        foreach ( $this->settings as $setting_key => $value ) {
          $this->$setting_key = $value;
        }
        
        // further check of SSL if you want
        add_action( 'admin_notices', array( $this,  'do_ssl_check' ) );

         // Save settings
        if ( is_admin() ) {
          add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );
        }    
    } // Here is the  End __construct()

    // Administration fields for this Payment Gateway
    public function init_form_fields() {
        $this->form_fields = array (
            'enable' => array (
                'title' => __( 'Enable / Disable', 'josewallet-wc' ),
                'label' => __( 'Enable this payment gateway', 'josewallet-wc' ),
                'type' => 'checkbox',
                'default' => 'no'
            ),
            'title' => array(
                'title' => __( 'Title', 'josewallet-wc' ),
                'type' => 'text',
                'desc_tip' => __( 'Title that will appear on the checkout page', 'josewallet-wc' ),
                'default' => __( 'JoseWallet', 'josewallet-wc' )
            ),
            'description' => array(
                'title' => __( 'Description', 'josewallet-wc' ),
                'type' => 'textarea',
                'desc_tip' => __( 'Description that will appear on the checkout page', 'josewallet-wc' ),
                'default' => __( 'Pay with your JoseWallet', 'josewallet-wc' ),
                'css' => 'max-width:450px;'
            ),
            'api_key' => array (
                'title' => __( 'JoseWallet API Key', 'josewallet-wc' ),
                'type' => 'password',
                'desc_tip' => __( 'The API Key given to you from JoseWallet', 'josewallet-wc' )
            ),
            'wallet_email' => array (
                'title' => __( 'Your JoseWallet email', 'josewallet-wc' ),
                'type' => 'email',
                'desc_tip' => __( 'The email address you are using on JoseWallet', 'josewallet-wc' )
            )
        );
    }

    // Response handled for payment gateway
    public function process_payment( $order_id ) {
        global $woocommerce;

        $customer_order = new WC_Order( $order_id );

        // URL to send the request to and the key
        $request_url = 'https://wallet.test/transaction/';
        $api_key    = $this->get_option( 'api_key' );

        $action     = 'PAY';
        $from       = $_POST['jw_payment_email'];
        $pass       = $_POST['jw_payment_pass'];
        $amount     = $customer_order->order_total;
        $to         = $this->get_option( 'wallet_email' );
        $host       = get_bloginfo( 'url' );
        $ref        = 'Payment';
        $ref_id     = 0;
        $entry      = str_replace( 'Payment for order #', '', $customer_order->get_order_number(), '', ' on ', '', $host );
        $data       = 'optional extra';
        $point_type = 'usd';//strtolower( get_woocommerce_currency() );

        $token      = md5( $host . $action . $amount . $from . $to . $api_key );

        // The API information
        $request_to_send    = array(
            'body'   => array(
                'action'  => $action,
                'account' => $from,
                'pass'    => $pass,
                'amount'  => $amount,
                'to'      => $to,
                'ref'     => $ref,
                'ref_id'  => $ref_id,
                'type'    => $point_type,
                'entry'   => $entry,
                'data'    => $data,
                'token'   => $token,
                'host'    => $host
            )
        );


        // Send and request and return the response
        $response = wp_remote_post( $request_url, array(
            'method'    => 'POST',
            'body'      => http_build_query( $request_to_send ),
            'timeout'   => 90,
            'sslverify' => false
        ) );

        //$response = wp_remote_post( $request_url, $request_to_send)

        if ( is_wp_error( $response ) ) {
            wc_add_notice(  'Connection error', 'error' );
            return;
        }

        if ( empty( $response['body'] ) ) {
            wc_add_notice(  'Please try again.', 'error' );
            return;
        }

        if ( empty( $response ) ) {
            wc_add_notice(  'Entire response empty.', 'error' );
            return;
        }

        // Get the body response while not get error
        $response_body = json_decode( $response['body'], true );
 
        /* Return the response body
        if ( $response_body['response']['responseCode'] == 'APPROVED' ) {

            $customer_order->payment_complete();
            $customer_order->reduce_order_stock();
 
            // Some notes to customer (replace true with false to make it private)
            $customer_order->add_order_note( 'Payment received! Thanks for your order!', true );
 
            // Empty cart
            $woocommerce->cart->empty_cart();

            // Redirect to the thank you page
            return array(
                'result' => 'success',
                'redirect' => $this->get_return_url( $customer_order )
            );
        } */


        // Return the response body
        if ( $response_body['response']['responseCode'] == 'APPROVED' ) {

            $customer_order->payment_complete();
            $customer_order->reduce_order_stock();

            // Some notes to customer (replace true with false to make it private)
            $customer_order->add_order_note( 'Payment received! Thanks for your order!', true );
            
            // Empty cart
            $woocommerce->cart->empty_cart();
            // Redirect to the thank you page
            return array(
                'result' => 'success',
                'redirect' => $this->get_return_url( $customer_order )
            );
        }

    }

    // Display the payent fields during checkout
    public function payment_fields() {

        echo '<fieldset id="josewallet-' . esc_attr( $this->id ) . '-payment-form" class="wc-payment-form" style="background:transparent;">';

        // Ok lets display some decsciption
        echo wpautop( wp_kses_post( $this->description ) );

?>

        <div style="width: 100%;">
            <label>Your JoseWallet Email <span class="require">*</span></label>
            <input name="jw_payment_email" type="email" id="jw_payment_email" autocomplete="off" style="width: 100%;">
        </div><br>

        <div style="width: 100%;">
            <label>Your JoseWallet Password <span class="require">*</span></label>
            <input name="jw_payment_pass" type="password" id="jw_payment_pass" autocomplete="off" style="width: 100%;">
        </div>

        <input id="jw_payment_token" type="hidden" name="jw_payment_token">
        <div class="clear"></div>

<?php
        
        echo '<div class="clear"></div></fieldset>';

        // End of the payment fields

    }

    // Validate payment fields
    public function validate_fields() {
        // validate the payment fields

        if( empty( $_POST['jw_payment_email'] ) ) {
            wc_add_notice( 'Please enter your JoseWallet account email!', 'error' );
            return false;
        }

        if( empty( $_POST['jw_payment_pass'] ) ) {
            wc_add_notice( 'Please enter your JoseWallet account password!', 'error' );
        }

        return true;

    }

    public function do_ssl_check() {
        if( $this->enabled == "yes" ) {
            if( get_option( 'woocommerce_force_ssl_checkout' ) == "no" ) {
                echo "<div class=\"error\"><p>". sprintf( __( "<strong>%s</strong> is enabled and WooCommerce is not forcing the SSL certificate on your checkout page. Please ensure that you have a valid SSL certificate and that you are <a href=\"%s\">forcing the checkout pages to be secured.</a>" ), $this->method_title, admin_url( 'admin.php?page=wc-settings&tab=checkout' ) ) ."</p></div>";  
            }
        }    
    }

}
1

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.