From 6d339241a2700f9719cc409cf230254791d374d8 Mon Sep 17 00:00:00 2001 From: Brent Shepherd Date: Wed, 11 Mar 2015 14:49:57 -0700 Subject: [PATCH 1/2] Catch exception on renewal payment with Simplify --- ...ss-wc-addons-gateway-simplify-commerce.php | 45 +++++++++++++------ 1 file changed, 31 insertions(+), 14 deletions(-) diff --git a/includes/gateways/simplify-commerce/class-wc-addons-gateway-simplify-commerce.php b/includes/gateways/simplify-commerce/class-wc-addons-gateway-simplify-commerce.php index 279f20b7b63..03f30b886d5 100644 --- a/includes/gateways/simplify-commerce/class-wc-addons-gateway-simplify-commerce.php +++ b/includes/gateways/simplify-commerce/class-wc-addons-gateway-simplify-commerce.php @@ -280,20 +280,37 @@ class WC_Addons_Gateway_Simplify_Commerce extends WC_Gateway_Simplify_Commerce { return new WP_Error( 'simplify_error', __( 'Customer not found', 'woocommerce' ) ); } - // Charge the customer - $payment = Simplify_Payment::createPayment( array( - 'amount' => $amount * 100, // In cents - 'customer' => $customer_id, - 'description' => trim( substr( $subscription_name, 0, 1024 ) ), - 'currency' => strtoupper( get_woocommerce_currency() ), - 'reference' => $order->id, - 'card.addressCity' => $order->billing_city, - 'card.addressCountry' => $order->billing_country, - 'card.addressLine1' => $order->billing_address_1, - 'card.addressLine2' => $order->billing_address_2, - 'card.addressState' => $order->billing_state, - 'card.addressZip' => $order->billing_postcode - ) ); + try { + // Charge the customer + $payment = Simplify_Payment::createPayment( array( + 'amount' => $amount * 100, // In cents + 'customer' => $customer_id, + 'description' => trim( substr( $subscription_name, 0, 1024 ) ), + 'currency' => strtoupper( get_woocommerce_currency() ), + 'reference' => $order->id, + 'card.addressCity' => $order->billing_city, + 'card.addressCountry' => $order->billing_country, + 'card.addressLine1' => $order->billing_address_1, + 'card.addressLine2' => $order->billing_address_2, + 'card.addressState' => $order->billing_state, + 'card.addressZip' => $order->billing_postcode + ) ); + + } catch ( Exception $e ) { + + $error_message = $e->getMessage(); + + if ( $e instanceof Simplify_BadRequestException && $e->hasFieldErrors() && $e->getFieldErrors() ) { + $error_message = ''; + foreach ( $e->getFieldErrors() as $error ) { + $error_message .= ' ' . $error->getFieldName() . ': "' . $error->getMessage() . '" (' . $error->getErrorCode() . ')'; + } + } + + $order->add_order_note( sprintf( __( 'Simplify payment error: %s', 'woocommerce' ), $error_message ) ); + + return new WP_Error( 'simplify_payment_declined', $e->getMessage(), array( 'status' => $e->getCode() ) ); + } if ( 'APPROVED' == $payment->paymentStatus ) { // Payment complete From 028cea41d3e4d9da33072d5c0dac19890f4c24e7 Mon Sep 17 00:00:00 2001 From: Brent Shepherd Date: Wed, 11 Mar 2015 14:50:25 -0700 Subject: [PATCH 2/2] Make sure Simplify customer ID is set on renewals So that if the renewal is for a failed payment, the the new value is set on the renewal order's `'_simplify_customer_id'` post meta value and can be copied back to the original order in `update_failing_payment_method()`. --- .../class-wc-addons-gateway-simplify-commerce.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/gateways/simplify-commerce/class-wc-addons-gateway-simplify-commerce.php b/includes/gateways/simplify-commerce/class-wc-addons-gateway-simplify-commerce.php index 03f30b886d5..e9a3ca6633a 100644 --- a/includes/gateways/simplify-commerce/class-wc-addons-gateway-simplify-commerce.php +++ b/includes/gateways/simplify-commerce/class-wc-addons-gateway-simplify-commerce.php @@ -62,7 +62,7 @@ class WC_Addons_Gateway_Simplify_Commerce extends WC_Gateway_Simplify_Commerce { * @return bool */ protected function order_contains_subscription( $order_id ) { - return class_exists( 'WC_Subscriptions_Order' ) && WC_Subscriptions_Order::order_contains_subscription( $order_id ); + return class_exists( 'WC_Subscriptions_Order' ) && ( WC_Subscriptions_Order::order_contains_subscription( $order_id ) || WC_Subscriptions_Renewal_Order::is_renewal( $order_id ) ); } /**