Plugin Directory

Changeset 2014867


Ignore:
Timestamp:
01/18/2019 02:01:49 PM (7 years ago)
Author:
litego
Message:

Change error processing and fix problem with saving auth_token/refresh_token

Location:
woo-litego/trunk/includes
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • woo-litego/trunk/includes/class-wc-gateway-litego.php

    r2014779 r2014867  
    188188        $this->form_fields = require( dirname( __FILE__ ) . '/admin/litego-settings.php' );
    189189    }
     190
     191    /**
     192     * Reset auth_token/refresh_token if we change smth in the backend settings
     193     *
     194     * @return mixed
     195     */
     196    public function process_admin_options() {
     197        $this->init_settings();
     198
     199        $this->settings['auth_token'] = "";
     200        $this->settings['refresh_token'] = "";
     201
     202        $post_data = $this->get_post_data();
     203
     204        foreach ( $this->get_form_fields() as $key => $field ) {
     205            if ( 'title' !== $this->get_field_type( $field ) ) {
     206                try {
     207                    $this->settings[ $key ] = $this->get_field_value( $key, $field, $post_data );
     208                } catch ( Exception $e ) {
     209                    $this->add_error( $e->getMessage() );
     210                }
     211            }
     212        }
     213
     214        return update_option( $this->get_option_key(), apply_filters( 'woocommerce_settings_api_sanitized_fields_' . $this->id, $this->settings ), 'yes' );
     215    }
    190216
    191217    /**
     
    330356    }
    331357
    332 
    333 
    334358    /**
    335359     * Send a charge creation request to Litego
     
    347371            );
    348372
    349 
    350373        //reauthenticate first
    351         if($result['error'] and $result['error_message']=="Forbidden") {
     374        if($result['error'] and $result['error_name'] == "Forbidden") {
    352375            $auth = $this->litegoApi->reauthenticate($this->refresh_token, $this->merchant_id, $this->secret_key);
    353376            $this->updateTokens($auth);
     
    363386        if($result['error'])
    364387        {
    365             $this->log("Error Response: " . print_r( $result['error_message'], true));
    366             if ($result['error_message'] == "SendCoinsError") {
     388            $this->log("Error Response: " . print_r( $result['error_name'] .": ". $result['error_message'], true));
     389            if ($result['error_name'] == "SendCoinsError") {
    367390                return new WP_Error('litego_error', __('Payment is too large, max payment allowed is 0.04294967 BTC', 'woo-litego'));
    368391            } else {
    369392                return new WP_Error('litego_error', __('There was a problem connecting to the payment gateway.', 'woo-litego'));
    370393            }
    371         }
    372 
    373         if (empty($result['payment_request'])) {
    374             return new WP_Error('litego_error', __('There was a problem connecting to the payment gateway.', 'woo-litego'));
    375         } else {
    376             return $result;
    377         }
     394
     395        }
     396
     397        return $result;
    378398    }
    379399
     
    403423
    404424        //reauthenticate first
    405         if($result['error'] and $result['error_message']=="Forbidden") {
     425        if($result['error'] and $result['error_name'] == "Forbidden") {
    406426            $auth = $this->litegoApi->reauthenticate($this->refresh_token, $this->merchant_id, $this->secret_key);
    407427            $this->updateTokens($auth);
     
    416436        if($result['error'])
    417437        {
    418             $this->log("Error Response: " . print_r( $result['error_message'], true));
     438            $this->log("Error Response: " . print_r( $result['error_name'] .": ". $result['error_message'], true));
    419439            return new WP_Error('litego_error', __('There was a problem connecting to the payment gateway.', 'woo-litego'));
    420 
    421         }
    422 
    423 
    424         if ( empty($result['payment_request']) ) {
    425             return new WP_Error('litego_error', __('There was a problem connecting to the payment gateway.', 'woo-litego'));
    426         } else {
    427             return $result;
    428         }
     440        }
     441
     442        return $result;
    429443    }
    430444   
  • woo-litego/trunk/includes/class-wc-litego-api.php

    r2014779 r2014867  
    2929
    3030    /**
    31      * @var array Map of standard HTTP status code/reason phrases
    32      */
    33     private static $phrases = [
    34         100 => 'Continue',
    35         101 => 'Switching Protocols',
    36         102 => 'Processing',
    37         200 => 'OK',
    38         201 => 'Created',
    39         202 => 'Accepted',
    40         203 => 'Non-Authoritative Information',
    41         204 => 'No Content',
    42         205 => 'Reset Content',
    43         206 => 'Partial Content',
    44         207 => 'Multi-status',
    45         208 => 'Already Reported',
    46         300 => 'Multiple Choices',
    47         301 => 'Moved Permanently',
    48         302 => 'Found',
    49         303 => 'See Other',
    50         304 => 'Not Modified',
    51         305 => 'Use Proxy',
    52         306 => 'Switch Proxy',
    53         307 => 'Temporary Redirect',
    54         400 => 'Bad Request',
    55         401 => 'Unauthorized',
    56         402 => 'Payment Required',
    57         403 => 'Forbidden',
    58         404 => 'Not Found',
    59         405 => 'Method Not Allowed',
    60         406 => 'Not Acceptable',
    61         407 => 'Proxy Authentication Required',
    62         408 => 'Request Time-out',
    63         409 => 'Conflict',
    64         410 => 'Gone',
    65         411 => 'Length Required',
    66         412 => 'Precondition Failed',
    67         413 => 'Request Entity Too Large',
    68         414 => 'Request-URI Too Large',
    69         415 => 'Unsupported Media Type',
    70         416 => 'Requested range not satisfiable',
    71         417 => 'Expectation Failed',
    72         418 => 'I\'m a teapot',
    73         422 => 'Unprocessable Entity',
    74         423 => 'Locked',
    75         424 => 'Failed Dependency',
    76         425 => 'Unordered Collection',
    77         426 => 'Upgrade Required',
    78         428 => 'Precondition Required',
    79         429 => 'Too Many Requests',
    80         431 => 'Request Header Fields Too Large',
    81         451 => 'Unavailable For Legal Reasons',
    82         500 => 'Internal Server Error',
    83         501 => 'Not Implemented',
    84         502 => 'Bad Gateway',
    85         503 => 'Service Unavailable',
    86         504 => 'Gateway Time-out',
    87         505 => 'HTTP Version not supported',
    88         506 => 'Variant Also Negotiates',
    89         507 => 'Insufficient Storage',
    90         508 => 'Loop Detected',
    91         511 => 'Network Authentication Required',
    92     ];
    93 
    94     /**
    9531     * @var string
    9632     */
     
    12258
    12359            if ($result['error']) {
    124                 throw new Exception('Litego API: requestAuthenticate error');
     60                throw new Exception('Litego API: ' . $result['error_message']);
    12561            }
    12662
     
    13571            $result = $this->refreshAuthToken($refreshToken, $timeout);
    13672
    137             if ($result['error'] && $result['error_message'] == "Forbidden") {
     73            if ($result['error'] && $result['error_name'] == "Forbidden") {
    13874                //try to get new auth and refresh token with secret key
    13975                return $this->reauthenticate("", $merchantId, $secretKey, $timeout);
     
    14177
    14278            if ($result['error']) {
    143                 //for display static error page without redirect (look class GeneralException)
    144                 throw new Exception('Litego API: requestRefreshAuthToken error');
     79                throw new Exception('Litego API: ' . $result['error_message']);
    14580            }
    14681
     
    183118                'code' => $result['response_code'] ? $result['response_code'] : self::CODE_400,
    184119                'error' => 1,
    185                 'error_message' => $result['response_error'],
     120                'error_name' => $result['response_result']['name'],
     121                'error_message' => $result['response_result']['detail'],
    186122            );
    187123        }
     
    219155                'code' => $result['response_code']?$result['response_code']:self::CODE_400,
    220156                'error' => 1,
    221                 'error_message' => $result['response_error'],
     157                'error_name' => $result['response_result']['name'],
     158                'error_message' => $result['response_result']['detail'],
    222159            );
    223160        }
     
    271208                'code' => $result['response_code'] ? $result['response_code'] : self::CODE_400,
    272209                'error' => 1,
    273                 'error_message' => $result['response_result']['name'],
     210                'error_name' => $result['response_result']['name'],
     211                'error_message' => $result['response_result']['detail'],
    274212            );
    275213        }
     
    314252                'code' => $result['response_code'] ? $result['response_code'] : self::CODE_400,
    315253                'error' => 1,
    316                 'error_message' => $result['response_error'],
     254                'error_name' => $result['response_result']['name'],
     255                'error_message' => $result['response_result']['detail'],
    317256            );
    318257        }
     
    388327                'response_code' => $responseCode,
    389328                'response_result' => $responseContent,
    390                 'response_error' => $this->getReasonPhrase($responseCode)
    391329            );
    392330
     
    398336        }
    399337    }
    400 
    401     private function getReasonPhrase($httpCode) {
    402         return self::$phrases[$httpCode];
    403     }
    404338}
Note: See TracChangeset for help on using the changeset viewer.