-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.php
180 lines (152 loc) · 6.74 KB
/
index.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
/*
Plugin Name: UPN plačilni nalog
Plugin URI: https://woocart.com
Description: Doda UPN plačilni nalog s QR kodo v vašo WooCommerce trgovino.
Version: 1.1.0
Author: WooCart
Author Email: [email protected]
License: GPLv2 or later
*/
namespace WooCart\UPNalog {
require_once "vendor/autoload.php";
class UPN
{
public $account_details;
public $instructions;
public function __construct()
{
// Get the gateways instance
$gateways = \WC_Payment_Gateways::instance();
// Get all available gateways, [id] => Object
$available_gateways = $gateways->get_available_payment_gateways();
if (isset($available_gateways['bacs'])) {
// If the gateway is available, remove the action hooks
remove_action('woocommerce_thankyou_bacs', array($available_gateways['bacs'], 'thankyou_page'));
remove_action('woocommerce_email_before_order_table', array($available_gateways['bacs'], 'email_instructions'), 10, 3);
$this->account_details = $available_gateways['bacs']->account_details;
$this->instructions = $available_gateways['bacs']->instructions;
add_action('woocommerce_email_before_order_table', array($this, 'upn_instructions'), 10, 3);
add_action('woocommerce_order_details_after_customer_details', array($this, 'upn_page'), 20);
add_shortcode('upn_order_awaiting_payment', array($this, 'upn_order_awaiting_payment'));
}
}
public function genUPN($order)
{
if (empty($this->account_details)) {
throw Exception("Empty accoutn details for BACS.");
return;
}
$bacs_accounts = apply_filters('woocommerce_bacs_accounts', $this->account_details, $order->get_id());
$bacs_account = (object) $bacs_accounts[0];
$png = (new \Media24si\UpnGenerator\UpnGenerator())
->setPayerName(sprintf("%s %s", $order->get_formatted_billing_full_name(), $order->get_billing_last_name()))
->setPayerAddress($order->get_billing_address_1())
->setPayerPost(sprintf("%s %s", $order->get_billing_postcode(), $order->get_billing_city()))
->setReceiverName($bacs_account->account_name)
->setReceiverAddress(WC()->countries->get_base_address())
->setReceiverPost(sprintf(
"%s %s",
WC()->countries->get_base_city(),
WC()->countries->get_base_postcode()
))
->setReceiverIban(preg_replace('/\s+/', '', $bacs_account->iban))
->setAmount($order->get_total())
->setCode(apply_filters('upn_code', "OTHR"))
->setReference(sprintf(apply_filters('upn_reference', "SI00 %s"), $order->get_order_number()))
->setDueDate(new \DateTime($order->order_date))
->setPurpose(sprintf(apply_filters('upn_purpose', 'Plačilo naročila %s'), $order->get_order_number()))
->png();
// Check for gd errors / buffer errors
if (!empty($png)) {
$data = base64_encode($png);
// Check for base64 errors
if ($data !== false) {
// Success
echo "<br/><img src='data:image/png;base64,$data'><br/>";
}
}
}
public function genUPNDescription($order)
{
if (empty($this->account_details)) {
return;
}
$bacs_accounts = apply_filters('woocommerce_bacs_accounts', $this->account_details, $order->get_id());
$bacs_account = (object) $bacs_accounts[0];
?>
<table class="woocommerce-table shop_table">
<tbody>
<tr>
<th>Prejemnik</th>
<td>
<?php echo wptexturize(wp_kses_post($bacs_account->account_name)); ?></br>
<?php echo wptexturize(wp_kses_post(WC()->countries->get_base_address())); ?></br>
<?php echo wptexturize(wp_kses_post(sprintf("%s %s", WC()->countries->get_base_city(), WC()->countries->get_base_postcode()))); ?>
</td>
</tr>
<tr>
<th>IBAN Prejemnika</th>
<td><?php echo wptexturize(wp_kses_post($bacs_account->iban)); ?></td>
</tr>
<tr>
<th>Namen</th>
<td><?php echo sprintf(apply_filters('upn_purpose', 'Plačilo naročila %s'), $order->get_order_number()); ?></td>
</tr>
<tr>
<th>Referenca Prejemnika</th>
<td><?php echo sprintf(apply_filters('upn_reference', "SI00 %s"), $order->get_order_number()); ?></td>
</tr>
</tbody>
</table>
<?php
}
/**
* Add content to the WC emails.
*
* @param WC_Order $order Order object.
* @param bool $sent_to_admin Sent to admin.
* @param bool $plain_text Email format: plain text or HTML.
*/
public function upn_instructions($order, $sent_to_admin, $plain_text = false)
{
if (!$sent_to_admin && 'bacs' === $order->get_payment_method() && $order->has_status('on-hold')) {
if ($this->instructions) {
echo wp_kses_post(wpautop(wptexturize($this->instructions)) . PHP_EOL);
}
$this->genUPNDescription($order);
$this->genUPN($order);
}
}
/**
* Output for the order received page.
*
* @param int $order_id Order ID.
*/
public function upn_page($order_id)
{
echo '</br>';
echo '<h2 class="woocommerce-column__title">UPN Nalog</h2>';
$order = wc_get_order($order_id);
$this->genUPNDescription($order);
$this->genUPN($order);
if ($this->instructions) {
echo wp_kses_post(wpautop(wptexturize(wp_kses_post($this->instructions))));
}
}
/**
* Shortcode helper.
*/
public function upn_order_awaiting_payment()
{
// Only show if not paid yet
$order_id = \WC()->session->get('order_awaiting_payment');
if ($order_id != null) {
$this->upn_page($order_id);
}
}
}
\add_action("woocommerce_init", function () {
return new UPN();
});
}