Skip to content

Commit

Permalink
mijireh gateway
Browse files Browse the repository at this point in the history
  • Loading branch information
mikejolley committed Jun 8, 2012
1 parent 0fce6ae commit 0f905e9
Show file tree
Hide file tree
Showing 24 changed files with 1,549 additions and 6 deletions.
2 changes: 1 addition & 1 deletion assets/css/admin.css

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions assets/css/admin.less
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,12 @@
#wc_get_started .main-button { position: absolute; right: 20px; top: 27px; }
#wc_get_started p { margin: 1em 0 .5em; }

#wc_get_started.mijireh { background: #5bc0de url(../images/mijireh-logo.png) no-repeat 15px 18px; border: 1px solid #339bb9; padding: 15px 15px 15px 152px; box-shadow: inset 1px 1px 0 rgba( 255, 255, 255, 0.5 ), inset -1px -1px 0 rgba( 255, 255, 255, 0.5 ); -moz-box-shadow: inset 1px 1px 0 rgba( 255, 255, 255, 0.5 ), inset -1px -1px 0 rgba( 255, 255, 255, 0.5 ); -webkit-box-shadow: inset 1px 1px 0 rgba( 255, 255, 255, 0.5 ), inset -1px -1px 0 rgba( 255, 255, 255, 0.5 ); }
#wc_get_started.mijireh span { color: #fff; text-shadow: 0 1px 0 #4a94ac; }
#wc_get_started.mijireh span a { color: #fff; }
#wc_get_started.mijireh a.button { border-color: #fff; box-shadow: inset 1px 1px 0 rgba( 255, 255, 255, 0.1 ), inset -1px -1px 0 rgba( 255, 255, 255, 0.1 ), 0 1px 3px #206d8b; -moz-box-shadow: inset 1px 1px 0 rgba( 255, 255, 255, 0.1 ), inset -1px -1px 0 rgba( 255, 255, 255, 0.1 ), 0 1px 3px #206d8b; -webkit-box-shadow: inset 1px 1px 0 rgba( 255, 255, 255, 0.1 ), inset -1px -1px 0 rgba( 255, 255, 255, 0.1 ), 0 1px 3px #206d8b; }
#wc_get_started.mijireh a.button-primary { background: #2b7e9f; border-color: #266e8b; }

#icon-woocommerce, .woocommerce_icon, .icon32-posts-product, .icon32-posts-shop_order, .icon32-posts-shop_coupon, .icon32-posts-product_variation {
background-image: url(../images/icons/woocommerce-icons.png) !important;
background-position: -11px -6px;
Expand Down
Binary file added assets/images/mijireh-logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion classes/class-wc-order.php
Original file line number Diff line number Diff line change
Expand Up @@ -758,7 +758,7 @@ function payment_complete() {

$this->update_status( $new_order_status );

add_post_meta( $this->id, '_paid_date', $meta_value, $unique);
add_post_meta( $this->id, '_paid_date', current_time('mysql'), true );

$this_order = array(
'ID' => $this->id,
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
116 changes: 116 additions & 0 deletions classes/gateways/mijireh/Mijireh.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php

$root_dir = dirname(__FILE__) . DIRECTORY_SEPARATOR;

// Require the mijireh library classes
require_once $root_dir . 'Mijireh/Rest.php';
require_once $root_dir . 'Mijireh/RestJSON.php';
require_once $root_dir . 'Mijireh/Model.php';
require_once $root_dir . 'Mijireh/Address.php';
require_once $root_dir . 'Mijireh/Item.php';
require_once $root_dir . 'Mijireh/Order.php';

class Mijireh_Exception extends Exception {}
class Mijireh_ClientError extends Mijireh_Exception {} /* Status: 400-499 */
class Mijireh_BadRequest extends Mijireh_ClientError {} /* Status: 400 */
class Mijireh_Unauthorized extends Mijireh_ClientError {} /* Status: 401 */
class Mijireh_NotFound extends Mijireh_ClientError {} /* Status: 404 */
class Mijireh_ServerError extends Mijireh_Exception {} /* Status: 500-599 */
class Mijireh_InternalError extends Mijireh_ServerError {} /* Status: 500 */

class Mijireh {

/* Live server urls */
public static $base_url = 'https://secure.mijireh.com/';
public static $url = 'https://secure.mijireh.com/api/1/';

public static $access_key;

/**
* Return the job id of the slurp
*/
public static function slurp($url) {
$url_format = '/^(https?):\/\/'. // protocol
'(([a-z0-9$_\.\+!\*\'\(\),;\?&=-]|%[0-9a-f]{2})+'. // username
'(:([a-z0-9$_\.\+!\*\'\(\),;\?&=-]|%[0-9a-f]{2})+)?'. // password
'@)?(?#'. // auth requires @
')((([a-z0-9][a-z0-9-]*[a-z0-9]\.)*'. // domain segments AND
'[a-z][a-z0-9-]*[a-z0-9]'. // top level domain OR
'|((\d|[1-9]\d|1\d{2}|2[0-4][0-9]|25[0-5])\.){3}'.
'(\d|[1-9]\d|1\d{2}|2[0-4][0-9]|25[0-5])'. // IP address
')(:\d+)?'. // port
')(((\/+([a-z0-9$_\.\+!\*\'\(\),;:@&=-]|%[0-9a-f]{2})*)*'. // path
'(\?([a-z0-9$_\.\+!\*\'\(\),;:@&=-]|%[0-9a-f]{2})*)'. // query string
'?)?)?'. // path and query string optional
'(#([a-z0-9$_\.\+!\*\'\(\),;:@&=-]|%[0-9a-f]{2})*)?'. // fragment
'$/i';

if(!preg_match($url_format, $url)) {
throw new Mijireh_NotFound('Unable to slurp invalid URL: $url');
}

try {
$rest = new Mijireh_Rest($url);
$html = $rest->get('');
$data = array(
'url' => $url,
'html' => $html,
);
$rest = new Mijireh_RestJSON(self::$url);
$rest->setupAuth(self::$access_key, '');
$result = $rest->post('slurps', $data);
return $result['job_id'];
}
catch(Mijireh_Rest_Unauthorized $e) {
throw new Mijireh_Unauthorized("Unauthorized. Please check your api access key");
}
catch(Mijireh_Rest_NotFound $e) {
throw new Mijireh_NotFound("Mijireh resource not found: " . $rest->last_request['url']);
}
catch(Mijireh_Rest_ClientError $e) {
throw new Mijireh_ClientError($e->getMessage());
}
catch(Mijireh_Rest_ServerError $e) {
throw new Mijireh_ServerError($e->getMessage());
}
catch(Mijireh_Rest_UnknownResponse $e) {
throw new Mijireh_Exception('Unable to slurp the URL: $url');
}
}

/**
* Return an array of store information
*/
public static function get_store_info() {
$rest = new Mijireh_RestJSON(self::$url);
$rest->setupAuth(self::$access_key, '');
try {
$result = $rest->get('store');
return $result;
}
catch(Mijireh_Rest_BadRequest $e) {
throw new Mijireh_BadRequest($e->getMessage());
}
catch(Mijireh_Rest_Unauthorized $e) {
throw new Mijireh_Unauthorized("Unauthorized. Please check your api access key");
}
catch(Mijireh_Rest_NotFound $e) {
throw new Mijireh_NotFound("Mijireh resource not found: " . $rest->last_request['url']);
}
catch(Mijireh_Rest_ClientError $e) {
throw new Mijireh_ClientError($e->getMessage());
}
catch(Mijireh_Rest_ServerError $e) {
throw new Mijireh_ServerError($e->getMessage());
}
}

public static function preview_checkout_link() {
if(empty(Mijireh::$access_key)) {
throw new Mijireh_Exception('Access key required to view checkout preview');
}

return self::$base_url . 'checkout/' . self::$access_key;
}

}
45 changes: 45 additions & 0 deletions classes/gateways/mijireh/Mijireh/Address.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
class Mijireh_Address extends Mijireh_Model {

public function __construct() {
$this->init();
}

public function init() {
$this->_data = array(
'first_name' => '',
'last_name' => '',
'street' => '',
'city' => '',
'state_province' => '',
'zip_code' => '',
'country' => '',
'company' => '',
'apt_suite' => '',
'phone' => ''
);
}

public function validate() {
$is_valid = $this->_check_required_fields();
return $is_valid;
}

/**
* Return true if all of the required fields have a non-empty value
*
* @return boolean
*/
private function _check_required_fields() {
$pass = true;
$fields = array('street', 'city', 'state_province', 'zip_code', 'country');
foreach($fields as $f) {
if(empty($this->_data[$f])) {
$pass = false;
$this->add_error("$f is required");
}
}
return $pass;
}

}
57 changes: 57 additions & 0 deletions classes/gateways/mijireh/Mijireh/Item.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
class Mijireh_Item extends Mijireh_Model {

private function _init() {
$this->_data = array(
'name' => null,
'price' => null,
'quantity' => 1,
'sku' => null
);
}

private function _check_required_fields() {
if(empty($this->_data['name'])) {
$this->add_error('item name is required.');
}

if(!is_numeric($this->_data['price'])) {
$this->add_error('price must be a number.');
}
}

private function _check_quantity() {
if($this->_data['quantity'] < 1) {
$this->add_error('quantity must be greater than or equal to 1');
}
}

public function __construct() {
$this->_init();
}

public function __get($key) {
$value = false;
if($key == 'total') {
$value = $this->_data['price'] * $this->_data['quantity'];
$value = number_format($value, 2, '.', '');
}
else {
$value = parent::__get($key);
}
return $value;
}

public function get_data() {
$data = parent::get_data();
$data['total'] = $this->total;
return $data;
}

public function validate() {
$this->_check_required_fields();
$this->_check_quantity();
return count($this->_errors) == 0;
}

}
133 changes: 133 additions & 0 deletions classes/gateways/mijireh/Mijireh/Model.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<?php

class Mijireh_Model {

protected $_data = array();
protected $_errors = array();

/**
* Set the value of one of the keys in the private $_data array.
*
* @param string $key The key in the $_data array
* @param string $value The value to assign to the key
* @return boolean
*/
public function __set($key, $value) {
$success = false;
if(array_key_exists($key, $this->_data)) {
$this->_data[$key] = $value;
$success = true;
}
return $success;
}

/**
* Get the value for the key from the private $_data array.
*
* Return false if the requested key does not exist
*
* @param string $key The key from the $_data array
* @return mixed
*/
public function __get($key) {
$value = false;
if(array_key_exists($key, $this->_data)) {
$value = $this->_data[$key];
}

/*
elseif(method_exists($this, $key)) {
$value = call_user_func_array(array($this, $key), func_get_args());
}
*/

return $value;
}

/**
* Return true if the given $key in the private $_data array is set
*
* @param string $key
* @return boolean
*/
public function __isset($key) {
return isset($this->_data[$key]);
}

/**
* Set the value of the $_data array to null for the given key.
*
* @param string $key
* @return void
*/
public function __unset($key) {
if(array_key_exists($key, $this->_data)) {
$this->_data[$key] = null;
}
}

/**
* Return the private $_data array
*
* @return mixed
*/
public function get_data() {
return $this->_data;
}

/**
* Return true if the given $key exists in the private $_data array
*
* @param string $key
* @return boolean
*/
public function field_exists($key) {
return array_key_exists($key, $this->_data);
}

public function copy_from(array $data) {
foreach($data as $key => $value) {
if(array_key_exists($key, $this->_data)) {
$this->_data[$key] = $value;
}
}
}

public function clear() {
foreach($this->_data as $key => $value) {
if($key == 'id') {
$this->_data[$key] = null;
}
else {
$this->_data[$key] = '';
}
}
}

public function add_error($error_message) {
if(!empty($error_message)) {
$this->_errors[] = $error_message;
}
}

public function clear_errors() {
$this->_errors = array();
}

public function get_errors() {
return $this->_errors;
}

public function get_error_lines($glue="\n") {
$error_lines = '';
if(count($this->_errors)) {
$error_lines = implode($glue, $this->_errors);
}
return $error_lines;
}

public function is_valid() {
return count($this->_errors) == 0;
}

}
Loading

0 comments on commit 0f905e9

Please sign in to comment.