Plugin Directory

Changeset 1965004


Ignore:
Timestamp:
10/29/2018 11:18:47 AM (7 years ago)
Author:
pragbarrett
Message:
  • Add coinase ID to order display and details
  • Make icons optional
  • Update change description
  • Add and handle cancel
  • Repo move
Location:
coinbase-commerce/trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • coinbase-commerce/trunk/class-wc-gateway-coinbase.php

    r1949700 r1965004  
    8686     */
    8787    public function get_icon() {
     88        if ( $this->get_option( 'show_icons' ) === 'no' ) {
     89            return '';
     90        }
     91
    8892        $image_path = plugin_dir_path( __FILE__ ) . 'assets/images';
    8993        $icon_html  = '';
     
    169173
    170174            ),
     175            'show_icons'     => array(
     176                'title'       => __( 'Show icons', 'coinbase' ),
     177                'type'        => 'checkbox',
     178                'label'       => __( 'Display currency icons on checkout page.', 'coinbase' ),
     179                'default'     => 'yes',
     180            ),
    171181            'debug'          => array(
    172182                'title'       => __( 'Debug log', 'woocommerce' ),
     
    188198        $order = wc_get_order( $order_id );
    189199
     200        // Create description for charge based on order's products. Ex: 1 x Product1, 2 x Product2
     201        try {
     202            $order_items = array_map( function( $item ) {
     203                return $item['quantity'] . ' x ' . $item['name'];
     204            }, $order->get_items() );
     205
     206            $description = mb_substr( implode( ', ', $order_items ), 0, 200 );
     207        } catch ( Exception $e ) {
     208            $description = null;
     209        }
     210
    190211        $this->init_api();
    191212
     
    197218        $result   = Coinbase_API_Handler::create_charge(
    198219            $order->get_total(), get_woocommerce_currency(), $metadata,
    199             $this->get_return_url( $order )
     220            $this->get_return_url( $order ), null, $description,
     221            $this->get_cancel_url( $order )
    200222        );
    201223
     
    213235            'redirect' => $charge['hosted_url'],
    214236        );
     237    }
     238
     239    /**
     240     * Get the cancel url.
     241     *
     242     * @param WC_Order $order Order object.
     243     * @return string
     244     */
     245    public function get_cancel_url( $order ) {
     246        $return_url = $order->get_cancel_order_url();
     247
     248        if ( is_ssl() || get_option( 'woocommerce_force_ssl_checkout' ) == 'yes' ) {
     249            $return_url = str_replace( 'http:', 'https:', $return_url );
     250        }
     251
     252        return apply_filters( 'woocommerce_get_cancel_url', $return_url, $order );
    215253    }
    216254
     
    314352            if ( 'EXPIRED' === $status ) {
    315353                $order->update_status( 'cancelled', __( 'Coinbase payment expired.', 'coinbase' ) );
     354            } elseif ( 'CANCELED' === $status ) {
     355                $order->update_status( 'cancelled', __( 'Coinbase payment cancelled.', 'coinbase' ) );
    316356            } elseif ( 'UNRESOLVED' === $status ) {
    317357                // translators: Coinbase error status for "unresolved" payment. Includes error status.
  • coinbase-commerce/trunk/coinbase-commerce.php

    r1949700 r1965004  
    44Plugin URI:   https://github.com/coinbase/coinbase-commerce-woocommerce/
    55Description:  A payment gateway that allows your customers to pay with cryptocurrency via Coinbase Commerce (https://commerce.coinbase.com/)
    6 Version:      1.0.1
     6Version:      1.1.0
    77Author:       Coinbase Commerce
    88Author URI:   https://commerce.coinbase.com/
     
    3939        add_filter( 'woocommerce_payment_gateways', 'cb_wc_add_coinbase_class' );
    4040        add_filter( 'wc_order_statuses', 'cb_wc_add_status' );
     41        add_action( 'woocommerce_admin_order_data_after_order_details', 'cb_order_meta_general' );
     42        add_action( 'woocommerce_order_details_after_order_table', 'cb_order_meta_general' );
     43        add_filter( 'woocommerce_email_order_meta_fields', 'cb_custom_woocommerce_email_order_meta_fields', 10, 3 );
    4144    }
    4245}
     
    110113    return $new_statuses_arr;
    111114}
     115
     116
     117/**
     118 * Add order Coinbase meta after General and before Billing
     119 *
     120 * @see: https://rudrastyh.com/woocommerce/customize-order-details.html
     121 *
     122 * @param WC_Order $order WC order instance
     123 */
     124function cb_order_meta_general( $order ){ ?>
     125
     126    <br class="clear" />
     127    <h3>Coinbase Commerce Data</h3>
     128    <div class="">
     129        <p>Coinbase Commerce Reference # <?php echo esc_html( $order->get_meta( '_coinbase_charge_id' ) ); ?></p>
     130    </div>
     131
     132    <?php
     133}
     134
     135
     136/**
     137 * Add Coinbase meta to WC emails
     138 *
     139 * @see https://docs.woocommerce.com/document/add-a-custom-field-in-an-order-to-the-emails/
     140 *
     141 * @param array    $fields indexed list of existing additional fields.
     142 * @param bool     $sent_to_admin If should sent to admin.
     143 * @param WC_Order $order WC order instance
     144 *
     145 */
     146function cb_custom_woocommerce_email_order_meta_fields( $fields, $sent_to_admin, $order ) {
     147    $fields['coinbase_commerce_reference'] = array(
     148        'label' => __( 'Coinbase Commerce Reference #' ),
     149        'value' => $order->get_meta( '_coinbase_charge_id' ),
     150    );
     151
     152    return $fields;
     153}
  • coinbase-commerce/trunk/includes/class-coinbase-api-handler.php

    r1949700 r1965004  
    3535     * Get the response from an API request.
    3636     * @param  string $endpoint
    37      * @param  array  $args
     37     * @param  array  $params
    3838     * @param  string $method
    3939     * @return array
    4040     */
    41     public static function send_request( $endpoint, $args = array(), $method = 'GET' ) {
    42         $url = esc_url_raw( add_query_arg( $args, self::$api_url . $endpoint ) );
     41    public static function send_request( $endpoint, $params = array(), $method = 'GET' ) {
    4342        // phpcs:ignore
    44         self::log( 'Coinbase Request Args for ' . $endpoint . ': ' . print_r( $args, true ) );
     43        self::log( 'Coinbase Request Args for ' . $endpoint . ': ' . print_r( $params, true ) );
    4544        $args = array(
    4645            'method'  => $method,
     
    4847                'X-CC-Api-Key' => self::$api_key,
    4948                'X-CC-Version' => self::$api_version,
    50             ),
     49                'Content-Type' => 'application/json'
     50            )
    5151        );
    5252
    53         $response = wp_remote_request( $url, $args );
     53        $url = self::$api_url . $endpoint;
     54
     55        if ( in_array( $method, array( 'POST', 'PUT' ) ) ) {
     56            $args['body'] = json_encode( $params );
     57        } else {
     58            $url = add_query_arg( $params, $url );
     59        }
     60        $response = wp_remote_request( esc_url_raw( $url ), $args );
    5461
    5562        if ( is_wp_error( $response ) ) {
     
    110117     * @param  string $name
    111118     * @param  string $desc
     119     * @param  string $cancel
    112120     * @return array
    113121     */
    114122    public static function create_charge( $amount = null, $currency = null, $metadata = null,
    115                                         $redirect = null, $name = null, $desc = null ) {
     123                                        $redirect = null, $name = null, $desc = null,
     124                                        $cancel = null ) {
    116125        $args = array(
    117126            'name'        => is_null( $name ) ? get_bloginfo( 'name' ) : $name,
    118127            'description' => is_null( $desc ) ? get_bloginfo( 'description' ) : $desc,
    119128        );
     129        $args['name'] = sanitize_text_field( $args['name'] );
     130        $args['description'] = sanitize_text_field( $args['description'] );
    120131
    121132        if ( is_null( $amount ) ) {
    122133            $args['pricing_type'] = 'no_price';
    123134        } elseif ( is_null( $currency ) ) {
    124             self::log( 'Error: if amount if given, currency must be given (in create_charge()).', 'error' );
     135            self::log( 'Error: if amount is given, currency must be given (in create_charge()).', 'error' );
    125136            return array( false, 'Missing currency.' );
    126137        } else {
     
    137148        if ( ! is_null( $redirect ) ) {
    138149            $args['redirect_url'] = $redirect;
     150        }
     151        if ( ! is_null( $cancel ) ) {
     152            $args['cancel_url'] = $cancel;
    139153        }
    140154
Note: See TracChangeset for help on using the changeset viewer.