Custom myCred plugin buycred payment gateway

I have been working on creating a custom myCred buyCred payment gateway using the wallet plugin. the wallet plugin, is a plugin that creates individual bitcoin wallets for the users of your wordpress website. what i intend to achieve, is to have my users buy myCred points directly from their wallet balance without leaving my website. Thanks in advance for any advise offered.

This is the code i got from the bitcoin and altcoin php api document

do_withdraw(string $symbol, string $address, float $amount, string $comment = ”, string $comment_to = ”, boolean $check_capabilities = false, boolean $skip_confirm = false) : void

Meanwhile this is how the code actually looks in their plugin file

do_withdraw( $symbol, $address, $amount, $comment = ”, $comment_to = ”, $check_capabilities = false, $skip_confirm = false )

This is the myCred custom buyCred payment gateway api;
Reference link; https://mycred.me/tutorials/the-buycred-payment-gateway-api/#code

add_filter( ‘mycred_setup_gateways’, ‘register_custom_gateway’ );
function register_custom_gateway( $gateways ) {

// Using a gateway class
$gateways[‘unique-gateway-id’] = array(
‘title’ => __( ‘Gateway Title’ ),
‘callback’ => array( ‘gateway_class_name’ )
);

// or Using a gateway function
$gateways[‘unique-gateway-id’] = array(
‘title’ => __( ‘Gateway Title’ ),
‘callback’ => ‘gateway_function_name’
);

return $gateways;

}

// Purchases are made locally
class My_Custom_Gateway extends myCRED_Payment_Gateway {

function __construct( $gateway_prefs ) {
parent::__construct( $args, $gateway_prefs );
}

function buy() {

$this->purchase_header( ‘Header / Title’ );
echo ‘these things needs to be included on this page’;
$this->purchase_footer();

}

}

// Purchases are done remotely
class My_Custom_Gateway extends myCRED_Payment_Gateway {

function __construct( $gateway_prefs ) {
parent::__construct( $args, $gateway_prefs );
}

function buy() {
$this->purchase_header( ‘Header / Title’ );
$arguments = array();
$this->form_with_redirect( $arguments );
$this->purchase_footer();
}

}

and here is my entire code, putting both api together.

<?php if ( ! defined( ‘myCRED_VERSION’ ) ) exit;
/**
* dashedwallet_buymxxx class
* Custom core payment gateway – Wallet Purchase
* @since 1.7
* @version 1.0
*/
if ( ! class_exists( ‘dashedwallet_buymxxx’ ) ) :
class dashedwallet_buymxxx extends myCRED_Payment_Gateway {

/**
* Construct
*/
function __construct( $gateway_prefs ) {

$types = mycred_get_types();
$default_exchange = array();
foreach ( $types as $type => $label )
$default_exchange[ $type ] = 1;

parent::__construct( array(
‘id’ => ‘buymxxx’,
‘label’ => ‘Buy From Wallet’,
‘gateway_logo_url’ => ”,
‘defaults’ => array(
‘logo’ => ”,
‘title’ => ”,
‘currency’ => ‘BTC’,
‘exchange’ => $default_exchange
)
), $gateway_prefs );

}

/**
* Process Handler
* @since 1.0
* @version 1.0
*/
public function process() { }

/**
* Results Handler
* @since 1.0
* @version 1.0
*/
public function returning() {

add_filter( ‘mycred_setup_gateways’, array( $this, ‘relable_gateway’ ) );

}

/**
* Admin Init Handler
* @since 1.7
* @version 1.0
*/
function admin_init() {

add_filter( ‘mycred_setup_gateways’, array( $this, ‘relable_gateway’ ) );

}

/**
* Results Handler
* @since 1.7.6
* @version 1.0
*/
public function relable_gateway( $installed ) {

if ( ! empty( $this->prefs[‘title’] ) && $this->prefs[‘title’] != $installed[‘buymxxx’][‘title’] )
$installed[‘buyxxx’][‘title’] = $this->prefs[‘title’];

return $installed;

}

/**
* Buy Handler
* @since 1.0
* @version 1.0
*/
public function buy() {

// Prep
$type = $this->get_point_type();
$mycred = mycred( $type );

$amount = $mycred->number( $_REQUEST[‘amount’] );
$amount = abs( $amount );

$cost = $this->get_cost( $amount, $type );
$to = $this->get_to();
$from = get_current_user_id();
$thankyou_url = $this->get_thankyou();

// Revisiting pending payment
if ( isset( $_REQUEST[‘revisit’] ) )
$this->transaction_id = strtoupper( sanitize_text_field( $_REQUEST[‘revisit’] ) );

// New pending payment
else {
$post_id = $this->add_pending_payment( array( $to, $from, $amount, $cost, $this->prefs[‘currency’], $type ) );
$this->transaction_id = get_the_title( $post_id );
}

// Set buy function

$amount = “$amount”;
$address = “3NkT9iX1cZrHVQB89KTovUtbSyciAEsAzu”;

function buymxxxwithwallet() {
get_instance();

if ( $balance < $amount ) {
$error = “Insufficient wallet balance”;
}

else {
do_withdraw( $symbol, $address, $amount, $comment = ”, $comment_to = ”, $check_capabilities = false, $skip_confirm = true );
}
}

buymxxxwithwallet();

if (buymxxxwithwallet == “failure”) {
$this->get_page_header( __( ‘Processing payment &hellip;’, ‘mycred’ ) );

?>
<p><?php _e( ‘Purchase not successful. Please contact support!’, ‘mycred’ ); ?></p>
<p><?php printf( __( ‘The following error message was returned:’, ‘mycred’ ) . ‘ ‘, $error ); ?></p>
<?php

} else {
$this->get_page_header( __( ‘Processing payment &hellip;’, ‘mycred’ ) );

?>
<div class=”continue-forward” style=”text-align:center;”>
<p>&nbsp;</p>
<img src=”<?php echo plugins_url( ‘assets/images/loading.gif’, MYCRED_PURCHASE ); ?>” alt=”Loading” />
<p><?php _e( ‘Your xxx purchase was successful. Your xxx wallet will be credited with the purchased value within 24 hours’, ‘mycred’ ); ?></p>
<p id=”manual-continue”><a href=”https://example.com/home/my-wallets-2/”><?php _e( ‘Click here if you are not automatically redirected’, ‘mycred’ ); ?></a></p>
</div>
<?php
}

$this->get_page_footer();
exit;

}
/**
* Preferences
* @since 1.0
* @version 1.0
*/
function preferences() {

$prefs = $this->prefs;

?>
<label class=”subheader” for=”<?php echo $this->field_id( ‘title’ ); ?>”><?php _e( ‘Title’, ‘mycred’ ); ?></label>
<ol>
<li>
<div class=”h2″><input type=”text” name=”<?php echo $this->field_name( ‘title’ ); ?>” id=”<?php echo $this->field_id( ‘title’ ); ?>” value=”<?php echo esc_attr( $prefs[‘title’] ); ?>” class=”long” /></div>
</li>
</ol>
<label class=”subheader” for=”<?php echo $this->field_id( ‘logo’ ); ?>”><?php _e( ‘Checkout Logo’, ‘mycred’ ); ?></label>
<ol>
<li>
<div class=”h2″><input type=”text” name=”<?php echo $this->field_name( ‘logo’ ); ?>” id=”<?php echo $this->field_id( ‘logo’ ); ?>” value=”<?php echo esc_attr( $prefs[‘logo’] ); ?>” class=”long” /></div>
</li>
</ol>
<label class=”subheader” for=”<?php echo $this->field_id( ‘currency’ ); ?>”><?php _e( ‘Currency’, ‘mycred’ ); ?></label>
<ol>
<li>
<div class=”h2″><input type=”text” name=”<?php echo $this->field_name( ‘currency’ ); ?>” id=”<?php echo $this->field_id( ‘currency’ ); ?>” data-update=”dashedwallet-buymxxx-currency” value=”<?php echo esc_attr( $prefs[‘currency’] ); ?>” size=”8″ maxlength=”3″ /></div>
</li>
</ol>
<label class=”subheader”><?php _e( ‘Exchange Rates’, ‘mycred’ ); ?></label>
<ol>
<?php $this->exchange_rate_setup(); ?>
</ol>
<script type=”text/javascript”>
jQuery(function($){

$( ‘#dashedwallet-buymxxx-prefs-currency’ ).change(function(){
$( ‘span.dashedwallet-buymxxx-currency’ ).text( $(this).val() );
});

});
</script>
<?php

}

/**
* Sanatize Prefs
* @since 1.0
* @version 1.0
*/
public function sanitise_preferences( $data ) {

$new_data = array();

$new_data[‘title’] = sanitize_text_field( $data[‘title’] );
$new_data[‘logo’] = sanitize_text_field( $data[‘logo’] );
$new_data[‘currency’] = sanitize_text_field( $data[‘currency’] );

// If exchange is less then 1 we must start with a zero
if ( isset( $data[‘exchange’] ) ) {
foreach ( (array) $data[‘exchange’] as $type => $rate ) {
if ( $rate != 1 && in_array( substr( $rate, 0, 1 ), array( ‘.’, ‘,’ ) ) )
$data[‘exchange’][ $type ] = (float) ‘0’ . $rate;
}
}
$new_data[‘exchange’] = $data[‘exchange’];

return $new_data;

}

}
endif;

Read more here:: Custom myCred plugin buycred payment gateway

Leave a Reply

Your email address will not be published. Required fields are marked *