commit ab16377d1e065730f03fa2763a1759bbc7537a72
parent 4394ee27a757b986e1d5ab457444e42339d2585e
Author: NicholasFarrow <nicholas.w.farrow@gmail.com>
Date: Fri, 15 Jan 2021 13:40:00 +1100
Functioning woocommerce plugin and separated donation form method
Diffstat:
4 files changed, 350 insertions(+), 284 deletions(-)
diff --git a/gateways/btcpyment.php b/gateways/btcpyment.php
@@ -1,273 +0,0 @@
-<?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
- *
-*/
-
-/* Based on https://rudrastyh.com/woocommerce/payment-gateway-plugin.html */
-
-/*
- * This action hook registers our PHP class as a WooCommerce payment gateway
- */
-
- if (!function_exists('write_log')) {
- function write_log($log) {
- if (true === WP_DEBUG) {
- if (is_array($log) || is_object($log)) {
- error_log(print_r($log, true));
- } else {
- error_log($log);
- }
- }
- }
-
- }
-
-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->btcpyment_server_url = $this->get_option( 'btcpyment_server_url' );
- $this->redirect_url = $this->get_option( 'redirect_url' );
- $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->callback_URL = str_replace( 'https:', 'http:', add_query_arg( 'wc-api', 'wc_btcpyment_gateway', home_url( '/' ) ) );
- // $this->callback_URL = home_url( '/' ) . 'wc-api/' . 'WC_Btcpyment_Gateway/';
-
- // 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_scri6pts', array( $this, 'payment_scripts' ) );
-
- // You can also register a webhook here
- add_action( 'woocommerce_api_wc_btcpyment_gateway', 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',
- ),
- 'btcpyment_server_url' => array(
- 'title' => 'BTCPyment URL',
- 'type' => 'text',
- 'description' => 'Points towards your instance of BTCPyment, should be IP or https://SERVER.com'
- ),
- 'redirect_url' => array(
- 'title' => 'Redirect URL',
- 'type' => 'text',
- 'description' => 'URL the user is redirected to after payment.'
- ),
- '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' );
- //
- // // 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(
- 'amount' => $order->get_total(),
- 'id' => $order->get_id(),
- 'webhook_url' => $this->callback_URL );
- // HASH??? FOR SECURE PAYMENTS?
-
- /*
- * Your API interaction could be built with wp_remote_post()
- */
- $payment_url = add_query_arg(
- $args,
- $this->btcpyment_server_url . "/pay"
- );
-
- return [
- 'result' => 'success',
- 'redirect' => $payment_url
- ];
-
- // 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() {
- header( 'HTTP/1.1 200 OK' );
- $order = wc_get_order( $_GET['id'] );
- write_log($_GET['id'])
- // $order->payment_complete();
- // $order->reduce_order_stock();
-
- update_option('webhook_debug', $_GET);
- }
- }
-}
diff --git a/gateways/woo_btcpyment.php b/gateways/woo_btcpyment.php
@@ -0,0 +1,234 @@
+<?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
+ *
+*/
+
+/* Based.
+* Based on https://rudrastyh.com/woocommerce/payment-gateway-plugin.html */
+
+/*
+ * This action hook registers our PHP class as a WooCommerce payment gateway
+ */
+
+//Debugging helper
+ if (!function_exists('write_log')) {
+ function write_log($log) {
+ if (true === WP_DEBUG) {
+ if (is_array($log) || is_object($log)) {
+ error_log(print_r($log, true));
+ } else {
+ error_log($log);
+ }
+ }
+ }
+ }
+
+// BTCPyment class
+add_filter( 'woocommerce_payment_gateways', 'btcpyment_add_gateway_class' );
+function btcpyment_add_gateway_class( $gateways ) {
+ $gateways[] = 'WC_Btcpyment_Gateway';
+ return $gateways;
+}
+
+// Extend existing payment gateway
+add_action( 'plugins_loaded', 'btcpyment_init_gateway_class' );
+function btcpyment_init_gateway_class() {
+
+ class WC_Btcpyment_Gateway extends WC_Payment_Gateway {
+
+ /**
+ * Class constructor
+ */
+ 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
+
+ $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->btcpyment_server_url = $this->get_option( 'btcpyment_server_url' );
+ $this->redirect_url = $this->get_option( 'redirect_url' );
+ $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->callback_URL = str_replace( 'https:', 'http:', add_query_arg( 'wc-api', 'wc_btcpyment_gateway', home_url( '/' ) ) );
+ // $this->callback_URL = home_url( '/' ) . 'wc-api/' . 'WC_Btcpyment_Gateway/';
+
+ // 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_scri6pts', array( $this, 'payment_scripts' ) );
+
+ // You can also register a webhook here
+ add_action( 'woocommerce_api_wc_btcpyment_gateway', array( $this, 'webhook' ) );
+ }
+
+ /**
+ * Plugin options
+ */
+ 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',
+ ),
+ 'btcpyment_server_url' => array(
+ 'title' => 'BTCPyment URL',
+ 'type' => 'text',
+ 'description' => 'Points towards your instance of BTCPyment, should be IP or https://SERVER.com'
+ ),
+ 'redirect_url' => array(
+ 'title' => 'Redirect URL',
+ 'type' => 'text',
+ 'description' => 'URL the user is redirected to after payment.'
+ ),
+ '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' );
+ //
+ // // 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' );
+
+ }
+
+
+ /*
+ * Processing the payments
+ */
+ 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(
+ 'amount' => $order->get_total(),
+ 'id' => $order->get_id(),
+ 'w_url' => $this->callback_URL );
+ // HASH??? FOR SECURE PAYMENTS?
+
+ $payment_url = add_query_arg(
+ $args,
+ $this->btcpyment_server_url . "/pay"
+ );
+
+ // Redirect to BTCPyment
+ return [
+ 'result' => 'success',
+ 'redirect' => $payment_url
+ ];
+ }
+
+ /*
+ * Webhook to confirm payment
+ */
+ public function webhook() {
+ header( 'HTTP/1.1 200 OK' );
+ $order = wc_get_order( $_GET['id'] );
+ $order->payment_complete();
+ $order->reduce_order_stock();
+
+ update_option('webhook_debug', $_GET);
+ }
+ }
+}
diff --git a/templates/donate.html b/templates/donate.html
@@ -0,0 +1,116 @@
+<!DOCTYPE HTML>
+<html>
+<head>
+ <title>BTCPyment</title>
+ <link rel="shortcut icon" href="{{ url_for('static', filename='favicon.ico') }}">
+
+ <script src="//code.jquery.com/jquery-1.12.4.min.js"></script>
+ <script src="//cdnjs.cloudflare.com/ajax/libs/socket.io/3.0.4/socket.io.js"></script>
+ <!-- <script src="{{ url_for('static', filename='server_connection.js') }}"></script> -->
+
+ <style>
+ html, body {
+ display: flex;
+ /* height:auto;
+ width:260px; */
+ }
+ body{
+ background-color: inherit;
+ overflow: hidden;
+ }
+ p {
+ /* display: inline-block; */
+ overflow-wrap: break-word;
+ word-wrap: break-word;
+ margin: 0;
+ padding: 5px;
+ }
+ h1 {
+ font-size: 2em;
+ }
+ #paybox {
+ background-color: #f7931a;
+ border-radius: 25px;
+ padding-left: 30px;
+ padding-right: 30px;
+ padding-top: 10px;
+ padding-bottom: 10px;
+ font-size: 16px
+
+ /* height:auto; */
+ height: 275px;
+ width:220px;
+ }
+ #row {
+ display: flex;
+ }
+ #left {
+ width: 60%;
+ padding: 0 10px 0 0;
+ float: left;
+ }
+ #right {
+ width: 40%;
+ padding: 0 10px 0 0;
+ float: right;
+ }
+ #address {
+ overflow-wrap: break-word;
+ text-align: right;
+ }
+ #about {
+ color:#000;
+ }
+ .qr {
+ transition: transform .2s;
+ object-fit: cover;
+ }
+ .qr:hover {
+ -ms-transform: scale(2.5); /* IE 9 */
+ -webkit-transform: scale(2.5); /* Safari 3-8 */
+ transform: scale(3);
+ }
+ </style>
+
+</head>
+
+
+<body>
+ <div id="paybox">
+ <div id="row" height="50px">
+ <div id="left" style="display:inline-block;" height="75px">
+ <h1>Donate Bitcoin</h>
+ </div>
+ <div id="right">
+ <a id="logo" target="_blank"><img id="logo" style="padding-top:25px;padding-left:30px;" width="65px" src="https://i.pinimg.com/originals/3a/dd/40/3add4023fa9b435e7da3c09156b88015.png"></a>
+ <a id="qrClick" target="_blank"><img class="qr" id="qrImage" style="padding-top:25px;display:none" width="65px" src="https://i.pinimg.com/originals/3a/dd/40/3add4023fa9b435e7da3c09156b88015.png"></a>
+ </div>
+ </div>
+
+ <div id="paymentForm">
+ <center>
+ <form id="pay" action='/pay' style="margin:0;padding0;">
+ <h2 style="margin:0;padding0;">Amount:
+ <input style="display:inline" size="4" type="float" name="amount" id="amount" placeholder="USD">
+ </h2>
+ <br>
+ <input style="width:100%" type="submit" value="Pay" onclick="hideAmountShowPayment()">
+ </form>
+ </center>
+ </div>
+
+ <!-- <div id="paymentDetails" style="display:none; padding: 0;">
+ <p style="padding:0;">Send: <b><span id="amount"></span></b> BTC</p>
+ <p style="padding:0;">To: </p><b><p id="address" onclick="copyTextFromElement('address')"></p></b>
+ <p style="padding:0;"><span id="status"></span></p>
+ <p id="timerContainer" style="padding:0;"><span id="timer"></span> seconds remaining.</p>
+ </div>
+
+ <div id="information" style="text-align: right; padding: 0;">
+ <small><a id="about" href="https://github.com/nickfarrow/BTCPyment" target="_blank">BTCPyment</a></small>
+ </div> -->
+
+ <div id="paybutton"></div>
+ </div>
+</body>
+</html>
diff --git a/templates/index.html b/templates/index.html
@@ -95,17 +95,6 @@
</div>
</div>
- <!-- <div id="paymentForm">
- <center>
- <form id="pay" method="POST" action='#' style="margin:0;padding0;">
- <h2 style="margin:0;padding0;">Amount:
- <input style="display:inline" size="4" type="float" name="pay_data" id="pay_data" placeholder="USD">
- </h2>
- <br>
- <input style="width:100%" type="submit" value="Pay" onclick="hideAmountShowPayment()">
- </form>
- </center>
- </div> -->
<div id="paymentDetails" style="display:block; padding: 0;">
<p style="padding:0;">Send: <b><span id="amount"></span></b> BTC</p>