Merge pull request #7694 from thenbrent/simplify_catch_exception

Catch exceptions in Simplify Commerce
This commit is contained in:
Claudio Sanches 2015-03-11 23:48:52 -03:00
commit 9787478f83
1 changed files with 32 additions and 15 deletions

View File

@ -62,7 +62,7 @@ class WC_Addons_Gateway_Simplify_Commerce extends WC_Gateway_Simplify_Commerce {
* @return bool * @return bool
*/ */
protected function order_contains_subscription( $order_id ) { 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' ) ); return new WP_Error( 'simplify_error', __( 'Customer not found', 'woocommerce' ) );
} }
// Charge the customer try {
$payment = Simplify_Payment::createPayment( array( // Charge the customer
'amount' => $amount * 100, // In cents $payment = Simplify_Payment::createPayment( array(
'customer' => $customer_id, 'amount' => $amount * 100, // In cents
'description' => trim( substr( $subscription_name, 0, 1024 ) ), 'customer' => $customer_id,
'currency' => strtoupper( get_woocommerce_currency() ), 'description' => trim( substr( $subscription_name, 0, 1024 ) ),
'reference' => $order->id, 'currency' => strtoupper( get_woocommerce_currency() ),
'card.addressCity' => $order->billing_city, 'reference' => $order->id,
'card.addressCountry' => $order->billing_country, 'card.addressCity' => $order->billing_city,
'card.addressLine1' => $order->billing_address_1, 'card.addressCountry' => $order->billing_country,
'card.addressLine2' => $order->billing_address_2, 'card.addressLine1' => $order->billing_address_1,
'card.addressState' => $order->billing_state, 'card.addressLine2' => $order->billing_address_2,
'card.addressZip' => $order->billing_postcode '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 ) { if ( 'APPROVED' == $payment->paymentStatus ) {
// Payment complete // Payment complete