Embbed a Coinbase EDD plugin










0














Hello I have a Wordpress site with EDD (Easy Digital Downloads) I have the Coinbase plugin for the Gateway, which works perfect, but has a problem when checking out the button redirects off the site and takes you to the Coinbase to process the I pay outside, which I find unprofessional.



I want to checkout the product to be processed within my own site and not leave.



I was looking for and found a solution that offers the Coinbase site that the "embedded" when trying to process the payment opens a "modal window" within the same site without leaving and proceeds to request payment without leaving it.



The live example is here:



https://canadiancrypto.io/accept-crypto-payments-with-coinbase-commerce/



But the example is a button that when pressed, the action is executed and it is embedded in the same place.



The code of the plugin to process the payment through the gateway is the following:



<?php

class EDD_Coinbase

private $api_key;

public function __construct()

if( ! function_exists( 'edd_get_option' ) )
return;


if( class_exists( 'EDD_License' ) && is_admin() )
$license = new EDD_License( __FILE__, 'Coinbase Payment Gateway', '1.1', 'Easy Digital Downloads' );


$this->api_key = trim( edd_get_option( 'edd_coinbase_api_key', '' ) );

add_action( 'init', array( $this, 'textdomain' ) );
add_action( 'edd_gateway_coinbase', array( $this, 'process_payment' ) );
add_action( 'init', array( $this, 'listener' ) );
add_action( 'edd_coinbase_cc_form', '__return_false' );
add_action( 'template_redirect', array( $this, 'process_confirmation' ) );

add_filter( 'edd_payment_gateways', array( $this, 'register_gateway' ) );
add_filter( 'edd_currencies', array( $this, 'currencies' ) );
add_filter( 'edd_sanitize_amount_decimals', array( $this, 'btc_decimals' ) );
add_filter( 'edd_format_amount_decimals', array( $this, 'btc_decimals' ) );
add_filter( 'edd_settings_sections_gateways', array( $this, 'subsection' ), 10, 1 );
add_filter( 'edd_settings_gateways', array( $this, 'settings' ) );



public function textdomain()

// Set filter for plugin's languages directory
$lang_dir = dirname( plugin_basename( __FILE__ ) ) . '/languages/';
$lang_dir = apply_filters( 'edd_coinbase_languages_directory', $lang_dir );

// Traditional WordPress plugin locale filter
$locale = apply_filters( 'plugin_locale', get_locale(), 'edd-coinbase' );
$mofile = sprintf( '%1$s-%2$s.mo', 'edd-coinbase', $locale );

// Setup paths to current locale file
$mofile_local = $lang_dir . $mofile;
$mofile_global = WP_LANG_DIR . '/edd-coinbase/' . $mofile;

if ( file_exists( $mofile_global ) )
load_textdomain( 'edd-coinbase', $mofile_global );
elseif ( file_exists( $mofile_local ) )
load_textdomain( 'edd-coinbase', $mofile_local );
else
// Load the default language files
load_plugin_textdomain( 'edd-coinbase', false, $lang_dir );




public function register_gateway( $gateways )

$gateways['coinbase'] = array(
'checkout_label' => edd_get_option( 'edd_coinbase_checkout_label', __( 'Bitcoin', 'edd-coinbase' ) ),
'admin_label' => __( 'Coinbase', 'edd-coinbase' )
);

return $gateways;



public function process_payment( $purchase_data )

if( ! $this->is_api_valid() )
edd_set_error( 'edd_coinbase_api_invalid', __( 'Please enter your Coinbase API key in Settings', 'edd-coinbase' ) );
edd_send_back_to_checkout( '?payment-mode=coinbase' );


$purchase_summary = '';
if( is_array( $purchase_data['cart_details'] ) && ! empty( $purchase_data['cart_details'] ) )

foreach( $purchase_data['cart_details'] as $item )
$purchase_summary .= $item['name'];
$price_id = isset( $item['item_number']['options']['price_id'] ) ? absint( $item['item_number']['options']['price_id'] ) : false;
if ( false !== $price_id )
$purchase_summary .= ' - ' . edd_get_price_option_name( $item['id'], $item['item_number']['options']['price_id'] );

$purchase_summary .= ', ';


$purchase_summary = rtrim( $purchase_summary, ', ' );



$headers = array(
'X-CC-Api-Key' => $this->api_key,
'X-CC-Version' => '2018-03-22',
'Content-Type' => 'application/json'
);

$args = array(
'headers' => $headers,
'body' => json_encode( array(
'name' => get_bloginfo( 'name' ),
'description' => $purchase_summary,
'pricing_type' => 'fixed_price',
'local_price' => array(
'amount' => $purchase_data['price'],
'currency' => edd_get_currency(),
),
'metadata' => array(
'payment_key' => $purchase_data['purchase_key'],
'email' => $purchase_data['user_email']
),
'redirect_url' => add_query_arg( array(
'payment_key' => $purchase_data['purchase_key'],
'gateway' => 'coinbase'
), edd_get_success_page_uri() )
) )
);

$request = wp_remote_post( 'https://api.commerce.coinbase.com/charges', $args );

// echo '<pre>'; print_r( $request ); echo '</pre>'; exit;
if( is_wp_error( $request ) )
edd_set_error( $request->get_error_code(), $request->get_error_message() );
edd_send_back_to_checkout( '?payment-mode=coinbase' );


$body = json_decode( wp_remote_retrieve_body( $request ) );

if( 200 !== wp_remote_retrieve_response_code( $request ) && 201 !== wp_remote_retrieve_response_code( $request ) )

edd_set_error( $body->error->code, $body->error->message );
edd_send_back_to_checkout( '?payment-mode=coinbase' );



$payment_data = array(
'price' => $purchase_data['price'],
'date' => $purchase_data['date'],
'user_email' => $purchase_data['user_email'],
'purchase_key' => $purchase_data['purchase_key'],
'currency' => edd_get_currency(),
'downloads' => $purchase_data['downloads'],
'cart_details' => $purchase_data['cart_details'],
'user_info' => $purchase_data['user_info'],
'status' => 'pending',
'gateway' => 'coinbase'
);

// record the pending payment
$payment_id = edd_insert_payment( $payment_data );
if ( $payment_id )

edd_set_payment_transaction_id( $payment_id, $body->data->code );
edd_insert_payment_note( $payment_id, sprintf( __( 'Pending Coinbase charge created, code %s', 'edd-coinbase' ), $body->data->code ) );
wp_redirect( $body->data->hosted_url ); exit;





public function listener()

if( empty( $_GET['edd-listener'] ) )
return;


if( 'coinbase' != $_GET['edd-listener'] )
return;


if( ! $this->is_api_valid() )
return;


$body = @file_get_contents( 'php://input' );
$data = json_decode( $body );

if( empty( $data->event ) )
return;


$type = $data->event->type;
$key = sanitize_text_field( $data->event->data->metadata->payment_key );
$trans_id = $data->event->data->code;
$payment_id = edd_get_purchase_id_by_key( $key );

edd_debug_log( 'Coinbase webhook processing for payment ' . $payment_id );

if( ! $payment_id )
edd_debug_log( 'Coinbase webhook processing stopped because payment was not found from provided key' );
die( 'no payment found' );


if( 'charge:confirmed' !== $type )
edd_debug_log( 'Coinbase webhook processing stopped because it is not a charge:confirmed type' );
die( 'charge not completed' );


if( ! edd_is_payment_complete( $payment_id ) ) 201 === wp_remote_retrieve_response_code( $request ) )

edd_debug_log( 'Coinbase webhook charge retrieved successfully. Charge object: ' . var_export( $body, true ) );

if( ! empty( $body->data->confirmed_at ) )

edd_debug_log( 'Coinbase webhook processing for payment ' . $payment_id . ' successful, marking payment as complete' );
edd_update_payment_status( $payment_id, 'publish' );



else

edd_debug_log( 'Coinbase webhook processing failed. Non 200/201 returned by Coinbase. Charge response: ' . var_export( $body, true ) );







public function process_confirmation() 'coinbase' !== $_GET['gateway'] )
return;


if( ! edd_is_success_page()

public function currencies( $currencies )

$currencies['BTC'] = __( 'Bitcoin', 'edd-coinbase' );

return $currencies;


function btc_decimals( $decimals = 2 )
global $edd_options;

$currency = edd_get_currency();

switch ( $currency )
case 'BTC' :

$decimals = 8;
break;


return $decimals;


public function subsection( $sections )
$sections['coinbase'] = __( 'Coinbase', 'edd-coinbase' );
return $sections;


public function settings( $settings )

$coinbase_settings = array(
array(
'id' => 'edd_coinbase_header',
'name' => '<strong>' . __( 'Coinbase', 'edd-coinbase' ) . '</strong>',
'desc' => '',
'type' => 'header',
'size' => 'regular'
),
array(
'id' => 'edd_coinbase_checkout_label',
'name' => __( 'Checkout Label', 'edd-coinbase' ),
'desc' => __( 'Enter text you would like shown on checkout for customers. This is shown when selecting the payment method.' ),
'type' => 'text',
'std' => 'Bitcoin'
),
array(
'id' => 'edd_coinbase_api_key',
'name' => __( 'API Key', 'edd-coinbase' ),
'desc' => __( 'Enter your Coinbase API key' ),
'type' => 'text'
),
array(
'id' => 'coinbase_webhook_description',
'type' => 'descriptive_text',
'name' => __( 'Webhooks', 'edd-coinbase' ),
'desc' =>
'<p>' . sprintf( __( 'In order for Coinbase to function completely, you must configure your webhooks. Visit your <a href="%s" target="_blank">account dashboard</a> to configure them. Please add a webhook endpoint for the URL below.', 'edd-coinbase' ), 'https://commerce.coinbase.com/dashboard/settings' ) . '</p>' .
'<p><strong>' . sprintf( __( 'Webhook URL: %s', 'edds' ), home_url( 'index.php?edd-listener=coinbase' ) ) . '</strong></p>' .
'<p>' . sprintf( __( 'See our <a href="%s">documentation</a> for more information.', 'edd-coinbase' ), 'https://docs.easydigitaldownloads.com/article/314-coinbase-payment-gateway-setup-documentation' ) . '</p>'
),
);

if ( version_compare( EDD_VERSION, 2.5, '>=' ) )
$coinbase_settings = array( 'coinbase' => $coinbase_settings );



return array_merge( $settings, $coinbase_settings );



public function is_api_valid()

if( empty( $this->api_key ) )
return false;


return true;




function edd_coinbase_init()
$coinbase = new EDD_Coinbase;

add_action( 'plugins_loaded', 'edd_coinbase_init' );


What I want is that when processing the payment does not leave the site but the entire payment process is done within the site.



What I want is that when processing the payment does not leave the site but the entire payment process is done within the site as in the previous example of canadiancrypto.io.



The solution to embed it in the button is used the following code:



<div>
<a class="donate-with-crypto"
href="https://commerce.coinbase.com/checkout/6da189f179bc31">
<span>Donate with Crypto</span>
</a>
<script src="https://commerce.coinbase.com/v1/checkout.js">
</script>
</div>


but I have not managed to implement it to the gateway successfully thank you for your help thanks










share|improve this question


























    0














    Hello I have a Wordpress site with EDD (Easy Digital Downloads) I have the Coinbase plugin for the Gateway, which works perfect, but has a problem when checking out the button redirects off the site and takes you to the Coinbase to process the I pay outside, which I find unprofessional.



    I want to checkout the product to be processed within my own site and not leave.



    I was looking for and found a solution that offers the Coinbase site that the "embedded" when trying to process the payment opens a "modal window" within the same site without leaving and proceeds to request payment without leaving it.



    The live example is here:



    https://canadiancrypto.io/accept-crypto-payments-with-coinbase-commerce/



    But the example is a button that when pressed, the action is executed and it is embedded in the same place.



    The code of the plugin to process the payment through the gateway is the following:



    <?php

    class EDD_Coinbase

    private $api_key;

    public function __construct()

    if( ! function_exists( 'edd_get_option' ) )
    return;


    if( class_exists( 'EDD_License' ) && is_admin() )
    $license = new EDD_License( __FILE__, 'Coinbase Payment Gateway', '1.1', 'Easy Digital Downloads' );


    $this->api_key = trim( edd_get_option( 'edd_coinbase_api_key', '' ) );

    add_action( 'init', array( $this, 'textdomain' ) );
    add_action( 'edd_gateway_coinbase', array( $this, 'process_payment' ) );
    add_action( 'init', array( $this, 'listener' ) );
    add_action( 'edd_coinbase_cc_form', '__return_false' );
    add_action( 'template_redirect', array( $this, 'process_confirmation' ) );

    add_filter( 'edd_payment_gateways', array( $this, 'register_gateway' ) );
    add_filter( 'edd_currencies', array( $this, 'currencies' ) );
    add_filter( 'edd_sanitize_amount_decimals', array( $this, 'btc_decimals' ) );
    add_filter( 'edd_format_amount_decimals', array( $this, 'btc_decimals' ) );
    add_filter( 'edd_settings_sections_gateways', array( $this, 'subsection' ), 10, 1 );
    add_filter( 'edd_settings_gateways', array( $this, 'settings' ) );



    public function textdomain()

    // Set filter for plugin's languages directory
    $lang_dir = dirname( plugin_basename( __FILE__ ) ) . '/languages/';
    $lang_dir = apply_filters( 'edd_coinbase_languages_directory', $lang_dir );

    // Traditional WordPress plugin locale filter
    $locale = apply_filters( 'plugin_locale', get_locale(), 'edd-coinbase' );
    $mofile = sprintf( '%1$s-%2$s.mo', 'edd-coinbase', $locale );

    // Setup paths to current locale file
    $mofile_local = $lang_dir . $mofile;
    $mofile_global = WP_LANG_DIR . '/edd-coinbase/' . $mofile;

    if ( file_exists( $mofile_global ) )
    load_textdomain( 'edd-coinbase', $mofile_global );
    elseif ( file_exists( $mofile_local ) )
    load_textdomain( 'edd-coinbase', $mofile_local );
    else
    // Load the default language files
    load_plugin_textdomain( 'edd-coinbase', false, $lang_dir );




    public function register_gateway( $gateways )

    $gateways['coinbase'] = array(
    'checkout_label' => edd_get_option( 'edd_coinbase_checkout_label', __( 'Bitcoin', 'edd-coinbase' ) ),
    'admin_label' => __( 'Coinbase', 'edd-coinbase' )
    );

    return $gateways;



    public function process_payment( $purchase_data )

    if( ! $this->is_api_valid() )
    edd_set_error( 'edd_coinbase_api_invalid', __( 'Please enter your Coinbase API key in Settings', 'edd-coinbase' ) );
    edd_send_back_to_checkout( '?payment-mode=coinbase' );


    $purchase_summary = '';
    if( is_array( $purchase_data['cart_details'] ) && ! empty( $purchase_data['cart_details'] ) )

    foreach( $purchase_data['cart_details'] as $item )
    $purchase_summary .= $item['name'];
    $price_id = isset( $item['item_number']['options']['price_id'] ) ? absint( $item['item_number']['options']['price_id'] ) : false;
    if ( false !== $price_id )
    $purchase_summary .= ' - ' . edd_get_price_option_name( $item['id'], $item['item_number']['options']['price_id'] );

    $purchase_summary .= ', ';


    $purchase_summary = rtrim( $purchase_summary, ', ' );



    $headers = array(
    'X-CC-Api-Key' => $this->api_key,
    'X-CC-Version' => '2018-03-22',
    'Content-Type' => 'application/json'
    );

    $args = array(
    'headers' => $headers,
    'body' => json_encode( array(
    'name' => get_bloginfo( 'name' ),
    'description' => $purchase_summary,
    'pricing_type' => 'fixed_price',
    'local_price' => array(
    'amount' => $purchase_data['price'],
    'currency' => edd_get_currency(),
    ),
    'metadata' => array(
    'payment_key' => $purchase_data['purchase_key'],
    'email' => $purchase_data['user_email']
    ),
    'redirect_url' => add_query_arg( array(
    'payment_key' => $purchase_data['purchase_key'],
    'gateway' => 'coinbase'
    ), edd_get_success_page_uri() )
    ) )
    );

    $request = wp_remote_post( 'https://api.commerce.coinbase.com/charges', $args );

    // echo '<pre>'; print_r( $request ); echo '</pre>'; exit;
    if( is_wp_error( $request ) )
    edd_set_error( $request->get_error_code(), $request->get_error_message() );
    edd_send_back_to_checkout( '?payment-mode=coinbase' );


    $body = json_decode( wp_remote_retrieve_body( $request ) );

    if( 200 !== wp_remote_retrieve_response_code( $request ) && 201 !== wp_remote_retrieve_response_code( $request ) )

    edd_set_error( $body->error->code, $body->error->message );
    edd_send_back_to_checkout( '?payment-mode=coinbase' );



    $payment_data = array(
    'price' => $purchase_data['price'],
    'date' => $purchase_data['date'],
    'user_email' => $purchase_data['user_email'],
    'purchase_key' => $purchase_data['purchase_key'],
    'currency' => edd_get_currency(),
    'downloads' => $purchase_data['downloads'],
    'cart_details' => $purchase_data['cart_details'],
    'user_info' => $purchase_data['user_info'],
    'status' => 'pending',
    'gateway' => 'coinbase'
    );

    // record the pending payment
    $payment_id = edd_insert_payment( $payment_data );
    if ( $payment_id )

    edd_set_payment_transaction_id( $payment_id, $body->data->code );
    edd_insert_payment_note( $payment_id, sprintf( __( 'Pending Coinbase charge created, code %s', 'edd-coinbase' ), $body->data->code ) );
    wp_redirect( $body->data->hosted_url ); exit;





    public function listener()

    if( empty( $_GET['edd-listener'] ) )
    return;


    if( 'coinbase' != $_GET['edd-listener'] )
    return;


    if( ! $this->is_api_valid() )
    return;


    $body = @file_get_contents( 'php://input' );
    $data = json_decode( $body );

    if( empty( $data->event ) )
    return;


    $type = $data->event->type;
    $key = sanitize_text_field( $data->event->data->metadata->payment_key );
    $trans_id = $data->event->data->code;
    $payment_id = edd_get_purchase_id_by_key( $key );

    edd_debug_log( 'Coinbase webhook processing for payment ' . $payment_id );

    if( ! $payment_id )
    edd_debug_log( 'Coinbase webhook processing stopped because payment was not found from provided key' );
    die( 'no payment found' );


    if( 'charge:confirmed' !== $type )
    edd_debug_log( 'Coinbase webhook processing stopped because it is not a charge:confirmed type' );
    die( 'charge not completed' );


    if( ! edd_is_payment_complete( $payment_id ) ) 201 === wp_remote_retrieve_response_code( $request ) )

    edd_debug_log( 'Coinbase webhook charge retrieved successfully. Charge object: ' . var_export( $body, true ) );

    if( ! empty( $body->data->confirmed_at ) )

    edd_debug_log( 'Coinbase webhook processing for payment ' . $payment_id . ' successful, marking payment as complete' );
    edd_update_payment_status( $payment_id, 'publish' );



    else

    edd_debug_log( 'Coinbase webhook processing failed. Non 200/201 returned by Coinbase. Charge response: ' . var_export( $body, true ) );







    public function process_confirmation() 'coinbase' !== $_GET['gateway'] )
    return;


    if( ! edd_is_success_page()

    public function currencies( $currencies )

    $currencies['BTC'] = __( 'Bitcoin', 'edd-coinbase' );

    return $currencies;


    function btc_decimals( $decimals = 2 )
    global $edd_options;

    $currency = edd_get_currency();

    switch ( $currency )
    case 'BTC' :

    $decimals = 8;
    break;


    return $decimals;


    public function subsection( $sections )
    $sections['coinbase'] = __( 'Coinbase', 'edd-coinbase' );
    return $sections;


    public function settings( $settings )

    $coinbase_settings = array(
    array(
    'id' => 'edd_coinbase_header',
    'name' => '<strong>' . __( 'Coinbase', 'edd-coinbase' ) . '</strong>',
    'desc' => '',
    'type' => 'header',
    'size' => 'regular'
    ),
    array(
    'id' => 'edd_coinbase_checkout_label',
    'name' => __( 'Checkout Label', 'edd-coinbase' ),
    'desc' => __( 'Enter text you would like shown on checkout for customers. This is shown when selecting the payment method.' ),
    'type' => 'text',
    'std' => 'Bitcoin'
    ),
    array(
    'id' => 'edd_coinbase_api_key',
    'name' => __( 'API Key', 'edd-coinbase' ),
    'desc' => __( 'Enter your Coinbase API key' ),
    'type' => 'text'
    ),
    array(
    'id' => 'coinbase_webhook_description',
    'type' => 'descriptive_text',
    'name' => __( 'Webhooks', 'edd-coinbase' ),
    'desc' =>
    '<p>' . sprintf( __( 'In order for Coinbase to function completely, you must configure your webhooks. Visit your <a href="%s" target="_blank">account dashboard</a> to configure them. Please add a webhook endpoint for the URL below.', 'edd-coinbase' ), 'https://commerce.coinbase.com/dashboard/settings' ) . '</p>' .
    '<p><strong>' . sprintf( __( 'Webhook URL: %s', 'edds' ), home_url( 'index.php?edd-listener=coinbase' ) ) . '</strong></p>' .
    '<p>' . sprintf( __( 'See our <a href="%s">documentation</a> for more information.', 'edd-coinbase' ), 'https://docs.easydigitaldownloads.com/article/314-coinbase-payment-gateway-setup-documentation' ) . '</p>'
    ),
    );

    if ( version_compare( EDD_VERSION, 2.5, '>=' ) )
    $coinbase_settings = array( 'coinbase' => $coinbase_settings );



    return array_merge( $settings, $coinbase_settings );



    public function is_api_valid()

    if( empty( $this->api_key ) )
    return false;


    return true;




    function edd_coinbase_init()
    $coinbase = new EDD_Coinbase;

    add_action( 'plugins_loaded', 'edd_coinbase_init' );


    What I want is that when processing the payment does not leave the site but the entire payment process is done within the site.



    What I want is that when processing the payment does not leave the site but the entire payment process is done within the site as in the previous example of canadiancrypto.io.



    The solution to embed it in the button is used the following code:



    <div>
    <a class="donate-with-crypto"
    href="https://commerce.coinbase.com/checkout/6da189f179bc31">
    <span>Donate with Crypto</span>
    </a>
    <script src="https://commerce.coinbase.com/v1/checkout.js">
    </script>
    </div>


    but I have not managed to implement it to the gateway successfully thank you for your help thanks










    share|improve this question
























      0












      0








      0







      Hello I have a Wordpress site with EDD (Easy Digital Downloads) I have the Coinbase plugin for the Gateway, which works perfect, but has a problem when checking out the button redirects off the site and takes you to the Coinbase to process the I pay outside, which I find unprofessional.



      I want to checkout the product to be processed within my own site and not leave.



      I was looking for and found a solution that offers the Coinbase site that the "embedded" when trying to process the payment opens a "modal window" within the same site without leaving and proceeds to request payment without leaving it.



      The live example is here:



      https://canadiancrypto.io/accept-crypto-payments-with-coinbase-commerce/



      But the example is a button that when pressed, the action is executed and it is embedded in the same place.



      The code of the plugin to process the payment through the gateway is the following:



      <?php

      class EDD_Coinbase

      private $api_key;

      public function __construct()

      if( ! function_exists( 'edd_get_option' ) )
      return;


      if( class_exists( 'EDD_License' ) && is_admin() )
      $license = new EDD_License( __FILE__, 'Coinbase Payment Gateway', '1.1', 'Easy Digital Downloads' );


      $this->api_key = trim( edd_get_option( 'edd_coinbase_api_key', '' ) );

      add_action( 'init', array( $this, 'textdomain' ) );
      add_action( 'edd_gateway_coinbase', array( $this, 'process_payment' ) );
      add_action( 'init', array( $this, 'listener' ) );
      add_action( 'edd_coinbase_cc_form', '__return_false' );
      add_action( 'template_redirect', array( $this, 'process_confirmation' ) );

      add_filter( 'edd_payment_gateways', array( $this, 'register_gateway' ) );
      add_filter( 'edd_currencies', array( $this, 'currencies' ) );
      add_filter( 'edd_sanitize_amount_decimals', array( $this, 'btc_decimals' ) );
      add_filter( 'edd_format_amount_decimals', array( $this, 'btc_decimals' ) );
      add_filter( 'edd_settings_sections_gateways', array( $this, 'subsection' ), 10, 1 );
      add_filter( 'edd_settings_gateways', array( $this, 'settings' ) );



      public function textdomain()

      // Set filter for plugin's languages directory
      $lang_dir = dirname( plugin_basename( __FILE__ ) ) . '/languages/';
      $lang_dir = apply_filters( 'edd_coinbase_languages_directory', $lang_dir );

      // Traditional WordPress plugin locale filter
      $locale = apply_filters( 'plugin_locale', get_locale(), 'edd-coinbase' );
      $mofile = sprintf( '%1$s-%2$s.mo', 'edd-coinbase', $locale );

      // Setup paths to current locale file
      $mofile_local = $lang_dir . $mofile;
      $mofile_global = WP_LANG_DIR . '/edd-coinbase/' . $mofile;

      if ( file_exists( $mofile_global ) )
      load_textdomain( 'edd-coinbase', $mofile_global );
      elseif ( file_exists( $mofile_local ) )
      load_textdomain( 'edd-coinbase', $mofile_local );
      else
      // Load the default language files
      load_plugin_textdomain( 'edd-coinbase', false, $lang_dir );




      public function register_gateway( $gateways )

      $gateways['coinbase'] = array(
      'checkout_label' => edd_get_option( 'edd_coinbase_checkout_label', __( 'Bitcoin', 'edd-coinbase' ) ),
      'admin_label' => __( 'Coinbase', 'edd-coinbase' )
      );

      return $gateways;



      public function process_payment( $purchase_data )

      if( ! $this->is_api_valid() )
      edd_set_error( 'edd_coinbase_api_invalid', __( 'Please enter your Coinbase API key in Settings', 'edd-coinbase' ) );
      edd_send_back_to_checkout( '?payment-mode=coinbase' );


      $purchase_summary = '';
      if( is_array( $purchase_data['cart_details'] ) && ! empty( $purchase_data['cart_details'] ) )

      foreach( $purchase_data['cart_details'] as $item )
      $purchase_summary .= $item['name'];
      $price_id = isset( $item['item_number']['options']['price_id'] ) ? absint( $item['item_number']['options']['price_id'] ) : false;
      if ( false !== $price_id )
      $purchase_summary .= ' - ' . edd_get_price_option_name( $item['id'], $item['item_number']['options']['price_id'] );

      $purchase_summary .= ', ';


      $purchase_summary = rtrim( $purchase_summary, ', ' );



      $headers = array(
      'X-CC-Api-Key' => $this->api_key,
      'X-CC-Version' => '2018-03-22',
      'Content-Type' => 'application/json'
      );

      $args = array(
      'headers' => $headers,
      'body' => json_encode( array(
      'name' => get_bloginfo( 'name' ),
      'description' => $purchase_summary,
      'pricing_type' => 'fixed_price',
      'local_price' => array(
      'amount' => $purchase_data['price'],
      'currency' => edd_get_currency(),
      ),
      'metadata' => array(
      'payment_key' => $purchase_data['purchase_key'],
      'email' => $purchase_data['user_email']
      ),
      'redirect_url' => add_query_arg( array(
      'payment_key' => $purchase_data['purchase_key'],
      'gateway' => 'coinbase'
      ), edd_get_success_page_uri() )
      ) )
      );

      $request = wp_remote_post( 'https://api.commerce.coinbase.com/charges', $args );

      // echo '<pre>'; print_r( $request ); echo '</pre>'; exit;
      if( is_wp_error( $request ) )
      edd_set_error( $request->get_error_code(), $request->get_error_message() );
      edd_send_back_to_checkout( '?payment-mode=coinbase' );


      $body = json_decode( wp_remote_retrieve_body( $request ) );

      if( 200 !== wp_remote_retrieve_response_code( $request ) && 201 !== wp_remote_retrieve_response_code( $request ) )

      edd_set_error( $body->error->code, $body->error->message );
      edd_send_back_to_checkout( '?payment-mode=coinbase' );



      $payment_data = array(
      'price' => $purchase_data['price'],
      'date' => $purchase_data['date'],
      'user_email' => $purchase_data['user_email'],
      'purchase_key' => $purchase_data['purchase_key'],
      'currency' => edd_get_currency(),
      'downloads' => $purchase_data['downloads'],
      'cart_details' => $purchase_data['cart_details'],
      'user_info' => $purchase_data['user_info'],
      'status' => 'pending',
      'gateway' => 'coinbase'
      );

      // record the pending payment
      $payment_id = edd_insert_payment( $payment_data );
      if ( $payment_id )

      edd_set_payment_transaction_id( $payment_id, $body->data->code );
      edd_insert_payment_note( $payment_id, sprintf( __( 'Pending Coinbase charge created, code %s', 'edd-coinbase' ), $body->data->code ) );
      wp_redirect( $body->data->hosted_url ); exit;





      public function listener()

      if( empty( $_GET['edd-listener'] ) )
      return;


      if( 'coinbase' != $_GET['edd-listener'] )
      return;


      if( ! $this->is_api_valid() )
      return;


      $body = @file_get_contents( 'php://input' );
      $data = json_decode( $body );

      if( empty( $data->event ) )
      return;


      $type = $data->event->type;
      $key = sanitize_text_field( $data->event->data->metadata->payment_key );
      $trans_id = $data->event->data->code;
      $payment_id = edd_get_purchase_id_by_key( $key );

      edd_debug_log( 'Coinbase webhook processing for payment ' . $payment_id );

      if( ! $payment_id )
      edd_debug_log( 'Coinbase webhook processing stopped because payment was not found from provided key' );
      die( 'no payment found' );


      if( 'charge:confirmed' !== $type )
      edd_debug_log( 'Coinbase webhook processing stopped because it is not a charge:confirmed type' );
      die( 'charge not completed' );


      if( ! edd_is_payment_complete( $payment_id ) ) 201 === wp_remote_retrieve_response_code( $request ) )

      edd_debug_log( 'Coinbase webhook charge retrieved successfully. Charge object: ' . var_export( $body, true ) );

      if( ! empty( $body->data->confirmed_at ) )

      edd_debug_log( 'Coinbase webhook processing for payment ' . $payment_id . ' successful, marking payment as complete' );
      edd_update_payment_status( $payment_id, 'publish' );



      else

      edd_debug_log( 'Coinbase webhook processing failed. Non 200/201 returned by Coinbase. Charge response: ' . var_export( $body, true ) );







      public function process_confirmation() 'coinbase' !== $_GET['gateway'] )
      return;


      if( ! edd_is_success_page()

      public function currencies( $currencies )

      $currencies['BTC'] = __( 'Bitcoin', 'edd-coinbase' );

      return $currencies;


      function btc_decimals( $decimals = 2 )
      global $edd_options;

      $currency = edd_get_currency();

      switch ( $currency )
      case 'BTC' :

      $decimals = 8;
      break;


      return $decimals;


      public function subsection( $sections )
      $sections['coinbase'] = __( 'Coinbase', 'edd-coinbase' );
      return $sections;


      public function settings( $settings )

      $coinbase_settings = array(
      array(
      'id' => 'edd_coinbase_header',
      'name' => '<strong>' . __( 'Coinbase', 'edd-coinbase' ) . '</strong>',
      'desc' => '',
      'type' => 'header',
      'size' => 'regular'
      ),
      array(
      'id' => 'edd_coinbase_checkout_label',
      'name' => __( 'Checkout Label', 'edd-coinbase' ),
      'desc' => __( 'Enter text you would like shown on checkout for customers. This is shown when selecting the payment method.' ),
      'type' => 'text',
      'std' => 'Bitcoin'
      ),
      array(
      'id' => 'edd_coinbase_api_key',
      'name' => __( 'API Key', 'edd-coinbase' ),
      'desc' => __( 'Enter your Coinbase API key' ),
      'type' => 'text'
      ),
      array(
      'id' => 'coinbase_webhook_description',
      'type' => 'descriptive_text',
      'name' => __( 'Webhooks', 'edd-coinbase' ),
      'desc' =>
      '<p>' . sprintf( __( 'In order for Coinbase to function completely, you must configure your webhooks. Visit your <a href="%s" target="_blank">account dashboard</a> to configure them. Please add a webhook endpoint for the URL below.', 'edd-coinbase' ), 'https://commerce.coinbase.com/dashboard/settings' ) . '</p>' .
      '<p><strong>' . sprintf( __( 'Webhook URL: %s', 'edds' ), home_url( 'index.php?edd-listener=coinbase' ) ) . '</strong></p>' .
      '<p>' . sprintf( __( 'See our <a href="%s">documentation</a> for more information.', 'edd-coinbase' ), 'https://docs.easydigitaldownloads.com/article/314-coinbase-payment-gateway-setup-documentation' ) . '</p>'
      ),
      );

      if ( version_compare( EDD_VERSION, 2.5, '>=' ) )
      $coinbase_settings = array( 'coinbase' => $coinbase_settings );



      return array_merge( $settings, $coinbase_settings );



      public function is_api_valid()

      if( empty( $this->api_key ) )
      return false;


      return true;




      function edd_coinbase_init()
      $coinbase = new EDD_Coinbase;

      add_action( 'plugins_loaded', 'edd_coinbase_init' );


      What I want is that when processing the payment does not leave the site but the entire payment process is done within the site.



      What I want is that when processing the payment does not leave the site but the entire payment process is done within the site as in the previous example of canadiancrypto.io.



      The solution to embed it in the button is used the following code:



      <div>
      <a class="donate-with-crypto"
      href="https://commerce.coinbase.com/checkout/6da189f179bc31">
      <span>Donate with Crypto</span>
      </a>
      <script src="https://commerce.coinbase.com/v1/checkout.js">
      </script>
      </div>


      but I have not managed to implement it to the gateway successfully thank you for your help thanks










      share|improve this question













      Hello I have a Wordpress site with EDD (Easy Digital Downloads) I have the Coinbase plugin for the Gateway, which works perfect, but has a problem when checking out the button redirects off the site and takes you to the Coinbase to process the I pay outside, which I find unprofessional.



      I want to checkout the product to be processed within my own site and not leave.



      I was looking for and found a solution that offers the Coinbase site that the "embedded" when trying to process the payment opens a "modal window" within the same site without leaving and proceeds to request payment without leaving it.



      The live example is here:



      https://canadiancrypto.io/accept-crypto-payments-with-coinbase-commerce/



      But the example is a button that when pressed, the action is executed and it is embedded in the same place.



      The code of the plugin to process the payment through the gateway is the following:



      <?php

      class EDD_Coinbase

      private $api_key;

      public function __construct()

      if( ! function_exists( 'edd_get_option' ) )
      return;


      if( class_exists( 'EDD_License' ) && is_admin() )
      $license = new EDD_License( __FILE__, 'Coinbase Payment Gateway', '1.1', 'Easy Digital Downloads' );


      $this->api_key = trim( edd_get_option( 'edd_coinbase_api_key', '' ) );

      add_action( 'init', array( $this, 'textdomain' ) );
      add_action( 'edd_gateway_coinbase', array( $this, 'process_payment' ) );
      add_action( 'init', array( $this, 'listener' ) );
      add_action( 'edd_coinbase_cc_form', '__return_false' );
      add_action( 'template_redirect', array( $this, 'process_confirmation' ) );

      add_filter( 'edd_payment_gateways', array( $this, 'register_gateway' ) );
      add_filter( 'edd_currencies', array( $this, 'currencies' ) );
      add_filter( 'edd_sanitize_amount_decimals', array( $this, 'btc_decimals' ) );
      add_filter( 'edd_format_amount_decimals', array( $this, 'btc_decimals' ) );
      add_filter( 'edd_settings_sections_gateways', array( $this, 'subsection' ), 10, 1 );
      add_filter( 'edd_settings_gateways', array( $this, 'settings' ) );



      public function textdomain()

      // Set filter for plugin's languages directory
      $lang_dir = dirname( plugin_basename( __FILE__ ) ) . '/languages/';
      $lang_dir = apply_filters( 'edd_coinbase_languages_directory', $lang_dir );

      // Traditional WordPress plugin locale filter
      $locale = apply_filters( 'plugin_locale', get_locale(), 'edd-coinbase' );
      $mofile = sprintf( '%1$s-%2$s.mo', 'edd-coinbase', $locale );

      // Setup paths to current locale file
      $mofile_local = $lang_dir . $mofile;
      $mofile_global = WP_LANG_DIR . '/edd-coinbase/' . $mofile;

      if ( file_exists( $mofile_global ) )
      load_textdomain( 'edd-coinbase', $mofile_global );
      elseif ( file_exists( $mofile_local ) )
      load_textdomain( 'edd-coinbase', $mofile_local );
      else
      // Load the default language files
      load_plugin_textdomain( 'edd-coinbase', false, $lang_dir );




      public function register_gateway( $gateways )

      $gateways['coinbase'] = array(
      'checkout_label' => edd_get_option( 'edd_coinbase_checkout_label', __( 'Bitcoin', 'edd-coinbase' ) ),
      'admin_label' => __( 'Coinbase', 'edd-coinbase' )
      );

      return $gateways;



      public function process_payment( $purchase_data )

      if( ! $this->is_api_valid() )
      edd_set_error( 'edd_coinbase_api_invalid', __( 'Please enter your Coinbase API key in Settings', 'edd-coinbase' ) );
      edd_send_back_to_checkout( '?payment-mode=coinbase' );


      $purchase_summary = '';
      if( is_array( $purchase_data['cart_details'] ) && ! empty( $purchase_data['cart_details'] ) )

      foreach( $purchase_data['cart_details'] as $item )
      $purchase_summary .= $item['name'];
      $price_id = isset( $item['item_number']['options']['price_id'] ) ? absint( $item['item_number']['options']['price_id'] ) : false;
      if ( false !== $price_id )
      $purchase_summary .= ' - ' . edd_get_price_option_name( $item['id'], $item['item_number']['options']['price_id'] );

      $purchase_summary .= ', ';


      $purchase_summary = rtrim( $purchase_summary, ', ' );



      $headers = array(
      'X-CC-Api-Key' => $this->api_key,
      'X-CC-Version' => '2018-03-22',
      'Content-Type' => 'application/json'
      );

      $args = array(
      'headers' => $headers,
      'body' => json_encode( array(
      'name' => get_bloginfo( 'name' ),
      'description' => $purchase_summary,
      'pricing_type' => 'fixed_price',
      'local_price' => array(
      'amount' => $purchase_data['price'],
      'currency' => edd_get_currency(),
      ),
      'metadata' => array(
      'payment_key' => $purchase_data['purchase_key'],
      'email' => $purchase_data['user_email']
      ),
      'redirect_url' => add_query_arg( array(
      'payment_key' => $purchase_data['purchase_key'],
      'gateway' => 'coinbase'
      ), edd_get_success_page_uri() )
      ) )
      );

      $request = wp_remote_post( 'https://api.commerce.coinbase.com/charges', $args );

      // echo '<pre>'; print_r( $request ); echo '</pre>'; exit;
      if( is_wp_error( $request ) )
      edd_set_error( $request->get_error_code(), $request->get_error_message() );
      edd_send_back_to_checkout( '?payment-mode=coinbase' );


      $body = json_decode( wp_remote_retrieve_body( $request ) );

      if( 200 !== wp_remote_retrieve_response_code( $request ) && 201 !== wp_remote_retrieve_response_code( $request ) )

      edd_set_error( $body->error->code, $body->error->message );
      edd_send_back_to_checkout( '?payment-mode=coinbase' );



      $payment_data = array(
      'price' => $purchase_data['price'],
      'date' => $purchase_data['date'],
      'user_email' => $purchase_data['user_email'],
      'purchase_key' => $purchase_data['purchase_key'],
      'currency' => edd_get_currency(),
      'downloads' => $purchase_data['downloads'],
      'cart_details' => $purchase_data['cart_details'],
      'user_info' => $purchase_data['user_info'],
      'status' => 'pending',
      'gateway' => 'coinbase'
      );

      // record the pending payment
      $payment_id = edd_insert_payment( $payment_data );
      if ( $payment_id )

      edd_set_payment_transaction_id( $payment_id, $body->data->code );
      edd_insert_payment_note( $payment_id, sprintf( __( 'Pending Coinbase charge created, code %s', 'edd-coinbase' ), $body->data->code ) );
      wp_redirect( $body->data->hosted_url ); exit;





      public function listener()

      if( empty( $_GET['edd-listener'] ) )
      return;


      if( 'coinbase' != $_GET['edd-listener'] )
      return;


      if( ! $this->is_api_valid() )
      return;


      $body = @file_get_contents( 'php://input' );
      $data = json_decode( $body );

      if( empty( $data->event ) )
      return;


      $type = $data->event->type;
      $key = sanitize_text_field( $data->event->data->metadata->payment_key );
      $trans_id = $data->event->data->code;
      $payment_id = edd_get_purchase_id_by_key( $key );

      edd_debug_log( 'Coinbase webhook processing for payment ' . $payment_id );

      if( ! $payment_id )
      edd_debug_log( 'Coinbase webhook processing stopped because payment was not found from provided key' );
      die( 'no payment found' );


      if( 'charge:confirmed' !== $type )
      edd_debug_log( 'Coinbase webhook processing stopped because it is not a charge:confirmed type' );
      die( 'charge not completed' );


      if( ! edd_is_payment_complete( $payment_id ) ) 201 === wp_remote_retrieve_response_code( $request ) )

      edd_debug_log( 'Coinbase webhook charge retrieved successfully. Charge object: ' . var_export( $body, true ) );

      if( ! empty( $body->data->confirmed_at ) )

      edd_debug_log( 'Coinbase webhook processing for payment ' . $payment_id . ' successful, marking payment as complete' );
      edd_update_payment_status( $payment_id, 'publish' );



      else

      edd_debug_log( 'Coinbase webhook processing failed. Non 200/201 returned by Coinbase. Charge response: ' . var_export( $body, true ) );







      public function process_confirmation() 'coinbase' !== $_GET['gateway'] )
      return;


      if( ! edd_is_success_page()

      public function currencies( $currencies )

      $currencies['BTC'] = __( 'Bitcoin', 'edd-coinbase' );

      return $currencies;


      function btc_decimals( $decimals = 2 )
      global $edd_options;

      $currency = edd_get_currency();

      switch ( $currency )
      case 'BTC' :

      $decimals = 8;
      break;


      return $decimals;


      public function subsection( $sections )
      $sections['coinbase'] = __( 'Coinbase', 'edd-coinbase' );
      return $sections;


      public function settings( $settings )

      $coinbase_settings = array(
      array(
      'id' => 'edd_coinbase_header',
      'name' => '<strong>' . __( 'Coinbase', 'edd-coinbase' ) . '</strong>',
      'desc' => '',
      'type' => 'header',
      'size' => 'regular'
      ),
      array(
      'id' => 'edd_coinbase_checkout_label',
      'name' => __( 'Checkout Label', 'edd-coinbase' ),
      'desc' => __( 'Enter text you would like shown on checkout for customers. This is shown when selecting the payment method.' ),
      'type' => 'text',
      'std' => 'Bitcoin'
      ),
      array(
      'id' => 'edd_coinbase_api_key',
      'name' => __( 'API Key', 'edd-coinbase' ),
      'desc' => __( 'Enter your Coinbase API key' ),
      'type' => 'text'
      ),
      array(
      'id' => 'coinbase_webhook_description',
      'type' => 'descriptive_text',
      'name' => __( 'Webhooks', 'edd-coinbase' ),
      'desc' =>
      '<p>' . sprintf( __( 'In order for Coinbase to function completely, you must configure your webhooks. Visit your <a href="%s" target="_blank">account dashboard</a> to configure them. Please add a webhook endpoint for the URL below.', 'edd-coinbase' ), 'https://commerce.coinbase.com/dashboard/settings' ) . '</p>' .
      '<p><strong>' . sprintf( __( 'Webhook URL: %s', 'edds' ), home_url( 'index.php?edd-listener=coinbase' ) ) . '</strong></p>' .
      '<p>' . sprintf( __( 'See our <a href="%s">documentation</a> for more information.', 'edd-coinbase' ), 'https://docs.easydigitaldownloads.com/article/314-coinbase-payment-gateway-setup-documentation' ) . '</p>'
      ),
      );

      if ( version_compare( EDD_VERSION, 2.5, '>=' ) )
      $coinbase_settings = array( 'coinbase' => $coinbase_settings );



      return array_merge( $settings, $coinbase_settings );



      public function is_api_valid()

      if( empty( $this->api_key ) )
      return false;


      return true;




      function edd_coinbase_init()
      $coinbase = new EDD_Coinbase;

      add_action( 'plugins_loaded', 'edd_coinbase_init' );


      What I want is that when processing the payment does not leave the site but the entire payment process is done within the site.



      What I want is that when processing the payment does not leave the site but the entire payment process is done within the site as in the previous example of canadiancrypto.io.



      The solution to embed it in the button is used the following code:



      <div>
      <a class="donate-with-crypto"
      href="https://commerce.coinbase.com/checkout/6da189f179bc31">
      <span>Donate with Crypto</span>
      </a>
      <script src="https://commerce.coinbase.com/v1/checkout.js">
      </script>
      </div>


      but I have not managed to implement it to the gateway successfully thank you for your help thanks







      php wordpress






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 12 at 7:04









      ichigocouk

      11




      11



























          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53257295%2fembbed-a-coinbase-edd-plugin%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53257295%2fembbed-a-coinbase-edd-plugin%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          How to read a connectionString WITH PROVIDER in .NET Core?

          In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

          Museum of Modern and Contemporary Art of Trento and Rovereto