/** * Process a refund if supported * @param int $order_id * @param float $amount * @param string $reason * @return bool|wp_error 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); $this->add_log('Begin Refund'); $this->add_log('Order: ' . print_r($order, true)); $this->add_log('Transaction ID: ' . print_r($order->get_transaction_id(), true)); $this->add_log('API Username: '******'API Password: '******'API Signature: ' . print_r($this->api_signature, true)); if (!$order || !$order->get_transaction_id() || !$this->api_username || !$this->api_password || !$this->api_signature) { return false; } $this->add_log('Include Class Request'); /* * Check if the PayPal class has already been established. */ if (!class_exists('Angelleye_PayPal')) { require_once 'lib/angelleye/paypal-php-library/includes/paypal.class.php'; } /* * Create PayPal object. */ $PayPalConfig = array('Sandbox' => $this->testmode == 'yes' ? TRUE : FALSE, 'APIUsername' => $this->api_username, 'APIPassword' => $this->api_password, 'APISignature' => $this->api_signature); $PayPal = new Angelleye_PayPal($PayPalConfig); if ($reason) { if (255 < strlen($reason)) { $reason = substr($reason, 0, 252) . '...'; } $reason = html_entity_decode($reason, ENT_NOQUOTES, 'UTF-8'); } // Prepare request arrays $RTFields = array('transactionid' => $order->get_transaction_id(), 'payerid' => '', 'invoiceid' => '', 'refundtype' => $order->get_total() == $amount ? 'Full' : 'Partial', 'amt' => number_format($amount, 2, '.', ''), 'currencycode' => $order->get_order_currency(), 'note' => $reason, 'retryuntil' => '', 'refundsource' => '', 'merchantstoredetail' => '', 'refundadvice' => '', 'refunditemdetails' => '', 'msgsubid' => '', 'storeid' => '', 'terminalid' => ''); $PayPalRequestData = array('RTFields' => $RTFields); $this->add_log('Refund Request: ' . print_r($PayPalRequestData, true)); // Pass data into class for processing with PayPal and load the response array into $PayPalResult $PayPalResult = $PayPal->RefundTransaction($PayPalRequestData); $this->add_log('Refund Information: ' . print_r($PayPalResult, true)); if ($PayPal->APICallSuccessful($PayPalResult['ACK'])) { $order->add_order_note('Refund Transaction ID:' . $PayPalResult['REFUNDTRANSACTIONID']); $max_remaining_refund = wc_format_decimal($order->get_total() - $order->get_total_refunded()); if (!$max_remaining_refund > 0) { $order->update_status('refunded'); } if (ob_get_length()) { ob_end_clean(); } return true; } else { $pc_message = apply_filters('angelleye_pc_refund_message', $PayPalResult['L_LONGMESSAGE0'], $PayPalResult['L_ERRORCODE'], $PayPalResult); return new WP_Error('ec_refund-error', $pc_message); } }