Skip to content

Commit

Permalink
Catch exceptions and return fail for process_payment, Handle ajax err…
Browse files Browse the repository at this point in the history
…or for creating the token
  • Loading branch information
tommy-omise committed Jan 20, 2015
1 parent 0aa41e6 commit b04e95e
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 83 deletions.
8 changes: 7 additions & 1 deletion assets/javascripts/omise-myaccount-card-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,13 @@
}, "json"
);
} else {
showError( response.responseJSON.message, $form );
if(response.responseJSON && response.responseJSON.message){
showError( "Unable to create a card. " + response.responseJSON.message, $form );
}else if(response.status==0){
showError( "Unable to create a card. No response from Omise Api.", $form );
}else {
showError( "Unable to create a card [ status=" + response.status + " ].", $form );
}
};
});
}else{
Expand Down
8 changes: 7 additions & 1 deletion assets/javascripts/omise-payment-form-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,13 @@
$( '#omise_card_security_code' ).val("");
$form.submit();
} else {
showError( response.responseJSON.message );
if(response.responseJSON && response.responseJSON.message){
showError( "Unable to process payment with Omise. " + response.responseJSON.message );
}else if(response.status==0){
showError( "Unable to process payment with Omise. No response from Omise Api." );
}else {
showError( "Unable to process payment with Omise [ status=" + response.status + " ]" );
}
$form.unblock();
};
});
Expand Down
163 changes: 82 additions & 81 deletions omise-wc-gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,96 +133,97 @@ public function process_payment($order_id) {
$order = wc_get_order ( $order_id );
$token = isset ( $_POST ['omise_token'] ) ? wc_clean ( $_POST ['omise_token'] ) : '';
$card_id = isset ( $_POST ['card_id'] ) ? wc_clean ( $_POST ['card_id'] ) : '';

if (empty ( $token ) && empty ( $card_id )) {
throw new Exception ( "Please select a card or enter new payment information" );
return;
}

$user = $order->get_user ();
$omise_customer_id = $this->sandbox ? $user->test_omise_customer_id : $user->live_omise_customer_id;

if (isset ( $_POST ['omise_save_customer_card'] ) && empty($card_id)) {
if (empty($token)){
throw new Exception ( "omise_token is required" );
return;
try{
if (empty ( $token ) && empty ( $card_id )) {
throw new Exception ( "Please select a card or enter new payment information." );
}

if (! empty ( $omise_customer_id )) {
// attach a new card to customer
$omise_customer = Omise::create_card ( $this->private_key, $omise_customer_id, $token );

if($omise_customer->object=="error"){
throw new Exception($omise_customer->message);
return;
$user = $order->get_user ();
$omise_customer_id = $this->sandbox ? $user->test_omise_customer_id : $user->live_omise_customer_id;

if (isset ( $_POST ['omise_save_customer_card'] ) && empty($card_id)) {
if (empty($token)){
throw new Exception ( "Omise card token is required." );
}

$card_id = $omise_customer->cards->data [$omise_customer->cards->total - 1]->id;
if (! empty ( $omise_customer_id )) {
// attach a new card to customer
$omise_customer = Omise::create_card ( $this->private_key, $omise_customer_id, $token );

if($omise_customer->object=="error"){
throw new Exception($omise_customer->message);
}

$card_id = $omise_customer->cards->data [$omise_customer->cards->total - 1]->id;
} else {
$description = "WooCommerce customer " . $user->id;
$customer_data = array (
"description" => $description,
"card" => $token
);

$omise_customer = Omise::create_customer ( $this->private_key, $customer_data );

if($omise_customer->object=="error"){
throw new Exception($omise_customer->message);
}

$omise_customer_id = $omise_customer->id;
if($this->sandbox){
update_user_meta ( $user->ID, 'test_omise_customer_id', $omise_customer_id );
}else{
update_user_meta ( $user->ID, 'live_omise_customer_id', $omise_customer_id );
}

if (0 == sizeof ( $omise_customer->cards->data )) {
throw new Exception ( "Something wrong with Omise gateway. No card available for creating a charge." );
}
$card = $omise_customer->cards->data [0]; //use the latest card
$card_id = $card->id;
}
}

$success = false;
$data = array (
"amount" => $order->get_total () * 100,
"currency" => $order->get_order_currency (),
"description" => "WooCommerce Order id " . $order_id
);

if (! empty ( $card_id ) && ! empty ( $omise_customer_id )) {
// create charge with a specific card of customer
$data["customer"] = $omise_customer_id;
$data["card"] = $card_id;
} else if (! empty ( $token )) {
$data["card"] = $token;
} else {
$description = "WooCommerce customer " . $user->id;
$customer_data = array (
"description" => $description,
"card" => $token
throw new Exception ( "Please select a card or enter new payment information." );
}

$result = Omise::create_charge ( $this->private_key, $data );
$success = $this->is_charge_success($result);

if ($success) {
$order->payment_complete ();
$order->add_order_note ( 'Payment with Omise successful' );
// Remove cart
WC()->cart->empty_cart();
return array (
'result' => 'success',
'redirect' => $this->get_return_url ( $order )
);

$omise_customer = Omise::create_customer ( $this->private_key, $customer_data );

if($omise_customer->object=="error"){
throw new Exception($omise_customer->message);
return;
}

$omise_customer_id = $omise_customer->id;
if($this->sandbox){
update_user_meta ( $user->ID, 'test_omise_customer_id', $omise_customer_id );
}else{
update_user_meta ( $user->ID, 'live_omise_customer_id', $omise_customer_id );
}

if (0 == sizeof ( $omise_customer->cards->data )) {
throw new Exception ( "Something wrong with Omise gateway. No card available for creating a charge." );
return;
}
$card = $omise_customer->cards->data [0]; //use the latest card
$card_id = $card->id;
} else {
throw new Exception($this->get_charge_error_message($result));
}
}

$success = false;
$data = array (
"amount" => $order->get_total () * 100,
"currency" => $order->get_order_currency (),
"description" => "WooCommerce Order id " . $order_id
);

if (! empty ( $card_id ) && ! empty ( $omise_customer_id )) {
// create charge with a specific card of customer
$data["customer"] = $omise_customer_id;
$data["card"] = $card_id;
} else if (! empty ( $token )) {
$data["card"] = $token;
} else {
throw new Exception ( "Please select a card or create new card" );
return;
}

$result = Omise::create_charge ( $this->private_key, $data );
$success = $this->is_charge_success($result);

if ($success) {
$order->payment_complete ();
$order->add_order_note ( 'Payment with Omise successful' );
// Remove cart
WC()->cart->empty_cart();
return array (
'result' => 'success',
'redirect' => $this->get_return_url ( $order )
catch( Exception $e ){
$error_message = $e->getMessage();
wc_add_notice( __('Payment error:', 'woothemes') . $error_message , 'error' );
$order->add_order_note ( 'Payment with Omise error : '. $error_message );
return array(
'result' => 'fail',
'redirect' => ''
);
} else {
$error_message = $this->get_charge_error_message($result);
wc_add_notice( __('Payment error:', 'woothemes') . $error_message, 'error' );
$order->add_order_note ( 'Payment with Omise error :'. $error_message );
return;
}
}

Expand Down

0 comments on commit b04e95e

Please sign in to comment.