|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * WooCommerce Dev Helper |
| 4 | + * |
| 5 | + * This source file is subject to the GNU General Public License v3.0 |
| 6 | + * that is bundled with this package in the file license.txt. |
| 7 | + * It is also available through the world-wide-web at this URL: |
| 8 | + * http://www.gnu.org/licenses/gpl-3.0.html |
| 9 | + * If you did not receive a copy of the license and are unable to |
| 10 | + * obtain it through the world-wide-web, please send an email |
| 11 | + * to license@skyverge.com so we can send you a copy immediately. |
| 12 | + * |
| 13 | + * @package WC-Dev-Helper/Classes |
| 14 | + * @author SkyVerge |
| 15 | + * @copyright Copyright (c) 2015-2017, SkyVerge, Inc. |
| 16 | + * @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0 |
| 17 | + */ |
| 18 | + |
| 19 | +defined( 'ABSPATH' ) or exit; |
| 20 | + |
| 21 | +/** |
| 22 | + * Adds a testing gateway that calls the WooCommerce payment_complete() method. |
| 23 | + * |
| 24 | + * @since 0.6.0 |
| 25 | + */ |
| 26 | + |
| 27 | +class WC_Bogus_Gateway extends WC_Payment_Gateway { |
| 28 | + |
| 29 | + /** |
| 30 | + * Constructor for the gateway. |
| 31 | + * |
| 32 | + * @since 0.6.0 |
| 33 | + */ |
| 34 | + public function __construct() { |
| 35 | + |
| 36 | + $this->id = 'bogus_gateway'; |
| 37 | + $this->icon = apply_filters('woocommerce_bogus_icon', ''); |
| 38 | + $this->has_fields = false; |
| 39 | + $this->method_title = __( 'Bogus', 'woocommerce-dev-helper' ); |
| 40 | + $this->method_description = __( 'A testing gateway that calls "payment complete" to simulate credit card transactions.', 'woocommerce-dev-helper' ); |
| 41 | + |
| 42 | + // Load the settings. |
| 43 | + $this->init_form_fields(); |
| 44 | + $this->init_settings(); |
| 45 | + |
| 46 | + // Define user set variables |
| 47 | + $this->title = $this->get_option( 'title' ); |
| 48 | + $this->description = $this->get_option( 'description' ); |
| 49 | + |
| 50 | + // Actions |
| 51 | + add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) ); |
| 52 | + } |
| 53 | + |
| 54 | + |
| 55 | + /** |
| 56 | + * Initialize gateway settings form fields. |
| 57 | + * |
| 58 | + * @since 0.6.0 |
| 59 | + */ |
| 60 | + public function init_form_fields() { |
| 61 | + |
| 62 | + $this->form_fields = apply_filters( 'wc_offline_form_fields', array( |
| 63 | + |
| 64 | + 'enabled' => array( |
| 65 | + 'title' => __( 'Enable/Disable', 'woocommerce-dev-helper' ), |
| 66 | + 'type' => 'checkbox', |
| 67 | + 'label' => __( 'Enable Bogus Gateway', 'woocommerce-dev-helper' ), |
| 68 | + 'default' => 'yes' |
| 69 | + ), |
| 70 | + |
| 71 | + 'title' => array( |
| 72 | + 'title' => __( 'Title', 'woocommerce-dev-helper' ), |
| 73 | + 'type' => 'text', |
| 74 | + 'description' => __( 'This controls the title for the payment method the customer sees during checkout.', 'woocommerce-dev-helper' ), |
| 75 | + 'default' => __( 'Bogus (Test)', 'woocommerce-dev-helper' ), |
| 76 | + 'desc_tip' => true, |
| 77 | + ), |
| 78 | + |
| 79 | + 'description' => array( |
| 80 | + 'title' => __( 'Description', 'woocommerce-dev-helper' ), |
| 81 | + 'type' => 'textarea', |
| 82 | + 'description' => __( 'Payment method description that the customer will see on your checkout.', 'woocommerce-dev-helper' ), |
| 83 | + 'default' => __( 'Nothingtodohere 🚀', 'woocommerce-dev-helper' ), |
| 84 | + 'desc_tip' => true, |
| 85 | + ), |
| 86 | + |
| 87 | + ) ); |
| 88 | + } |
| 89 | + |
| 90 | + |
| 91 | + /** |
| 92 | + * Process the payment and return the result. |
| 93 | + * |
| 94 | + * @since 0.6.0 |
| 95 | + * @param int $order_id |
| 96 | + * @return array |
| 97 | + */ |
| 98 | + public function process_payment( $order_id ) { |
| 99 | + |
| 100 | + $order = wc_get_order( $order_id ); |
| 101 | + |
| 102 | + // Update order status and add a transaction note |
| 103 | + $order->payment_complete(); |
| 104 | + $order->add_order_note( __( 'Bogus is always approved 😎', 'woocommerce-dev-helper' ) ); |
| 105 | + |
| 106 | + // Remove cart |
| 107 | + WC()->cart->empty_cart(); |
| 108 | + |
| 109 | + // Return thank you redirect |
| 110 | + return array( |
| 111 | + 'result' => 'success', |
| 112 | + 'redirect' => $this->get_return_url( $order ) |
| 113 | + ); |
| 114 | + } |
| 115 | + |
| 116 | + |
| 117 | +} |
0 commit comments