/**
  * Public omise_create_card ajax hook
  */
 public function omise_create_card()
 {
     $token = isset($_POST["omise_token"]) ? wc_clean($_POST["omise_token"]) : "";
     if (empty($token)) {
         Omise_Util::render_json_error("omise_token is required");
         die;
     }
     if (!wp_verify_nonce($_POST["omise_nonce"], "omise_add_card")) {
         Omise_Util::render_json_error("Nonce verification failure");
         die;
     }
     $card = Omise::create_card($this->private_key, $this->omise_customer_id, $token);
     echo json_encode($card);
     die;
 }
 function test_call_api_should_call_wp_remote_request_with_desired_params_and_return_error_if_not_successful()
 {
     add_filter("pre_http_request", array($this, "get_error_http_response"), 10, 3);
     $this->assertNull($this->http_request);
     $this->assertNull($this->url);
     $expected = '{ "object": "error", "message": "User has blocked requests through HTTP." }';
     $omise = new Omise();
     $response = $omise->call_api("private_key", "GET", "/customers/customer_id/cards");
     $this->assertEquals($expected, $response);
     global $wp_version;
     $expected = "GET";
     $actual = $this->http_request["method"];
     $this->assertEquals($expected, $actual);
     $expected = 60;
     $actual = $this->http_request["timeout"];
     $this->assertEquals($expected, $actual);
     $expected = "Basic cHJpdmF0ZV9rZXk6";
     $actual = $this->http_request["headers"]["Authorization"];
     $this->assertEquals($expected, $actual);
     $expected = "2014-07-27";
     $actual = $this->http_request["headers"]["Omise-Version"];
     $this->assertEquals($expected, $actual);
     $expected = "OmiseWooCommerce/" . OMISE_WOOCOMMERCE_PLUGIN_VERSION . " WooCommerce/" . WC_VERSION . " Wordpress/" . $wp_version;
     $actual = $this->http_request["headers"]["User-Agent"];
     $this->assertEquals($expected, $actual);
     $expected = "https://api.omise.co/customers/customer_id/cards";
     $this->assertEquals($expected, $this->url);
     remove_filter("pre_http_request", array($this, "get_error_http_response"), 10, 3);
 }
 /**
  * Process payment
  * 
  * @see WC_Payment_Gateway::process_payment()
  */
 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']) : '';
     try {
         if (empty($token) && empty($card_id)) {
             throw new Exception("Please select a card or enter new payment information.");
         }
         $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.");
             }
             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 {
                 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));
         } else {
             throw new Exception($this->get_charge_error_message($result));
         }
     } 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' => '');
     }
 }
 public function init_dashboard()
 {
     try {
         $balance = Omise::get_balance($this->private_key);
         if ($balance->object == 'balance') {
             $balance->formatted_total = wc_price($balance->total / 100);
             $balance->formatted_available = wc_price($balance->available / 100);
             $viewData['balance'] = $balance;
             $this->extract_result_message($viewData);
             $viewData["current_account_mode"] = $this->test_mode ? "TEST" : "LIVE";
             Omise_Util::render_view('includes/templates/omise-wp-admin-page.php', $viewData);
             $this->register_dashboard_script();
         } else {
             echo "<div class='wrap'><div class='error'>Unable to get the balance information. Please verify that your private key is valid. [" . esc_html($balance->message) . "]</div></div>";
         }
     } catch (Exception $e) {
         echo "<div class='wrap'><div class='error'>" . esc_html($e->getMessage()) . "</div></div>";
     }
 }
 public function omise_3ds_handler()
 {
     if (!$_GET["order_id"]) {
         wp_die("Order was not found", "Omise Payment Gateway: Checkout", array("response" => 500));
     }
     $order_id = $_GET["order_id"];
     $posts = get_posts(array("post_type" => "omise_charge_items", "meta_query" => array(array("key" => "_wc_order_id", "value" => $order_id, "compare" => "="))));
     if (empty($posts)) {
         wp_die("Charge was not found", "Omise Payment Gateway: Checkout", array("response" => 500));
     }
     $order = wc_get_order($order_id);
     if (!$order) {
         wp_die("Order was not found", "Omise Payment Gateway: Checkout", array("response" => 500));
     }
     $confirmed_url = get_post_custom_values("_wc_confirmed_url", $posts[0]->ID);
     $confirmed_url = $confirmed_url[0];
     $charge_id = get_post_custom_values("_omise_charge_id", $posts[0]->ID);
     $charge_id = $charge_id[0];
     $omise = new Omise();
     $result = $omise->get_charge($this->private_key, $charge_id);
     if ($this->is_charge_success($result)) {
         $order->payment_complete();
         $order->add_order_note("Payment with Omise successful");
         WC()->cart->empty_cart();
         header("Location: " . $confirmed_url);
         die;
     } else {
         if ($result->failure_code && $result->failure_message) {
             $order->add_order_note("Charge was not completed, " . $result->failure_message);
             wp_die($result->failure_message, "Charge was not completed", array("response" => 500));
         } else {
             wp_die("Charge still in progress", "Charge still in progress", array("response" => 500));
         }
     }
     wp_die("Access denied", "Access Denied", array("response" => 401));
     die;
 }