-
Notifications
You must be signed in to change notification settings - Fork 6
/
class-wc-wallee-admin-document.php
311 lines (291 loc) · 7.58 KB
/
class-wc-wallee-admin-document.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
<?php
/**
*
* WC_Wallee_Admin_Document Class
*
* Wallee
* This plugin will add support for all Wallee payments methods and connect the Wallee servers to your WooCommerce webshop (https://www.wallee.com).
*
* @category Class
* @package Wallee
* @author wallee AG (https://www.wallee.com)
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache Software License (ASL 2.0)
*/
if ( ! defined( 'ABSPATH' ) ) {
exit();
}
/**
* Shows the document downloads buttons and handles the downloads in the order overview.
*/
class WC_Wallee_Admin_Document {
/**
* Init WC_Wallee_Admin_Document.
*
* @return void
*/
public static function init() {
add_action(
'add_meta_boxes',
array(
__CLASS__,
'add_meta_box',
),
40
);
add_action(
'woocommerce_admin_order_actions_end',
array(
__CLASS__,
'add_buttons_to_overview',
),
12,
1
);
add_action(
'admin_init',
array(
__CLASS__,
'download_document',
)
);
}
/**
* Add buttons to overview.
*
* @param WC_Order $order Wc Order.
* @return void
*/
public static function add_buttons_to_overview( WC_Order $order ) {
$method = wc_get_payment_gateway_by_order( $order );
if ( ! ( $method instanceof WC_Wallee_Gateway ) ) {
return;
}
$transaction_info = WC_Wallee_Entity_Transaction_Info::load_by_order_id( $order->get_id() );
if ( $transaction_info->get_id() === null ) {
return;
}
if ( in_array(
$transaction_info->get_state(),
array(
\Wallee\Sdk\Model\TransactionState::COMPLETED,
\Wallee\Sdk\Model\TransactionState::FULFILL,
\Wallee\Sdk\Model\TransactionState::DECLINE,
),
true
) ) {
$url = wp_nonce_url(
add_query_arg(
array(
'post' => $order->get_id(),
'refer' => 'overview',
'wallee_admin' => 'download_invoice',
),
admin_url( 'post.php' )
),
'download_invoice',
'nonce'
);
$title = esc_attr( __( 'Invoice', 'woo-wallee' ) );
printf( '<a class="button tips wallee-action-button wallee-button-download-invoice" href="%1s" data-tip="%2s">%2s</a>', esc_url( $url ), esc_textarea( $title ), esc_textarea( $title ) );
}
if ( $transaction_info->get_state() === \Wallee\Sdk\Model\TransactionState::FULFILL ) {
$url = wp_nonce_url(
add_query_arg(
array(
'post' => $order->get_id(),
'refer' => 'overview',
'wallee_admin' => 'download_packing',
),
admin_url( 'post.php' )
),
'download_packing',
'nonce'
);
$title = esc_attr( __( 'Packing Slip', 'woo-wallee' ) );
printf( '<a class="button tips wallee-action-button wallee-button-download-packingslip" href="%1s" data-tip="%2s">%2s</a>', esc_url( $url ), esc_textarea( $title ), esc_textarea( $title ) );
}
}
/**
* Add WC Meta boxes.
*/
public static function add_meta_box() {
global $post;
if ( 'shop_order' !== $post->post_type ) {
return;
}
$order = WC_Order_Factory::get_order( $post->ID );
$method = wc_get_payment_gateway_by_order( $order );
if ( ! ( $method instanceof WC_Wallee_Gateway ) ) {
return;
}
$transaction_info = WC_Wallee_Entity_Transaction_Info::load_by_order_id( $order->get_id() );
if ( $transaction_info->get_id() !== null && in_array(
$transaction_info->get_state(),
array(
\Wallee\Sdk\Model\TransactionState::COMPLETED,
\Wallee\Sdk\Model\TransactionState::FULFILL,
\Wallee\Sdk\Model\TransactionState::DECLINE,
),
true
) ) {
add_meta_box(
'woocommerce-order-wallee-documents',
__( 'wallee Documents', 'woo-wallee' ),
array(
__CLASS__,
'output',
),
'shop_order',
'side',
'default'
);
}
}
/**
* Output the metabox.
*
* @param WP_Post $post Post.
*/
public static function output( $post ) {
global $post;
$order = WC_Order_Factory::get_order( $post->ID );
$method = wc_get_payment_gateway_by_order( $order );
if ( ! ( $method instanceof WC_Wallee_Gateway ) ) {
return;
}
$transaction_info = WC_Wallee_Entity_Transaction_Info::load_by_order_id( $order->get_id() );
if ( $transaction_info->get_id() === null ) {
return;
}
if ( in_array(
$transaction_info->get_state(),
array(
\Wallee\Sdk\Model\TransactionState::COMPLETED,
\Wallee\Sdk\Model\TransactionState::FULFILL,
\Wallee\Sdk\Model\TransactionState::DECLINE,
),
true
) ) {
?>
<ul class="woocommerce-order-admin-wallee-downloads">
<li><a
href="
<?php
echo esc_url(
wp_nonce_url(
add_query_arg(
array(
'post' => $order->get_id(),
'refer' => 'edit',
'wallee_admin' => 'download_invoice',
),
admin_url( 'post.php' )
),
'download_invoice',
'nonce'
)
);
?>
"
class="wallee-admin-download wallee-admin-download-invoice button"><?php esc_attr_e( 'Invoice', 'woo-wallee' ); ?></a></li>
<?php if ( $transaction_info->get_state() === \Wallee\Sdk\Model\TransactionState::FULFILL ) : ?>
<li><a
href="
<?php
echo esc_url(
wp_nonce_url(
add_query_arg(
array(
'post' => $order->get_id(),
'refer' => 'edit',
'wallee_admin' => 'download_packing',
),
admin_url( 'post.php' ),
true
),
'download_packing',
'nonce'
)
);
?>
"
class="wallee-admin-download wallee-admin-download-packingslip button"><?php esc_attr_e( 'Packing Slip', 'woo-wallee' ); ?></a></li>
<?php endif; ?>
</ul>
<?php
}
}
/**
* Admin pdf actions callback.
* Within admin by default only administrator and shop managers have permission to view, create, cancel invoice.
*/
public static function download_document() {
if ( ! self::is_download_request() ) {
return;
}
// sanitize data and verify nonce.
$action = isset( $_GET['wallee_admin'] ) ? sanitize_key( $_GET['wallee_admin'] ) : null;
$nonce = isset( $_GET['nonce'] ) ? sanitize_key( $_GET['nonce'] ) : null;
if ( ! wp_verify_nonce( $nonce, $action ) ) {
wp_die( 'Invalid request.' );
}
// validate allowed user roles.
$user = wp_get_current_user();
$allowed_roles = apply_filters(
'wc_wallee_allowed_roles_to_download_documents',
array(
'administrator',
'shop_manager',
)
);
if ( ! array_intersect( $allowed_roles, $user->roles ) ) {
wp_die( 'Access denied' );
}
$order_id = isset( $_GET['post'] ) ? intval( $_GET['post'] ) : null;
try {
switch ( $action ) {
case 'download_invoice':
WC_Wallee_Download_Helper::download_invoice( $order_id );
break;
case 'download_packing':
WC_Wallee_Download_Helper::download_packing_slip( $order_id );
break;
}
} catch ( Exception $e ) {
$message = $e->getMessage();
$cleaned = preg_replace( '/^\[[A-Fa-f\d\-]+\] /', '', $message );
wp_die( esc_html__( 'Could not fetch the document from wallee.', 'woo-wallee' ) . ' ' . esc_textarea( $cleaned ) );
}
$refer = isset( $_GET['refer'] );
if ( 'edit' === $refer ) {
wp_safe_redirect(
add_query_arg(
array(
'post' => $order_id,
'action' => 'edit',
),
admin_url( 'post.php' )
)
);
} else {
wp_safe_redirect(
add_query_arg(
array(
'post_type' => 'shop_order',
),
admin_url( 'edit.php' )
)
);
}
exit();
}
/**
* Check if request is PDF action.
*
* @return bool
*/
private static function is_download_request() {
return ( isset( $_GET['post'] ) && isset( $_GET['wallee_admin'] ) && isset( $_GET['nonce'] ) );
}
}
WC_Wallee_Admin_Document::init();