WC_Gateway_Paypal::process_refund PHP Method

process_refund() public method

Process a refund if supported.
public process_refund ( integer $order_id, float $amount = null, string $reason = '' ) : boolean
$order_id integer
$amount float
$reason string
return boolean True or false based on success, or a WP_Error object
    public function process_refund($order_id, $amount = null, $reason = '')
    {
        $order = wc_get_order($order_id);
        if (!$this->can_refund_order($order)) {
            $this->log('Refund Failed: No transaction ID');
            return new WP_Error('error', __('Refund failed: No transaction ID', 'woocommerce'));
        }
        $this->init_api();
        $result = WC_Gateway_Paypal_API_Handler::refund_transaction($order, $amount, $reason);
        if (is_wp_error($result)) {
            $this->log('Refund Failed: ' . $result->get_error_message());
            return new WP_Error('error', $result->get_error_message());
        }
        $this->log('Refund Result: ' . print_r($result, true));
        switch (strtolower($result->ACK)) {
            case 'success':
            case 'successwithwarning':
                $order->add_order_note(sprintf(__('Refunded %1$s - Refund ID: %2$s', 'woocommerce'), $result->GROSSREFUNDAMT, $result->REFUNDTRANSACTIONID));
                return true;
                break;
        }
        return isset($result->L_LONGMESSAGE0) ? new WP_Error('error', $result->L_LONGMESSAGE0) : false;
    }