SatSale

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit f50639e88be1e97180c26cab38b4c17f071e8e3a
parent 60a3b30547de4827cb4821dcf03a350b8a9ee627
Author: NicholasFarrow <nicholas.w.farrow@gmail.com>
Date:   Thu, 14 Jan 2021 12:34:20 +1100

Initial woocommerce plugin upload

Diffstat:
Agateways/btcpyment.js | 35+++++++++++++++++++++++++++++++++++
Agateways/btcpyment.php | 233+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 268 insertions(+), 0 deletions(-)

diff --git a/gateways/btcpyment.js b/gateways/btcpyment.js @@ -0,0 +1,35 @@ +var successCallback = function(data) { + + var checkout_form = $( 'form.woocommerce-checkout' ); + + // add a token to our hidden input field + // console.log(data) to find the token + checkout_form.find('#btcpyment_token').val(data.token); + + // deactivate the tokenRequest function event + checkout_form.off( 'checkout_place_order', tokenRequest ); + + // submit the form now + checkout_form.submit(); + +}; + +var errorCallback = function(data) { + console.log(data); +}; + +var tokenRequest = function() { + + // here will be a payment gateway function that process all the card data from your form, + // maybe it will need your Publishable API key which is misha_params.publishableKey + // and fires successCallback() on success and errorCallback on failure + return false; + +}; + +jQuery(function($){ + + var checkout_form = $( 'form.woocommerce-checkout' ); + checkout_form.on( 'checkout_place_order', tokenRequest ); + +}); diff --git a/gateways/btcpyment.php b/gateways/btcpyment.php @@ -0,0 +1,233 @@ +<?php +/* + * Plugin Name: BTCPyment + * Plugin URI: https://github.com/nickfarrow/BTCPyment + * Description: Take credit card payments on your store. + * Author: Nick Farrow + * Author URI: https://nickfarrow.com + * Version: 1.0.1 + * + +/* + * This action hook registers our PHP class as a WooCommerce payment gateway + */ +add_filter( 'woocommerce_payment_gateways', 'btcpyment_add_gateway_class' ); +function btcpyment_add_gateway_class( $gateways ) { + $gateways[] = 'WC_btcpyment_Gateway'; // your class name is here + return $gateways; +} + +/* + * The class itself, please note that it is inside plugins_loaded action hook + */ +add_action( 'plugins_loaded', 'btcpyment_init_gateway_class' ); +function btcpyment_init_gateway_class() { + + class WC_btcpyment_Gateway extends WC_Payment_Gateway { + + /** + * Class constructor, more about it in Step 3 + */ + public function __construct() { + + $this->id = 'btcpyment'; // payment gateway plugin ID + $this->icon = ''; // URL of the icon that will be displayed on checkout page near your gateway name + $this->has_fields = true; // in case you need a custom credit card form + $this->method_title = 'btcpyment Gateway'; + $this->method_description = 'Description of btcpyment payment gateway'; // will be displayed on the options page + + // gateways can support subscriptions, refunds, saved payment methods, + // but in this tutorial we begin with simple payments + $this->supports = array( + 'products' + ); + + // Method with all the options fields + $this->init_form_fields(); + + // Load the settings. + $this->init_settings(); + $this->title = $this->get_option( 'title' ); + $this->description = $this->get_option( 'description' ); + $this->enabled = $this->get_option( 'enabled' ); + $this->testmode = 'yes' === $this->get_option( 'testmode' ); + $this->private_key = $this->testmode ? $this->get_option( 'test_private_key' ) : $this->get_option( 'private_key' ); + $this->publishable_key = $this->testmode ? $this->get_option( 'test_publishable_key' ) : $this->get_option( 'publishable_key' ); + + // This action hook saves the settings + add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) ); + + // We need custom JavaScript to obtain a token + add_action( 'wp_enqueue_scripts', array( $this, 'payment_scripts' ) ); + + // You can also register a webhook here + // add_action( 'woocommerce_api_BTCPyment', array( $this, 'webhook' ) ); + } + + /** + * Plugin options, we deal with it in Step 3 too + */ + public function init_form_fields(){ + + $this->form_fields = array( + 'enabled' => array( + 'title' => 'Enable/Disable', + 'label' => 'Enable btcpyment Gateway', + 'type' => 'checkbox', + 'description' => '', + 'default' => 'no' + ), + 'title' => array( + 'title' => 'Title', + 'type' => 'text', + 'description' => 'This controls the title which the user sees during checkout.', + 'default' => 'Bitcoin', + 'desc_tip' => true, + ), + 'description' => array( + 'title' => 'Description', + 'type' => 'textarea', + 'description' => 'This controls the description which the user sees during checkout.', + 'default' => 'Pay with Bitcoin via BTCPyment', + ), + 'testmode' => array( + 'title' => 'Test mode', + 'label' => 'Enable Test Mode', + 'type' => 'checkbox', + 'description' => 'Place the payment gateway in test mode using test API keys.', + 'default' => 'yes', + 'desc_tip' => true, + ), + 'test_publishable_key' => array( + 'title' => 'Test Publishable Key', + 'type' => 'text' + ), + 'test_private_key' => array( + 'title' => 'Test Private Key', + 'type' => 'password', + ), + 'publishable_key' => array( + 'title' => 'Live Publishable Key', + 'type' => 'text' + ), + 'private_key' => array( + 'title' => 'Live Private Key', + 'type' => 'password' + ) + ); + } + + + /* + * Custom CSS and JS, in most cases required only when you decided to go with a custom credit card form + */ + public function payment_scripts() { + + // we need JavaScript to process a token only on cart/checkout pages, right? + if ( ! is_cart() && ! is_checkout() && ! isset( $_GET['pay_for_order'] ) ) { + return; + } + + // if our payment gateway is disabled, we do not have to enqueue JS too + if ( 'no' === $this->enabled ) { + return; + } + + // no reason to enqueue JavaScript if API keys are not set + if ( empty( $this->private_key ) || empty( $this->publishable_key ) ) { + return; + } + + // do not work with card detailes without SSL unless your website is in a test mode + if ( ! $this->testmode && ! is_ssl() ) { + return; + } + + // let's suppose it is our payment processor JavaScript that allows to obtain a token + wp_enqueue_script( 'btcpyment_js', 'https://btcpyment.nickfarrow.com/pay.js' ); + + // and this is our custom JS in your plugin directory that works with token.js + wp_register_script( 'woocommerce_btcpyment', plugins_url( 'btcpyment.js', __FILE__ ), array( 'jquery', 'btcpyment_js' ) ); + + // in most payment processors you have to use PUBLIC KEY to obtain a token + wp_localize_script( 'woocommerce_btcpyment', 'btcpyment_params', array( + 'publishableKey' => $this->publishable_key + ) ); + + wp_enqueue_script( 'woocommerce_btcpyment' ); + + } + + + /* + * We're processing the payments here, everything about it is in Step 5 + */ + public function process_payment( $order_id ) { + + global $woocommerce; + + // we need it to get any order detailes + $order = wc_get_order( $order_id ); + + + /* + * Array with parameters for API interaction + */ + $args = array( + bogus => 2 + ); + + /* + * Your API interaction could be built with wp_remote_post() + */ + $response = wp_remote_post( '{payment processor endpoint}', $args ); + + + if( !is_wp_error( $response ) ) { + + $body = json_decode( $response['body'], true ); + + // it could be different depending on your payment processor + if ( $body['response']['responseCode'] == 'APPROVED' ) { + + // we received the payment + $order->payment_complete(); + $order->reduce_order_stock(); + + // some notes to customer (replace true with false to make it private) + $order->add_order_note( 'Hey, your order is paid! Thank you!', true ); + + // Empty cart + $woocommerce->cart->empty_cart(); + + // Redirect to the thank you page + return array( + 'result' => 'success', + 'redirect' => $this->get_return_url( $order ) + ); + + } else { + wc_add_notice( 'Please try again.', 'error' ); + return; + } + + } else { + wc_add_notice( 'Connection error.', 'error' ); + return; + } + + } + + /* + * In case you need a webhook, like PayPal IPN etc + */ + public function webhook() { + + $order = wc_get_order( $_GET['id'] ); + $order->payment_complete(); + $order->reduce_order_stock(); + + update_option('webhook_debug', $_GET); + } + } +}