Skip to content

Commit

Permalink
Merge pull request #8 from eddPragmatic/master
Browse files Browse the repository at this point in the history
Update to Version 1.1
  • Loading branch information
guacamoli committed Oct 24, 2018
2 parents 62a9fb3 + d9becbb commit 82d0d8b
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 11 deletions.
26 changes: 25 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,20 @@ A WooCommerce payment gateway that allows your customers to pay with cryptocurre

## Installation

### From WordPress.org

This plugin is available on the [WordPress.org plugin repository], and can be installed either directly from there or from the admin dashboard within your website.

#### Within your WordPress dashboard
1. Visit ‘Plugins > Add New’
2. Search for ‘Coinbase Commerce’
3. Activate Coinbase Commerce from your Plugins page.

#### From WordPress.org plugin repository
1. Download Coinbase Commerce from <https://wordpress.org/plugins/coinbase-commerce/>
2. Upload to your ‘/wp-content/plugins/’ directory, using your favorite method (ftp, sftp, scp, etc…)
3. Activate Coinbase Commerce from your Plugins page.

### From this repository

Within the Github repository, click the Clone or Download button and Download a zip file of the repository, or clone it directly via command line.
Expand Down Expand Up @@ -80,6 +94,16 @@ This project is licensed under the Apache 2.0 License

## Changelog

## 1.1.0 ##
* Added support for charge cancel url.
* Handle cancelled events from API.
* Add option to disable icons on checkout page.
* Add Coinbase Commerce transaction ID to WooCommerce order output (Admin order page, Customer order page, email confirmation).
* Updated README.md

## 1.0.2 ##
* Tested against WordPress 4.9.4

## 1.0.1 ##
* Tested against WordPress 4.9.7
* Tested against WooCommerce 3.4.3
Expand All @@ -95,4 +119,4 @@ This project is licensed under the Apache 2.0 License
[Coinbase Commerce settings page]: <https://commerce.coinbase.com/dashboard/settings/>
[WooCommerce]: <https://woocommerce.com/>
[WordPress]: <https://wordpress.org/>

[WordPress.org plugin repository]: <https://wordpress.org/plugins/coinbase-commerce/>
42 changes: 41 additions & 1 deletion class-wc-gateway-coinbase.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ public static function log( $message, $level = 'info' ) {
* @return string
*/
public function get_icon() {
if ( $this->get_option( 'show_icons' ) === 'no' ) {
return '';
}

$image_path = plugin_dir_path( __FILE__ ) . 'assets/images';
$icon_html = '';
$methods = get_option( 'coinbase_payment_methods', array( 'bitcoin', 'bitcoincash', 'ethereum', 'litecoin' ) );
Expand Down Expand Up @@ -168,6 +172,12 @@ public function init_form_fields() {
__( '4. Click "Show shared secret" and paste into the box above.', 'coinbase' ),

),
'show_icons' => array(
'title' => __( 'Show icons', 'coinbase' ),
'type' => 'checkbox',
'label' => __( 'Display currency icons on checkout page.', 'coinbase' ),
'default' => 'yes',
),
'debug' => array(
'title' => __( 'Debug log', 'woocommerce' ),
'type' => 'checkbox',
Expand All @@ -187,6 +197,17 @@ public function init_form_fields() {
public function process_payment( $order_id ) {
$order = wc_get_order( $order_id );

// Create description for charge based on order's products. Ex: 1 x Product1, 2 x Product2
try {
$order_items = array_map( function( $item ) {
return $item['quantity'] . ' x ' . $item['name'];
}, $order->get_items() );

$description = mb_substr( implode( ', ', $order_items ), 0, 200 );
} catch ( Exception $e ) {
$description = null;
}

$this->init_api();

// Create a new charge.
Expand All @@ -196,7 +217,8 @@ public function process_payment( $order_id ) {
);
$result = Coinbase_API_Handler::create_charge(
$order->get_total(), get_woocommerce_currency(), $metadata,
$this->get_return_url( $order )
$this->get_return_url( $order ), null, $description,
$this->get_cancel_url( $order )
);

if ( ! $result[0] ) {
Expand All @@ -214,6 +236,22 @@ public function process_payment( $order_id ) {
);
}

/**
* Get the cancel url.
*
* @param WC_Order $order Order object.
* @return string
*/
public function get_cancel_url( $order ) {
$return_url = $order->get_cancel_order_url();

if ( is_ssl() || get_option( 'woocommerce_force_ssl_checkout' ) == 'yes' ) {
$return_url = str_replace( 'http:', 'https:', $return_url );
}

return apply_filters( 'woocommerce_get_cancel_url', $return_url, $order );
}

/**
* Check payment statuses on orders and update order statuses.
*/
Expand Down Expand Up @@ -313,6 +351,8 @@ public function _update_order_status( $order, $timeline ) {

if ( 'EXPIRED' === $status ) {
$order->update_status( 'cancelled', __( 'Coinbase payment expired.', 'coinbase' ) );
} elseif ( 'CANCELED' === $status ) {
$order->update_status( 'cancelled', __( 'Coinbase payment cancelled.', 'coinbase' ) );
} elseif ( 'UNRESOLVED' === $status ) {
// translators: Coinbase error status for "unresolved" payment. Includes error status.
$order->update_status( 'failed', sprintf( __( 'Coinbase payment unresolved, reason: %s.', 'coinbase' ), $last_update['context'] ) );
Expand Down
44 changes: 43 additions & 1 deletion coinbase-commerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Plugin Name: Coinbase Commerce
Plugin URI: https://github.com/coinbase/coinbase-commerce-woocommerce/
Description: A payment gateway that allows your customers to pay with cryptocurrency via Coinbase Commerce (https://commerce.coinbase.com/)
Version: 1.0.1
Version: 1.1.0
Author: Coinbase Commerce
Author URI: https://commerce.coinbase.com/
License: GPLv3+
Expand Down Expand Up @@ -38,6 +38,9 @@ function cb_init_gateway() {
add_action( 'cb_check_orders', 'cb_wc_check_orders' );
add_filter( 'woocommerce_payment_gateways', 'cb_wc_add_coinbase_class' );
add_filter( 'wc_order_statuses', 'cb_wc_add_status' );
add_action( 'woocommerce_admin_order_data_after_order_details', 'cb_order_meta_general' );
add_action( 'woocommerce_order_details_after_order_table', 'cb_order_meta_general' );
add_filter( 'woocommerce_email_order_meta_fields', 'cb_custom_woocommerce_email_order_meta_fields', 10, 3 );
}
}
add_action( 'plugins_loaded', 'cb_init_gateway' );
Expand Down Expand Up @@ -109,3 +112,42 @@ function cb_wc_add_status( $wc_statuses_arr ) {

return $new_statuses_arr;
}


/**
* Add order Coinbase meta after General and before Billing
*
* @see: https://rudrastyh.com/woocommerce/customize-order-details.html
*
* @param WC_Order $order WC order instance
*/
function cb_order_meta_general( $order ){ ?>

<br class="clear" />
<h3>Coinbase Commerce Data</h3>
<div class="">
<p>Coinbase Commerce Reference # <?php echo esc_html( $order->get_meta( '_coinbase_charge_id' ) ); ?></p>
</div>

<?php
}


/**
* Add Coinbase meta to WC emails
*
* @see https://docs.woocommerce.com/document/add-a-custom-field-in-an-order-to-the-emails/
*
* @param array $fields indexed list of existing additional fields.
* @param bool $sent_to_admin If should sent to admin.
* @param WC_Order $order WC order instance
*
*/
function cb_custom_woocommerce_email_order_meta_fields( $fields, $sent_to_admin, $order ) {
$fields['coinbase_commerce_reference'] = array(
'label' => __( 'Coinbase Commerce Reference #' ),
'value' => $order->get_meta( '_coinbase_charge_id' ),
);

return $fields;
}
30 changes: 22 additions & 8 deletions includes/class-coinbase-api-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,30 @@ public static function log( $message, $level = 'info' ) {
/**
* Get the response from an API request.
* @param string $endpoint
* @param array $args
* @param array $params
* @param string $method
* @return array
*/
public static function send_request( $endpoint, $args = array(), $method = 'GET' ) {
$url = esc_url_raw( add_query_arg( $args, self::$api_url . $endpoint ) );
public static function send_request( $endpoint, $params = array(), $method = 'GET' ) {
// phpcs:ignore
self::log( 'Coinbase Request Args for ' . $endpoint . ': ' . print_r( $args, true ) );
self::log( 'Coinbase Request Args for ' . $endpoint . ': ' . print_r( $params, true ) );
$args = array(
'method' => $method,
'headers' => array(
'X-CC-Api-Key' => self::$api_key,
'X-CC-Version' => self::$api_version,
),
'Content-Type' => 'application/json'
)
);

$response = wp_remote_request( $url, $args );
$url = self::$api_url . $endpoint;

if ( in_array( $method, array( 'POST', 'PUT' ) ) ) {
$args['body'] = json_encode( $params );
} else {
$url = add_query_arg( $params, $url );
}
$response = wp_remote_request( esc_url_raw( $url ), $args );

if ( is_wp_error( $response ) ) {
self::log( 'WP response error: ' . $response->get_error_message() );
Expand Down Expand Up @@ -109,19 +116,23 @@ public static function check_auth() {
* @param string $redirect
* @param string $name
* @param string $desc
* @param string $cancel
* @return array
*/
public static function create_charge( $amount = null, $currency = null, $metadata = null,
$redirect = null, $name = null, $desc = null ) {
$redirect = null, $name = null, $desc = null,
$cancel = null ) {
$args = array(
'name' => is_null( $name ) ? get_bloginfo( 'name' ) : $name,
'description' => is_null( $desc ) ? get_bloginfo( 'description' ) : $desc,
);
$args['name'] = sanitize_text_field( $args['name'] );
$args['description'] = sanitize_text_field( $args['description'] );

if ( is_null( $amount ) ) {
$args['pricing_type'] = 'no_price';
} elseif ( is_null( $currency ) ) {
self::log( 'Error: if amount if given, currency must be given (in create_charge()).', 'error' );
self::log( 'Error: if amount is given, currency must be given (in create_charge()).', 'error' );
return array( false, 'Missing currency.' );
} else {
$args['pricing_type'] = 'fixed_price';
Expand All @@ -137,6 +148,9 @@ public static function create_charge( $amount = null, $currency = null, $metadat
if ( ! is_null( $redirect ) ) {
$args['redirect_url'] = $redirect;
}
if ( ! is_null( $cancel ) ) {
$args['cancel_url'] = $cancel;
}

$result = self::send_request( 'charges', $args, 'POST' );

Expand Down

0 comments on commit 82d0d8b

Please sign in to comment.