Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto cancellation & refund based on transaction Seller Protection response #429

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion classes/wc-gateway-paypal-express-angelleye.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public function __construct() {
$this->payment_action = isset($this->settings['payment_action']) ? $this->settings['payment_action'] : 'Sale';
$this->billing_address = isset($this->settings['billing_address']) ? $this->settings['billing_address'] : 'no';
$this->send_items = isset($this->settings['send_items']) && $this->settings['send_items'] == 'no' ? false : true;
$this->order_cancellations = isset($this->settings['order_cancellations']) ? $this->settings['order_cancellations'] : 'no_unauthorized_payment_protection';
$this->customer_id = get_current_user_id();
$this->enable_notifyurl = isset($this->settings['enable_notifyurl']) && $this->settings['enable_notifyurl'] == 'no' ? false : true;
$this->notifyurl = '';
Expand Down Expand Up @@ -493,6 +494,18 @@ function init_form_fields() {
'description' => __('Your URL for receiving Instant Payment Notification (IPN) about transactions.', 'paypal-for-woocommerce'),
'class' => 'angelleye_notifyurl'
),
'order_cancellations' => array(
'title' => __('Cancel/Refund orders that: ', 'paypal-for-woocommerce'),
'label' => '',
'description' => __('Allows you to cancel and refund orders that do not meet PayPal\'s Seller Protection criteria', 'paypal-for-woocommerce'),
'type' => 'select',
'options' => array(
'no_seller_protection' => __('Do *not* have PayPal Seller Protection', 'paypal-for-woocommerce'),
'no_unauthorized_payment_protection' => __('Do *not* have PayPal Unauthorized Payment Protection', 'paypal-for-woocommerce'),
'disabled' => __('Do not cancel any orders', 'paypal-for-woocommerce'),
),
'default' => 'no_unauthorized_payment_protection'
),

/* 'Locale' => array(
'title' => __( 'Locale', 'paypal-for-woocommerce' ),
Expand Down Expand Up @@ -1186,6 +1199,25 @@ function paypal_express_checkout($posted = null) {


if ($result['ACK'] == 'Success' || $result['ACK'] == 'SuccessWithWarning') {
/**
* Check for Seller Protection Settings
*/
if(AngellEYE_Gateway_Paypal::angelleye_woocommerce_sellerprotection_should_cancel_order($this,$result)) {
$this->add_log('Order '.$order_id.' ('.$result['PAYMENTINFO_0_TRANSACTIONID'].') did not meet our Seller Protection requirements. Cancelling and refunding order.');
$order->add_order_note(__('Transaction did not meet our Seller Protection requirements. Cancelling and refunding order.', 'paypal-for-woocommerce'));
$admin_email = get_option("admin_email");
wp_mail($admin_email, __('PayPal Express Checkout payment declined due to our Seller Protection Settings', 'paypal-for-woocommerce'), __('Order #', 'paypal-for-woocommerce').$order_id);
// Payment was succesfull, add transaction ID to our order so we can refund it
update_post_meta($order_id, '_transaction_id', $result['PAYMENTINFO_0_TRANSACTIONID']);
// Also add the express token
update_post_meta($order_id, '_express_checkout_token', $this->get_session('TOKEN'));
// Process the refund
$this->process_refund($order_id,$order->order_total,__('There was a problem processing your order. Please contact customer support.', 'paypal-for-woocommerce'));
$order->cancel_order();
wc_add_notice(__('Thank you for your recent order. Unfortunately it has been cancelled and refunded. Please contact our customer support team.', 'paypal-for-woocommerce'), 'error');
wp_redirect(get_permalink(wc_get_page_id('cart')));
exit();
}
$this->add_log('Payment confirmed with PayPal successfully');
$result = apply_filters('woocommerce_payment_successful_result', $result, $order_id);

Expand Down Expand Up @@ -2281,4 +2313,4 @@ public function angelleye_check_cart_items() {
exit();
}
}
}
}
49 changes: 49 additions & 0 deletions i18n/languages/paypal-for-woocommerce.pot
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,55 @@ msgstr ""
msgid "Pay with Credit Card"
msgstr ""

#: classes/wc-gateway-paypal-express-angelleye.php:498
msgid "Cancel/Refund orders that: "
msgstr ""

#: classes/wc-gateway-paypal-express-angelleye.php:500
msgid ""
"Allows you to cancel and refund orders that do not meet PayPal's "
"Seller Protection criteria"
msgstr ""

#: classes/wc-gateway-paypal-express-angelleye.php:503
msgid "Do *not* have PayPal Seller Protection"
msgstr ""

#: classes/wc-gateway-paypal-express-angelleye.php:504
msgid "Do *not* have PayPal Unauthorized Payment Protection"
msgstr ""

#: classes/wc-gateway-paypal-express-angelleye.php:505
msgid "Do not cancel any orders"
msgstr ""

#: classes/wc-gateway-paypal-express-angelleye.php:1207
msgid ""
"Transaction did not meet our Seller Protection requirements. Cancelling and "
"refunding order."
""
msgstr ""

#: classes/wc-gateway-paypal-express-angelleye.php:1209
msgid ""
"PayPal Express Checkout payment declined due to our Seller Protection "
"Settings"
msgstr ""

#: classes/wc-gateway-paypal-express-angelleye.php:1209
msgid "Order #"
msgstr ""

#: classes/wc-gateway-paypal-express-angelleye.php:1215
msgid "There was a problem processing your order. Please contact customer support."
msgstr ""

#: classes/wc-gateway-paypal-express-angelleye.php:1217
msgid ""
"Thank you for your recent order. Unfortunately it has been cancelled and refunded. "
"Please contact our customer support team."
msgstr ""

#: classes/wc-gateway-paypal-plus-angelleye.php:26
#: classes/wc-gateway-paypal-plus-angelleye.php:121
#: classes/wc-gateway-paypal-plus-angelleye.php:161
Expand Down
51 changes: 50 additions & 1 deletion paypal-for-woocommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -1192,6 +1192,55 @@ public static function angelleye_paypal_for_woocommerce_curl_error_handler($PayP
}
}

/*
* Check payment gateway settings to cancel order based on transaction's seller protection response
* @param WC_Payment_Gateway $Payment_Gateway
* @param array $PayPalResult
* @return bool
*/
public static function angelleye_woocommerce_sellerprotection_should_cancel_order(&$Payment_Gateway,&$PayPalResult) {
// Following check should not be needed, but in case something goes wrong, we know what happened.
if(in_array('WC_Payment_Gateway',class_parents($Payment_Gateway)) === false) {
error_log('FATAL ERROR! Payment gateway provided to angelleye_woocommerce_sellerprotection_should_cancel_order() is not of WC_Payment_Gateway.');
return false;
}
// TODO: Add $order_cancellations setting to all applicable Angell EYE payment gateways
// If there is no setting available, this will become a NULL, which will default in the following case switch.
// NOTE: All gateways that use this function need to correctly add a note to the order which will explain WHY
// it wias cancelled (i.e. seller protection protection requirements failed)
$order_cancellation_setting = @$Payment_Gateway->order_cancellations;
// TODO: (?) Add some function that will take the returned (and verified) PayPal transaction details and return the applicable
// seller protection value based on the payment gateway/API call. **The following line is only for PayPal Express!**
$txn_protection_eligibility_response = isset($PayPalResult['PAYMENTINFO_0_PROTECTIONELIGIBILITY'])?$PayPalResult['PAYMENTINFO_0_PROTECTIONELIGIBILITY']:'ERROR!';
// TODO: (?) Same goes for the transaction ID. **The following line is only for PayPal Express!**
$txn_id = isset($PayPalResult['PAYMENTINFO_0_TRANSACTIONID'])?$PayPalResult['PAYMENTINFO_0_TRANSACTIONID']:'ERROR!';
switch($order_cancellation_setting) {
// If transaction does not have ANY seller protection
case 'no_seller_protection':
if($txn_protection_eligibility_response != 'Eligible' && $txn_protection_eligibility_response != 'PartiallyEligible') {
$Payment_Gateway->add_log('Transaction '.$txn_id.' is BAD. Setting: no_seller_protection, Response: '.$txn_protection_eligibility_response);
return true;
}
$Payment_Gateway->add_log('Transaction '.$txn_id.' is OK. Setting: no_seller_protection, Response: '.$txn_protection_eligibility_response);
return false;
// If transaction is not protected for unauthorized payments
case 'no_unauthorized_payment_protection':
if($txn_protection_eligibility_response != 'Eligible') {
$Payment_Gateway->add_log('Transaction '.$txn_id.' is BAD. Setting: no_unauthorized_payment_protection, Response: '.$txn_protection_eligibility_response);
return true;
}
$Payment_Gateway->add_log('Transaction '.$txn_id.' is OK. Setting: no_unauthorized_payment_protection, Response: '.$txn_protection_eligibility_response);
return false;
// If we have disabled this check/feature
case 'disabled':
$Payment_Gateway->add_log('Transaction '.$txn_id.' is OK. Setting: disabled, Response: '.$txn_protection_eligibility_response);
return false;
// Catch all other invalid values
default:
$Payment_Gateway->add_log('ERROR! order_cancellations setting for '.$Payment_Gateway->method_title.' is not valid!');
return true;
}
}

/**
* Express Checkout - Adjust button on product details page. #208
Expand Down Expand Up @@ -1478,4 +1527,4 @@ private function set_session($key, $value) {
}
}
}
new AngellEYE_Gateway_Paypal();
new AngellEYE_Gateway_Paypal();