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..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 ) ); } /** @@ -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