This repository has been archived by the owner on Mar 13, 2024. It is now read-only.
forked from agraddy/wp-woo-fake-pay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.php
110 lines (93 loc) · 3.32 KB
/
main.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
/*
Plugin Name: Fake Pay For WooCommerce
Description: Creates a fake payment gateway for admin users.
Author: Anthony Graddy, Melchior Kokernoot
Author URI: https://www.dashboardq.com, https://melchiorkokernoot.nl
Plugin URI: https://github.com/agraddy/wp-woo-fake-pay
Version: 1.0.1
*/
if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ), true ) ) {
add_action( 'plugins_loaded', 'fake_pay_init_gateway_class' );
add_filter( 'woocommerce_payment_gateways', 'fake_pay_add_gateway_class' );
} else {
unset( $_GET['activate'] );
add_action( 'admin_notices', function () {
echo <<<EOT
<div class="notice notice-error is-dismissible">
<p>It appears WooCommerce is not active.</p>
</div>
EOT;
} );
disable_fake_pay();
}
function disable_fake_pay() {
$active_plugins = get_option( 'active_plugins' );
if ( ( $key = array_search( 'wp-woo-fake-pay/main.php', $active_plugins ) ) !== false ) {
unset( $active_plugins[ $key ] );
update_option( 'active_plugins', $active_plugins );
}
}
function fake_pay_init_gateway_class() {
class WC_Gateway_Fake_Pay extends WC_Payment_Gateway {
public function __construct() {
$this->id = 'fake_pay';
$this->method_title = 'Fake Pay';
$this->method_description = 'Creates a fake payment gateway for admin users.';
$this->init_form_fields();
$this->init_settings();
$this->title = $this->get_option( 'title' );
$this->description = $this->get_option( 'description' );
if ( ! current_user_can( 'administrator' ) ) {
$this->enabled = false;
}
add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, [ $this, 'process_admin_options' ] );
}
public function init_form_fields() {
$this->form_fields = [
'enabled' => [
'title' => __( 'Enable/Disable', 'woocommerce' ),
'type' => 'checkbox',
'label' => __( 'Enable Fake Pay', 'woocommerce' ),
'default' => 'no',
],
'title' => [
'title' => __( 'Title', 'woocommerce' ),
'type' => 'text',
'description' => __( 'Payment method title that the customer will see on your website.', 'woocommerce' ),
'default' => __( 'Fake Pay', 'woocommerce' ),
'desc_tip' => true,
],
'description' => [
'title' => __( 'Description', 'woocommerce' ),
'type' => 'textarea',
'description' => __( 'Payment method description that the customer will see on your website.', 'woocommerce' ),
'desc_tip' => true,
'default' => __( 'This option is only available to admin users.', 'woocommerce' ),
],
];
}
public function process_payment( $order_id ): array {
global $woocommerce;
$order = new WC_Order( $order_id );
if ( ! current_user_can( 'administrator' ) ) {
$order->update_status( 'failed', '' );
$error_message = 'This payment option is not available.';
wc_add_notice( __( 'Payment error:', 'woothemes' ) . $error_message, 'error' );
return [];
}
$order->payment_complete();
// Remove cart
$woocommerce->cart->empty_cart();
// Return thankyou redirect
return [
'result' => 'success',
'redirect' => $this->get_return_url( $order ),
];
}
}
}
function fake_pay_add_gateway_class( $methods ) {
$methods[] = 'WC_Gateway_Fake_Pay';
return $methods;
}