-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSericaPay.php
executable file
·164 lines (140 loc) · 5.74 KB
/
SericaPay.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
/**
* WC SericaPay Gateway Class.
* Built the SericaPay method.
*/
class WC_SericaPay extends WC_Payment_Gateway {
/**
* Constructor for the gateway.
*
* @return void
*/
public function __construct() {
global $woocommerce;
$this->id = 'SericaPay';
$this->icon = apply_filters( 'woocommerce_SericaPay_icon', '' );
$this->has_fields = false;
$this->method_title = __( 'SericaPay', 'SericaPay' );
// Load the form fields.
$this->init_form_fields();
// Load the settings.
$this->init_settings();
// Define user set variables.
$this->title = $this->settings['title'];
$this->merchantId = $this->settings['merchantId'];
$this->description = $this->settings['description'];
$this->instructions = $this->get_option( 'instructions' );
$this->enable_for_methods = $this->get_option( 'enable_for_methods', array() );
// Actions.
if ( version_compare( WOOCOMMERCE_VERSION, '2.0.0', '>=' ) ) {
add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( &$this, 'process_admin_options' ) );
} else {
add_action( 'woocommerce_update_options_payment_gateways', array( &$this, 'process_admin_options' ) );
}
add_action('woocommerce_receipt_' . $this->id, array($this, 'receipt_page'));
}
/* Admin Panel Options.*/
function admin_options() {
?>
<h3><?php _e('SericaPay','SericaPay'); ?></h3>
<table class="form-table">
<?php $this->generate_settings_html(); ?>
</table> <?php
}
/* Initialise Gateway Settings Form Fields. */
public function init_form_fields() {
$this->form_fields = array(
'enabled' => array(
'title' => __( 'Enable/Disable', 'SericaPay' ),
'type' => 'checkbox',
'label' => __( 'Enable SericaPay', 'SericaPay' ),
'default' => 'no'
),
'merchantId' => array(
'title' => __( 'Merchant Id', 'SericaPay' ),
'type' => 'text',
'description' => __( 'Put merchant id here.', 'SericaPay' ),
'desc_tip' => true,
'default' => __( 'none', 'SericaPay' )
),
'title' => array(
'title' => __( 'Title', 'SericaPay' ),
'type' => 'text',
'description' => __( 'This controls the title which the user sees during checkout.', 'SericaPay' ),
'desc_tip' => true,
'default' => __( '<img src="https://sericatrading.com/media/SericaPayWoo.png" />', 'SericaPay' )
),
'description' => array(
'title' => __( 'Description', 'SericaPay' ),
'type' => 'textarea',
'description' => __( 'This controls the description which the user sees during checkout.', 'SericaPay' ),
'default' => __( 'Special Offer! Get <strong>$10.00 off your next purchase</strong>. Choose <strong>SericaPay</strong> as your payment method, place order, and signup. (First time customers, only.)', 'SericaPay' )
),
);
}
function receipt_page ($order_id) {
global $woocommerce;
$order = new WC_Order( $order_id );
$items = $woocommerce->cart->get_cart();
$taxes = $woocommerce->cart->get_tax_totals();
$woocommerce->cart->calculate_shipping();
$shipping = $woocommerce->cart->shipping_total;
?>
<script type="text/javascript">
window.onSericaPayLoad = function () {
window.SericaPay.setup({
'merchant': '<?php echo $this->merchantId ?>',
'container': 'sericapay'
});
<?php
foreach ($items as $i => $value) {
$product = array(
'id' => $value['data']->get_title(),
'price' => $value['data']->get_price(),
'qty' => $value['quantity']
);
?>
SericaPay.addProduct(<?php echo json_encode($product); ?>);
<?php
}
?>
<?php
foreach ($taxes as $i => $value) {
$tax = array('id' => $i, 'price' => $value->amount, 'qty' => 1 );
?>
SericaPay.addProduct(<? echo json_encode($tax); ?>);
<?php
}
?>
<?php
if ($shipping > 0) {
?>
SericaPay.addProduct({"id": "Shipping", "price": <?=$shipping?>, "qty": 1});
<?php
}
?>
SericaPay.setCartID('<?php echo $order_id; ?>');
SericaPay.checkout();
};
</script>
<script type="text/javascript" src="https://sericatrading.com/js/SericaPayButton.js"></script>
<?php
$woocommerce->cart->empty_cart();
// Mark as on-hold
$order->update_status('on-hold', __( 'Your order wont be shipped until the funds have cleared in our account.', 'woocommerce' ));
// Reduce stock levels
$order->reduce_order_stock();
}
/* Process the payment and return the result. */
function process_payment ($order_id) {
$order = new WC_Order( $order_id );
return array(
'result' => 'success',
'redirect' => add_query_arg('order', $order->id, add_query_arg('key', $order->order_key, get_permalink(get_option('woocommerce_pay_page_id'))))
);
}
/* Output for the order received page. */
function thankyou() {
echo $this->instructions != '' ? wpautop( $this->instructions ) : '';
}
}