From f193de66922f2547ebd067b85bdb8b086525e278 Mon Sep 17 00:00:00 2001 From: kcppdevelopers Date: Fri, 4 Mar 2016 12:53:24 +0530 Subject: [PATCH] PayPal REST Credit Card Payments, ref #414 --- angelleye-includes/angelleye-utility.php | 70 ++- .../paypal-rest-api-utility.php | 502 ++++++++++++++++++ ...way-paypal-credit-cards-rest-angelleye.php | 209 ++++++++ paypal-for-woocommerce.php | 3 + 4 files changed, 783 insertions(+), 1 deletion(-) create mode 100644 angelleye-includes/paypal-rest-api-utility.php create mode 100644 classes/wc-gateway-paypal-credit-cards-rest-angelleye.php diff --git a/angelleye-includes/angelleye-utility.php b/angelleye-includes/angelleye-utility.php index 096388318..198e9b54b 100644 --- a/angelleye-includes/angelleye-utility.php +++ b/angelleye-includes/angelleye-utility.php @@ -33,7 +33,7 @@ public function add_ec_angelleye_paypal_php_library() { if (!class_exists('Angelleye_PayPal')) { require_once( PAYPAL_FOR_WOOCOMMERCE_PLUGIN_DIR . '/classes/lib/angelleye/paypal-php-library/includes/paypal.class.php' ); } - if ($this->payment_method = 'paypal_express') { + if ($this->payment_method == 'paypal_express') { $gateway_obj = new WC_Gateway_PayPal_Express_AngellEYE(); } else { $gateway_obj = new WC_Gateway_PayPal_Pro_AngellEYE(); @@ -502,5 +502,73 @@ public function angelleye_write_request_response_api_log($PayPalResult) { $this->ec_add_log('Request: ' . print_r($this->paypal->NVPToArray($this->paypal->MaskAPIResult($PayPalRequest)), true)); $this->ec_add_log('Response: ' . print_r($this->paypal->NVPToArray($this->paypal->MaskAPIResult($PayPalResponse)), true)); } + + public static function angelleye_paypal_credit_card_rest_setting_fields() { + return array( + 'enabled' => array( + 'title' => __('Enable/Disable', 'paypal-for-woocommerce'), + 'type' => 'checkbox', + 'label' => __('Enable PayPal Credit Card (REST)', 'paypal-for-woocommerce'), + 'default' => 'no' + ), + 'title' => array( + 'title' => __('Title', 'paypal-for-woocommerce'), + 'type' => 'text', + 'description' => __('This controls the title which the user sees during checkout.', 'paypal-for-woocommerce'), + 'default' => __('PayPal Credit Card (REST)', 'paypal-for-woocommerce') + ), + 'description' => array( + 'title' => __('Description', 'paypal-for-woocommerce'), + 'type' => 'text', + 'description' => __('This controls the description which the user sees during checkout.', 'paypal-for-woocommerce'), + 'default' => __('PayPal Credit Card (REST) description', 'paypal-for-woocommerce') + ), + 'testmode' => array( + 'title' => __('Test Mode', 'paypal-for-woocommerce'), + 'type' => 'checkbox', + 'label' => __('Enable PayPal Sandbox/Test Mode', 'paypal-for-woocommerce'), + 'default' => 'yes', + 'description' => sprintf(__('Place the payment gateway in development mode. Sign up for a developer account here', 'paypal-for-woocommerce'), 'https://developer.paypal.com/'), + ), + 'rest_client_id_sandbox' => array( + 'title' => __('Sandbox Client ID', 'paypal-for-woocommerce'), + 'type' => 'password', + 'description' => 'Enter your Sandbox PayPal Rest API Client ID', + 'default' => '' + ), + 'rest_secret_id_sandbox' => array( + 'title' => __('Sandbox Secret ID', 'paypal-for-woocommerce'), + 'type' => 'password', + 'description' => __('Enter your Sandbox PayPal Rest API Secret ID.', 'paypal-for-woocommerce'), + 'default' => '' + ), + 'rest_client_id' => array( + 'title' => __('Live Client ID', 'paypal-for-woocommerce'), + 'type' => 'password', + 'description' => 'Enter your PayPal Rest API Client ID', + 'default' => '' + ), + 'rest_secret_id' => array( + 'title' => __('Live Secret ID', 'paypal-for-woocommerce'), + 'type' => 'password', + 'description' => __('Enter your PayPal Rest API Secret ID.', 'paypal-for-woocommerce'), + 'default' => '' + ), + 'invoice_prefix' => array( + 'title' => __('Invoice Prefix', 'paypal-for-woocommerce'), + 'type' => 'text', + 'description' => __('Please enter a prefix for your invoice numbers. If you use your PayPal account for multiple stores ensure this prefix is unique as PayPal will not allow orders with the same invoice number.', 'woocommerce'), + 'default' => 'WC-PCCR', + 'desc_tip' => true, + ), + 'debug' => array( + 'title' => __('Debug Log', 'paypal-for-woocommerce'), + 'type' => 'checkbox', + 'label' => __('Enable logging', 'paypal-for-woocommerce'), + 'default' => 'no', + 'description' => __('Log PayPal events, such as Secured Token requests, inside woocommerce/logs/paypal_credit_card_rest.txt', 'paypal-for-woocommerce'), + ) + ); + } } \ No newline at end of file diff --git a/angelleye-includes/paypal-rest-api-utility.php b/angelleye-includes/paypal-rest-api-utility.php new file mode 100644 index 000000000..2b8c368da --- /dev/null +++ b/angelleye-includes/paypal-rest-api-utility.php @@ -0,0 +1,502 @@ +add_paypal_rest_api_lib(); + $this->create_transaction_method_obj(); + $this->payment_method = (isset($_POST['payment_method'])) ? $_POST['payment_method'] : 'paypal_credit_card_rest'; + if ($this->payment_method == 'paypal_credit_card_rest') { + $this->gateway = new WC_Gateway_PayPal_Credit_Card_Rest_AngellEYE(); + } + $this->testmode = 'yes' === $this->gateway->get_option('testmode', 'no'); + $this->mode = $this->testmode == 'yes' ? 'SANDBOX' : 'LIVE'; + $this->debug = 'yes' === $this->gateway->get_option('debug', 'no'); + if ($this->testmode) { + $this->rest_client_id = $this->gateway->get_option('rest_client_id_sandbox', false); + $this->rest_secret_id = $this->gateway->get_option('rest_secret_id_sandbox', false); + } else { + $this->rest_client_id = $this->gateway->get_option('rest_client_id', false); + $this->rest_secret_id = $this->gateway->get_option('rest_secret_id', false); + } + } + + /** + * @since 1.2 + * @global type $woocommerce + * @param type $order + * @param type $card_data + * @return type + */ + public function create_payment($order, $card_data) { + global $woocommerce; + try { + $this->set_trnsaction_obj_value($order, $card_data); + $this->payment->create($this->getAuth()); + if ($this->payment->state == "approved") { + $transactions = $this->payment->getTransactions(); + $relatedResources = $transactions[0]->getRelatedResources(); + $sale = $relatedResources[0]->getSale(); + $saleId = $sale->getId(); + $order->add_order_note(__('PayPal Credit Card (REST) payment completed', 'paypal-for-woocommerce')); + $order->payment_complete($saleId); + WC()->cart->empty_cart(); + $return_url = $order->get_checkout_order_received_url(); + if (is_ajax()) { + wp_send_json(array( + 'result' => 'success', + 'redirect' => apply_filters('woocommerce_checkout_no_payment_needed_redirect', $return_url, $order) + )); + } else { + wp_safe_redirect( + apply_filters('woocommerce_checkout_no_payment_needed_redirect', $return_url, $order) + ); + exit; + } + } else { + wc_add_notice(__('Error Payment state:' . $this->payment->state, 'paypal-for-woocommerce'), 'error'); + $this->add_log(__('Error Payment state:' . $this->payment->state, 'paypal-for-woocommerce')); + return array( + 'result' => 'fail', + 'redirect' => '' + ); + } + } catch (PayPal\Exception\PayPalConnectionException $ex) { + wc_add_notice(__("Error processing checkout. Please try again. ", 'paypal-for-woocommerce'), 'error'); + $this->add_log($ex->getData()); + return array( + 'result' => 'fail', + 'redirect' => '' + ); + exit; + } catch (Exception $ex) { + wc_add_notice(__("Error processing checkout. Please try again. ", 'paypal-for-woocommerce'), 'error'); + $this->add_log($ex->getData()); + return array( + 'result' => 'fail', + 'redirect' => '' + ); + } + } + + /** + * @since 1.2 + * @param type $order + * @param type $card_data + */ + public function set_trnsaction_obj_value($order, $card_data) { + $this->set_card_details($order, $card_data); + $this->fundingInstrument = new FundingInstrument(); + $this->fundingInstrument->setCreditCard($this->card); + $this->payer = new Payer(); + $this->payer->setPaymentMethod("credit_card"); + $this->payer->setFundingInstruments(array($this->fundingInstrument)); + $this->set_item($order); + $this->set_item_list(); + $this->set_detail_values(); + $this->set_amount_values($order); + $this->set_transaction(); + $this->set_payment(); + } + + /** + * @since 1.2 + * @param type $order + */ + public function set_item($order) { + $this->item = new Item(); + $this->payment_data = AngellEYE_Gateway_Paypal::calculate($order, $this->send_items); + foreach ($this->payment_data['order_items'] as $item) { + $this->item->setName($item['name']); + $this->item->setCurrency(get_woocommerce_currency()); + $this->item->setQuantity($item['qty']); + $this->item->setPrice($item['amt']); + array_push($this->order_item, $this->item); + } + } + + /** + * @since 1.2 + */ + public function set_item_list() { + $this->item_list = new ItemList(); + $this->item_list->setItems($this->order_item); + } + + /** + * @since 1.2 + */ + public function set_detail_values() { + $this->details = new Details(); + if (isset($this->payment_data['shippingamt'])) { + $this->details->setShipping($this->payment_data['shippingamt']); + } + if (isset($this->payment_data['taxamt'])) { + $this->details->setTax($this->payment_data['taxamt']); + } + if ($this->payment_data['itemamt']) { + $this->details->setSubtotal($this->payment_data['itemamt']); + } + } + + /** + * @since 1.2 + * @param type $order + */ + public function set_amount_values($order) { + $this->amount = new Amount(); + $this->amount->setCurrency(get_woocommerce_currency()); + $this->amount->setTotal($this->number_format($order->get_total(), $order)); + $this->amount->setDetails($this->details); + } + + /** + * @since 1.2 + */ + public function set_transaction() { + $this->transaction = new Transaction(); + $this->transaction->setAmount($this->amount); + $this->transaction->setItemList($this->item_list); + $this->transaction->setDescription("Payment description"); + $this->transaction->setInvoiceNumber(uniqid()); + } + + /** + * @since 1.2 + */ + public function set_payment() { + $this->payment = new Payment(); + $this->payment->setIntent("sale"); + $this->payment->setPayer($this->payer); + $this->payment->setTransactions(array($this->transaction)); + } + + /** + * @since 1.2 + * @param type $account_number + * @return type + */ + public function card_type_from_account_number($account_number) { + $types = array( + 'visa' => '/^4/', + 'mc' => '/^5[1-5]/', + 'amex' => '/^3[47]/', + 'discover' => '/^(6011|65|64[4-9]|622)/', + 'diners' => '/^(36|38|30[0-5])/', + 'jcb' => '/^35/', + 'maestro' => '/^(5018|5020|5038|6304|6759|676[1-3])/', + 'laser' => '/^(6706|6771|6709)/', + ); + foreach ($types as $type => $pattern) { + if (1 === preg_match($pattern, $account_number)) { + return $type; + } + } + return null; + } + + /** + * @since 1.2 + * @return ApiContext + */ + public function getAuth() { + $auth = new ApiContext(new OAuthTokenCredential($this->rest_client_id, $this->rest_secret_id)); + $auth->setConfig(array('mode' => $this->mode, 'http.headers.PayPal-Partner-Attribution-Id' => 'AngellEYE_SP_WooCommerce')); + return $auth; + } + + /** + * @since 1.2 + * @param type $order + * @param type $card_data + */ + public function set_card_details($order, $card_data) { + $this->set_card_type($card_data); + $this->set_card_number($card_data); + $this->set_card_expire_month($card_data); + $this->set_card_expire_year($card_data); + $this->set_card_cvv($card_data); + $this->set_card_first_name($order); + $this->set_card_set_last_name($order); + } + + /** + * @since 1.2 + * @param type $card_data + */ + public function set_card_type($card_data) { + $first_four = substr($card_data->number, 0, 4); + $card_type = $this->card_type_from_account_number($first_four); + $this->card->setType($card_type); + } + + /** + * @since 1.2 + * @param type $card_data + */ + public function set_card_number($card_data) { + $this->card->setNumber($card_data->number); + } + + /** + * @since 1.2 + * @param type $card_data + */ + public function set_card_expire_month($card_data) { + $this->card->setExpireMonth($card_data->exp_month); + } + + /** + * @since 1.2 + * @param type $card_data + */ + public function set_card_expire_year($card_data) { + $this->card->setExpireYear($card_data->exp_year); + } + + /** + * @since 1.2 + * @param type $card_data + */ + public function set_card_cvv($card_data) { + $this->card->setCvv2($card_data->cvc); + } + + /** + * @since 1.2 + * @param type $order + */ + public function set_card_first_name($order) { + $this->card->setFirstName($order->billing_first_name); + } + + /** + * @since 1.2 + * @param type $order + */ + public function set_card_set_last_name($order) { + $this->card->setLastName($order->billing_last_name); + } + + /** + * @since 1.2 + */ + public function create_transaction_method_obj() { + $this->card = new CreditCard(); + $this->order_item = array(); + $this->send_items = true; + } + + /** + * @since 1.2 + * @return type + */ + public function add_paypal_rest_api_lib() { + if (!class_exists('WC_Payment_Gateway')) { + return; + } + if (!class_exists('Angelleye_PayPal')) { + require_once( PAYPAL_FOR_WOOCOMMERCE_PLUGIN_DIR . '/classes/lib/autoload.php' ); + } + } + + /** + * @since 1.2 + * @param type $message + */ + public function add_log($message) { + if ($this->debug == 'yes') { + if (empty($this->log)) { + $this->log = new WC_Logger(); + } + $this->log->add('paypal_credit_card_rest', $message); + } + } + + /** + * @since 1.2 + * @return type + */ + public function get_posted_card() { + $card_number = isset($_POST['paypal_credit_card_rest-card-number']) ? wc_clean($_POST['paypal_credit_card_rest-card-number']) : ''; + $card_cvc = isset($_POST['paypal_credit_card_rest-card-cvc']) ? wc_clean($_POST['paypal_credit_card_rest-card-cvc']) : ''; + $card_expiry = isset($_POST['paypal_credit_card_rest-card-expiry']) ? wc_clean($_POST['paypal_credit_card_rest-card-expiry']) : ''; + $card_number = str_replace(array(' ', '-'), '', $card_number); + $card_expiry = array_map('trim', explode('/', $card_expiry)); + $card_exp_month = str_pad($card_expiry[0], 2, "0", STR_PAD_LEFT); + $card_exp_year = isset($card_expiry[1]) ? $card_expiry[1] : ''; + if (strlen($card_exp_year) == 2) { + $card_exp_year += 2000; + } + return (object) array( + 'number' => $card_number, + 'type' => '', + 'cvc' => $card_cvc, + 'exp_month' => $card_exp_month, + 'exp_year' => $card_exp_year, + ); + } + + /** + * @since 1.2 + */ + public function add_dependencies_admin_notices() { + $missing_extensions = $this->get_missing_dependencies(); + if (count($missing_extensions) > 0) { + $message = sprintf( + _n( + '%s requires the %s PHP extension to function. Contact your host or server administrator to configure and install the missing extension.', '%s requires the following PHP extensions to function: %s. Contact your host or server administrator to configure and install the missing extensions.', count($missing_extensions), 'paypal-for-woocommerce' + ), "PayPal Credit Card (REST)", '' . implode(', ', $missing_extensions) . '' + ); + echo '

' . $message . '

'; + } + } + + /** + * @since 1.2 + * @return type + */ + public function get_missing_dependencies() { + $missing_extensions = array(); + foreach ($this->get_dependencies() as $ext) { + if (!extension_loaded($ext)) { + $missing_extensions[] = $ext; + } + } + return $missing_extensions; + } + + /** + * @since 1.2 + * @return type + */ + public function get_dependencies() { + return array('curl', 'json', 'openssl'); + } + + /** + * @since 1.2 + * @param type $order_id + * @param type $amount + * @param type $reason + * @return \WP_Error|boolean + */ + public function payment_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)); + if (!$order || !$order->get_transaction_id() || !$this->rest_client_id || !$this->rest_secret_id) { + return false; + } + if ($reason) { + if (255 < strlen($reason)) { + $reason = substr($reason, 0, 252) . '...'; + } + + $reason = html_entity_decode($reason, ENT_NOQUOTES, 'UTF-8'); + } + $sale = Sale::get($order->get_transaction_id(), $this->getAuth()); + $this->amount = new Amount(); + $this->amount->setCurrency(get_woocommerce_currency()); + $this->amount->setTotal($this->number_format($amount, $order)); + $refund = new Refund(); + $refund->setAmount($this->amount); + try { + $this->add_log('Refund Request: ' . print_r($refund, true)); + $refundedSale = $sale->refund($refund, $this->getAuth()); + if ($refundedSale->state == 'completed') { + $order->add_order_note('Refund Transaction ID:' . $refundedSale->getId()); + if (isset($reason) && !empty($reason)) { + $order->add_order_note('Reason for Refund :' . $reason); + } + $max_remaining_refund = wc_format_decimal($order->get_total() - $order->get_total_refunded()); + if (!$max_remaining_refund > 0) { + $order->update_status('refunded'); + } + return true; + } + } catch (PayPal\Exception\PayPalConnectionException $ex) { + $this->add_log($ex->getData()); + $error_data = json_decode($ex->getData()); + if (is_object($error_data) && !empty($error_data)) { + $error_message = ($error_data->message) ? $error_data->message : $error_data->information_link; + return new WP_Error('paypal_credit_card_rest_refund-error', $error_message); + } else { + return new WP_Error('paypal_credit_card_rest_refund-error', $ex->getData()); + } + } catch (Exception $ex) { + $this->add_log($ex->getMessage()); + return new WP_Error('paypal_credit_card_rest_refund-error', $ex->getMessage()); + } + } + + /** + * @since 1.2 + * @param type $currency + * @return boolean + */ + public function currency_has_decimals($currency) { + if (in_array($currency, array('HUF', 'JPY', 'TWD'))) { + return false; + } + return true; + } + + /** + * @since 1.2 + * @param type $price + * @param type $order + * @return type + */ + public function round($price, $order) { + $precision = 2; + if (!$this->currency_has_decimals($order->get_order_currency())) { + $precision = 0; + } + return round($price, $precision); + } + + /** + * @since 1.2 + * @param type $price + * @param type $order + * @return type + */ + public function number_format($price, $order) { + $decimals = 2; + if (!$this->currency_has_decimals($order->get_order_currency())) { + $decimals = 0; + } + return number_format($price, $decimals, '.', ''); + } + +} \ No newline at end of file diff --git a/classes/wc-gateway-paypal-credit-cards-rest-angelleye.php b/classes/wc-gateway-paypal-credit-cards-rest-angelleye.php new file mode 100644 index 000000000..b1f180e72 --- /dev/null +++ b/classes/wc-gateway-paypal-credit-cards-rest-angelleye.php @@ -0,0 +1,209 @@ +id = 'paypal_credit_card_rest'; + $this->icon = apply_filters('woocommerce_braintree_icon', plugins_url('assets/images/cards.png', __DIR__)); + $this->has_fields = true; + $this->method_title = 'PayPal Credit Card (REST)'; + $this->woocommerce_paypal_supported_currencies = array('AUD', 'BRL', 'CAD', 'CZK', 'DKK', 'EUR', 'HKD', 'HUF', 'ILS', 'JPY', 'MYR', 'MXN', 'TWD', 'NZD', 'NOK', 'PHP', 'PLN', 'GBP', 'RUB', 'SGD', 'SEK', 'CHF', 'THB', 'TRY', 'USD'); + $this->method_description = 'The Payments API provides an easy and secure way to accept online and mobile payments. You can take advantage of all the great benefits of PayPal while enabling customers to make payments with only a few clicks.'; + $this->supports = array( + 'products', + 'refunds' + ); + $this->init_form_fields(); + $this->init_settings(); + $this->title = $this->get_option('title'); + $this->description = $this->get_option('description'); + $this->testmode = 'yes' === $this->get_option('testmode', 'no'); + $this->mode = $this->testmode == 'yes' ? "SANDBOX" : "LIVE"; + $this->debug = 'yes' === $this->get_option('debug', 'no'); + if ($this->testmode) { + $this->rest_client_id = $this->get_option('rest_client_id_sandbox', false); + $this->rest_secret_id = $this->get_option('rest_secret_id_sandbox', false); + } else { + $this->rest_client_id = $this->get_option('rest_client_id', false); + $this->rest_secret_id = $this->get_option('rest_secret_id', false); + } + add_action('woocommerce_update_options_payment_gateways', array($this, 'process_admin_options')); + add_action('woocommerce_update_options_payment_gateways_' . $this->id, array($this, 'process_admin_options')); + add_action('admin_notices', array($this, 'checks')); + } + + /** + * @since 1.2 + */ + public function init_form_fields() { + $this->form_fields = AngellEYE_Utility::angelleye_paypal_credit_card_rest_setting_fields(); + } + + /** + * @since 1.2 + */ + public function admin_options() { + if ($this->is_valid_for_use()) { + ?> +

method_title) ) ? $this->method_title : __('Settings', 'paypal-for-woocommerce'); ?>

+ method_description) ) ? wpautop($this->method_description) : ''; ?> + + generate_settings_html(); ?> +
+

:

add_rest_api_utility(); + if ($this->enabled == 'no') { + return; + } + if (version_compare(phpversion(), '5.2.1', '<')) { + echo '

' . sprintf(__('PayPal Credit Card (REST): PayPal Credit Card (REST) requires PHP 5.2.1 and above. You are using version %s.', 'woocommerce'), phpversion()) . '

'; + } + $this->paypal_rest_api->add_dependencies_admin_notices(); + } + + /** + * @since 1.2 + * @return boolean + */ + public function is_available() { + if ($this->enabled === "yes") { + if (!$this->rest_client_id || !$this->rest_secret_id) { + return false; + } + if (!in_array(get_woocommerce_currency(), apply_filters('paypal_rest_api_supported_currencies', $this->woocommerce_paypal_supported_currencies))) { + return false; + } + return true; + } + return false; + } + + /** + * @since 1.2 + */ + public function payment_fields() { + if ($this->description) { + echo wpautop(wptexturize($this->description)); + } + $this->credit_card_form(); + } + + /** + * @since 1.2 + * @return type + */ + public function is_valid_for_use() { + return in_array(get_woocommerce_currency(), apply_filters('paypal_rest_api_supported_currencies', $this->woocommerce_paypal_supported_currencies)); + } + + /** + * @since 1.2 + * @return boolean + * @throws Exception + */ + public function validate_fields() { + $this->add_rest_api_utility(); + try { + $card = $this->paypal_rest_api->get_posted_card(); + if (empty($card->exp_month) || empty($card->exp_year)) { + throw new Exception(__('Card expiration date is invalid', 'woocommerce-gateway-paypal-pro')); + } + if (!ctype_digit($card->cvc)) { + throw new Exception(__('Card security code is invalid (only digits are allowed)', 'woocommerce-gateway-paypal-pro')); + } + if (!ctype_digit($card->exp_month) || !ctype_digit($card->exp_year) || $card->exp_month > 12 || $card->exp_month < 1 || $card->exp_year < date('y')) { + throw new Exception(__('Card expiration date is invalid', 'woocommerce-gateway-paypal-pro')); + } + if (empty($card->number) || !ctype_digit($card->number)) { + throw new Exception(__('Card number is invalid', 'woocommerce-gateway-paypal-pro')); + } + return true; + } catch (Exception $e) { + wc_add_notice($e->getMessage(), 'error'); + return false; + } + } + + /** + * @since 1.2 + * Process the payment + */ + public function process_payment($order_id) { + $this->add_rest_api_utility(); + $order = wc_get_order($order_id); + $card = $this->paypal_rest_api->get_posted_card(); + return $this->do_payment($order, $card); + } + + /** + * @since 1.2 + * @param type $order + * @param type $card + */ + public function do_payment($order, $card) { + $this->add_rest_api_utility(); + $this->paypal_rest_api->create_payment($order, $card); + } + + /** + * @since 1.2 + * @param type $order_id + * @param type $amount + * @param type $reason + * @return type + */ + public function process_refund($order_id, $amount = null, $reason = '') { + $this->add_rest_api_utility(); + $return = $this->paypal_rest_api->payment_refund($order_id, $amount, $reason); + if ($return) { + return $return; + } + } + + /** + * @since 1.2 + */ + public function add_rest_api_utility() { + if (empty($this->paypal_rest_api)) { + if (class_exists('PayPal_Rest_API_Utility')) { + $this->paypal_rest_api = new PayPal_Rest_API_Utility(); + } else { + include_once ( PAYPAL_FOR_WOOCOMMERCE_PLUGIN_DIR . '/angelleye-includes/paypal-rest-api-utility.php' ); + $this->paypal_rest_api = new PayPal_Rest_API_Utility(); + } + } + } + +} \ No newline at end of file diff --git a/paypal-for-woocommerce.php b/paypal-for-woocommerce.php index b9e667a51..4778e6fcc 100644 --- a/paypal-for-woocommerce.php +++ b/paypal-for-woocommerce.php @@ -269,6 +269,7 @@ function init(){ require_once('classes/wc-gateway-braintree-angelleye.php'); require_once('classes/wc-gateway-paypal-express-angelleye.php'); require_once('classes/wc-gateway-paypal-advanced-angelleye.php'); + require_once('classes/wc-gateway-paypal-credit-cards-rest-angelleye.php'); if (version_compare(phpversion(), '5.3.0', '>=')) { require_once('classes/wc-gateway-paypal-plus-angelleye.php'); @@ -413,9 +414,11 @@ function angelleye_add_paypal_pro_gateway( $methods ) { $methods[] = 'WC_Gateway_PayPal_Express_AngellEYE'; $methods[] = 'WC_Gateway_Braintree_AngellEYE'; $methods[] = 'WC_Gateway_PayPal_Advanced_AngellEYE'; + $methods[] = 'WC_Gateway_PayPal_Credit_Card_Rest_AngellEYE'; if (version_compare(phpversion(), '5.3.0', '>=')) { $methods[] = 'WC_Gateway_PayPal_Plus_AngellEYE'; } + return $methods; }