diff --git a/assets/css/omise-css.css b/assets/css/omise-css.css index ee7e58b8..6b604002 100644 --- a/assets/css/omise-css.css +++ b/assets/css/omise-css.css @@ -8,4 +8,54 @@ input[name='card_id']:checked ~ #new_card_form { #new_card_info:checked ~ #new_card_form { display: block; +} + +.omise-dashboard-header{ + float:left; +} + +.omise-dashboard-account-mode{ + float:left; + padding: 10px; + border-radius: 5px; + background-color: white; +} + +.omise-dashboard-balance{ + float:left; + margin-left:20px; +} + +.omise-dashboard-balance-label{ + float:left; + margin-left: 5px; + padding: 10px; + font-size:1.3em; +} + +.omise-dashboard-balance-value{ + float:left; + margin-left: 5px; + padding: 10px; + border-radius: 5px; + background-color: white; + font-size:1.3em; +} + +.omise-dashboard-white-box{ + float:left; + margin-left: 5px; + padding: 10px; + border-radius: 5px; + background-color: white; + font-size:1.3em; +} + +.omise-transfer-form{ + margin-left: 5px; + padding: 10px; +} + +.omise-clear-both{ + clear:both; } \ No newline at end of file diff --git a/assets/javascripts/omise-dashboard-handler.js b/assets/javascripts/omise-dashboard-handler.js new file mode 100644 index 00000000..3b54a9f8 --- /dev/null +++ b/assets/javascripts/omise-dashboard-handler.js @@ -0,0 +1,22 @@ +(function ( $, undefined ) { + $("#omise_transfer_full_amount").change(function(){ + if($(this).is(":checked")){ + $("#omise_transfer_specific_amount").hide(); + $("#omise_transfer_amount").removeAttr('required'); + $("#omise_transfer_amount").val(""); + }else{ + $("#omise_transfer_specific_amount").show(); + $("#omise_transfer_amount").attr('required', 'required'); + } + }) + + $("#omise_create_transfer_form").submit(function(e){ + e.preventDefault(); + amount_field = $("#omise_transfer_amount"); + if(amount_field.prop('required') && amount_field.val()==""){ + alert("Please specify transfer amount !"); + }else{ + this.submit(); + } + }); +})(jQuery); \ No newline at end of file diff --git a/includes/templates/omise-wp-admin-page.php b/includes/templates/omise-wp-admin-page.php new file mode 100644 index 00000000..d4077713 --- /dev/null +++ b/includes/templates/omise-wp-admin-page.php @@ -0,0 +1,63 @@ + +
+
+

Omise Merchant Account

+
+
;"> + MODE +
+
+
+ Omise payment gateway plugin is in + mode. Go to WooCommerce settings to change mode. +
+ +
+
+
Total Balance :
+
formatted_total ?>
+
+
+
Available Balance :
+
formatted_available ?>
+
+
+

Request a transfer

+
+
' + method='POST'> + + + +
+
+
------ Or ------
+ Partial amount : THB +
+
+
+
+ + +
+ + +
\ No newline at end of file diff --git a/omise-api-wrapper.php b/omise-api-wrapper.php index 2d8be833..3170248a 100644 --- a/omise-api-wrapper.php +++ b/omise-api-wrapper.php @@ -1,98 +1,111 @@ 'Basic ' . base64_encode( $apiKey.':' ), - 'User-Agent' => 'OmiseWooCommerce/'.OMISE_WOOCOMMERCE_PLUGIN_VERSION.' WooCommerce/'.WC_VERSION.' Wordpress/'.$wp_version - ); + /** + * Creates a customer + * @param string $apiKey + * @param Array $customer_data + * @return mixed + */ + public static function create_customer($apiKey, $customer_data){ + $result = self::call_api($apiKey, "POST", "/customers", $customer_data); + return json_decode($result); + } - $request_info = array( - 'timeout' => 60, - 'method' => $method, - 'headers' => $headers, - 'body' => $data - ); + /** + * Get list of customer's cards + * @param string $apiKey + * @param string $customer_id + * @return mixed + */ + public static function get_customer_cards($apiKey, $customer_id){ + $result = self::call_api($apiKey, "GET", "/customers/{$customer_id}/cards"); + return json_decode($result); + } - $response = wp_remote_request($url, $request_info); + /** + * Creates a card + * @param string $apiKey + * @param string $customer_id + * @param string $token + * @return mixed + */ + public static function create_card($apiKey, $customer_id, $token){ + $result = self::call_api($apiKey, "PATCH", "/customers/{$customer_id}", "card=".$token); + return json_decode($result); + } + + /** + * Deletes customer card + * @param string $apiKey + * @param string $customer_id + * @param string $card_id + * @return mixed + */ + public static function delete_card($apiKey, $customer_id, $card_id){ + $result = self::call_api($apiKey, "DELETE", "/customers/{$customer_id}/cards/{$card_id}"); + return json_decode($result); + } + + public static function get_balance($apiKey){ + $result = self::call_api($apiKey, "GET", "/balance"); + return json_decode($result); + } + + public static function create_transfer($apiKey, $amount=null){ + $post_data = isset($amount) ? "amount=".$amount : null; + $result = self::call_api($apiKey, "POST", "/transfers", $post_data); + return json_decode($result); + } - if(is_wp_error($response)){ - return '{ "object": "error", "message": "'.$response->get_error_message().'" }'; - }else{ - $response_body = wp_remote_retrieve_body($response); - return $response_body; + /** + * Make a request to the API endpoint + * @param string $apiKey + * @param string $method + * @param string $endpoint + * @param mixed $data + * @return string + */ + private static function call_api($apiKey, $method, $endpoint, $data = false){ + global $wp_version; + $url = OMISE_PROTOCOL_PREFIX.OMISE_API_HOST.$endpoint; + + $headers = array( + 'Authorization' => 'Basic ' . base64_encode( $apiKey.':' ), + 'User-Agent' => 'OmiseWooCommerce/'.OMISE_WOOCOMMERCE_PLUGIN_VERSION.' WooCommerce/'.WC_VERSION.' Wordpress/'.$wp_version + ); + + $request_info = array( + 'timeout' => 60, + 'method' => $method, + 'headers' => $headers, + 'body' => $data + ); + + $response = wp_remote_request($url, $request_info); + + if(is_wp_error($response)){ + return '{ "object": "error", "message": "'.$response->get_error_message().'" }'; + }else{ + $response_body = wp_remote_retrieve_body($response); + return $response_body; + } } } } diff --git a/omise-gateway.php b/omise-gateway.php index 15570216..fc3c62fa 100644 --- a/omise-gateway.php +++ b/omise-gateway.php @@ -14,13 +14,15 @@ define("OMISE_PROTOCOL_PREFIX", "https://"); define("OMISE_VAULT_HOST", "vault.omise.co"); define("OMISE_API_HOST", "api.omise.co"); -define("OMISE_WOOCOMMERCE_PLUGIN_VERSION", "1.0"); +define("OMISE_WOOCOMMERCE_PLUGIN_VERSION", "1.0.1"); require_once 'omise-util.php'; require_once 'omise-api-wrapper.php'; require_once 'omise-wc-gateway.php'; require_once 'omise-wc-myaccount.php'; +require_once 'omise-wp-admin.php'; add_action ( 'plugins_loaded', 'register_omise_wc_gateway_plugin', 0 ); add_action ( 'plugins_loaded', 'prepare_omise_myaccount_panel', 0 ); -?> \ No newline at end of file +add_action ( 'plugins_loaded', array ( Omise_Admin::get_instance(), 'register_admin_page_and_actions' ) ); +?> diff --git a/omise-wp-admin.php b/omise-wp-admin.php new file mode 100644 index 00000000..b57af465 --- /dev/null +++ b/omise-wp-admin.php @@ -0,0 +1,168 @@ +test_mode = isset ( $settings ["sandbox"] ) && $settings ["sandbox"] == 'yes'; + $this->private_key = $this->test_mode ? $settings ["test_private_key"] : $settings ["live_private_key"]; + $this->public_key = $this->test_mode ? $settings ["test_public_key"] : $settings ["live_public_key"]; + + if (empty ( $this->private_key ) || empty ( $this->public_key )) { + return; + } + } + + public function no_op(){ + exit("Not permitted"); + } + + public function create_transfer() { + if (! wp_verify_nonce ( $_POST ['omise_create_transfer_nonce'], 'omise_create_transfer' )) + die ( 'Nonce verification failure' ); + + if (! isset ( $_POST ['_wp_http_referer'] )) + die ( 'Missing target' ); + + $transfer_amount = isset ( $_POST ['omise_transfer_amount'] ) ? $_POST ['omise_transfer_amount'] : ''; + $result_message = ''; + try { + if (! empty ( $transfer_amount ) && ! is_numeric ( $transfer_amount )) { + throw new Exception ( "Transfer amount must be a numeric" ); + } + + $transfer = Omise::create_transfer ( $this->private_key, empty ( $transfer_amount ) ? null : $transfer_amount * 100 ); // transfer in satangs + + if ($this->is_transfer_success($transfer)) { + $result_message = "A fund transfer request has been sent."; + } else { + $result_message = $this->get_transfer_error_message($transfer); + } + + } catch ( Exception $e ) { + $result_message = $e->getMessage (); + } + + $url = add_query_arg ( 'omise_result_msg', urlencode ( $result_message ), urldecode ( $_POST ['_wp_http_referer'] ) ); + + wp_safe_redirect ( $url ); + exit (); + } + + private function is_transfer_success($transfer){ + return isset ( $transfer->id ) && isset( $transfer->object ) && $transfer->object == 'transfer' && $transfer->failure_code==null && $transfer->failure_message==null; + } + + private function get_transfer_error_message($transfer){ + $message = ""; + + if(isset($transfer->message) && !empty($transfer->message)){ + $message .= $transfer->message." "; + } + + if(isset($transfer->failure_code) && !empty($transfer->failure_code)){ + $message .= "[".$transfer-> failure_code."] "; + } + + if(isset($transfer->failure_message) && !empty($transfer->failure_message)){ + $message .= $transfer-> failure_message; + } + + return trim($message); + } + + 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 "
Unable to get the balance information. " . esc_html ( $balance->message ) . "
"; + } + } catch ( Exception $e ) { + echo "
" . esc_html ( $e->getMessage () ) . "
"; + } + + } + + function extract_result_message(&$viewData) { + $viewData ["message"] = isset ( $_GET ['omise_result_msg'] ) ? $_GET ['omise_result_msg'] : ''; + } + + function register_dashboard_script() { + + wp_enqueue_script ( 'omise-dashboard-js', plugins_url ( '/assets/javascripts/omise-dashboard-handler.js', __FILE__ ), array ( + 'jquery' + ), OMISE_WOOCOMMERCE_PLUGIN_VERSION, true ); + + wp_enqueue_style ( 'omise-css', plugins_url ( '/assets/css/omise-css.css', __FILE__ ), array (), OMISE_WOOCOMMERCE_PLUGIN_VERSION ); + + } + + } + +} + +?>