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

PartialSuccess Error Handling, PFW-636 #1458

Merged
merged 8 commits into from
Jun 12, 2020
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,23 @@ public function angelleye_do_express_checkout_payment() {
wc_clear_notices();
$this->angelleye_wp_safe_redirect(add_query_arg('utm_nooverride', '1', $this->gateway->get_return_url($order)), 'do_express_checkout_payment');
exit();
} elseif ($this->response_helper->ec_is_response_partialsuccess($this->paypal_response)) {
do_action('angelleye_express_checkout_order_data', $this->paypal_response, $order_id);
apply_filters('woocommerce_payment_successful_result', array('result' => 'success'), $order_id);
do_action('woocommerce_before_pay_action', $order);
update_post_meta($order_id, 'is_sandbox', $this->testmode);
$order->update_status('wc-partial-payment');
if ($old_wc) {
if (!get_post_meta($orderid, '_order_stock_reduced', true)) {
$order->reduce_order_stock();
}
} else {
wc_maybe_reduce_stock_levels($orderid);
}
WC()->cart->empty_cart();
wc_clear_notices();
$this->angelleye_wp_safe_redirect(add_query_arg('utm_nooverride', '1', $this->gateway->get_return_url($order)), 'do_express_checkout_payment');
exit();
} else {
$this->angelleye_add_order_note_with_error($order, $paypal_action_name = 'DoExpressCheckoutPayment');
$this->angelleye_write_error_log_and_send_email_notification($paypal_action_name = 'DoExpressCheckoutPayment');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,10 @@ public function ec_is_response_successwithwarning($paypal_response) {
return true;
}
}

public function ec_is_response_partialsuccess($paypal_response) {
if (!empty($paypal_response['ACK']) && strtoupper($paypal_response['ACK']) == 'PARTIALSUCCESS') {
return true;
}
}
}
99 changes: 99 additions & 0 deletions classes/wc-email-customer-partial-paid-order.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

if (!defined('ABSPATH')) {
exit; // Exit if accessed directly.
}

if (!class_exists('WC_Email_Partially_Paid_Order', false)) :

class WC_Email_Partially_Paid_Order extends WC_Email {

public function __construct() {
$this->id = 'customer_partially_paid_order';
$this->customer_email = true;

$this->title = __('Partially Paid order', 'paypal-for-woocommerce');
$this->description = __('This is an order notification sent to customers containing order details after partially paid.', 'paypal-for-woocommerce');
$this->template_html = 'angelleye-customer-partial-paid-order.php';
$this->template_plain = 'plain/angelleye-customer-partial-paid-order.php';
$this->placeholders = array(
'{order_date}' => '',
'{order_number}' => '',
);

// Triggers for this email.
add_action('woocommerce_order_status_cancelled_to_partial-payment_notification', array($this, 'trigger'), 10, 2);
add_action('woocommerce_order_status_failed_to_partial-payment_notification', array($this, 'trigger'), 10, 2);
add_action('woocommerce_order_status_on-hold_to_partial-payment_notification', array($this, 'trigger'), 10, 2);
add_action('woocommerce_order_status_pending_to_partial-payment_notification', array($this, 'trigger'), 10, 2);

// Call parent constructor.
parent::__construct();

$this->template_base = PAYPAL_FOR_WOOCOMMERCE_DIR_PATH . '/template/emails';
}

public function get_default_subject() {
return __('Your {site_title} order has been received!', 'paypal-for-woocommerce');
}

public function get_default_heading() {
return __('Thank you for your order', 'paypal-for-woocommerce');
}

public function trigger($order_id, $order = false) {
$this->setup_locale();

if ($order_id && !is_a($order, 'WC_Order')) {
$order = wc_get_order($order_id);
}

if (is_a($order, 'WC_Order')) {
$this->object = $order;
$this->recipient = $this->object->get_billing_email();
$this->placeholders['{order_date}'] = wc_format_datetime($this->object->get_date_created());
$this->placeholders['{order_number}'] = $this->object->get_order_number();
}

if ($this->is_enabled() && $this->get_recipient()) {
$this->send($this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments());
}

$this->restore_locale();
}

public function get_content_html() {
return wc_get_template_html(
$this->template_html, array(
'order' => $this->object,
'email_heading' => $this->get_heading(),
'additional_content' => $this->get_additional_content(),
'sent_to_admin' => false,
'plain_text' => false,
'email' => $this,
)
);
}

public function get_content_plain() {
return wc_get_template_html(
$this->template_plain, array(
'order' => $this->object,
'email_heading' => $this->get_heading(),
'additional_content' => $this->get_additional_content(),
'sent_to_admin' => false,
'plain_text' => true,
'email' => $this,
)
);
}

public function get_default_additional_content() {
return __('Thanks for using {site_address}!', 'paypal-for-woocommerce');
}

}

endif;

return new WC_Email_Partially_Paid_Order();
41 changes: 41 additions & 0 deletions paypal-for-woocommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,11 @@ public function __construct()
add_action( 'wp', array( __CLASS__, 'angelleye_delete_payment_method_action' ), 10 );
add_filter( 'woocommerce_saved_payment_methods_list', array($this, 'angelleye_synce_braintree_save_payment_methods'), 5, 2 );
add_filter( 'woocommerce_thankyou_order_received_text', array($this, 'order_received_text'), 10, 2);
add_filter( 'wc_order_statuses', array($this, 'angelleye_wc_order_statuses'), 10, 1);
add_action( 'init', array( $this, 'angelleye_register_post_status' ), 99 );
add_filter( 'woocommerce_email_classes', array($this, 'angelleye_woocommerce_email_classes'), 10, 1);
add_filter( 'wc_get_template', array($this, 'own_angelleye_wc_get_template'), 10, 5);
add_filter( 'woocommerce_email_actions', array($this, 'own_angelleye_woocommerce_email_actions'), 10);
$this->customer_id;
}

Expand Down Expand Up @@ -1359,6 +1364,42 @@ public function order_received_text($text, $order) {
}
return $text;
}

public function angelleye_wc_order_statuses($order_statuses) {
$order_statuses['wc-partial-payment'] = _x( 'Partially Paid', 'Order status', 'paypal-for-woocommerce' );
return $order_statuses;
}

public function angelleye_register_post_status() {
register_post_status( 'wc-partial-payment', array(
'label' => _x( 'Partially Paid', 'Order status', 'paypal-for-woocommerce' ),
'public' => false,
'exclude_from_search' => false,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
'label_count' => _n_noop( 'Partially Paid <span class="count">(%s)</span>', 'Partially Paid <span class="count">(%s)</span>', 'paypal-for-woocommerce' ),
) );
}

public function angelleye_woocommerce_email_classes($emails) {
$emails['WC_Email_Partially_Paid_Order'] = include PAYPAL_FOR_WOOCOMMERCE_DIR_PATH . '/classes/wc-email-customer-partial-paid-order.php';
return $emails;
}

public function own_angelleye_wc_get_template($template, $template_name, $args, $template_path, $default_path) {
if(!empty($template) && ($template_name === 'angelleye-customer-partial-paid-order.php' || $template_name === 'plain/angelleye-customer-partial-paid-order.php')) {
$template = PAYPAL_FOR_WOOCOMMERCE_DIR_PATH . '/template/emails/' . $template_name;
}
return $template;
}

public function own_angelleye_woocommerce_email_actions($actions) {
$actions[] = 'woocommerce_order_status_cancelled_to_partial-payment';
$actions[] = 'woocommerce_order_status_failed_to_partial-payment';
$actions[] = 'woocommerce_order_status_on-hold_to_partial-payment';
$actions[] = 'woocommerce_order_status_pending_to_partial-payment';
return $actions;
}
}
}
new AngellEYE_Gateway_Paypal();
26 changes: 26 additions & 0 deletions template/emails/angelleye-customer-partial-paid-order.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
if (!defined('ABSPATH')) {
exit;
}


do_action('woocommerce_email_header', $email_heading, $email);
?>

<?php ?>
<p><?php printf(esc_html__('Hi %s,', 'woocommerce'), esc_html($order->get_billing_first_name())); ?></p>
<?php ?>
<p><?php printf(esc_html__('Just to let you know &mdash; we\'ve received your order #%s, and it is now being processed:', 'woocommerce'), esc_html($order->get_order_number())); ?></p>

<?php
do_action('woocommerce_email_order_details', $order, $sent_to_admin, $plain_text, $email);

do_action('woocommerce_email_order_meta', $order, $sent_to_admin, $plain_text, $email);

do_action('woocommerce_email_customer_details', $order, $sent_to_admin, $plain_text, $email);

if ($additional_content) {
echo wp_kses_post(wpautop(wptexturize($additional_content)));
}

do_action('woocommerce_email_footer', $email);
29 changes: 29 additions & 0 deletions template/emails/plain/angelleye-customer-partial-paid-order.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

defined('ABSPATH') || exit;

echo "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n";
echo esc_html(wp_strip_all_tags($email_heading));
echo "\n=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n\n";


echo sprintf(esc_html__('Hi %s,', 'woocommerce'), esc_html($order->get_billing_first_name())) . "\n\n";

echo sprintf(esc_html__('Just to let you know &mdash; we\'ve received your order #%s, and it is now being processed:', 'woocommerce'), esc_html($order->get_order_number())) . "\n\n";

do_action('woocommerce_email_order_details', $order, $sent_to_admin, $plain_text, $email);

echo "\n----------------------------------------\n\n";

do_action('woocommerce_email_order_meta', $order, $sent_to_admin, $plain_text, $email);

do_action('woocommerce_email_customer_details', $order, $sent_to_admin, $plain_text, $email);

echo "\n\n----------------------------------------\n\n";

if ($additional_content) {
echo esc_html(wp_strip_all_tags(wptexturize($additional_content)));
echo "\n\n----------------------------------------\n\n";
}

echo wp_kses_post(apply_filters('woocommerce_email_footer_text', get_option('woocommerce_email_footer_text')));