Skip to content

Commit

Permalink
Merge pull request #10 from omise/develop
Browse files Browse the repository at this point in the history
Release v1.1.0
  • Loading branch information
fred committed Sep 24, 2015
2 parents 56f9858 + dfbd077 commit e4ddfc6
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 6 deletions.
11 changes: 11 additions & 0 deletions omise-api-wrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@ public static function create_charge($apiKey, $chargeInfo){
$result = self::call_api($apiKey, "POST", "/charges", $chargeInfo);
return json_decode($result);
}

/**
* Retrieve a charge
* @param string $apiKey
* @param Array $charge_id
* @return mixed
*/
public static function get_charge($apiKey, $charge_id){
$result = self::call_api($apiKey, "GET", "/charges/{$charge_id}");
return json_decode($result);
}

/**
* Creates a customer
Expand Down
1 change: 1 addition & 0 deletions omise-gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
require_once 'omise-wc-myaccount.php';
require_once 'omise-wp-admin.php';

add_action ( 'init', 'register_omise_wc_gateway_post_type' );
add_action ( 'plugins_loaded', 'register_omise_wc_gateway_plugin', 0 );
add_action ( 'plugins_loaded', 'prepare_omise_myaccount_panel', 0 );
add_action ( 'plugins_loaded', array ( Omise_Admin::get_instance(), 'register_admin_page_and_actions' ) );
Expand Down
110 changes: 104 additions & 6 deletions omise-wc-gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,16 @@ public function __construct() {
$this->title = $this->settings ['title'];
$this->description = $this->settings ['description'];
$this->sandbox = isset($this->settings ['sandbox']) && $this->settings ['sandbox'] == 'yes';
$this->omise_3ds = isset($this->settings ['omise_3ds']) && $this->settings ['omise_3ds'] == 'yes';
$this->test_private_key = $this->settings ['test_private_key'];
$this->test_public_key = $this->settings ['test_public_key'];
$this->live_private_key = $this->settings ['live_private_key'];
$this->live_public_key = $this->settings ['live_public_key'];
$this->public_key = $this->sandbox ? $this->test_public_key : $this->live_public_key;
$this->private_key = $this->sandbox ? $this->test_private_key : $this->live_private_key;

add_action( 'woocommerce_api_wc_gateway_' . $this->id, array( $this, 'omise_3ds_handler' ) );

add_action ( 'woocommerce_update_options_payment_gateways_' . $this->id, array (
&$this,
'process_admin_options'
Expand All @@ -39,6 +42,7 @@ public function __construct() {
$this,
'omise_scripts'
));

}

/**
Expand All @@ -59,6 +63,12 @@ function init_form_fields() {
'label' => __ ( 'Sandbox mode means everything is in TEST mode', $this->gateway_name ),
'default' => 'yes'
),
'omise_3ds' => array (
'title' => __ ( '3DSecure Support', $this->gateway_name ),
'type' => 'checkbox',
'label' => __ ( 'Enables 3DSecure on this account', $this->gateway_name ),
'default' => 'no'
),
'title' => array (
'title' => __ ( 'Title:', $this->gateway_name ),
'type' => 'text',
Expand Down Expand Up @@ -120,7 +130,7 @@ function payment_fields() {
$viewData ["existingCards"] = $cards;
}
}

Omise_Util::render_view ( 'includes/templates/omise-payment-form.php', $viewData );
}

Expand All @@ -137,7 +147,7 @@ public function process_payment($order_id) {
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;

Expand Down Expand Up @@ -187,7 +197,8 @@ public function process_payment($order_id) {
$data = array (
"amount" => $order->get_total () * 100,
"currency" => $order->get_order_currency (),
"description" => "WooCommerce Order id " . $order_id
"description" => "WooCommerce Order id " . $order_id,
"return_uri" => add_query_arg( 'order_id', $order_id, site_url()."?wc-api=wc_gateway_omise" )
);

if (! empty ( $card_id ) && ! empty ( $omise_customer_id )) {
Expand All @@ -199,10 +210,34 @@ public function process_payment($order_id) {
} 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);

$result = Omise::create_charge($this->private_key, $data);
if ($result->object === "error") {
throw new Exception ($result->message);
} if ($result->failure_code) {
throw new Exception ($result->failure_message);
} else {
$post_id = wp_insert_post(array(
'post_title' => 'Omise Charge Id '.$result->id,
'post_type' => 'omise_charge_items',
'post_status' => 'publish'
));

add_post_meta($post_id, '_omise_charge_id', $result->id);
add_post_meta($post_id, '_wc_order_id', $order_id);
add_post_meta($post_id, '_wc_confirmed_url', $this->get_return_url($order));
}

$success = false;
if ($this->omise_3ds) {
return array (
'result' => 'success',
'redirect' => $result->authorize_uri
);
} else {
$success = $this->is_charge_success($result);
}

if ($success) {
$order->payment_complete ();
$order->add_order_note ( 'Payment with Omise successful' );
Expand Down Expand Up @@ -281,6 +316,59 @@ public function omise_scripts() {
'vault_url' => OMISE_VAULT_HOST
) );
}

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];

$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');

// Remove cart
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();
}
}
}

Expand All @@ -292,4 +380,14 @@ function add_omise_gateway($methods) {
add_filter ( 'woocommerce_payment_gateways', 'add_omise_gateway' );
}

function register_omise_wc_gateway_post_type() {
register_post_type('omise_charge_items', array(
'label' => 'Omise Charge Items',
'labels' => array(
'name' => 'Omise Charge Items',
'singular_name' => 'Omise Charge Item'
),
'supports' => array('title','custom-fields')
));
}
?>

0 comments on commit e4ddfc6

Please sign in to comment.