/** * 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); /** * cURL Error Handling #146 * @since 1.1.8 */ AngellEYE_Gateway_Paypal::angelleye_paypal_for_woocommerce_curl_error_handler($PayPalResult, $methos_name = 'RefundTransaction', $gateway = 'PayPal Website Payments Pro (DoDirectPayment)', $this->error_email_notify); $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('ae_ppddp_refund_error_message', $PayPalResult['L_LONGMESSAGE0'], $PayPalResult['L_ERRORCODE'], $PayPalResult); return new WP_Error('ec_refund-error', $pc_message); } }
/** * 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 = '') { do_action('angelleye_before_fc_refund', $order_id, $amount, $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)); if (!$order || !$order->get_transaction_id() || !$this->paypal_user || !$this->paypal_password || !$this->paypal_vendor) { return false; } /* * Check if the PayPal_PayFlow class has already been established. */ if (!class_exists('Angelleye_PayPal_PayFlow')) { require_once 'lib/angelleye/paypal-php-library/includes/paypal.class.php'; require_once 'lib/angelleye/paypal-php-library/includes/paypal.payflow.class.php'; } /** * Create PayPal_PayFlow object. */ $PayPalConfig = array('Sandbox' => $this->testmode == 'yes' ? true : false, 'APIUsername' => $this->paypal_user, 'APIPassword' => trim($this->paypal_password), 'APIVendor' => $this->paypal_vendor, 'APIPartner' => $this->paypal_partner); $PayPal = new Angelleye_PayPal_PayFlow($PayPalConfig); $PayPalRequestData = array('TENDER' => 'C', 'TRXTYPE' => 'C', 'ORIGID' => $order->get_transaction_id(), 'AMT' => $amount, 'CURRENCY' => $order->get_order_currency()); $this->add_log('Refund Request: ' . print_r($PayPalRequestData, true)); $PayPalResult = $PayPal->ProcessTransaction($PayPalRequestData); /** * cURL Error Handling #146 * @since 1.1.8 */ AngellEYE_Gateway_Paypal::angelleye_paypal_for_woocommerce_curl_error_handler($PayPalResult, $methos_name = 'Refund Request', $gateway = 'PayPal Payments Pro 2.0 (PayFlow)', $this->error_email_notify); $this->add_log('Refund Information: ' . print_r($PayPalResult, true)); add_action('angelleye_after_refund', $PayPalResult, $order, $amount, $reason); if (isset($PayPalResult['RESULT']) && ($PayPalResult['RESULT'] == 0 || $PayPalResult['RESULT'] == 126)) { $order->add_order_note('Refund Transaction ID:' . $PayPalResult['PNREF']); $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 { $fc_refund_error = apply_filters('ae_pppf_refund_error_message', $PayPalResult['RESPMSG'], $PayPalResult); return new WP_Error('paypal-error', $fc_refund_error); } return false; }