PluginProbe
Welcart e-Commerce / 2.11.31
Welcart e-Commerce v2.11.31
2.12.4 2.12.3 2.11.35 2.12.2 2.12.1 2.11.34 2.11.33 2.11.32 2.11.31 2.11.30 1.3.16 1.3.17 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.4.0 1.4.1 1.4.10 1.4.11 1.4.12 All 292 releases
usc-e-shop / classes / paymentPayPalCP.class.php

paymentPayPalCP.class.php in Welcart e-Commerce 2.11.31, at classes/paymentPayPalCP.class.php

7,203 lines 315.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Settlement Class.
4 * PayPal
5 *
6 * @package Welcart
7 * @author Collne Inc.
8 * @version 1.1.0
9 * @since 2.1.0
10 */
11
12 /**
13 * PayPal Commerce Platform
14 */
15 class PAYPAL_CP_SETTLEMENT {
16
17 const API_BN_CODE_PCP = 'Welcart_Cart_PCP_JP';
18 const API_BN_CODE_ACDC = 'Collne_Cart_ACDC_JP';
19 const API_FRAUDNET_S = 'Collne_T7SS625X3B32Q_RT';
20 const API_URL = 'https://api-m.paypal.com';
21 const API_SANDBOX_URL = 'https://api-m.sandbox.paypal.com';
22 const API_TIMEOUT = 30;
23
24 /**
25 * Instance of this class.
26 *
27 * @var object
28 */
29 protected static $instance = null;
30
31 /**
32 * 決済代行会社ID
33 *
34 * @var string
35 */
36 protected $paymod_id;
37
38 /**
39 * 決済種別
40 *
41 * @var string
42 */
43 protected $pay_method;
44
45 /**
46 * 決済代行会社略称
47 *
48 * @var string
49 */
50 protected $acting_name;
51
52 /**
53 * 決済代行会社正式名称
54 *
55 * @var string
56 */
57 protected $acting_formal_name;
58
59 /**
60 * 決済代行会社URL
61 *
62 * @var string
63 */
64 protected $acting_company_url;
65
66 /**
67 * 併用不可決済モジュール
68 *
69 * @var array
70 */
71 protected $unavailable_method;
72
73 /**
74 * エラーメッセージ
75 *
76 * @var string
77 */
78 protected $error_mes;
79
80 /**
81 * 自動継続課金処理結果メール
82 *
83 * @var array
84 */
85 protected $continuation_charging_mail;
86
87 /**
88 * Construct.
89 */
90 public function __construct() {
91
92 $this->paymod_id = 'paypal_cp';
93 $this->pay_method = array(
94 'acting_paypal_cp',
95 'acting_paypal_card',
96 );
97 $this->acting_name = 'PayPal(CP)';
98 $this->acting_formal_name = 'PayPal Commerce Platform';
99
100 $this->initialize_data();
101
102 add_action( 'init', array( $this, 'init' ), 20 );
103 add_filter( 'http_headers_useragent', array( $this, 'http_headers_useragent' ), 10, 2 );
104 if ( $this->is_validity_acting() ) {
105 add_action( 'template_redirect', array( $this, 'respond_to_ipn' ), 1 );
106 }
107
108 if ( is_admin() ) {
109 add_action( 'wp_ajax_onboarded', array( $this, 'onboarded' ) );
110 add_action( 'usces_action_admin_ajax', array( $this, 'admin_ajax' ) );
111 add_action( 'admin_print_footer_scripts', array( $this, 'admin_scripts' ) );
112 add_action( 'usces_action_admin_settlement_update', array( $this, 'settlement_update' ) );
113 add_action( 'usces_action_settlement_tab_title', array( $this, 'settlement_tab_title' ) );
114 add_action( 'usces_action_settlement_tab_body', array( $this, 'settlement_tab_body' ) );
115 }
116
117 if ( $this->is_validity_acting() ) {
118 add_action( 'wp_ajax_create_billing_agreement', array( $this, 'create_billing_agreement' ) );
119 add_action( 'wp_ajax_nopriv_create_billing_agreement', array( $this, 'create_billing_agreement' ) );
120 add_action( 'wp_ajax_create_order', array( $this, 'create_order' ) );
121 add_action( 'wp_ajax_nopriv_create_order', array( $this, 'create_order' ) );
122 if ( $this->is_validity_acting( 'card' ) ) {
123 add_action( 'wp_ajax_create_order_card', array( $this, 'create_order_card' ) );
124 add_action( 'wp_ajax_nopriv_create_order_card', array( $this, 'create_order_card' ) );
125 add_action( 'wp_ajax_create_order_member', array( $this, 'create_order_member' ) );
126 add_action( 'wp_ajax_nopriv_create_order_member', array( $this, 'create_order_member' ) );
127 add_action( 'wp_ajax_show_order_details_card', array( $this, 'show_order_details_card' ) );
128 add_action( 'wp_ajax_nopriv_show_order_details_card', array( $this, 'show_order_details_card' ) );
129 add_filter( 'usces_filter_payments_str', array( $this, 'payments_str' ), 10, 2 );
130 add_filter( 'usces_filter_payments_arr', array( $this, 'payments_arr' ), 10, 2 );
131 add_filter( 'usces_filter_delivery_secure_form_loop', array( $this, 'delivery_secure_form_loop' ), 10, 2 );
132 add_filter( 'usces_filter_delivery_check', array( $this, 'delivery_check' ), 15 );
133 add_action( 'usces_action_confirm_page_point_inform', array( $this, 'e_point_inform' ), 10, 5 );
134 add_filter( 'usces_filter_confirm_point_inform', array( $this, 'point_inform' ), 10, 5 );
135 if ( defined( 'WCEX_COUPON' ) ) {
136 add_filter( 'wccp_filter_coupon_inform', array( $this, 'point_inform' ), 10, 5 );
137 }
138 add_filter( 'usces_filter_get_error_settlement', array( $this, 'error_page_message' ) );
139 add_filter( 'usces_filter_template_redirect', array( $this, 'member_update_settlement' ), 1 );
140 add_action( 'usces_action_member_submenu_list', array( $this, 'e_update_settlement' ) );
141 add_filter( 'usces_filter_member_submenu_list', array( $this, 'update_settlement' ), 10, 2 );
142 add_action( 'usces_action_pre_delete_memberdata', array( $this, 'delete_member' ) );
143 }
144 add_filter( 'usces_filter_confirm_inform', array( $this, 'confirm_inform' ), 10, 5 );
145 add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
146 add_action( 'wp_print_footer_scripts', array( $this, 'footer_scripts' ) );
147 add_action( 'usces_action_acting_processing', array( $this, 'acting_processing' ), 10, 2 );
148 add_filter( 'usces_filter_check_acting_return_results', array( $this, 'acting_return' ) );
149 add_action( 'usces_action_reg_orderdata', array( $this, 'register_orderdata' ) );
150 add_filter( 'usces_filter_is_complete_settlement', array( $this, 'is_complete_settlement' ), 10, 3 );
151 add_filter( 'usces_filter_delete_member_check', array( $this, 'delete_member_check' ), 10, 2 );
152 add_filter( 'usces_filter_delete_member_check_front', array( $this, 'delete_member_check' ), 10, 2 );
153 if ( is_admin() ) {
154 add_filter( 'usces_filter_settle_info_field_meta_keys', array( $this, 'settlement_info_field_meta_keys' ) );
155 add_filter( 'usces_filter_settle_info_field_keys', array( $this, 'settlement_info_field_keys' ), 10, 2 );
156 add_filter( 'usces_filter_settle_info_field_value', array( $this, 'settlement_info_field_value' ), 10, 3 );
157 add_filter( 'usces_filter_orderlist_detail_value', array( $this, 'orderlist_settlement_status' ), 10, 4 );
158 add_action( 'usces_action_order_edit_form_status_block_middle', array( $this, 'settlement_status' ), 10, 3 );
159 add_action( 'usces_action_order_edit_form_settle_info', array( $this, 'settlement_information' ), 10, 2 );
160 add_action( 'usces_action_endof_order_edit_form', array( $this, 'settlement_dialog' ), 10, 2 );
161 add_action( 'usces_action_admin_member_info', array( $this, 'admin_member_info' ), 10, 3 );
162 add_action( 'usces_action_post_update_memberdata', array( $this, 'admin_update_memberdata' ), 10, 2 );
163 }
164
165 /* WCEX DL Seller */
166 if ( defined( 'WCEX_DLSELLER' ) ) {
167 add_filter( 'usces_filter_the_continue_payment_method', array( $this, 'continuation_payment_method' ) );
168 add_filter( 'dlseller_action_reg_continuationdata', array( $this, 'register_continuationdata' ) );
169 add_filter( 'dlseller_filter_first_charging', array( $this, 'first_charging_date' ), 9, 5 );
170 add_filter( 'dlseller_filter_the_payment_method_restriction', array( $this, 'payment_method_restriction' ), 10, 2 );
171 add_filter( 'dlseller_filter_continue_memberlist_column', array( $this, 'continue_memberlist_column' ), 10, 2 );
172 add_filter( 'dlseller_filter_continue_memberlist_sql_jointable', array( $this, 'continue_memberlist_sql_jointable' ), 10, 2 );
173 add_filter( 'dlseller_filter_continue_memberlist_sql_select', array( $this, 'continue_memberlist_sql_select' ), 10, 3 );
174 add_filter( 'dlseller_filter_continue_member_list_condition', array( $this, 'continue_member_list_condition' ), 10, 4 );
175 add_action( 'dlseller_action_continue_member_list_page', array( $this, 'continue_member_list_page' ) );
176 add_action( 'dlseller_action_do_continuation_charging', array( $this, 'auto_continuation_charging' ), 10, 4 );
177 add_action( 'dlseller_action_do_continuation', array( $this, 'do_auto_continuation' ), 10, 2 );
178 add_filter( 'dlseller_filter_reminder_mail_body', array( $this, 'reminder_mail_body' ), 10, 3 );
179 add_filter( 'dlseller_filter_contract_renewal_mail_body', array( $this, 'contract_renewal_mail_body' ), 10, 3 );
180 add_filter( 'usces_filter_order_confirm_mail_payment', array( $this, 'continue_order_mail_payment' ), 11, 4 );
181 add_filter( 'usces_filter_send_order_mail_payment', array( $this, 'continue_order_mail_payment' ), 11, 4 );
182 }
183
184 /* WCEX Auto Delivery */
185 if ( defined( 'WCEX_AUTO_DELIVERY' ) ) {
186 add_filter( 'wcad_action_reg_regulardata', array( $this, 'register_regulardata' ) );
187 add_filter( 'wcad_filter_shippinglist_acting', array( $this, 'set_shippinglist_acting' ) );
188 add_filter( 'wcad_filter_available_regular_payment_method', array( $this, 'available_regular_payment_method' ) );
189 add_filter( 'wcad_filter_the_payment_method_restriction', array( $this, 'payment_method_restriction' ), 10, 2 );
190 add_action( 'wcad_action_reg_auto_orderdata', array( $this, 'register_auto_orderdata' ) );
191 add_filter( 'wcad_filter_send_settlement_error_mail_message_head', array( $this, 'settlement_error_mail_message_header' ), 10, 2 );
192 if ( version_compare( WCEX_AUTO_DELIVERY_VERSION, '1.2.5', '>=' ) ) {
193 add_filter( 'wcad_filter_send_settlement_error_mail_message', array( $this, 'settlement_error_mail_message' ), 10, 3 );
194 }
195 add_action( 'wcad_action_regular_information_edit_form', array( $this, 'regular_information_edit_form' ) );
196 add_filter( 'usces_filter_order_confirm_mail_payment', array( $this, 'regular_order_mail_payment' ), 11, 4 );
197 add_filter( 'usces_filter_send_order_mail_payment', array( $this, 'regular_order_mail_payment' ), 11, 4 );
198 add_filter( 'wcad_filter_order_confirm_mail_payment', array( $this, 'regular_order_mail_payment' ), 11, 4 );
199 }
200 }
201 }
202
203 /**
204 * Return an instance of this class.
205 */
206 public static function get_instance() {
207 if ( is_null( self::$instance ) ) {
208 self::$instance = new self();
209 }
210 return self::$instance;
211 }
212
213 /**
214 * Initialize.
215 */
216 public function initialize_data() {
217 $options = get_option( 'usces', array() );
218 $options['acting_settings']['paypal_cp']['activate'] = ( isset( $options['acting_settings']['paypal_cp']['activate'] ) ) ? $options['acting_settings']['paypal_cp']['activate'] : 'off';
219 $options['acting_settings']['paypal_cp']['cp_activate'] = ( isset( $options['acting_settings']['paypal_cp']['cp_activate'] ) ) ? $options['acting_settings']['paypal_cp']['cp_activate'] : 'off';
220 $options['acting_settings']['paypal_cp']['card_activate'] = ( isset( $options['acting_settings']['paypal_cp']['card_activate'] ) ) ? $options['acting_settings']['paypal_cp']['card_activate'] : 'off';
221 $options['acting_settings']['paypal_cp']['card_vault'] = ( isset( $options['acting_settings']['paypal_cp']['card_vault'] ) ) ? $options['acting_settings']['paypal_cp']['card_vault'] : 'off';
222 $options['acting_settings']['paypal_cp']['card_prefix'] = ( isset( $options['acting_settings']['paypal_cp']['card_prefix'] ) ) ? $options['acting_settings']['paypal_cp']['card_prefix'] : '';
223 $options['acting_settings']['paypal_cp']['card_3ds'] = ( isset( $options['acting_settings']['paypal_cp']['card_3ds'] ) ) ? $options['acting_settings']['paypal_cp']['card_3ds'] : 'off';
224 $options['acting_settings']['paypal_cp']['environment'] = ( isset( $options['acting_settings']['paypal_cp']['environment'] ) ) ? $options['acting_settings']['paypal_cp']['environment'] : 'live';
225 $options['acting_settings']['paypal_cp']['bncode'] = ( isset( $options['acting_settings']['paypal_cp']['bncode'] ) ) ? $options['acting_settings']['paypal_cp']['bncode'] : '';
226 $options['acting_settings']['paypal_cp']['client_id'] = ( isset( $options['acting_settings']['paypal_cp']['client_id'] ) ) ? $options['acting_settings']['paypal_cp']['client_id'] : '';
227 $options['acting_settings']['paypal_cp']['secret'] = ( isset( $options['acting_settings']['paypal_cp']['secret'] ) ) ? $options['acting_settings']['paypal_cp']['secret'] : '';
228 $options['acting_settings']['paypal_cp']['intent'] = ( isset( $options['acting_settings']['paypal_cp']['intent'] ) ) ? $options['acting_settings']['paypal_cp']['intent'] : 'CAPTURE';
229 $options['acting_settings']['paypal_cp']['autobilling_intent'] = ( isset( $options['acting_settings']['paypal_cp']['autobilling_intent'] ) ) ? $options['acting_settings']['paypal_cp']['autobilling_intent'] : 'CAPTURE';
230 $options['acting_settings']['paypal_cp']['autobilling_email'] = ( isset( $options['acting_settings']['paypal_cp']['autobilling_email'] ) ) ? $options['acting_settings']['paypal_cp']['autobilling_email'] : 'off';
231 $options['acting_settings']['paypal_cp']['button_layout'] = ( isset( $options['acting_settings']['paypal_cp']['button_layout'] ) ) ? $options['acting_settings']['paypal_cp']['button_layout'] : 'vertical';
232 $options['acting_settings']['paypal_cp']['button_color'] = ( isset( $options['acting_settings']['paypal_cp']['button_color'] ) ) ? $options['acting_settings']['paypal_cp']['button_color'] : 'gold';
233 $options['acting_settings']['paypal_cp']['button_shape'] = ( isset( $options['acting_settings']['paypal_cp']['button_shape'] ) ) ? $options['acting_settings']['paypal_cp']['button_shape'] : 'rect';
234 $options['acting_settings']['paypal_cp']['button_label'] = ( isset( $options['acting_settings']['paypal_cp']['button_label'] ) ) ? $options['acting_settings']['paypal_cp']['button_label'] : 'paypal';
235 $options['acting_settings']['paypal_cp']['agree'] = ( isset( $options['acting_settings']['paypal_cp']['agree'] ) ) ? $options['acting_settings']['paypal_cp']['agree'] : '';
236 update_option( 'usces', $options );
237
238 $available_settlement = get_option( 'usces_available_settlement', array() );
239 if ( ! in_array( $this->paymod_id, $available_settlement ) ) {
240 $available_settlement[ $this->paymod_id ] = $this->acting_formal_name;
241 update_option( 'usces_available_settlement', $available_settlement );
242 }
243
244 $this->unavailable_method = array( 'acting_paypal_ec', 'acting_paypal_wpp' );
245 }
246
247 /**
248 * 決済有効判定
249 * 支払方法で使用している場合に true
250 *
251 * @param string $type Module type.
252 * @return boolean
253 */
254 public function is_validity_acting( $type = '' ) {
255 $acting_opts = $this->get_acting_settings();
256 if ( empty( $acting_opts ) ) {
257 return false;
258 }
259
260 $payment_method = usces_get_system_option( 'usces_payment_method', 'sort' );
261 $method = false;
262
263 switch ( $type ) {
264 case 'cp':
265 foreach ( $payment_method as $payment ) {
266 if ( 'acting_paypal_cp' === $payment['settlement'] && 'activate' === $payment['use'] ) {
267 $method = true;
268 break;
269 }
270 }
271 if ( $method && $this->is_activate_paypal_cp() ) {
272 return true;
273 } else {
274 return false;
275 }
276 case 'card':
277 foreach ( $payment_method as $payment ) {
278 if ( 'acting_paypal_card' === $payment['settlement'] && 'activate' === $payment['use'] ) {
279 $method = true;
280 break;
281 }
282 }
283 if ( $method && $this->is_activate_paypal_card() ) {
284 return true;
285 } else {
286 return false;
287 }
288 break;
289 default:
290 if ( 'on' === $acting_opts['activate'] ) {
291 return true;
292 } else {
293 return false;
294 }
295 }
296 }
297
298 /**
299 * 決済利用判定
300 * PayPal CP を「利用する」のとき true
301 *
302 * @return boolean
303 */
304 public function is_activate_paypal_cp() {
305 $acting_opts = $this->get_acting_settings();
306 if ( ( isset( $acting_opts['activate'] ) && 'on' === $acting_opts['activate'] ) &&
307 ( isset( $acting_opts['cp_activate'] ) && 'on' === $acting_opts['cp_activate'] ) ) {
308 $res = true;
309 } else {
310 $res = false;
311 }
312 return $res;
313 }
314
315 /**
316 * 決済利用判定
317 * PayPal カード決済を「利用する」のとき true
318 *
319 * @return boolean
320 */
321 public function is_activate_paypal_card() {
322 $acting_opts = $this->get_acting_settings();
323 if ( ( isset( $acting_opts['activate'] ) && 'on' === $acting_opts['activate'] ) &&
324 ( isset( $acting_opts['card_activate'] ) && 'on' === $acting_opts['card_activate'] ) ) {
325 $res = true;
326 } else {
327 $res = false;
328 }
329 return $res;
330 }
331
332 /**
333 * Resolve conflicts.
334 * init
335 */
336 public function init() {
337 global $usces;
338
339 $payment_method = usces_get_system_option( 'usces_payment_method', 'sort' );
340 foreach ( $payment_method as $payment ) {
341 if ( 'acting_paypal_ec' === $payment['settlement'] && 'activate' === $payment['use'] ) {
342 $payment['use'] = 'deactivate';
343 usces_update_system_option( 'usces_payment_method', $payment['id'], $payment );
344 } elseif ( 'acting_paypal_wpp' === $payment['settlement'] && 'activate' === $payment['use'] ) {
345 $payment['use'] = 'deactivate';
346 usces_update_system_option( 'usces_payment_method', $payment['id'], $payment );
347 }
348 }
349 }
350
351 /**
352 * Respond to PayPal IPN with HTTP 200.
353 *
354 * Billing Agreements created before the notify_url removal
355 * still trigger IPN on each recurring payment. Since PayPal CP
356 * does not use IPN, simply return HTTP 200 to stop failure alerts.
357 *
358 * @return void
359 */
360 public function respond_to_ipn() {
361 if ( 'POST' !== $_SERVER['REQUEST_METHOD'] ) {
362 return;
363 }
364 if ( ! isset( $_POST['ipn_track_id'] ) || ! isset( $_POST['txn_type'] ) ) {
365 return;
366 }
367 status_header( 200 );
368 exit;
369 }
370
371 /**
372 * Filters the user agent value sent with an HTTP request.
373 * http_headers_useragent
374 *
375 * @param string $user_agent WordPress user agent string.
376 * @param string $url The request URL.
377 */
378 public function http_headers_useragent( $user_agent, $url ) {
379 if ( false !== strpos( $url, self::API_URL ) || false !== strpos( $url, self::API_SANDBOX_URL ) ) {
380 $user_agent = 'Welcart/' . USCES_VERSION . '; ' . get_bloginfo( 'url' );
381 }
382 return $user_agent;
383 }
384
385 /**
386 * 管理画面スクリプト
387 * admin_print_footer_scripts
388 */
389 public function admin_scripts() {
390 global $usces, $usces_settings;
391
392 $admin_page = ( isset( $_GET['page'] ) ) ? wp_unslash( $_GET['page'] ) : '';
393 switch ( $admin_page ) :
394 /* クレジット決済設定画面 */
395 case 'usces_settlement':
396 $settlement_selected = get_option( 'usces_settlement_selected', array() );
397 if ( in_array( $this->paymod_id, (array) $settlement_selected ) ) :
398 $acting_opts = $this->get_acting_settings();
399 $cp_activate = ( isset( $acting_opts['cp_activate'] ) ) ? $acting_opts['cp_activate'] : 'off';
400 $card_activate = ( isset( $acting_opts['card_activate'] ) ) ? $acting_opts['card_activate'] : 'off';
401 $cp_environment = ( isset( $acting_opts['environment'] ) ) ? $acting_opts['environment'] : 'live';
402 ?>
403 <script src="https://www.paypal.com/webapps/merchantboarding/js/lib/lightbox/partner.js" id="paypal-js"></script>
404 <script type="text/javascript">
405 jQuery( document ).ready( function( $ ) {
406 var card_activate = "<?php echo esc_js( $card_activate ); ?>";
407 if ( "on" == card_activate ) {
408 $( ".paypal_card_form" ).css( "display", "" );
409 } else {
410 $( ".paypal_card_form" ).css( "display", "none" );
411 }
412 $( document ).on( "change", ".activate_paypal_card", function() {
413 if ( "on" == $( this ).val() ) {
414 $( ".paypal_card_form" ).css( "display", "" );
415 } else {
416 $( ".paypal_card_form" ).css( "display", "none" );
417 }
418 });
419
420 var environment_cp = "<?php echo esc_js( $cp_environment ); ?>";
421 if ( "live" == environment_cp ) {
422 $( "#upfront_onboarding_paypal_cp" ).prop( "disabled", false );
423 } else {
424 $( "#upfront_onboarding_paypal_cp" ).prop( "disabled", true );
425 }
426 $( document ).on( "change", ".cp_environment", function() {
427 if ( "live" == $( this ).val() ) {
428 $( "#upfront_onboarding_paypal_cp" ).prop( "disabled", false );
429 } else {
430 $( "#upfront_onboarding_paypal_cp" ).prop( "disabled", true );
431 }
432 });
433
434 $( document ).on( "change", ".button_color_paypal_cp", function() {
435 if ( "gold" == $( this ).val() ) {
436 $( "#button_preview_paypal_cp" ).addClass( "color-gold" );
437 $( "#button_preview_paypal_cp" ).removeClass( "color-blue" );
438 $( "#button_preview_paypal_cp" ).removeClass( "color-silver" );
439 $( "#button_preview_paypal_cp" ).removeClass( "color-white" );
440 $( "#button_preview_paypal_cp" ).removeClass( "color-black" );
441 } else if ( "blue" == $( this ).val() ) {
442 $( "#button_preview_paypal_cp" ).removeClass( "color-gold" );
443 $( "#button_preview_paypal_cp" ).addClass( "color-blue" );
444 $( "#button_preview_paypal_cp" ).removeClass( "color-silver" );
445 $( "#button_preview_paypal_cp" ).removeClass( "color-white" );
446 $( "#button_preview_paypal_cp" ).removeClass( "color-black" );
447 } else if ( "silver" == $( this ).val() ) {
448 $( "#button_preview_paypal_cp" ).removeClass( "color-gold" );
449 $( "#button_preview_paypal_cp" ).removeClass( "color-blue" );
450 $( "#button_preview_paypal_cp" ).addClass( "color-silver" );
451 $( "#button_preview_paypal_cp" ).removeClass( "color-white" );
452 $( "#button_preview_paypal_cp" ).removeClass( "color-black" );
453 } else if ( "white" == $( this ).val() ) {
454 $( "#button_preview_paypal_cp" ).removeClass( "color-gold" );
455 $( "#button_preview_paypal_cp" ).removeClass( "color-blue" );
456 $( "#button_preview_paypal_cp" ).removeClass( "color-silver" );
457 $( "#button_preview_paypal_cp" ).addClass( "color-white" );
458 $( "#button_preview_paypal_cp" ).removeClass( "color-black" );
459 } else if ( "black" == $( this ).val() ) {
460 $( "#button_preview_paypal_cp" ).removeClass( "color-gold" );
461 $( "#button_preview_paypal_cp" ).removeClass( "color-blue" );
462 $( "#button_preview_paypal_cp" ).removeClass( "color-silver" );
463 $( "#button_preview_paypal_cp" ).removeClass( "color-white" );
464 $( "#button_preview_paypal_cp" ).addClass( "color-black" );
465 }
466 });
467
468 $( document ).on( "change", ".button_shape_paypal_cp", function() {
469 if ( "rect" == $( this ).val() ) {
470 $( "#button_preview_paypal_cp" ).addClass( "shape-rect" );
471 $( "#button_preview_paypal_cp" ).removeClass( "shape-pill" );
472 } else if ( "pill" == $( this ).val() ) {
473 $( "#button_preview_paypal_cp" ).removeClass( "shape-rect" );
474 $( "#button_preview_paypal_cp" ).addClass( "shape-pill" );
475 }
476 });
477
478 $( document ).on( "change", ".button_label_paypal_cp", function() {
479 if ( "paypal" == $( this ).val() ) {
480 $( "#button_preview_paypal_cp" ).addClass( "label-paypal" );
481 $( "#button_preview_paypal_cp" ).removeClass( "label-checkout" );
482 $( "#button_preview_paypal_cp" ).removeClass( "label-buynow" );
483 $( "#button_preview_paypal_cp" ).removeClass( "label-pay" );
484 } else if ( "checkout" == $( this ).val() ) {
485 $( "#button_preview_paypal_cp" ).removeClass( "label-paypal" );
486 $( "#button_preview_paypal_cp" ).addClass( "label-checkout" );
487 $( "#button_preview_paypal_cp" ).removeClass( "label-buynow" );
488 $( "#button_preview_paypal_cp" ).removeClass( "label-pay" );
489 } else if ( "buynow" == $( this ).val() ) {
490 $( "#button_preview_paypal_cp" ).removeClass( "label-paypal" );
491 $( "#button_preview_paypal_cp" ).removeClass( "label-checkout" );
492 $( "#button_preview_paypal_cp" ).addClass( "label-buynow" );
493 $( "#button_preview_paypal_cp" ).removeClass( "label-pay" );
494 } else if ( "pay" == $( this ).val() ) {
495 $( "#button_preview_paypal_cp" ).removeClass( "label-paypal" );
496 $( "#button_preview_paypal_cp" ).removeClass( "label-checkout" );
497 $( "#button_preview_paypal_cp" ).removeClass( "label-buynow" );
498 $( "#button_preview_paypal_cp" ).addClass( "label-pay" );
499 }
500 });
501
502 $( document ).on( "click", "#upfront_onboarding_paypal_cp", function() {
503 $.ajax({
504 url: ajaxurl,
505 type: "POST",
506 cache: false,
507 dataType: "json",
508 data: {
509 action: "usces_admin_ajax",
510 mode: "upfront_onboarding",
511 wc_nonce: $( "#wc_nonce" ).val()
512 }
513 }).done( function( retVal, dataType ) {
514 var action_url = retVal.action_url;
515 $( "#seller_nonce" ).val( retVal.seller_nonce );
516 $( '#onboarding' ).attr( 'href', action_url + '&displayMode=minibrowser' );
517 $( '#onboarding' )[0].click();
518 }).fail( function( jqXHR, textStatus, errorThrown ) {
519 console.log( textStatus );
520 console.log( jqXHR.status );
521 console.log( errorThrown.message );
522 });
523 return false;
524 });
525 });
526 </script>
527 <?php
528 endif;
529 break;
530
531 /* 受注編集画面・継続課金会員詳細画面 */
532 case 'usces_orderlist':
533 case 'usces_continue':
534 $order_id = '';
535 $acting_flg = '';
536 $order_action = ( isset( $_GET['order_action'] ) ) ? wp_unslash( $_GET['order_action'] ) : '';
537 $continue_action = ( isset( $_GET['continue_action'] ) ) ? wp_unslash( $_GET['continue_action'] ) : '';
538 if ( ( 'usces_orderlist' === $admin_page && ( 'edit' === $order_action || 'editpost' === $order_action || 'newpost' === $order_action ) ) ||
539 ( 'usces_continue' === $admin_page && 'settlement_paypal_cp' === $continue_action ) ) {
540 $order_id = ( isset( $_REQUEST['order_id'] ) ) ? wp_unslash( $_REQUEST['order_id'] ) : '';
541 if ( ! empty( $order_id ) ) {
542 $acting_flg = $this->get_order_acting_flg( $order_id );
543 }
544 }
545 if ( in_array( $acting_flg, $this->pay_method ) ) :
546 $args = compact( 'order_id', 'acting_flg', 'admin_page' );
547 $cr = $usces->options['system']['currency'];
548 if ( isset( $usces_settings['currency'][ $cr ] ) ) {
549 list( $crcode, $decimal, $point, $seperator, $symbol ) = $usces_settings['currency'][ $cr ];
550 if ( 'JPY' === $crcode ) {
551 $currency = __( $crcode, 'usces' );
552 } else {
553 $currency = ( usces_is_entity( $symbol ) ) ? mb_convert_encoding( $symbol, 'UTF-8', 'HTML-ENTITIES' ) : $symbol;
554 }
555 } else {
556 $currency = '';
557 }
558 if ( defined( 'WCEX_AUTO_DELIVERY' ) && 'acting_paypal_cp' === $acting_flg ) {
559 $reg_id = $this->get_regular_id( $order_id );
560 } else {
561 $reg_id = '';
562 }
563 ?>
564 <script type="text/javascript">
565 jQuery( document ).ready( function( $ ) {
566 adminOrderEdit = {
567 getPayPalCP : function() {
568 $( "#settlement-response" ).html( "" );
569 $( "#settlement-response-loading" ).html( '<img src="' + uscesL10n.USCES_PLUGIN_URL + '/images/loading.gif" />' );
570 var order_num = $( "#order_num" ).val();
571 $.ajax({
572 url: ajaxurl,
573 type: "POST",
574 cache: false,
575 dataType: "json",
576 data: {
577 action: "usces_admin_ajax",
578 mode: "get_paypal_cp",
579 order_id: $( "#order_id" ).val(),
580 order_num: order_num,
581 tracking_id: $( "#tracking_id" ).val(),
582 member_id: $( "#member_id" ).val(),
583 <?php if ( 'usces_continue' === $admin_page && 'acting_paypal_cp' === $acting_flg ) : ?>
584 con_id: $( "#con_id" ).val(),
585 <?php endif; ?>
586 <?php if ( ! empty( $reg_id ) ) : ?>
587 reg_id: $( "#reg_id" ).val(),
588 <?php endif; ?>
589 acting: $( "#acting" ).val(),
590 wc_nonce: $( "#wc_nonce" ).val()
591 }
592 }).done( function( retVal, dataType ) {
593 if ( retVal.result ) {
594 $( "#settlement-response" ).html( retVal.result );
595 }
596 $( "#settlement-response-loading" ).html( "" );
597 }).fail( function( jqXHR, textStatus, errorThrown ) {
598 console.log( textStatus );
599 console.log( jqXHR.status );
600 console.log( errorThrown.message );
601 $( "#settlement-response-loading" ).html( "" );
602 });
603 return false;
604 },
605 capturePayPalCP : function( amount ) {
606 $( "#settlement-response" ).html( "" );
607 $( "#settlement-response-loading" ).html( '<img src="' + uscesL10n.USCES_PLUGIN_URL + '/images/loading.gif" />' );
608 var order_num = $( "#order_num" ).val();
609 $.ajax({
610 url: ajaxurl,
611 type: "POST",
612 cache: false,
613 dataType: "json",
614 data: {
615 action: "usces_admin_ajax",
616 mode: "capture_paypal_cp",
617 order_id: $( "#order_id" ).val(),
618 order_num: order_num,
619 tracking_id: $( "#tracking_id" ).val(),
620 member_id: $( "#member_id" ).val(),
621 amount: amount,
622 <?php if ( 'usces_continue' === $admin_page ) : ?>
623 con_id: $( "#con_id" ).val(),
624 <?php endif; ?>
625 acting: $( "#acting" ).val(),
626 wc_nonce: $( "#wc_nonce" ).val()
627 }
628 }).done( function( retVal, dataType ) {
629 if ( retVal.result ) {
630 $( "#settlement-response" ).html( retVal.result );
631 $( "#settlement-status-" + order_num ).html( retVal.acting_status );
632 }
633 $( "#settlement-response-loading" ).html( "" );
634 }).fail( function( jqXHR, textStatus, errorThrown ) {
635 console.log( textStatus );
636 console.log( jqXHR.status );
637 console.log( errorThrown.message );
638 $( "#settlement-response-loading" ).html( "" );
639 });
640 return false;
641 },
642 voidPayPalCP : function() {
643 $( "#settlement-response" ).html( "" );
644 $( "#settlement-response-loading" ).html( '<img src="' + uscesL10n.USCES_PLUGIN_URL + '/images/loading.gif" />' );
645 var order_num = $( "#order_num" ).val();
646 $.ajax({
647 url: ajaxurl,
648 type: "POST",
649 cache: false,
650 dataType: "json",
651 data: {
652 action: "usces_admin_ajax",
653 mode: "void_paypal_cp",
654 order_id: $( "#order_id" ).val(),
655 order_num: order_num,
656 tracking_id: $( "#tracking_id" ).val(),
657 member_id: $( "#member_id" ).val(),
658 <?php if ( 'usces_continue' === $admin_page ) : ?>
659 con_id: $( "#con_id" ).val(),
660 <?php endif; ?>
661 acting: $( "#acting" ).val(),
662 wc_nonce: $( "#wc_nonce" ).val()
663 }
664 }).done( function( retVal, dataType ) {
665 if ( retVal.result ) {
666 $( "#settlement-response" ).html( retVal.result );
667 $( "#settlement-status-" + order_num ).html( retVal.acting_status );
668 }
669 $( "#settlement-response-loading" ).html( "" );
670 }).fail( function( jqXHR, textStatus, errorThrown ) {
671 console.log( textStatus );
672 console.log( jqXHR.status );
673 console.log( errorThrown.message );
674 $( "#settlement-response-loading" ).html( "" );
675 });
676 return false;
677 },
678 refundPayPalCP : function( amount ) {
679 $( "#settlement-response" ).html( "" );
680 $( "#settlement-response-loading" ).html( '<img src="' + uscesL10n.USCES_PLUGIN_URL + '/images/loading.gif" />' );
681 var order_num = $( "#order_num" ).val();
682 $.ajax({
683 url: ajaxurl,
684 type: "POST",
685 cache: false,
686 dataType: "json",
687 data: {
688 action: "usces_admin_ajax",
689 mode: "refund_paypal_cp",
690 order_id: $( "#order_id" ).val(),
691 order_num: order_num,
692 tracking_id: $( "#tracking_id" ).val(),
693 member_id: $( "#member_id" ).val(),
694 amount: amount,
695 <?php if ( 'usces_continue' === $admin_page ) : ?>
696 con_id: $( "#con_id" ).val(),
697 <?php endif; ?>
698 acting: $( "#acting" ).val(),
699 wc_nonce: $( "#wc_nonce" ).val()
700 }
701 }).done( function( retVal, dataType ) {
702 if ( retVal.result ) {
703 $( "#settlement-response" ).html( retVal.result );
704 $( "#settlement-status-" + order_num ).html( retVal.acting_status );
705 }
706 $( "#settlement-response-loading" ).html( "" );
707 }).fail( function( jqXHR, textStatus, errorThrown ) {
708 console.log( textStatus );
709 console.log( jqXHR.status );
710 console.log( errorThrown.message );
711 $( "#settlement-response-loading" ).html( "" );
712 });
713 return false;
714 },
715 reSettlementPayPalCP : function( amount, intent ) {
716 $( "#settlement-response" ).html( "" );
717 $( "#settlement-response-loading" ).html( '<img src="' + uscesL10n.USCES_PLUGIN_URL + '/images/loading.gif" />' );
718 var order_num = $( "#order_num" ).val();
719 $.ajax({
720 url: ajaxurl,
721 type: "POST",
722 cache: false,
723 dataType: "json",
724 data: {
725 action: "usces_admin_ajax",
726 mode: "re_settlement_paypal_cp",
727 order_id: $( "#order_id" ).val(),
728 order_num: order_num,
729 tracking_id: $( "#tracking_id" ).val(),
730 member_id: $( "#member_id" ).val(),
731 amount: amount,
732 intent: intent,
733 <?php if ( 'usces_continue' === $admin_page ) : ?>
734 con_id: $( "#con_id" ).val(),
735 <?php endif; ?>
736 <?php if ( ! empty( $reg_id ) ) : ?>
737 reg_id: $( "#reg_id" ).val(),
738 <?php endif; ?>
739 acting: $( "#acting" ).val(),
740 wc_nonce: $( "#wc_nonce" ).val()
741 }
742 }).done( function( retVal, dataType ) {
743 if ( retVal.result ) {
744 $( "#settlement-response" ).html( retVal.result );
745 $( "#settlement-status-" + order_num ).html( retVal.acting_status );
746 }
747 $( "#settlement-response-loading" ).html( "" );
748 }).fail( function( jqXHR, textStatus, errorThrown ) {
749 console.log( textStatus );
750 console.log( jqXHR.status );
751 console.log( errorThrown.message );
752 $( "#settlement-response-loading" ).html( "" );
753 });
754 return false;
755 },
756 };
757
758 $( "#settlement_dialog" ).dialog({
759 dialogClass: "admin-paypal-dialog",
760 bgiframe: true,
761 autoOpen: false,
762 height: "auto",
763 width: 800,
764 resizable: true,
765 modal: true,
766 buttons: {
767 "<?php esc_html_e( 'Close' ); ?>": function() {
768 $( this ).dialog( "close" );
769 }
770 },
771 open: function() {
772 adminOrderEdit.getPayPalCP();
773 },
774 close: function() {
775 <?php do_action( 'usces_action_paypalcp_settlement_dialog_close', $args ); ?>
776 }
777 });
778
779 $( document ).on( "click", ".settlement-information", function() {
780 var tracking_id = $( this ).attr( "data-tracking_id" );
781 var order_num = $( this ).attr( "data-num" );
782 $( "#tracking_id" ).val( tracking_id );
783 $( "#order_num" ).val( order_num );
784 $( "#settlement_dialog" ).dialog( "option", "title", "<?php echo esc_js( __( 'PayPal Commerce Platform', 'usces' ) ); ?>" );
785 $( "#settlement_dialog" ).dialog( "open" );
786 });
787
788 $( document ).on( "click", "#capture-settlement", function() {
789 var amount_original = parseFloat( $( "#amount_original" ).val() ) || 0;
790 var amount_refund = parseFloat( $( "#amount_refund" ).val() ) || 0;
791 var amount_capture = amount_original - amount_refund;
792 if ( 0 == amount_capture ) {
793 return;
794 }
795 if ( amount_capture == amount_original ) {
796 if ( ! confirm( "<?php esc_html_e( 'Execute the capture payment process. Are you sure?', 'usces' ); ?>" ) ) {
797 return;
798 }
799 } else {
800 if ( ! confirm( <?php printf( __( "'Execute the refund payment process for %s' + amount_refund + '. Are you sure?'", 'usces' ), $currency ); ?> ) ) {
801 return;
802 }
803 }
804 adminOrderEdit.capturePayPalCP( amount_capture );
805 });
806
807 $( document ).on( "click", "#void-settlement", function() {
808 if ( ! confirm( "<?php esc_html_e( 'Execute the void payment process. Are you sure?', 'usces' ); ?>" ) ) {
809 return;
810 }
811 adminOrderEdit.voidPayPalCP();
812 });
813
814 $( document ).on( "click", "#refund-settlement", function() {
815 var amount_original = parseFloat( $( "#amount_original" ).val() ) || 0;
816 var amount_refund = parseFloat( $( "#amount_refund" ).val() ) || 0;
817 if ( 0 == amount_refund ) {
818 return;
819 }
820 if ( amount_refund > amount_original ) {
821 alert( "<?php esc_html_e( 'Amounts in excess of the transaction amount are not refundable.', 'usces' ); ?>" );
822 return;
823 }
824 if ( amount_refund == amount_original ) {
825 if ( ! confirm( "<?php esc_html_e( 'Execute the refund payment process. Are you sure?', 'usces' ); ?>" ) ) {
826 return;
827 }
828 } else {
829 if ( ! confirm( <?php printf( __( "'Execute the refund payment process for %s' + amount_refund + '. Are you sure?'", 'usces' ), $currency ); ?> ) ) {
830 return;
831 }
832 }
833 adminOrderEdit.refundPayPalCP( amount_refund );
834 });
835
836 $( document ).on( "click", "#re-authorize-settlement", function() {
837 var amount_authorize = parseFloat( $( "#amount_resettlement" ).val() ) || 0;
838 if ( 0 == amount_authorize ) {
839 return;
840 }
841 if ( ! confirm( <?php printf( __( "'Execute the void payment process for %s' + amount_authorize + '. Are you sure?'", 'usces' ), $currency ); ?> ) ) {
842 return;
843 }
844 adminOrderEdit.reSettlementPayPalCP( amount_authorize, 'AUTHORIZE' );
845 });
846
847 $( document ).on( "click", "#re-capture-settlement", function() {
848 var amount_capture = parseFloat( $( "#amount_resettlement" ).val() ) || 0.0;
849 if ( 0 == amount_capture ) {
850 return;
851 }
852 if ( ! confirm( <?php printf( __( "'Execute the capture payment process for %s' + amount_capture + '. Are you sure?'", 'usces' ), $currency ); ?> ) ) {
853 return;
854 }
855 adminOrderEdit.reSettlementPayPalCP( amount_capture, 'CAPTURE' );
856 });
857
858 $( document ).on( "keydown", ".amount", function( e ) {
859 var halfVal = $( this ).val().replace( /[-]/g,
860 function( tmpStr ) {
861 return String.fromCharCode( tmpStr.charCodeAt(0) - 0xFEE0 );
862 }
863 );
864 $( this ).val( halfVal.replace( /[^0-9]/g, '' ) );
865 });
866 $( document ).on( "keyup", ".amount", function() {
867 this.value = this.value.replace( /[^0-9]+/i, '' );
868 this.value = Number( this.value ) || 0;
869 });
870 $( document ).on( "blur", ".amount", function() {
871 this.value = this.value.replace( /[^0-9]+/i, '' );
872 });
873 <?php if ( 'usces_continue' === $admin_page ) : ?>
874 adminContinuation = {
875 update : function() {
876 $.ajax({
877 url: ajaxurl,
878 type: "POST",
879 cache: false,
880 dataType: 'json',
881 data: {
882 action: "usces_admin_ajax",
883 mode: "continuation_update",
884 member_id: $( "#member_id" ).val(),
885 order_id: $( "#order_id" ).val(),
886 contracted_year: $( "#contracted-year option:selected" ).val(),
887 contracted_month: $( "#contracted-month option:selected" ).val(),
888 contracted_day: $( "#contracted-day option:selected" ).val(),
889 charged_year: $( "#charged-year option:selected" ).val(),
890 charged_month: $( "#charged-month option:selected" ).val(),
891 charged_day: $( "#charged-day option:selected" ).val(),
892 price: $( "#price" ).val(),
893 status: $( "#dlseller-status" ).val(),
894 wc_nonce: $( "#wc_nonce" ).val()
895 }
896 }).done( function( retVal, dataType ) {
897 if ( "OK" == retVal.status ) {
898 adminOperation.setActionStatus( "success", "<?php esc_html_e( 'The update was completed.', 'usces' ); ?>" );
899 } else {
900 var message = ( retVal.message != "" ) ? retVal.message : "<?php esc_html_e( 'failure in update', 'usces' ); ?>";
901 adminOperation.setActionStatus( "error", message );
902 }
903 }).fail( function( jqXHR, textStatus, errorThrown ) {
904 console.log( textStatus );
905 console.log( jqXHR.status );
906 console.log( errorThrown.message );
907 adminOperation.setActionStatus( "error", "<?php esc_html_e( 'failure in update', 'usces' ); ?>" );
908 });
909 return false;
910 }
911 };
912
913 $( document ).on( "click", "#continuation-update", function() {
914 var status = $( "#dlseller-status option:selected" ).val();
915 if ( "continuation" == status ) {
916 var year = $( "#charged-year option:selected" ).val();
917 var month = $( "#charged-month option:selected" ).val();
918 var day = $( "#charged-day option:selected" ).val();
919 if ( 0 == year || 0 == month || 0 == day ) {
920 alert( "<?php esc_html_e( 'Data have deficiency.', 'usces' ); ?>" );
921 $( "#charged-year" ).focus();
922 return;
923 }
924 if ( "" == $( "#price" ).val() || 0 == parseFloat( $( "#price" ).val() ) ) {
925 alert( "<?php printf( __( 'Input the %s', 'usces' ), esc_html__( 'Amount', 'dlseller' ) ); ?>" );
926 $( "#price" ).focus();
927 return;
928 }
929 }
930 if ( ! confirm( "<?php esc_html_e( 'Are you sure you want to update the settings?', 'usces' ); ?>" ) ) {
931 return;
932 }
933 adminContinuation.update();
934 });
935 <?php endif; ?>
936 });
937 </script>
938 <?php
939 endif;
940 break;
941 endswitch;
942 }
943
944 /**
945 * 決済オプション登録・更新
946 * usces_action_admin_settlement_update
947 */
948 public function settlement_update() {
949 global $usces;
950
951 if ( $this->paymod_id != wp_unslash( $_POST['acting'] ) ) {
952 return;
953 }
954
955 $this->error_mes = '';
956 $options = get_option( 'usces', array() );
957 $payment_method = usces_get_system_option( 'usces_payment_method', 'settlement' );
958 $post_data = wp_unslash( $_POST );
959
960 unset( $options['acting_settings']['paypal_cp'] );
961 $options['acting_settings']['paypal_cp']['cp_activate'] = ( isset( $post_data['cp_activate'] ) ) ? $post_data['cp_activate'] : 'off';
962 $options['acting_settings']['paypal_cp']['card_activate'] = ( isset( $post_data['card_activate'] ) ) ? $post_data['card_activate'] : 'off';
963 $options['acting_settings']['paypal_cp']['card_vault'] = ( isset( $post_data['card_vault'] ) ) ? $post_data['card_vault'] : 'off';
964 $options['acting_settings']['paypal_cp']['card_prefix'] = ( isset( $post_data['card_prefix'] ) ) ? $post_data['card_prefix'] : '';
965 $options['acting_settings']['paypal_cp']['card_3ds'] = ( isset( $post_data['card_3ds'] ) ) ? $post_data['card_3ds'] : 'off';
966 $options['acting_settings']['paypal_cp']['environment'] = ( isset( $post_data['cp_environment'] ) ) ? $post_data['cp_environment'] : 'live';
967 $options['acting_settings']['paypal_cp']['client_id'] = ( isset( $post_data['cp_client_id'] ) ) ? trim( $post_data['cp_client_id'] ) : '';
968 $options['acting_settings']['paypal_cp']['secret'] = ( isset( $post_data['cp_secret'] ) ) ? trim( $post_data['cp_secret'] ) : '';
969 $options['acting_settings']['paypal_cp']['intent'] = ( isset( $post_data['cp_intent'] ) ) ? $post_data['cp_intent'] : 'CAPTURE';
970 $options['acting_settings']['paypal_cp']['autobilling_intent'] = ( isset( $post_data['cp_autobilling_intent'] ) ) ? $post_data['cp_autobilling_intent'] : 'CAPTURE';
971 $options['acting_settings']['paypal_cp']['autobilling_email'] = ( isset( $post_data['cp_autobilling_email'] ) ) ? $post_data['cp_autobilling_email'] : 'off';
972 $options['acting_settings']['paypal_cp']['button_layout'] = ( isset( $post_data['cp_button_layout'] ) ) ? $post_data['cp_button_layout'] : 'vertical';
973 $options['acting_settings']['paypal_cp']['button_color'] = ( isset( $post_data['cp_button_color'] ) ) ? $post_data['cp_button_color'] : 'gold';
974 $options['acting_settings']['paypal_cp']['button_shape'] = ( isset( $post_data['cp_button_shape'] ) ) ? $post_data['cp_button_shape'] : 'rect';
975 $options['acting_settings']['paypal_cp']['button_label'] = ( isset( $post_data['cp_button_label'] ) ) ? $post_data['cp_button_label'] : 'paypal';
976 $options['acting_settings']['paypal_cp']['agree'] = ( isset( $post_data['cp_agree'] ) ) ? $post_data['cp_agree'] : '';
977
978 if ( 'on' === $options['acting_settings']['paypal_cp']['cp_activate'] || 'on' === $options['acting_settings']['paypal_cp']['card_activate'] ) {
979 $unavailable_activate = false;
980 foreach ( $payment_method as $settlement => $payment ) {
981 if ( in_array( $settlement, $this->unavailable_method ) && 'deactivate' !== $payment['use'] ) {
982 $unavailable_activate = true;
983 break;
984 }
985 }
986
987 if ( $unavailable_activate ) {
988 $this->error_mes .= __( '* Settlement that can not be used together is activated.', 'usces' ) . '<br />';
989 } else {
990 if ( WCUtils::is_blank( $post_data['cp_client_id'] ) ) {
991 $this->error_mes .= __( '* Enter Client ID.', 'usces' ) . '<br />';
992 }
993 if ( WCUtils::is_blank( $post_data['cp_secret'] ) ) {
994 $this->error_mes .= __( '* Enter Secret.', 'usces' ) . '<br />';
995 }
996 if ( 'on' === $options['acting_settings']['paypal_cp']['card_activate'] && 'on' === $options['acting_settings']['paypal_cp']['card_vault'] ) {
997 if ( ! $this->check_prefix( $options['acting_settings']['paypal_cp']['card_prefix'] ) ) {
998 $this->error_mes .= __( '* The prefix must be at least one and no more than four single-byte uppercase alphabetic characters.', 'usces' ) . '<br />';
999 }
1000 }
1001 if ( empty( $options['acting_settings']['paypal_cp']['agree'] ) ) {
1002 $this->error_mes .= __( '* There was no agreement on terms of use.', 'usces' ) . '<br />';
1003 }
1004 }
1005 }
1006
1007 if ( '' === $this->error_mes ) {
1008 $usces->action_status = 'success';
1009 $usces->action_message = __( 'Options are updated.', 'usces' );
1010 if ( 'on' === $options['acting_settings']['paypal_cp']['cp_activate'] || 'on' === $options['acting_settings']['paypal_cp']['card_activate'] ) {
1011 $options['acting_settings']['paypal_cp']['activate'] = 'on';
1012 if ( 'live' === $options['acting_settings']['paypal_cp']['environment'] ) {
1013 $options['acting_settings']['paypal_cp']['api_request_url'] = self::API_URL;
1014 } else {
1015 $options['acting_settings']['paypal_cp']['api_request_url'] = self::API_SANDBOX_URL;
1016 }
1017 $toactive = array();
1018 if ( 'on' === $options['acting_settings']['paypal_cp']['cp_activate'] ) {
1019 $usces->payment_structure['acting_paypal_cp'] = $this->acting_name;
1020 foreach ( $payment_method as $settlement => $payment ) {
1021 if ( 'acting_paypal_cp' === $settlement && 'activate' !== $payment['use'] ) {
1022 $toactive[] = $payment['name'];
1023 }
1024 }
1025 }
1026 if ( 'on' === $options['acting_settings']['paypal_cp']['card_activate'] ) {
1027 $usces->payment_structure['acting_paypal_card'] = __( 'PayPal(CreditCard)', 'usces' );
1028 foreach ( $payment_method as $settlement => $payment ) {
1029 if ( 'acting_paypal_card' === $settlement && 'activate' !== $payment['use'] ) {
1030 $toactive[] = $payment['name'];
1031 }
1032 }
1033 }
1034 if ( 0 < count( $toactive ) ) {
1035 $usces->action_message .= __( 'Please update the payment method to "Activate". <a href="admin.php?page=usces_initial#payment_method_setting">General Setting > Payment Methods</a>', 'usces' );
1036 }
1037 usces_admin_orderlist_show_wc_trans_id();
1038 $this->setting_update_notification( $options );
1039 } else {
1040 $options['acting_settings']['paypal_cp']['activate'] = 'off';
1041 unset( $usces->payment_structure['acting_paypal_cp'] );
1042 unset( $usces->payment_structure['acting_paypal_card'] );
1043 $deactivate = array();
1044 foreach ( $payment_method as $settlement => $payment ) {
1045 if ( ! array_key_exists( $settlement, $usces->payment_structure ) ) {
1046 if ( 'deactivate' !== $payment['use'] ) {
1047 $payment['use'] = 'deactivate';
1048 $deactivate[] = $payment['name'];
1049 usces_update_system_option( 'usces_payment_method', $payment['id'], $payment );
1050 }
1051 }
1052 }
1053 if ( 0 < count( $deactivate ) ) {
1054 $deactivate_message = sprintf( __( '"Deactivate" %s of payment method.', 'usces' ), implode( ',', $deactivate ) );
1055 $usces->action_message .= $deactivate_message;
1056 }
1057 }
1058 } else {
1059 $usces->action_status = 'error';
1060 $usces->action_message = __( 'Data have deficiency.', 'usces' );
1061 $options = get_option( 'usces', array() );
1062 $options['acting_settings']['paypal_cp']['activate'] = 'off';
1063 $options['acting_settings']['paypal_cp']['agree'] = '';
1064 unset( $usces->payment_structure['acting_paypal_cp'] );
1065 unset( $usces->payment_structure['acting_paypal_card'] );
1066 $deactivate = array();
1067 foreach ( $payment_method as $settlement => $payment ) {
1068 if ( in_array( $settlement, $this->pay_method ) ) {
1069 if ( 'deactivate' !== $payment['use'] ) {
1070 $payment['use'] = 'deactivate';
1071 $deactivate[] = $payment['name'];
1072 usces_update_system_option( 'usces_payment_method', $payment['id'], $payment );
1073 }
1074 }
1075 }
1076 if ( 0 < count( $deactivate ) ) {
1077 $deactivate_message = sprintf( __( '"Deactivate" %s of payment method.', 'usces' ), implode( ',', $deactivate ) );
1078 $usces->action_message .= $deactivate_message . __( 'Please complete the setup and update the payment method to "Activate".', 'usces' );
1079 }
1080 }
1081 ksort( $usces->payment_structure );
1082 update_option( 'usces', $options );
1083 update_option( 'usces_payment_structure', $usces->payment_structure );
1084 }
1085
1086 /**
1087 * 決済設定通知
1088 *
1089 * @param array $options Setting values.
1090 */
1091 private function setting_update_notification( $options ) {
1092 $message = 'サイト名/屋号:' . html_entity_decode( get_option( 'blogname' ) ) . "\n" .
1093 'サイトURL:' . esc_url( home_url( '/' ) ) . "\n" .
1094 'Eメールアドレス:' . get_option( 'admin_email' ) . "\n" .
1095 '動作環境:' . $options['acting_settings']['paypal_cp']['environment'] . "\n";
1096 $sendmail_params = array(
1097 'to_name' => 'PayPal',
1098 'to_address' => 'welcart-setting-update@paypal.com',
1099 'from_name' => get_option( 'blogname' ),
1100 'from_address' => get_option( 'admin_email' ),
1101 'reply_name' => get_option( 'blogname' ),
1102 'reply_to' => usces_get_first_order_mail(),
1103 'return_path' => get_option( 'admin_email' ),
1104 'subject' => '[Welcart]PayPal決済設定通知',
1105 'message' => do_shortcode( $message ),
1106 );
1107 usces_send_mail( $sendmail_params );
1108 }
1109
1110 /**
1111 * クレジット決済設定画面タブ
1112 * usces_action_settlement_tab_title
1113 */
1114 public function settlement_tab_title() {
1115 $settlement_selected = get_option( 'usces_settlement_selected', array() );
1116 if ( in_array( $this->paymod_id, (array) $settlement_selected ) ) {
1117 echo '<li><a href="#uscestabs_' . esc_html( $this->paymod_id ) . '">' . esc_html( $this->acting_name ) . '</a></li>';
1118 }
1119 }
1120
1121 /**
1122 * クレジット決済設定画面フォーム
1123 * usces_action_settlement_tab_body
1124 */
1125 public function settlement_tab_body() {
1126 global $usces;
1127
1128 $acting_opts = $this->get_acting_settings();
1129 $settlement_selected = get_option( 'usces_settlement_selected', array() );
1130 if ( in_array( $this->paymod_id, (array) $settlement_selected ) ) :
1131 $cp_activate = ( isset( $acting_opts['cp_activate'] ) && 'on' === $acting_opts['cp_activate'] ) ? 'on' : 'off';
1132 $card_activate = ( isset( $acting_opts['card_activate'] ) && 'on' === $acting_opts['card_activate'] ) ? 'on' : 'off';
1133 $card_vault = ( isset( $acting_opts['card_vault'] ) && 'on' === $acting_opts['card_vault'] ) ? 'on' : 'off';
1134 $card_prefix = ( isset( $acting_opts['card_prefix'] ) ) ? $acting_opts['card_prefix'] : '';
1135 $card_3ds = ( isset( $acting_opts['card_3ds'] ) && 'on' === $acting_opts['card_3ds'] ) ? 'on' : 'off';
1136 $cp_environment = ( isset( $acting_opts['environment'] ) && 'live' === $acting_opts['environment'] ) ? 'live' : 'sandbox';
1137 $cp_client_id = ( isset( $acting_opts['client_id'] ) ) ? $acting_opts['client_id'] : '';
1138 $cp_secret = ( isset( $acting_opts['secret'] ) ) ? $acting_opts['secret'] : '';
1139 $cp_intent = ( isset( $acting_opts['intent'] ) ) ? $acting_opts['intent'] : '';
1140 if ( defined( 'WCEX_DLSELLER' ) ) {
1141 $cp_autobilling_intent = ( isset( $acting_opts['autobilling_intent'] ) ) ? $acting_opts['autobilling_intent'] : '';
1142 $cp_autobilling_email = ( isset( $acting_opts['autobilling_email'] ) && 'on' === $acting_opts['autobilling_email'] ) ? 'on' : 'off';
1143 }
1144 $cp_button_layout = ( isset( $acting_opts['button_layout'] ) && 'horizontal' === $acting_opts['button_layout'] ) ? 'horizontal' : 'vertical';
1145 $class_button_color = '';
1146 $cp_button_color_gold = '';
1147 $cp_button_color_blue = '';
1148 $cp_button_color_silver = '';
1149 $cp_button_color_white = '';
1150 $cp_button_color_black = '';
1151 if ( isset( $acting_opts['button_color'] ) && 'blue' === $acting_opts['button_color'] ) {
1152 $cp_button_color_blue = ' checked="checked"';
1153 $class_button_color = ' color-blue';
1154 } elseif ( isset( $acting_opts['button_color'] ) && 'silver' === $acting_opts['button_color'] ) {
1155 $cp_button_color_silver = ' checked="checked"';
1156 $class_button_color = ' color-silver';
1157 } elseif ( isset( $acting_opts['button_color'] ) && 'white' === $acting_opts['button_color'] ) {
1158 $cp_button_color_white = ' checked="checked"';
1159 $class_button_color = ' color-white';
1160 } elseif ( isset( $acting_opts['button_color'] ) && 'black' === $acting_opts['button_color'] ) {
1161 $cp_button_color_black = ' checked="checked"';
1162 $class_button_color = ' color-black';
1163 } else {
1164 $cp_button_color_gold = ' checked="checked"'; /* default */
1165 $class_button_color = ' color-gold';
1166 }
1167 $class_button_shape = '';
1168 $cp_button_shape_rect = '';
1169 $cp_button_shape_pill = '';
1170 if ( isset( $acting_opts['button_shape'] ) && 'pill' === $acting_opts['button_shape'] ) {
1171 $cp_button_shape_pill = ' checked="checked"';
1172 $class_button_shape = ' shape-pill';
1173 } else {
1174 $cp_button_shape_rect = ' checked="checked"'; /* default */
1175 $class_button_shape = ' shape-rect';
1176 }
1177 $class_button_label = '';
1178 $cp_button_label_paypal = '';
1179 $cp_button_label_checkout = '';
1180 $cp_button_label_buynow = '';
1181 $cp_button_label_pay = '';
1182 if ( isset( $acting_opts['button_label'] ) && 'checkout' === $acting_opts['button_label'] ) {
1183 $cp_button_label_checkout = ' checked="checked"';
1184 $class_button_label = ' label-checkout';
1185 } elseif ( isset( $acting_opts['button_label'] ) && 'buynow' === $acting_opts['button_label'] ) {
1186 $cp_button_label_buynow = ' checked="checked"';
1187 $class_button_label = ' label-buynow';
1188 } elseif ( isset( $acting_opts['button_label'] ) && 'pay' === $acting_opts['button_label'] ) {
1189 $cp_button_label_pay = ' checked="checked"';
1190 $class_button_label = ' label-pay';
1191 } else {
1192 $cp_button_label_paypal = ' checked="checked"'; /* default */
1193 $class_button_label = ' label-paypal';
1194 }
1195 $cp_agree = ( isset( $acting_opts['agree'] ) && 'agree' === $acting_opts['agree'] ) ? 'agree' : '';
1196 ?>
1197 <div id="uscestabs_paypal_cp">
1198 <div class="settlement_service"><span class="service_title"><?php esc_html_e( 'PayPal Commerce Platform', 'usces' ); ?></span></div>
1199 <?php
1200 if ( isset( $_POST['acting'] ) && $this->paymod_id === wp_unslash( $_POST['acting'] ) ) :
1201 if ( ! empty( $this->error_mes ) ) :
1202 ?>
1203 <div class="error_message"><?php wel_esc_script_e( $this->error_mes ); ?></div>
1204 <?php
1205 elseif ( isset( $acting_opts['activate'] ) && 'on' === $acting_opts['activate'] ) :
1206 ?>
1207 <div class="message"><?php esc_html_e( 'Test thoroughly before use.', 'usces' ); ?></div>
1208 <?php
1209 endif;
1210 endif;
1211 ?>
1212 <form action="" method="post" name="paypal_cp_form" id="paypal_cp_form">
1213 <table class="settle_table">
1214 <tr>
1215 <th><?php esc_html_e( 'PayPal Commerce Platform', 'usces' ); ?></th>
1216 <td><label><input type="radio" class="activate_paypal_cp" name="cp_activate" value="on"<?php checked( $cp_activate, 'on' ); ?>/><span><?php esc_html_e( 'Use', 'usces' ); ?></span></label><br />
1217 <label><input type="radio" class="activate_paypal_cp" name="cp_activate" value="off"<?php checked( $cp_activate, 'off' ); ?>/><span><?php esc_html_e( 'Do not Use', 'usces' ); ?></span></label>
1218 </td>
1219 </tr>
1220 <tr>
1221 <th><a class="explanation-label" id="label_ex_card_activate_paypal_cp"><?php esc_html_e( 'Credit card settlement', 'usces' ); ?></a></th>
1222 <td><label><input type="radio" class="activate_paypal_card" name="card_activate" value="on"<?php checked( $card_activate, 'on' ); ?>/><span><?php esc_html_e( 'Use', 'usces' ); ?></span></label><br />
1223 <label><input type="radio" class="activate_paypal_card" name="card_activate" value="off"<?php checked( $card_activate, 'off' ); ?>/><span><?php esc_html_e( 'Do not Use', 'usces' ); ?></span></label>
1224 </td>
1225 </tr>
1226 <tr id="ex_card_activate_paypal_cp" class="explanation"><td colspan="2"><?php esc_html_e( 'Advanced credit and debit cards requires that your business account be evaluated and approved by PayPal.', 'usces' ); ?></td></tr>
1227 <tr class="paypal_card_form">
1228 <th><a class="explanation-label" id="label_ex_card_vault_paypal_cp"><?php esc_html_e( 'Save credit card information', 'usces' ); ?></a></th>
1229 <td><label><input type="radio" name="card_vault" value="on"<?php checked( $card_vault, 'on' ); ?>/><span><?php esc_html_e( 'Save', 'usces' ); ?></span></label>
1230 <label><input type="text" name="card_prefix" value="<?php echo esc_attr( $card_prefix ); ?>" class="small-text"><span><?php esc_html_e( '* Prefix', 'usces' ); ?></span></label><br />
1231 <label><input type="radio" name="card_vault" value="off"<?php checked( $card_vault, 'off' ); ?>/><span><?php esc_html_e( 'Not save', 'usces' ); ?></span></label>
1232 </td>
1233 </tr>
1234 <tr id="ex_card_vault_paypal_cp" class="explanation paypal_card_form"><td colspan="2"><?php esc_html_e( 'To use this feature, you need to apply for a Client ID with PayPal.', 'usces' ); ?><br /><?php esc_html_e( 'Also need to use the Welcart membership system.', 'usces' ); ?></td></tr>
1235 <tr class="paypal_card_form">
1236 <th><?php esc_html_e( '3D Secure', 'usces' ); ?></th>
1237 <td><label><input type="radio" name="card_3ds" value="on"<?php checked( $card_3ds, 'on' ); ?>/><span><?php esc_html_e( 'Use', 'usces' ); ?></span></label><br />
1238 <label><input type="radio" name="card_3ds" value="off"<?php checked( $card_3ds, 'off' ); ?>/><span><?php esc_html_e( 'Do not Use', 'usces' ); ?></span></label>
1239 </td>
1240 </tr>
1241 <tr class="paypal_cp_form">
1242 <th><a class="explanation-label" id="label_ex_environment_paypal_cp"><?php esc_html_e( 'Operation Environment', 'usces' ); ?></a></th>
1243 <td><div><label><input type="radio" class="cp_environment" name="cp_environment" value="live"<?php checked( $cp_environment, 'live' ); ?>/><span><?php esc_html_e( 'Live environment', 'usces' ); ?></span></label>
1244 <script>
1245 function onboardedCallback( authCode, sharedId ) {
1246 let params = new URLSearchParams();
1247 params.append( "action", "onboarded" );
1248 params.append( "authCode", authCode );
1249 params.append( "sharedId", sharedId );
1250 params.append( "seller_nonce", document.getElementById( "seller_nonce" ).value );
1251 params.append( "wc_nonce", document.getElementById( "wc_nonce" ).value );
1252 return fetch( ajaxurl, {
1253 method: 'POST',
1254 body: params
1255 }).then( function( res ) {
1256 return res.json();
1257 }).then( function( data ) {
1258 if ( data.client_id ) {
1259 document.getElementById( "cp_client_id" ).value = data.client_id;
1260 }
1261 if ( data.client_secret ) {
1262 document.getElementById( "cp_secret" ).value = data.client_secret;
1263 }
1264 });
1265 }
1266 </script>
1267 <a target="_blank" data-paypal-onboard-complete="onboardedCallback" href="" data-paypal-button="true" id="onboarding"></a>
1268 <input type="hidden" id="seller_nonce">
1269 <button type="button" id="upfront_onboarding_paypal_cp" value="upfront_onboarding" class="button"><?php esc_html_e( 'Sign up for PayPal', 'usces' ); ?></button>
1270 </div>
1271 <label><input type="radio" class="cp_environment" name="cp_environment" value="sandbox"<?php checked( $cp_environment, 'sandbox' ); ?>/><span><?php esc_html_e( 'Test environment (Sandbox)', 'usces' ); ?></span></label>
1272 </td>
1273 </tr>
1274 <tr id="ex_environment_paypal_cp" class="explanation paypal_cp_form"><td colspan="2"><?php esc_html_e( 'In the "production environment", you can "connect to PayPal account" to automatically retrieve the "Client ID" and "Secret". In the "test environment (Sandbox)", please manually obtain the Sandbox "Client ID" and "Secret".', 'usces' ); ?></td></tr>
1275 <tr class="paypal_cp_form">
1276 <th><?php esc_html_e( 'Client ID', 'usces' ); ?></th>
1277 <td><textarea name="cp_client_id" id="cp_client_id" class="regular-text code"><?php echo esc_attr( $cp_client_id ); ?></textarea></td>
1278 </tr>
1279 <tr class="paypal_cp_form">
1280 <th><?php esc_html_e( 'Secret', 'usces' ); ?></th>
1281 <td><textarea name="cp_secret" id="cp_secret" class="regular-text code"><?php echo esc_attr( $cp_secret ); ?></textarea></td>
1282 </tr>
1283 <tr class="paypal_cp_form">
1284 <th><a class="explanation-label" id="label_ex_intent_paypal_cp"><?php esc_html_e( 'Intent', 'usces' ); ?></a></th>
1285 <td><label><input type="radio" name="cp_intent" value="CAPTURE"<?php checked( $cp_intent, 'CAPTURE' ); ?>/><span><?php esc_html_e( 'Capture', 'usces' ); ?></span></label><br />
1286 <label><input type="radio" name="cp_intent" value="AUTHORIZE"<?php checked( $cp_intent, 'AUTHORIZE' ); ?>/><span><?php esc_html_e( 'Authorize', 'usces' ); ?></span></label>
1287 </td>
1288 </tr>
1289 <tr id="ex_intent_paypal_cp" class="explanation paypal_cp_form"><td colspan="2"><?php esc_html_e( 'Select whether to capture sales (CAPTURE) at the time of payment or only perform authorization (AUTHORIZE).', 'usces' ); ?></td></tr>
1290 <?php if ( defined( 'WCEX_DLSELLER' ) ) : ?>
1291 <tr class="paypal_cp_form">
1292 <th><a class="explanation-label" id="label_ex_autobilling_intent_cp"><?php esc_html_e( 'Automatic recurring billing intent', 'usces' ); ?></a></th>
1293 <td><label><input type="radio" name="cp_autobilling_intent" value="CAPTURE"<?php checked( $cp_autobilling_intent, 'CAPTURE' ); ?>/><span><?php esc_html_e( 'Capture', 'usces' ); ?></span></label><br />
1294 <label><input type="radio" name="cp_autobilling_intent" value="AUTHORIZE"<?php checked( $cp_autobilling_intent, 'AUTHORIZE' ); ?>/><span><?php esc_html_e( 'Authorize', 'usces' ); ?></span></label>
1295 </td>
1296 </tr>
1297 <tr id="ex_autobilling_intent_cp" class="explanation paypal_cp_form"><td colspan="2"><?php esc_html_e( 'Processing classification when automatic continuing charging (required WCEX DLSeller).', 'usces' ); ?></td></tr>
1298 <tr class="paypal_cp_form">
1299 <th><a class="explanation-label" id="label_ex_autobilling_email_paypal_cp"><?php esc_html_e( 'Automatic Continuing Charging Completion Mail', 'usces' ); ?></a></th>
1300 <td><label><input type="radio" name="cp_autobilling_email" value="on"<?php checked( $cp_autobilling_email, 'on' ); ?>/><span><?php esc_html_e( 'Send', 'usces' ); ?></span></label><br />
1301 <label><input type="radio" name="cp_autobilling_email" value="off"<?php checked( $cp_autobilling_email, 'off' ); ?>/><span><?php esc_html_e( "Don't send", 'usces' ); ?></span></label>
1302 </td>
1303 </tr>
1304 <tr id="ex_autobilling_email_paypal_cp" class="explanation paypal_cp_form"><td colspan="2"><?php esc_html_e( 'Send billing completion mail to the member on which automatic continuing charging processing (required WCEX DLSeller) is executed.', 'usces' ); ?></td></tr>
1305 <?php endif; ?>
1306 </table>
1307 <table class="settle_table paypal_cp_form">
1308 <tr>
1309 <th rowspan="4"><?php esc_html_e( 'Customize the PayPal Buttons', 'usces' ); ?>
1310 <div id="button_preview_paypal_cp" class="<?php echo esc_attr( $class_button_color ); ?><?php echo esc_attr( $class_button_shape ); ?><?php echo esc_attr( $class_button_label ); ?>"><span></span></div>
1311 </th>
1312 <td><span><?php esc_html_e( 'Layout', 'usces' ); ?></span><br />
1313 <label><input type="radio" name="cp_button_layout" value="vertical"<?php checked( $cp_button_layout, 'vertical' ); ?>/><span><?php esc_html_e( 'Arrange the buttons vertically.', 'usces' ); ?></span></label><br />
1314 <label><input type="radio" name="cp_button_layout" value="horizontal"<?php checked( $cp_button_layout, 'horizontal' ); ?>/><span><?php esc_html_e( 'Arrange the buttons horizontally.', 'usces' ); ?></span></label>
1315 </td>
1316 </tr>
1317 <tr>
1318 <td><span><?php esc_html_e( 'Color', 'usces' ); ?></span><br />
1319 <label><input type="radio" name="cp_button_color" class="button_color_paypal_cp" value="gold"<?php echo esc_attr( $cp_button_color_gold ); ?>/><span><?php esc_html_e( 'Gold', 'usces' ); ?></span></label><br />
1320 <label><input type="radio" name="cp_button_color" class="button_color_paypal_cp" value="blue"<?php echo esc_attr( $cp_button_color_blue ); ?>/><span><?php esc_html_e( 'Blue', 'usces' ); ?></span></label><br />
1321 <label><input type="radio" name="cp_button_color" class="button_color_paypal_cp" value="silver"<?php echo esc_attr( $cp_button_color_silver ); ?>/><span><?php esc_html_e( 'Silver', 'usces' ); ?></span></label><br />
1322 <label><input type="radio" name="cp_button_color" class="button_color_paypal_cp" value="white"<?php echo esc_attr( $cp_button_color_white ); ?>/><span><?php esc_html_e( 'White', 'usces' ); ?></span></label><br />
1323 <label><input type="radio" name="cp_button_color" class="button_color_paypal_cp" value="black"<?php echo esc_attr( $cp_button_color_black ); ?>/><span><?php esc_html_e( 'Black', 'usces' ); ?></span></label>
1324 </td>
1325 </tr>
1326 <tr>
1327 <td><span><?php esc_html_e( 'Shape', 'usces' ); ?></span><br />
1328 <label><input type="radio" name="cp_button_shape" class="button_shape_paypal_cp" value="rect"<?php echo esc_attr( $cp_button_shape_rect ); ?>/><span><?php esc_html_e( 'Rect', 'usces' ); ?></span></label><br />
1329 <label><input type="radio" name="cp_button_shape" class="button_shape_paypal_cp" value="pill"<?php echo esc_attr( $cp_button_shape_pill ); ?>/><span><?php esc_html_e( 'Pill', 'usces' ); ?></span></label>
1330 </td>
1331 </tr>
1332 <tr>
1333 <td><span><?php esc_html_e( 'Label', 'usces' ); ?></span><br />
1334 <label><input type="radio" name="cp_button_label" class="button_label_paypal_cp" value="paypal"<?php echo esc_attr( $cp_button_label_paypal ); ?>/><span><?php esc_html_e( 'Display the PayPal logo.', 'usces' ); ?></span></label><br />
1335 <label><input type="radio" name="cp_button_label" class="button_label_paypal_cp" value="checkout"<?php echo esc_attr( $cp_button_label_checkout ); ?>/><span><?php esc_html_e( 'Display the PayPal Checkout button.', 'usces' ); ?></span></label><br />
1336 <label><input type="radio" name="cp_button_label" class="button_label_paypal_cp" value="buynow"<?php echo esc_attr( $cp_button_label_buynow ); ?>/><span><?php esc_html_e( 'Display the PayPal Buy Now button.', 'usces' ); ?></span></label><br />
1337 <label><input type="radio" name="cp_button_label" class="button_label_paypal_cp" value="pay"<?php echo esc_attr( $cp_button_label_pay ); ?>/><span><?php esc_html_e( 'Display the Pay With PayPal button.', 'usces' ); ?></span></label>
1338 </td>
1339 </tr>
1340 </table>
1341 <input name="acting" type="hidden" value="paypal_cp" />
1342 <input name="usces_option_update" id="paypal_cp" type="submit" class="button button-primary" value="<?php esc_html_e( 'Update the PayPal Commerce Platform settings', 'usces' ); ?>" />
1343 <span class="paypal_cp_form_agree"><label><input type="checkbox" name="cp_agree" value="agree"<?php checked( $cp_agree, 'agree' ); ?> /><span><?php esc_html_e( 'I agree to the following terms and conditions of use.', 'usces' ); ?></span></label></span>
1344 <p class="paypal_cp_form_agree"><?php esc_html_e( 'You agree that the information you submit during the application process will be provided to our partner company PayPal Pte. Ltd. and will be used by PayPal to evaluate, improve and enhance its services and for marketing purposes and PayPal Pte. Ltd. may send you information for marketing and promotional purposes (including sending e-mails, etc.).', 'usces' ); ?></p>
1345 <?php wp_nonce_field( 'admin_settlement', 'wc_nonce' ); ?>
1346 </form>
1347 <div class="settle_exp">
1348 <p><strong><?php esc_html_e( 'PayPal Commerce Platform', 'usces' ); ?></strong></p>
1349 <p>問い合わせ�
1350 �<br />
1351 新規ビジネス・サービス導�
1352 �・お申込専用ライン<br />
1353 Tel:03-6739-7135 9:30~17:30(土日祝休)※通話料がかかります</p>
1354 <p>すでにペイパルアカウントをお持ちの方(カスタマーサービス)<br />
1355 Tel:0120-271-888 または 03-6739-7360(携帯電話と海外からはこちら)※通話料がかかります<br />
1356 9:00~18:00(土日休)</p>
1357 <p><a href="https://www.paypal.com/jp/webapps/mpp/corporate/contact">WEBでのお問い合わせ</a>
1358 <?php
1359 $wcex = '';
1360 $billing = '';
1361 if ( defined( 'WCEX_DLSELLER' ) ) {
1362 $wcex = 'WCEX DLSeller での自動継続課金';
1363 $billing = '自動継続課金';
1364 } elseif ( defined( 'WCEX_AUTO_DELIVERY' ) ) {
1365 $wcex = 'WCEX Auto Delivery での定期購�
1366 �';
1367 $billing = '定期購�
1368 �';
1369 }
1370 if ( '' !== $wcex ) :
1371 ?>
1372 <p><?php echo esc_html( $wcex ); ?>を行う場合は、ペイパルへの「従量課金」の利用申請・審査が�
1373 要となります。<br />
1374 利用申請については、<a href="https://www.paypal.com/jp/smarthelp/contact-us">こちら</a>からお問い合わせください。<br />
1375 ※上記の問い合わせページで「その他のお問い合わせ方法」の下にある「メッセージを送る」を選択ください。<br />
1376 ※メッセージボックスに、「Welcart にて Reference Transaction を利用するための審査・設定をしてほしい」旨をご記�
1377 �ください。</p>
1378 <?php
1379 endif;
1380 ?>
1381 <p>クレジットカード決済のご利用には別途ペイパルへのお申込みと審査が�
1382 要です。<br />
1383 クレジットカード決済の申込みは<a href="https://www.paypal.com/jp/webapps/mpp/contact-sales">こちら</a>から行えます。</p>
1384 </div>
1385 </div><!--uscestabs_paypal_cp-->
1386 <?php
1387 endif;
1388 }
1389
1390 /**
1391 * 支払方法 JavaScript 用決済名追加
1392 * usces_filter_payments_str
1393 *
1394 * @param string $payments_str Payments.
1395 * @param array $payment Selected payment.
1396 * @return string
1397 */
1398 public function payments_str( $payments_str, $payment ) {
1399 if ( 'acting_paypal_card' === $payment['settlement'] && 'activate' === $payment['use'] ) {
1400 $payments_str .= "'" . $payment['name'] . "': 'paypal_card_form', ";
1401 }
1402 return $payments_str;
1403 }
1404
1405 /**
1406 * 支払方法 JavaScript 用決済追加
1407 * usces_filter_payments_arr
1408 *
1409 * @param array $payments_arr Payments.
1410 * @param array $payment Selected payment.
1411 * @return array
1412 */
1413 public function payments_arr( $payments_arr, $payment ) {
1414 if ( 'acting_paypal_card' === $payment['settlement'] && 'activate' === $payment['use'] ) {
1415 $payments_arr[] = 'paypal_card_form';
1416 }
1417 return $payments_arr;
1418 }
1419
1420 /**
1421 * 支払方法ページ用�
1422 �力フォーム
1423 * usces_filter_delivery_secure_form_loop
1424 *
1425 * @param string $nouse Empty.
1426 * @param array $payment Payment data.
1427 * @return string
1428 */
1429 public function delivery_secure_form_loop( $nouse, $payment ) {
1430 global $usces;
1431
1432 $form = '';
1433 if ( isset( $payment['settlement'] ) && 'acting_paypal_card' === $payment['settlement'] && 'activate' === $payment['use'] ) {
1434 $acting_opts = $this->get_acting_settings();
1435 $card_vault = 'off';
1436 $payment_token_id = '';
1437 $payment_token_last_digits = '';
1438 $payment_token_brand = '';
1439 if ( usces_is_member_system() && usces_is_login() ) {
1440 $card_vault = ( isset( $acting_opts['card_vault'] ) ) ? $acting_opts['card_vault'] : 'off';
1441 if ( 'on' === $card_vault ) {
1442 $member = $usces->get_member();
1443
1444 /* Get Access Token */
1445 $access_token = $this->get_access_token();
1446
1447 $payment_tokens = $this->api_get_payment_tokens( $access_token, $member['ID'] );
1448 if ( ! empty( $payment_tokens['payment_tokens'] ) && is_array( $payment_tokens['payment_tokens'] ) ) {
1449 $tokens_count = count( $payment_tokens['payment_tokens'] );
1450 if ( 1 === $tokens_count ) {
1451 $payment_token = current( $payment_tokens['payment_tokens'] );
1452 $payment_token_id = $payment_token['id'];
1453 $payment_token_last_digits = $payment_token['source']['card']['last_digits'];
1454 $payment_token_brand = $payment_token['source']['card']['brand'];
1455 } else {
1456 foreach ( $payment_tokens['payment_tokens'] as $payment_token ) {
1457 $delete_status = $this->api_delete_payment_tokens( $access_token, $payment_token['id'] );
1458 }
1459 }
1460 }
1461 }
1462 }
1463 $tracking_id = usces_acting_key();
1464
1465 ob_start();
1466 ?>
1467 <table class="customer_form" id="paypal_card_form">
1468 <tbody><tr>
1469 <th scope="row"><em><?php esc_html_e( '*', 'usces' ); ?></em><?php esc_html_e( 'Credit card information', 'usces' ); ?></th>
1470 <td>
1471 <?php if ( 'on' === $card_vault && ! empty( $payment_token_id ) ) : ?>
1472 <div class="card_selection"><input type="radio" id="paypal_payment_card_vault" name="paypal_payment_card" value="vault" checked /><label for="paypal_payment_card_vault"><?php esc_html_e( 'Pay with vaulted card', 'usces' ); ?></label>
1473 <p class="card_last_digits"><?php echo esc_html( $payment_token_brand ); ?> <?php esc_html_e( 'Lower 4 digits', 'usces' ); ?>:<?php echo esc_html( $payment_token_last_digits ); ?></p>
1474 </div>
1475 <div class="card_selection"><input type="radio" id="paypal_payment_card_another" name="paypal_payment_card" value="another"><label for="paypal_payment_card_another"><?php esc_html_e( 'Pay with another card', 'usces' ); ?></label></div>
1476 <?php endif; ?>
1477 <div class="card_container">
1478 <div>
1479 <label for="card-number"><?php esc_html_e( 'Card Number', 'usces' ); ?></label>
1480 <div id="card-number" class="card_field"></div>
1481 </div>
1482 <div>
1483 <label for="expiration-date"><?php esc_html_e( 'Expiration Date', 'usces' ); ?></label>
1484 <div id="expiration-date" class="card_field"></div>
1485 </div>
1486 <div>
1487 <label for="cvv"><?php esc_html_e( 'CVV', 'usces' ); ?></label>
1488 <div id="cvv" class="card_field"></div>
1489 </div>
1490 <?php if ( 'on' === $card_vault ) : ?>
1491 <div>
1492 <input type="checkbox" id="vault" name="paypal_vault" value="vault">
1493 <label for="vault"><?php esc_html_e( 'Save your card', 'usces' ); ?></label>
1494 </div>
1495 <?php else : ?>
1496 <input type="hidden" id="vault" value="">
1497 <?php endif; ?>
1498 </div>
1499 <input type="hidden" id="paypal_tracking_id" name="paypal_tracking_id" value="<?php echo esc_attr( $tracking_id ); ?>" />
1500 <input type="hidden" id="paypal_resource_id" name="paypal_resource_id" value="" />
1501 <input type="hidden" id="paypal_payment_token_id " name="paypal_payment_token_id" value="<?php echo esc_attr( $payment_token_id ); ?>" />
1502 <input type="hidden" id="paypal_f" name="paypal_f" value="" />
1503 </td></tr></tbody>
1504 </table>
1505 <?php
1506 $form = ob_get_contents();
1507 ob_end_clean();
1508 }
1509 return $form;
1510 }
1511
1512 /**
1513 * 支払方法チェック
1514 * usces_filter_delivery_check
1515 *
1516 * @param string $mes Validation check message.
1517 * @return string
1518 */
1519 public function delivery_check( $mes ) {
1520 if ( ! isset( $_POST['offer']['payment_name'] ) ) {
1521 return $mes;
1522 }
1523
1524 if ( ! empty( $mes ) ) {
1525 return $mes;
1526 }
1527
1528 $payment = usces_get_payments_by_name( wp_unslash( $_POST['offer']['payment_name'] ) );
1529 if ( isset( $payment['settlement'] ) && 'acting_paypal_card' === $payment['settlement'] ) {
1530 if ( isset( $_POST['paypal_payment_card'] ) && 'vault' === wp_unslash( $_POST['paypal_payment_card'] ) ) {
1531 } else {
1532 if ( isset( $_POST['paypal_resource_id'] ) && empty( $_POST['paypal_resource_id'] ) ) {
1533 $mes .= __( 'Please enter the card information correctly.', 'usces' ) . '<br />';
1534 } else {
1535 if ( ! wel_check_credit_security() ) {
1536 $mes .= __( 'Update has been locked. Please contact the store administrator.', 'usces' ) . '<br />';
1537 }
1538 }
1539 }
1540 }
1541 return $mes;
1542 }
1543
1544 /**
1545 * �
1546 容確認ページ ポイントフォーム
1547 * usces_action_confirm_page_point_inform
1548 */
1549 public function e_point_inform() {
1550 $html = $this->point_inform( '' );
1551 wel_esc_script_e( $html );
1552 }
1553
1554 /**
1555 * �
1556 容確認ページ ポイントフォーム
1557 * usces_filter_confirm_point_inform
1558 *
1559 * @param string $html Input point form.
1560 * @return string
1561 */
1562 public function point_inform( $html ) {
1563 global $usces;
1564
1565 $entry = $usces->cart->get_entry();
1566 $payment = usces_get_payments_by_name( $entry['order']['payment_name'] );
1567 if ( isset( $payment['settlement'] ) && 'acting_paypal_card' === $payment['settlement'] ) {
1568 if ( isset( $_POST['paypal_tracking_id'] ) ) {
1569 $html .= '<input type="hidden" name="paypal_tracking_id" value="' . esc_attr( sanitize_text_field( wp_unslash( $_POST['paypal_tracking_id'] ) ) ) . '">';
1570 }
1571 if ( isset( $_POST['paypal_resource_id'] ) ) {
1572 $html .= '<input type="hidden" name="paypal_resource_id" value="' . esc_attr( sanitize_text_field( wp_unslash( $_POST['paypal_resource_id'] ) ) ) . '">';
1573 }
1574 if ( isset( $_POST['paypal_payment_card'] ) ) {
1575 $html .= '<input type="hidden" name="paypal_payment_card" value="' . esc_attr( sanitize_text_field( wp_unslash( $_POST['paypal_payment_card'] ) ) ) . '">';
1576 }
1577 if ( isset( $_POST['paypal_payment_token_id'] ) ) {
1578 $html .= '<input type="hidden" name="paypal_payment_token_id" value="' . esc_attr( sanitize_text_field( wp_unslash( $_POST['paypal_payment_token_id'] ) ) ) . '">';
1579 }
1580 if ( isset( $_POST['paypal_vault'] ) ) {
1581 $html .= '<input type="hidden" name="paypal_vault" value="' . esc_attr( sanitize_text_field( wp_unslash( $_POST['paypal_vault'] ) ) ) . '">';
1582 }
1583 if ( isset( $_POST['paypal_f'] ) ) {
1584 $html .= '<input type="hidden" name="paypal_f" value="' . esc_attr( sanitize_text_field( wp_unslash( $_POST['paypal_f'] ) ) ) . '">';
1585 }
1586 if ( isset( $_POST['paypal_liability_shift'] ) ) {
1587 $html .= '<input type="hidden" name="paypal_liability_shift" value="' . esc_attr( sanitize_text_field( wp_unslash( $_POST['paypal_liability_shift'] ) ) ) . '">';
1588 }
1589 if ( isset( $_POST['paypal_enrollment_status'] ) ) {
1590 $html .= '<input type="hidden" name="paypal_enrollment_status" value="' . esc_attr( sanitize_text_field( wp_unslash( $_POST['paypal_enrollment_status'] ) ) ) . '">';
1591 }
1592 if ( isset( $_POST['paypal_authentication_status'] ) ) {
1593 $html .= '<input type="hidden" name="paypal_authentication_status" value="' . esc_attr( sanitize_text_field( wp_unslash( $_POST['paypal_authentication_status'] ) ) ) . '">';
1594 }
1595 }
1596 return $html;
1597 }
1598
1599 /**
1600 * �
1601 容確認ページ [注文する] ボタン
1602 * usces_filter_confirm_inform
1603 *
1604 * @param string $form Purchase post form.
1605 * @param array $payment Payment method info.
1606 * @param string $acting_flg Payment type.
1607 * @param string $rand Welcart transaction key.
1608 * @param string $purchase_disabled Disable purchase button.
1609 * @return string
1610 */
1611 public function confirm_inform( $form, $payment, $acting_flg, $rand, $purchase_disabled ) {
1612 global $usces;
1613
1614 $entry = $usces->cart->get_entry();
1615 $cart = $usces->cart->get_cart();
1616 if ( empty( $entry ) || empty( $cart ) ) {
1617 return $form;
1618 }
1619 if ( empty( $entry['order']['total_full_price'] ) ) {
1620 return $form;
1621 }
1622
1623 if ( 'acting_paypal_cp' === $acting_flg ) {
1624 usces_save_order_acting_data( $rand );
1625 $form = '<form name="purchase_form" action="' . USCES_CART_URL . '" method="post" onKeyDown="if(event.keyCode == 13){return false;}">
1626 <input type="hidden" name="purchase" value="' . $acting_flg . '">
1627 <input type="hidden" name="tracking_id" value="' . $rand . '">
1628 <input type="hidden" name="resource_id">
1629 <input type="hidden" name="billing_token">
1630 <input type="hidden" name="_nonce" value="' . wp_create_nonce( $acting_flg ) . '">
1631 <div class="send paypal-cp-send"><div id="checkout_paypal_cp"></div></div>
1632 </form>
1633 <form action="' . USCES_CART_URL . '" method="post" onKeyDown="if(event.keyCode == 13){return false;}">
1634 <div class="send">
1635 ' . apply_filters( 'usces_filter_confirm_before_backbutton', '', $payment, $acting_flg, $rand ) . '
1636 <input name="backDelivery" type="submit" id="back_button" class="back_to_delivery_button" value="' . __( 'Back', 'usces' ) . '"' . apply_filters( 'usces_filter_confirm_prebutton', '' ) . ' />
1637 </div>';
1638
1639 } elseif ( 'acting_paypal_card' === $acting_flg ) {
1640 $tracking_id = ( isset( $_POST['paypal_tracking_id'] ) ) ? sanitize_text_field( wp_unslash( $_POST['paypal_tracking_id'] ) ) : '';
1641 $resource_id = ( isset( $_POST['paypal_resource_id'] ) ) ? sanitize_text_field( wp_unslash( $_POST['paypal_resource_id'] ) ) : '';
1642 $payment_card = ( isset( $_POST['paypal_payment_card'] ) ) ? sanitize_text_field( wp_unslash( $_POST['paypal_payment_card'] ) ) : '';
1643 $payment_token_id = ( isset( $_POST['paypal_payment_token_id'] ) ) ? sanitize_text_field( wp_unslash( $_POST['paypal_payment_token_id'] ) ) : '';
1644 $vault = ( isset( $_POST['paypal_vault'] ) ) ? sanitize_text_field( wp_unslash( $_POST['paypal_vault'] ) ) : '';
1645 $correlation_id = ( isset( $_POST['paypal_f'] ) ) ? sanitize_text_field( wp_unslash( $_POST['paypal_f'] ) ) : '';
1646 $liability_shift = ( isset( $_POST['paypal_liability_shift'] ) ) ? sanitize_text_field( wp_unslash( $_POST['paypal_liability_shift'] ) ) : '';
1647 $enrollment_status = ( isset( $_POST['paypal_enrollment_status'] ) ) ? sanitize_text_field( wp_unslash( $_POST['paypal_enrollment_status'] ) ) : '';
1648 $authentication_status = ( isset( $_POST['paypal_authentication_status'] ) ) ? sanitize_text_field( wp_unslash( $_POST['paypal_authentication_status'] ) ) : '';
1649 usces_save_order_acting_data( $tracking_id );
1650 $form = '<form name="purchase_form" action="' . USCES_CART_URL . '" method="post" onKeyDown="if(event.keyCode == 13){return false;}">
1651 <input type="hidden" name="purchase" value="' . $acting_flg . '">
1652 <input type="hidden" name="tracking_id" value="' . esc_attr( $tracking_id ) . '">
1653 <input type="hidden" name="resource_id" value="' . esc_attr( $resource_id ) . '">
1654 <input type="hidden" name="payment_card" value="' . esc_attr( $payment_card ) . '">
1655 <input type="hidden" name="payment_token_id" value="' . esc_attr( $payment_token_id ) . '">
1656 <input type="hidden" name="vault" value="' . esc_attr( $vault ) . '">
1657 <input type="hidden" name="correlation_id" value="' . esc_attr( $correlation_id ) . '">
1658 <input type="hidden" name="liability_shift" value="' . esc_attr( $liability_shift ) . '">
1659 <input type="hidden" name="enrollment_status" value="' . esc_attr( $enrollment_status ) . '">
1660 <input type="hidden" name="authentication_status" value="' . esc_attr( $authentication_status ) . '">
1661 <input type="hidden" name="_nonce" value="' . wp_create_nonce( $acting_flg ) . '">
1662 <div class="send">' . apply_filters( 'usces_filter_confirm_before_backbutton', null, $payment, $acting_flg, $tracking_id ) . '
1663 <input name="backDelivery" type="submit" id="back_button" class="back_to_delivery_button" value="' . __( 'Back', 'usces' ) . '"' . apply_filters( 'usces_filter_confirm_prebutton', null ) . ' />
1664 <input name="purchase" type="submit" id="purchase_button" class="checkout_button" value="' . apply_filters( 'usces_filter_confirm_checkout_button_value', __( 'Checkout', 'usces' ) ) . '"' . apply_filters( 'usces_filter_confirm_nextbutton', null ) . $purchase_disabled . ' />
1665 </div>';
1666 }
1667 return $form;
1668 }
1669
1670 /**
1671 * 決済エラーメッセージ
1672 * usces_filter_get_error_settlement
1673 *
1674 * @param string $form Payment error message.
1675 * @return string
1676 */
1677 public function error_page_message( $form ) {
1678 $acting = ( isset( $_GET['acting'] ) ) ? wp_unslash( $_GET['acting'] ) : '';
1679 $acting_return = ( isset( $_GET['acting_return'] ) ) ? wp_unslash( $_GET['acting_return'] ) : '';
1680 if ( 'paypal_card' === $acting && '0' === $acting_return ) {
1681 if ( isset( $_GET['retry'] ) ) {
1682 $retry_url = add_query_arg(
1683 array(
1684 'backDelivery' => 'paypal_card',
1685 're-enter' => 1,
1686 ),
1687 USCES_CUSTOMER_URL
1688 );
1689 $form .= '<div class="support_box">' . __( 'Your credit card information is incorrect.', 'usces' ) . '<br />' . sprintf( __( 'Click <a href="%s">here</a> to re-enter your credit card number.', 'usces' ), $retry_url ) . '</div>';
1690 }
1691 }
1692 return $form;
1693 }
1694
1695 /**
1696 * フロントスクリプト
1697 * wp_enqueue_scripts
1698 */
1699 public function enqueue_scripts() {
1700 global $usces;
1701
1702 if ( ! is_admin() && $this->is_validity_acting( 'card' ) ) {
1703 $usces_page = ( isset( $_GET['usces_page'] ) ) ? wp_unslash( $_GET['usces_page'] ) : '';
1704 if ( ( $usces->is_cart_page( $_SERVER['REQUEST_URI'] ) && 'delivery' === $usces->page ) ||
1705 ( $usces->is_member_page( $_SERVER['REQUEST_URI'] ) && ( 'member_register_settlement' === $usces_page || 'member_update_settlement' === $usces_page ) ) ) {
1706 $paypal_style_url = apply_filters( 'usces_filter_paypalcp_card_style', USCES_FRONT_PLUGIN_URL . '/css/paypal_style.css' );
1707 wp_enqueue_style( 'usces-paypal-style', $paypal_style_url, array() );
1708 wp_enqueue_style( 'usces-loading', USCES_FRONT_PLUGIN_URL . '/css/loading.css', array() );
1709 }
1710 }
1711 }
1712
1713 /**
1714 * フロントスクリプト
1715 * wp_print_footer_scripts
1716 */
1717 public function footer_scripts() {
1718 global $usces;
1719
1720 if ( 'delivery' === $usces->page && $this->is_validity_acting( 'card' ) ) :
1721 $entry = $usces->cart->get_entry();
1722 $cart = $usces->cart->get_cart();
1723 if ( empty( $cart ) || empty( $entry ) ) {
1724 return;
1725 }
1726
1727 $continue = ( defined( 'WCEX_DLSELLER' ) ) ? usces_have_continue_charge( $cart ) : false;
1728 $regular = ( defined( 'WCEX_AUTO_DELIVERY' ) ) ? usces_have_regular_order() : false;
1729 if ( $continue || $regular ) {
1730 return;
1731 }
1732
1733 $acting_opts = $this->get_acting_settings();
1734 $cp_client_id = ( isset( $acting_opts['client_id'] ) ) ? $acting_opts['client_id'] : '';
1735 $cp_intent = ( isset( $acting_opts['intent'] ) ) ? $acting_opts['intent'] : '';
1736 $cp_environment = ( isset( $acting_opts['environment'] ) ) ? $acting_opts['environment'] : 'live';
1737 $card_vault = ( isset( $acting_opts['card_vault'] ) ) ? $acting_opts['card_vault'] : 'off';
1738 $card_3ds = ( isset( $acting_opts['card_3ds'] ) ) ? $acting_opts['card_3ds'] : 'off';
1739
1740 $customer_id = '';
1741 if ( usces_is_login() && 'on' === $card_vault ) {
1742 $member = $usces->get_member();
1743 $customer_id = $member['ID'];
1744 }
1745
1746 $paypal_card_name = '';
1747 $payment_method = usces_get_system_option( 'usces_payment_method', 'settlement' );
1748 if ( isset( $payment_method['acting_paypal_card'] ) ) {
1749 $paypal_card_name = $payment_method['acting_paypal_card']['name'];
1750 }
1751
1752 $currency_code = $usces->get_currency_code();
1753 $query = array();
1754 $query['components'] = 'hosted-fields';
1755 $query['client-id'] = $cp_client_id;
1756 $query['currency'] = $currency_code;
1757 $query['locale'] = $this->get_locale();
1758 if ( 'AUTHORIZE' === $cp_intent ) {
1759 $query['intent'] = 'authorize';
1760 }
1761 $sdk_query = http_build_query( $query );
1762 $client_token = $this->get_client_token( $customer_id );
1763 if ( usces_is_login() && 'on' === $card_vault ) :
1764 $fraudnet_session_id = $this->get_fraudnet_session_id();
1765 ?>
1766 <script type="application/json" fncls="fnparams-dede7cc5-15fd-4c75-a9f4-36c430ee3a99">
1767 {
1768 "f":"<?php echo esc_attr( $fraudnet_session_id ); ?>",
1769 "s":"<?php echo self::API_FRAUDNET_S; ?>"<?php if ( 'live' !== $cp_environment ) : ?>,
1770 "sandbox":true
1771 <?php endif; ?>}
1772 </script>
1773 <script type="text/javascript" src="https://c.paypal.com/da/r/fb.js"></script>
1774 <script type="text/javascript">
1775 document.getElementById( "paypal_f" ).value = "<?php echo esc_attr( $fraudnet_session_id ); ?>";
1776 </script><?php endif; ?>
1777 <div id="welcart-overlay" style="display:none"><div id="welcart-loading"></div><div id="welcart-loading-text"><?php esc_html_e( 'Processing...', 'usces' ); ?></div></div>
1778 <script src="https://www.paypal.com/sdk/js?<?php echo $sdk_query; // no escape due to fixed format. ?>" data-client-token="<?php echo $client_token; // no escape due to fixed format. ?>"></script>
1779 <script type="text/javascript">
1780 jQuery(document).ready( function($) {
1781 $( 'body input[type="submit"]' ).each( function( i, elem ) {
1782 if ( "confirm" == $( this ).attr( "name" ) ) {
1783 $( this ).parents( "form" ).attr( "id", "delivery-form" );
1784 $( this ).parents( "form" ).attr( "name", "delivery_form" );
1785 $( this ).attr( "id", "confirm" );
1786 }
1787 });
1788 });
1789 </script>
1790 <script type="text/javascript">
1791 document.getElementById( "welcart-overlay" ).style.display = 'none';
1792 let paypal_card_name = "<?php echo esc_js( $paypal_card_name ); ?>";
1793 if ( paypal.HostedFields.isEligible() ) {
1794 paypal.HostedFields.render({
1795 createOrder: function() {
1796 let params = new URLSearchParams();
1797 params.append( "action", "create_order_card" );
1798 params.append( "tracking_id", document.getElementById( "paypal_tracking_id" ).value );
1799 return fetch( uscesL10n.ajaxurl+"?uscesid="+uscesL10n.uscesid, {
1800 method: "POST",
1801 body: params
1802 }).then( function( res ) {
1803 return res.json();
1804 }).then( function( data ) {
1805 if ( data.message ) {
1806 alert( data.message );
1807 return false;
1808 }
1809 if ( data.stock && 'error' == data.stock ) {
1810 location.href = "<?php echo esc_js( USCES_CART_URL ); ?>";
1811 }
1812 document.getElementById( "paypal_resource_id" ).value = data.id;
1813 return data.id;
1814 });
1815 },
1816 fields: {
1817 number: {
1818 selector: "#card-number",
1819 placeholder: "4111 1111 1111 1111"
1820 },
1821 expirationDate: {
1822 selector: "#expiration-date",
1823 placeholder: "MM/YY"
1824 },
1825 cvv: {
1826 selector: "#cvv",
1827 placeholder: "123"
1828 }
1829 },
1830 styles: {
1831 '.valid': {
1832 'color': 'green'
1833 },
1834 '.invalid': {
1835 'color': 'red'
1836 }
1837 }
1838 }).then( function( hostedFieldsInstance ) {
1839 document.querySelector( "#confirm" ).onclick = function( event ) {
1840 var payment_name = document.getElementsByName( "offer[payment_name]" );
1841 for ( var select_payment = "", i = payment_name.length; i--; ) {
1842 if ( payment_name[i].checked ) {
1843 var select_payment = payment_name[i].value;
1844 break;
1845 }
1846 }
1847 <?php if ( 'on' === $card_vault ) : ?>
1848 var payment_card = document.getElementsByName( "paypal_payment_card" );
1849 for ( var select_vault = "", i = payment_card.length; i--; ) {
1850 if ( payment_card[i].checked ) {
1851 var select_vault = payment_card[i].value;
1852 break;
1853 }
1854 }
1855 <?php else : ?>
1856 var select_vault = "";
1857 <?php endif; ?>
1858 if ( paypal_card_name == select_payment && "vault" != select_vault ) {
1859 <?php if ( 'on' !== $card_3ds ) : ?>
1860 document.getElementById( "welcart-overlay" ).style.display = "block";
1861 <?php endif; ?>
1862 event.preventDefault();
1863 hostedFieldsInstance.submit(<?php if ( 'on' === $card_vault || 'on' === $card_3ds ) : ?>{
1864 <?php if ( 'on' === $card_vault ) : ?>
1865 vault: document.querySelector( '#vault' ).checked<?php endif; ?><?php if ( 'on' === $card_vault && 'on' === $card_3ds ) : ?>,
1866 <?php endif; ?>
1867 <?php if ( 'on' === $card_3ds ) : ?>
1868 contingencies: ['SCA_ALWAYS']
1869 <?php endif; ?>
1870 }<?php endif; ?>).then( function( payload ) {
1871 <?php if ( 'on' === $card_3ds ) : ?>
1872 if ( "POSSIBLE" == payload.liabilityShift ) {
1873 let params3ds = new URLSearchParams();
1874 params3ds.append( "action", "show_order_details_card" );
1875 params3ds.append( "tracking_id", document.getElementById( "paypal_tracking_id" ).value );
1876 params3ds.append( "resource_id", payload.orderId );
1877 return fetch( uscesL10n.ajaxurl + "?uscesid=" + uscesL10n.uscesid, {
1878 method: "POST",
1879 body: params3ds
1880 }).then( function( res ) {
1881 return res.json();
1882 }).then( function( res3ds ) {
1883 document.getElementById( "paypal_resource_id" ).value = payload.orderId;
1884 var elem_liability_shift = document.createElement( "input" );
1885 elem_liability_shift.setAttribute( "type", "hidden" );
1886 elem_liability_shift.setAttribute( "name", "paypal_liability_shift" );
1887 elem_liability_shift.setAttribute( "value", res3ds.payment_source.card.authentication_result.liability_shift );
1888 document.delivery_form.appendChild( elem_liability_shift );
1889 var elem_enrollment_status = document.createElement( "input" );
1890 elem_enrollment_status.setAttribute( "type", "hidden" );
1891 elem_enrollment_status.setAttribute( "name", "paypal_enrollment_status" );
1892 elem_enrollment_status.setAttribute( "value", res3ds.payment_source.card.authentication_result.three_d_secure.enrollment_status );
1893 document.delivery_form.appendChild( elem_enrollment_status );
1894 var elem_authentication_status = document.createElement( "input" );
1895 elem_authentication_status.setAttribute( "type", "hidden" );
1896 elem_authentication_status.setAttribute( "name", "paypal_authentication_status" );
1897 elem_authentication_status.setAttribute( "value", res3ds.payment_source.card.authentication_result.three_d_secure.authentication_status );
1898 document.delivery_form.appendChild( elem_authentication_status );
1899 var elem_confirm = document.createElement( "input" );
1900 elem_confirm.setAttribute( "type", "hidden" );
1901 elem_confirm.setAttribute( "name", "confirm" );
1902 elem_confirm.setAttribute( "value", "confirm" );
1903 document.delivery_form.appendChild( elem_confirm );
1904 document.delivery_form.submit();
1905 });
1906 } else {
1907 alert( "3D Secure authorization failed. Please try another card number." );
1908 return false;
1909 }
1910 <?php else : ?>
1911 document.getElementById( "paypal_resource_id" ).value = payload.orderId;
1912 var elem_confirm = document.createElement( "input" );
1913 elem_confirm.setAttribute( "type", "hidden" );
1914 elem_confirm.setAttribute( "name", "confirm" );
1915 elem_confirm.setAttribute( "value", "confirm" );
1916 document.delivery_form.appendChild( elem_confirm );
1917 document.delivery_form.submit();
1918 <?php endif; ?>
1919 }).catch(( reason ) => {
1920 document.getElementById( "welcart-overlay" ).style.display = 'none';
1921 alert( 'Payment could not be captured! ' + reason.name + ' ' + reason.message );
1922 return false;
1923 });
1924 }
1925 }
1926 }).catch( function( err ) {
1927 if ( err ) {
1928 alert( 'Payment could not be captured! ' + JSON.stringify( err ) );
1929 return false;
1930 }
1931 });
1932 } else {
1933 document.querySelector( "#paypal_card_form" ).style = 'display: none';
1934 }
1935 </script>
1936 <?php
1937 elseif ( 'confirm' === $usces->page ) :
1938 $entry = $usces->cart->get_entry();
1939 $cart = $usces->cart->get_cart();
1940 if ( empty( $cart ) || empty( $entry['order']['total_full_price'] ) ) {
1941 return;
1942 }
1943
1944 $payment = usces_get_payments_by_name( $entry['order']['payment_name'] );
1945
1946 /* PayPal CP */
1947 if ( 'acting_paypal_cp' === $payment['settlement'] && 'activate' === $payment['use'] ) :
1948 $continue = ( defined( 'WCEX_DLSELLER' ) ) ? usces_have_continue_charge( $cart ) : false;
1949 $regular = ( defined( 'WCEX_AUTO_DELIVERY' ) ) ? usces_have_regular_order() : false;
1950
1951 $acting_opts = $this->get_acting_settings();
1952 $cp_client_id = ( isset( $acting_opts['client_id'] ) ) ? $acting_opts['client_id'] : '';
1953 $cp_intent = ( isset( $acting_opts['intent'] ) ) ? $acting_opts['intent'] : '';
1954 $cp_button_layout = ( isset( $acting_opts['button_layout'] ) ) ? $acting_opts['button_layout'] : 'vertical';
1955 $cp_button_color = ( isset( $acting_opts['button_color'] ) ) ? $acting_opts['button_color'] : 'gold';
1956 $cp_button_shape = ( isset( $acting_opts['button_shape'] ) ) ? $acting_opts['button_shape'] : 'rect';
1957 $cp_button_label = ( isset( $acting_opts['button_label'] ) ) ? $acting_opts['button_label'] : 'paypal';
1958
1959 $currency_code = $usces->get_currency_code();
1960 $query = array();
1961 $query['client-id'] = $cp_client_id;
1962 $query['currency'] = $currency_code;
1963 $query['locale'] = $this->get_locale();
1964 if ( $continue || $regular ) {
1965 $query['vault'] = 'true';
1966 $query['intent'] = 'tokenize';
1967 $billing = ( $continue ) ? 'continue' : 'regular';
1968 } else {
1969 if ( 'AUTHORIZE' === $cp_intent ) {
1970 $query['intent'] = 'authorize';
1971 }
1972 }
1973 $sdk_query = http_build_query( $query );
1974 ?>
1975 <script src="https://www.paypal.com/sdk/js?<?php echo $sdk_query; // no escape due to fixed format. ?>"></script>
1976 <script>
1977 paypal.Buttons({
1978 style: {
1979 layout: "<?php echo esc_html( $cp_button_layout ); ?>",
1980 color: "<?php echo esc_html( $cp_button_color ); ?>",
1981 shape: "<?php echo esc_html( $cp_button_shape ); ?>",
1982 label: "<?php echo esc_html( $cp_button_label ); ?>"
1983 },<?php if ( $continue || $regular ) : ?>
1984 createBillingAgreement: function() {
1985 let params = new URLSearchParams();
1986 params.append( "action", "create_billing_agreement" );
1987 params.append( "tracking_id", document.getElementsByName( "tracking_id" )[0].value );
1988 params.append( "billing", "<?php echo esc_html( $billing ); ?>" );
1989 return fetch( uscesL10n.ajaxurl + "?uscesid=" + uscesL10n.uscesid, {
1990 method: "POST",
1991 body: params
1992 }).then( function( res ) {
1993 return res.json();
1994 }).then( function( data ) {
1995 if ( data.stock && 'error' == data.stock ) {
1996 location.href = "<?php echo esc_js( USCES_CART_URL ); ?>";
1997 }
1998 return data.token_id;
1999 });
2000 },
2001 onApprove: function( data ) {
2002 document.getElementById( "checkout_paypal_cp" ).style.pointerEvents = "none";
2003 var purchase_form = document.forms.purchase_form;
2004 purchase_form.resource_id.value = data.orderID;
2005 purchase_form.billing_token.value = data.billingToken;
2006 purchase_form.submit();
2007 },<?php else : ?>
2008 createOrder: function() {
2009 let params = new URLSearchParams();
2010 params.append( "action", "create_order" );
2011 params.append( "tracking_id", document.getElementsByName( "tracking_id" )[0].value );
2012 return fetch( uscesL10n.ajaxurl + "?uscesid=" + uscesL10n.uscesid, {
2013 method: "POST",
2014 body: params
2015 }).then( function( res ) {
2016 return res.json();
2017 }).then( function( data ) {
2018 if ( data.stock && 'error' == data.stock ) {
2019 location.href = "<?php echo esc_js( USCES_CART_URL ); ?>";
2020 }
2021 return data.id;
2022 });
2023 },
2024 onApprove: function( data ) {
2025 document.getElementById( "checkout_paypal_cp" ).style.pointerEvents = "none";
2026 var purchase_form = document.forms.purchase_form;
2027 purchase_form.resource_id.value = data.orderID;
2028 purchase_form.submit();
2029 },<?php endif; ?>
2030 onCancel: function( data ) {
2031 },
2032 onError: function( err ) {
2033 alert( err );
2034 }
2035 }).render( "#checkout_paypal_cp" );
2036 </script>
2037 <?php
2038 endif;
2039 elseif ( $usces->is_member_page( $_SERVER['REQUEST_URI'] ) && ( $this->is_validity_acting( 'cp' ) || $this->is_validity_acting( 'card' ) ) ) :
2040 $member = $usces->get_member();
2041 if ( usces_have_member_continue_order( $member['ID'] ) || usces_have_member_regular_order( $member['ID'] ) ) :
2042 ?>
2043 <script type="text/javascript">
2044 jQuery( document ).ready( function( $ ) {
2045 $( "input[name='deletemember']" ).css( "display", "none" );
2046 });
2047 </script>
2048 <?php
2049 endif;
2050 $usces_page = ( isset( $_GET['usces_page'] ) ) ? wp_unslash( $_GET['usces_page'] ) : '';
2051 if ( $this->is_validity_acting( 'card' ) && ( 'member_update_settlement' === $usces_page || 'member_register_settlement' === $usces_page ) ) :
2052 $acting_opts = $this->get_acting_settings();
2053 $cp_client_id = ( isset( $acting_opts['client_id'] ) ) ? $acting_opts['client_id'] : '';
2054 $card_vault = ( isset( $acting_opts['card_vault'] ) ) ? $acting_opts['card_vault'] : 'off';
2055 if ( 'on' !== $card_vault ) {
2056 return;
2057 }
2058
2059 $member = $usces->get_member();
2060 $currency_code = $usces->get_currency_code();
2061
2062 $query = array();
2063 $query['components'] = 'hosted-fields';
2064 $query['client-id'] = $cp_client_id;
2065 $query['currency'] = $currency_code;
2066 $query['locale'] = $this->get_locale();
2067 $query['intent'] = 'authorize';
2068 $sdk_query = http_build_query( $query );
2069 $client_token = $this->get_client_token( $member['ID'] );
2070 ?>
2071 <div id="welcart-overlay" style="display:none"><div id="welcart-loading"></div><div id="welcart-loading-text"><?php esc_html_e( 'Processing...', 'usces' ); ?></div></div>
2072 <script src="https://www.paypal.com/sdk/js?<?php echo $sdk_query; // no escape due to fixed format. ?>" data-client-token="<?php echo $client_token; // no escape due to fixed format. ?>"></script>
2073 <script>
2074 document.getElementById( "welcart-overlay" ).style.display = 'none';
2075 if ( paypal.HostedFields.isEligible() ) {
2076 paypal.HostedFields.render({
2077 createOrder: function() {
2078 let params = new URLSearchParams();
2079 params.append( "action", "create_order_member" );
2080 params.append( "tracking_id", document.getElementById( "paypal_tracking_id" ).value );
2081 return fetch( uscesL10n.ajaxurl + "?uscesid=" + uscesL10n.uscesid, {
2082 method: "POST",
2083 body: params
2084 }).then( function( res ) {
2085 return res.json();
2086 }).then( function( data ) {
2087 if ( data.message ) {
2088 alert( data.message );
2089 return false;
2090 }
2091 document.getElementById( "paypal_resource_id" ).value = data.id;
2092 return data.id;
2093 });
2094 },
2095 fields: {
2096 number: {
2097 selector: "#card-number",
2098 placeholder: "4111 1111 1111 1111"
2099 },
2100 expirationDate: {
2101 selector: "#expiration-date",
2102 placeholder: "MM/YY"
2103 },
2104 cvv: {
2105 selector: "#cvv",
2106 placeholder: "123"
2107 }
2108 },
2109 styles: {
2110 '.valid': {
2111 'color': 'green'
2112 },
2113 '.invalid': {
2114 'color': 'red'
2115 }
2116 }
2117 }).then( function( hostedFieldsInstance ) {
2118 document.querySelector( "#card-update" ).onclick = function( event ) {
2119 document.getElementById( "welcart-overlay" ).style.display = 'block';
2120 event.preventDefault();
2121 hostedFieldsInstance.submit({
2122 vault: true
2123 }).then( function( payload ) {
2124 document.getElementById( "paypal_resource_id" ).value = payload.orderId;
2125 document.member_card_update.submit();
2126 }).catch(( reason ) => {
2127 document.getElementById( "welcart-overlay" ).style.display = 'none';
2128 alert( 'Credit card could not be renewed! ' + reason.name + ' ' + reason.message );
2129 });
2130 }
2131 }).catch( function( err ) {
2132 if ( err ) {
2133 alert( 'Credit card could not be renewed! ' + JSON.stringify( err ) );
2134 }
2135 });
2136 } else {
2137 document.querySelector( "#paypal_card_form" ).style = 'display: none';
2138 }
2139 </script>
2140 <?php
2141 print_google_recaptcha_response( $usces->page, 'member-card-update', 'member_card_update' );
2142 endif;
2143 endif;
2144 }
2145
2146 /**
2147 * Get FraudNet Session Identifier.
2148 */
2149 private function get_fraudnet_session_id() {
2150 $session_id = substr( base_convert( hash( 'sha256', uniqid() ), 16, 36 ), 0, 32 );
2151 return $session_id;
2152 }
2153
2154 /**
2155 * Get seller nonce.
2156 * S256 - The code verifier must be high-entropy cryptographic random string with a byte length of 43-128 range.
2157 */
2158 private function get_seller_nonce() {
2159 $seller_nonce = substr( base_convert( hash( 'sha256', uniqid() ), 16, 36 ), 0, 48 );
2160 return $seller_nonce;
2161 }
2162
2163 /**
2164 * Get Access Token.
2165 */
2166 private function get_access_token() {
2167 $acting_opts = $this->get_acting_settings();
2168 $cp_client_id = ( isset( $acting_opts['client_id'] ) ) ? $acting_opts['client_id'] : '';
2169 $cp_secret = ( isset( $acting_opts['secret'] ) ) ? $acting_opts['secret'] : '';
2170 $api_request_url = ( isset( $acting_opts['api_request_url'] ) ) ? $acting_opts['api_request_url'] : '';
2171 $headers = array(
2172 'Content-Type: application/x-www-form-urlencoded;application/json',
2173 'Accept: application/json',
2174 );
2175 $ch = curl_init();
2176 curl_setopt( $ch, CURLOPT_URL, $api_request_url . '/v1/oauth2/token' );
2177 curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
2178 curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
2179 curl_setopt( $ch, CURLOPT_USERPWD, $cp_client_id . ':' . $cp_secret );
2180 curl_setopt( $ch, CURLOPT_POST, true );
2181 curl_setopt( $ch, CURLOPT_POSTFIELDS, 'grant_type=client_credentials' );
2182
2183 $result = curl_exec( $ch );
2184 if ( ! $result ) {
2185 $error = curl_error( $ch );
2186 unset( $ch );
2187 return json_decode( $error, true );
2188 }
2189 unset( $ch );
2190 return json_decode( $result, true );
2191 }
2192
2193 /**
2194 * Get Client Token.
2195 *
2196 * @param string $customer_id Member ID.
2197 * @return string
2198 */
2199 private function get_client_token( $customer_id = '' ) {
2200 $acting_opts = $this->get_acting_settings();
2201 $api_request_url = ( isset( $acting_opts['api_request_url'] ) ) ? $acting_opts['api_request_url'] : '';
2202 $card_prefix = ( isset( $acting_opts['card_prefix'] ) ) ? $acting_opts['card_prefix'] : '';
2203
2204 /* Get Access Token */
2205 $access_token = $this->get_access_token();
2206
2207 $params = array(
2208 'method' => 'POST',
2209 'headers' => array(
2210 'Content-Type' => 'application/json;charset=utf-8',
2211 'Authorization' => 'Bearer ' . $access_token['access_token'],
2212 ),
2213 'timeout' => self::API_TIMEOUT,
2214 );
2215 if ( ! empty( $customer_id ) ) {
2216 $body = array();
2217 $body['customer_id'] = $card_prefix . $customer_id;
2218 $params['body'] = wp_json_encode( $body );
2219 }
2220 $response = wp_remote_post( $api_request_url . '/v1/identity/generate-token', $params );
2221 $response_data = json_decode( wp_remote_retrieve_body( $response ) );
2222 return $response_data->client_token;
2223 }
2224
2225 /**
2226 * Onboarded.
2227 * Get seller REST API credentials.
2228 */
2229 public function onboarded() {
2230 if ( ! current_user_can( 'wel_manage_setting' ) ) {
2231 wp_send_json_error( array( 'message' => 'Forbidden' ), 403 );
2232 }
2233 check_ajax_referer( 'admin_settlement', 'wc_nonce' );
2234
2235 $acting_opts = $this->get_acting_settings();
2236 $api_request_url = self::API_URL;
2237
2238 $seller_nonce = $this->get_seller_nonce();
2239 $auth_code = $_POST['authCode'];
2240 $shared_id = $_POST['sharedId'];
2241 $seller_nonce = $_POST['seller_nonce'];
2242
2243 $headers = array(
2244 'Content-Type: application/x-www-form-urlencoded;application/json',
2245 'Accept: application/json',
2246 );
2247 $ch = curl_init();
2248 curl_setopt( $ch, CURLOPT_URL, $api_request_url . '/v1/oauth2/token' );
2249 curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
2250 curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
2251 curl_setopt( $ch, CURLOPT_USERPWD, $shared_id . ':' );
2252 curl_setopt( $ch, CURLOPT_POST, true );
2253 curl_setopt( $ch, CURLOPT_POSTFIELDS, 'grant_type=authorization_code&code=' . $auth_code . '&code_verifier=' . $seller_nonce );
2254
2255 $result = curl_exec( $ch );
2256 unset( $ch );
2257 $seller_access_token = json_decode( $result, true );
2258 if ( isset( $seller_access_token['access_token'] ) ) {
2259 $partner_merchant_id = 'T7SS625X3B32Q';
2260 $params = array(
2261 'method' => 'GET',
2262 'headers' => array(
2263 'Content-Type' => 'application/json;charset=utf-8',
2264 'Authorization' => 'Bearer ' . $seller_access_token['access_token'],
2265 ),
2266 'timeout' => self::API_TIMEOUT,
2267 );
2268 $response = wp_remote_get( $api_request_url . '/v1/customer/partners/' . $partner_merchant_id . '/merchant-integrations/credentials/', $params );
2269 $response_data = json_decode( wp_remote_retrieve_body( $response ), true );
2270 wp_send_json( $response_data );
2271 } else {
2272 wp_send_json( $seller_access_token );
2273 }
2274 }
2275
2276 /**
2277 * Create billing agreement.
2278 */
2279 public function create_billing_agreement() {
2280 global $usces;
2281
2282 $entry = $usces->cart->get_entry();
2283 $cart = $usces->cart->get_cart();
2284
2285 $usces->error_message = $usces->zaiko_check();
2286 if ( ! empty( $usces->error_message ) || 0 === (int) $usces->cart->num_row() ) {
2287 wp_send_json( array( 'stock' => 'error' ) );
2288 }
2289
2290 $tracking_id = wp_unslash( $_POST['tracking_id'] );
2291 $billing = wp_unslash( $_POST['billing'] ); /* continue|regular */
2292 $shipping = usces_have_shipped( $cart );
2293
2294 $acting_opts = $this->get_acting_settings();
2295 $api_request_url = ( isset( $acting_opts['api_request_url'] ) ) ? $acting_opts['api_request_url'] : '';
2296
2297 /* Get Access Token */
2298 $access_token = $this->get_access_token();
2299
2300 if ( 'continue' === $billing ) {
2301 $description = __( '[ Automatic recurring billing ]', 'usces' );
2302 } elseif ( 'regular' === $billing ) {
2303 $description = __( '[ Regular Purchase ]', 'usces' );
2304 } else {
2305 $description = 'Billing Agreement.';
2306 }
2307
2308 $body = array();
2309 $body['description'] = $description;
2310 $body['payer']['payment_method'] = 'PAYPAL';
2311 $body['plan']['type'] = 'MERCHANT_INITIATED_BILLING';
2312 $body['plan']['merchant_preferences']['return_url'] = USCES_CART_URL;
2313 $body['plan']['merchant_preferences']['cancel_url'] = USCES_CART_URL;
2314 $body['plan']['merchant_preferences']['accepted_pymt_type'] = 'INSTANT';
2315 if ( $shipping ) {
2316 $name = usces_localized_name( trim( $entry['delivery']['name1'] ), trim( $entry['delivery']['name2'] ), 'return' );
2317 $country = ( ! empty( $entry['delivery']['country'] ) ) ? $entry['delivery']['country'] : usces_get_base_country();
2318 $country_code = apply_filters( 'usces_filter_paypalcp_shipping_country_code', $country );
2319 $shipping_address = array();
2320 $body['shipping_address']['line2'] = trim( $entry['delivery']['address3'] );
2321 $body['shipping_address']['line1'] = trim( $entry['delivery']['address2'] );
2322 $body['shipping_address']['city'] = trim( $entry['delivery']['address1'] );
2323 $body['shipping_address']['state'] = trim( $entry['delivery']['pref'] );
2324 $body['shipping_address']['postal_code'] = trim( $entry['delivery']['zipcode'] );
2325 $body['shipping_address']['country_code'] = $country_code;
2326 $body['shipping_address']['recipient_name'] = $name;
2327 $body['plan']['merchant_preferences']['skip_shipping_address'] = false;
2328 $body['plan']['merchant_preferences']['immutable_shipping_address'] = true;
2329 } else {
2330 $body['plan']['merchant_preferences']['skip_shipping_address'] = true;
2331 $body['plan']['merchant_preferences']['immutable_shipping_address'] = false;
2332 }
2333
2334 $params = array(
2335 'method' => 'POST',
2336 'headers' => array(
2337 'Content-Type' => 'application/json;charset=utf-8',
2338 'Authorization' => 'Bearer ' . $access_token['access_token'],
2339 'PayPal-Partner-Attribution-Id' => self::API_BN_CODE_PCP,
2340 'PayPal-Request-Id' => $tracking_id,
2341 ),
2342 'timeout' => self::API_TIMEOUT,
2343 'body' => wp_json_encode( $body ),
2344 );
2345 $response = wp_remote_post( $api_request_url . '/v1/billing-agreements/agreement-tokens', $params );
2346 $response_data = json_decode( wp_remote_retrieve_body( $response ) );
2347 wp_send_json( $response_data );
2348 }
2349
2350 /**
2351 * Create order.
2352 */
2353 public function create_order() {
2354 global $usces;
2355
2356 $entry = $usces->cart->get_entry();
2357 $cart = $usces->cart->get_cart();
2358
2359 $usces->error_message = $usces->zaiko_check();
2360 if ( ! empty( $usces->error_message ) || 0 === (int) $usces->cart->num_row() ) {
2361 wp_send_json( array( 'stock' => 'error' ) );
2362 }
2363
2364 $bn_code = self::API_BN_CODE_PCP;
2365 $tracking_id = wp_unslash( $_POST['tracking_id'] );
2366
2367 /* Get Access Token */
2368 $access_token = $this->get_access_token();
2369
2370 /* Create order */
2371 $response_data = $this->api_create_order( $access_token, $bn_code, '', $tracking_id, $entry, $cart, false );
2372 wp_send_json( $response_data );
2373 }
2374
2375 /**
2376 * Create order.
2377 */
2378 public function create_order_card() {
2379 global $usces;
2380
2381 $entry = $usces->cart->get_entry();
2382 $cart = $usces->cart->get_cart();
2383
2384 $usces->error_message = $usces->zaiko_check();
2385 if ( ! empty( $usces->error_message ) || 0 === (int) $usces->cart->num_row() ) {
2386 wp_send_json( array( 'stock' => 'error' ) );
2387 }
2388
2389 $bn_code = self::API_BN_CODE_ACDC;
2390 $tracking_id = wp_unslash( $_POST['tracking_id'] );
2391 $entry['order']['total_items_price'] = $usces->get_total_price( $cart );
2392 $entry['pre_card_order'] = true;
2393
2394 /* Get Access Token */
2395 $access_token = $this->get_access_token();
2396
2397 /* Create order */
2398 $response_data = $this->api_create_order( $access_token, $bn_code, '', $tracking_id, $entry, $cart, false );
2399 wp_send_json( $response_data );
2400 }
2401
2402 /**
2403 * Create order.
2404 */
2405 public function create_order_member() {
2406 global $usces;
2407
2408 $acting_opts = $this->get_acting_settings();
2409 $tracking_id = wp_unslash( $_POST['tracking_id'] );
2410
2411 /* Get Access Token */
2412 $access_token = $this->get_access_token();
2413
2414 /* Create order */
2415 $api_request_url = ( isset( $acting_opts['api_request_url'] ) ) ? $acting_opts['api_request_url'] : '';
2416 $currency_code = $usces->get_currency_code();
2417
2418 $body = array();
2419 $body['intent'] = 'AUTHORIZE';
2420 $purchase_units = array();
2421 $purchase_units['reference_id'] = $tracking_id;
2422 $purchase_units['amount']['currency_code'] = $currency_code;
2423 $purchase_units['amount']['value'] = 10;
2424 $body['purchase_units'] = array( $purchase_units );
2425
2426 $member = $usces->get_member();
2427 $payer = array();
2428 $phone = ( ! empty( $member['tel'] ) ) ? str_replace( array( '-', '+', ' ' ), '', mb_convert_kana( $member['tel'], 'a', 'UTF-8' ) ) : '';
2429 $country = ( ! empty( $member['country'] ) ) ? $member['country'] : usces_get_base_country();
2430 $country_code = apply_filters( 'usces_filter_paypalcp_customer_country_code', $country );
2431 $payer['name']['given_name'] = trim( $member['name2'] );
2432 $payer['name']['surname'] = trim( $member['name1'] );
2433 $payer['email_address'] = trim( $member['mailaddress1'] );
2434 if ( ! empty( $phone ) ) {
2435 $payer['phone']['phone_number']['national_number'] = ltrim( $phone, '0' );
2436 }
2437 if ( ! empty( $member['address1'] ) ) {
2438 $payer['address']['address_line_2'] = trim( $member['address3'] );
2439 $payer['address']['address_line_1'] = trim( $member['address2'] );
2440 $payer['address']['admin_area_2'] = trim( $member['address1'] );
2441 }
2442 if ( ! empty( $member['pref'] ) ) {
2443 $payer['address']['admin_area_1'] = trim( $member['pref'] );
2444 }
2445 if ( ! empty( $member['zipcode'] ) ) {
2446 $payer['address']['postal_code'] = trim( mb_convert_kana( $member['zipcode'], 'a', 'UTF-8' ) );
2447 }
2448 $payer['address']['country_code'] = $country_code;
2449 $body['payer'] = $payer;
2450
2451 $params = array(
2452 'method' => 'POST',
2453 'headers' => array(
2454 'Content-Type' => 'application/json;charset=utf-8',
2455 'Authorization' => 'Bearer ' . $access_token['access_token'],
2456 'PayPal-Partner-Attribution-Id' => self::API_BN_CODE_ACDC,
2457 'PayPal-Request-Id' => $tracking_id,
2458 ),
2459 'timeout' => self::API_TIMEOUT,
2460 'body' => wp_json_encode( $body ),
2461 );
2462 $response = wp_remote_post( $api_request_url . '/v2/checkout/orders', $params );
2463 if ( isset( $response['response']['code'] ) && ( '200' === (string) $response['response']['code'] || '201' === (string) $response['response']['code'] ) ) {
2464 $response_data = json_decode( wp_remote_retrieve_body( $response ), true );
2465 } else {
2466 $response_data = json_decode( wp_remote_retrieve_body( $response ), true );
2467 if ( is_array( $response_data ) && ! isset( $response_data['status'] ) && ! isset( $response_data['name'] ) ) {
2468 $response_data['name'] = 'CREATE ORDER ERROR';
2469 }
2470 }
2471 wp_send_json( $response_data );
2472 }
2473
2474 /**
2475 * Show Order Details.
2476 */
2477 public function show_order_details_card() {
2478 $bn_code = self::API_BN_CODE_ACDC;
2479 $tracking_id = ( isset( $_POST['tracking_id'] ) ) ? wp_unslash( $_POST['tracking_id'] ) : ''; /* rand|reference_id */
2480 $resource_id = ( isset( $_POST['resource_id'] ) ) ? wp_unslash( $_POST['resource_id'] ) : '';
2481
2482 /* Get Access Token */
2483 $access_token = $this->get_access_token();
2484
2485 /* Get order */
2486 $response_data = $this->api_get_order( $access_token, $bn_code, $tracking_id, $resource_id );
2487 wp_send_json( $response_data );
2488 }
2489
2490 /**
2491 * 決済処理
2492 * usces_action_acting_processing
2493 *
2494 * @param string $acting_flg Payment type.
2495 * @param array $post_query Post data.
2496 */
2497 public function acting_processing( $acting_flg, $post_query ) {
2498 global $usces;
2499
2500 if ( 'acting_paypal_cp' === $acting_flg ) {
2501 parse_str( $post_query, $post_data );
2502 $tracking_id = ( isset( $post_data['tracking_id'] ) ) ? $post_data['tracking_id'] : ''; /* rand|reference_id */
2503 $resource_id = ( isset( $post_data['resource_id'] ) ) ? $post_data['resource_id'] : '';
2504 $billing_token = ( isset( $post_data['billing_token'] ) ) ? $post_data['billing_token'] : '';
2505
2506 if ( empty( $tracking_id ) ) {
2507 $log = array(
2508 'acting' => $this->paymod_id,
2509 'key' => '(empty)',
2510 'result' => 'ON APPROVE ERROR',
2511 'data' => $post_data,
2512 );
2513 usces_save_order_acting_error( $log );
2514 wp_redirect(
2515 add_query_arg(
2516 array(
2517 'acting' => $this->paymod_id,
2518 'acting_return' => 0,
2519 'result' => 0,
2520 ),
2521 USCES_CART_URL
2522 )
2523 );
2524 exit();
2525 }
2526
2527 $acting_opts = $this->get_acting_settings();
2528 $cp_intent = ( isset( $acting_opts['intent'] ) ) ? $acting_opts['intent'] : '';
2529 $api_request_url = ( isset( $acting_opts['api_request_url'] ) ) ? $acting_opts['api_request_url'] : '';
2530 $bn_code = self::API_BN_CODE_PCP;
2531 $correlation_id = '';
2532 $payment_source = array();
2533
2534 /* Get Access Token */
2535 $access_token = $this->get_access_token();
2536
2537 if ( $billing_token ) {
2538 $params_billing_agreements = array(
2539 'method' => 'POST',
2540 'headers' => array(
2541 'Content-Type' => 'application/json;charset=utf-8',
2542 'Authorization' => 'Bearer ' . $access_token['access_token'],
2543 'PayPal-Partner-Attribution-Id' => $bn_code,
2544 'PayPal-Request-Id' => $tracking_id,
2545 ),
2546 'timeout' => self::API_TIMEOUT,
2547 );
2548 $response_billing_agreements = wp_remote_post( $api_request_url . '/v1/billing-agreements/' . $billing_token . '/agreements', $params_billing_agreements );
2549 $response_billing_agreements_data = json_decode( wp_remote_retrieve_body( $response_billing_agreements ), true );
2550 if ( isset( $response_billing_agreements['response']['code'] ) && '201' === (string) $response_billing_agreements['response']['code'] && isset( $response_billing_agreements_data['id'] ) ) {
2551 $ba_id = $response_billing_agreements_data['id'];
2552 $payment_source['token'] = array(
2553 'id' => $ba_id,
2554 'type' => 'BILLING_AGREEMENT',
2555 );
2556 $entry = $usces->cart->get_entry();
2557 $cart = $usces->cart->get_cart();
2558 $continue = ( defined( 'WCEX_DLSELLER' ) ) ? usces_have_continue_charge( $cart ) : false;
2559 $regular = ( defined( 'WCEX_AUTO_DELIVERY' ) ) ? usces_have_regular_order() : false;
2560
2561 if ( $continue ) {
2562 $chargingday = $usces->getItemChargingDay( $cart[0]['post_id'] );
2563 /* 受注日課金 */
2564 if ( 99 !== (int) $chargingday ) {
2565 $cp_intent = 'AUTHORIZE';
2566 }
2567 }
2568
2569 /* Create order */
2570 $response_order_data = $this->api_create_order( $access_token, $bn_code, $cp_intent, $tracking_id, $entry, $cart );
2571 if ( isset( $response_order_data['id'] ) && isset( $response_order_data['status'] ) && 'CREATED' === $response_order_data['status'] ) {
2572 $resource_id = $response_order_data['id'];
2573 } else {
2574 $result = 'CREATE ORDER ERROR';
2575 $params_cancel_agreements = array(
2576 'method' => 'POST',
2577 'headers' => array(
2578 'Content-Type' => 'application/json;charset=utf-8',
2579 'Authorization' => 'Bearer ' . $access_token['access_token'],
2580 'PayPal-Partner-Attribution-Id' => $bn_code,
2581 'PayPal-Request-Id' => $tracking_id,
2582 ),
2583 'timeout' => self::API_TIMEOUT,
2584 );
2585 $response_cancel_agreements = wp_remote_post( $api_request_url . '/v1/billing-agreements/agreements/' . $ba_id . '/cancel', $params_cancel_agreements );
2586 if ( isset( $response_cancel_agreements['response']['code'] ) && '200' === (string) $response_cancel_agreements['response']['code'] ) {
2587 $result .= ' (BILLING AGREEMENT CANCEL COMPLETED)';
2588 }
2589 $log = array(
2590 'acting' => $this->paymod_id,
2591 'key' => $tracking_id,
2592 'result' => $result,
2593 'data' => $response_order_data,
2594 );
2595 usces_save_order_acting_error( $log );
2596 wp_redirect(
2597 add_query_arg(
2598 array(
2599 'acting' => $this->paymod_id,
2600 'acting_return' => 0,
2601 'result' => 0,
2602 ),
2603 USCES_CART_URL
2604 )
2605 );
2606 exit();
2607 }
2608 } else {
2609 $log = array(
2610 'acting' => $this->paymod_id,
2611 'key' => $tracking_id,
2612 'result' => 'BILLING AGREEMENT ERROR',
2613 'data' => $response_billing_agreements_data,
2614 );
2615 usces_save_order_acting_error( $log );
2616 wp_redirect(
2617 add_query_arg(
2618 array(
2619 'acting' => $this->paymod_id,
2620 'acting_return' => 0,
2621 'result' => 0,
2622 ),
2623 USCES_CART_URL
2624 )
2625 );
2626 exit();
2627 }
2628 } else {
2629 /* Get Order */
2630 $response_order_data = $this->api_get_order( $access_token, $bn_code, $tracking_id, $resource_id );
2631 if ( isset( $response_order_data['intent'] ) && $cp_intent === $response_order_data['intent'] && isset( $response_order_data['status'] ) && 'APPROVED' === $response_order_data['status'] ) {
2632 } else {
2633 /**
2634 * GET ORDER ERROR frequent, avoid because not required.
2635 if ( empty( $response_order_data ) ) {
2636 $response_order_data = $post_data;
2637 }
2638 $log = array(
2639 'acting' => $this->paymod_id,
2640 'key' => $tracking_id,
2641 'result' => 'GET ORDER ERROR',
2642 'data' => $response_order_data,
2643 );
2644 usces_save_order_acting_error( $log );
2645 wp_redirect(
2646 add_query_arg(
2647 array(
2648 'acting' => $this->paymod_id,
2649 'acting_return' => 0,
2650 'result' => 0,
2651 ),
2652 USCES_CART_URL
2653 )
2654 );
2655 exit();
2656 */
2657 usces_log( '[' . $this->paymod_id . '] GET ORDER ERROR : ' . print_r( $response_order_data, true ), 'acting_transaction.log' );
2658 }
2659 }
2660
2661 $usces->error_message = $usces->zaiko_check();
2662 if ( ! empty( $usces->error_message ) || 0 === (int) $usces->cart->num_row() ) {
2663 wp_redirect( USCES_CART_URL );
2664 exit();
2665 }
2666
2667 if ( 'CAPTURE' === $cp_intent ) {
2668 /* Capture */
2669 $response_data = $this->api_capture_order( $access_token, $bn_code, $tracking_id, $resource_id, $correlation_id, $payment_source );
2670 } elseif ( 'AUTHORIZE' === $cp_intent ) {
2671 /* Authorize */
2672 $response_data = $this->api_authorize_order( $access_token, $bn_code, $tracking_id, $resource_id, $correlation_id, $payment_source );
2673 }
2674 if ( isset( $response_data['id'] ) && isset( $response_data['status'] ) && 'COMPLETED' === $response_data['status'] ) {
2675 if ( ! empty( $ba_id ) ) {
2676 $response_data['ba_id'] = $ba_id;
2677 }
2678
2679 /* Welcart order data registration */
2680 $res = $usces->order_processing( $response_data );
2681 if ( 'ordercompletion' === $res ) {
2682 wp_redirect(
2683 add_query_arg(
2684 array(
2685 'acting' => $this->paymod_id,
2686 'acting_return' => 1,
2687 'result' => 1,
2688 '_nonce' => $post_data['_nonce'],
2689 ),
2690 USCES_CART_URL
2691 )
2692 );
2693 } else {
2694 $log = array(
2695 'acting' => $this->paymod_id,
2696 'key' => $tracking_id,
2697 'result' => 'ORDER DATA REGISTRATION ERROR',
2698 'data' => $response_data,
2699 );
2700 usces_save_order_acting_error( $log );
2701 wp_redirect(
2702 add_query_arg(
2703 array(
2704 'acting' => $this->paymod_id,
2705 'acting_return' => 0,
2706 'result' => 0,
2707 ),
2708 USCES_CART_URL
2709 )
2710 );
2711 }
2712 exit();
2713 } else {
2714 $log = array(
2715 'acting' => $this->paymod_id,
2716 'key' => $tracking_id,
2717 'result' => $cp_intent . ' ERROR',
2718 'data' => $response_data,
2719 );
2720 usces_save_order_acting_error( $log );
2721 wp_redirect(
2722 add_query_arg(
2723 array(
2724 'acting' => $this->paymod_id,
2725 'acting_return' => 0,
2726 'result' => 0,
2727 ),
2728 USCES_CART_URL
2729 )
2730 );
2731 exit();
2732 }
2733 } elseif ( 'acting_paypal_card' === $acting_flg ) {
2734 $entry = $usces->cart->get_entry();
2735 $cart = $usces->cart->get_cart();
2736 if ( empty( $cart ) || empty( $entry ) ) {
2737 return;
2738 }
2739
2740 parse_str( $post_query, $post_data );
2741 $tracking_id = ( isset( $post_data['tracking_id'] ) ) ? $post_data['tracking_id'] : ''; /* rand|reference_id */
2742 $resource_id = ( isset( $post_data['resource_id'] ) ) ? $post_data['resource_id'] : '';
2743 $payment_card = ( isset( $post_data['payment_card'] ) ) ? $post_data['payment_card'] : '';
2744 $payment_token_id = ( isset( $post_data['payment_token_id'] ) ) ? $post_data['payment_token_id'] : '';
2745 $vault = ( isset( $post_data['vault'] ) ) ? $post_data['vault'] : '';
2746
2747 if ( 'vault' === $payment_card && ! empty( $payment_token_id ) ) {
2748 if ( empty( $tracking_id ) ) {
2749 $log = array(
2750 'acting' => 'paypal_card',
2751 'key' => '(empty)',
2752 'result' => 'VAULT ORDER ERROR',
2753 'data' => $post_data,
2754 );
2755 usces_save_order_acting_error( $log );
2756 wp_redirect(
2757 add_query_arg(
2758 array(
2759 'acting' => 'paypal_card',
2760 'acting_return' => 0,
2761 'result' => 0,
2762 ),
2763 USCES_CART_URL
2764 )
2765 );
2766 exit();
2767 }
2768 } else {
2769 if ( empty( $tracking_id ) || empty( $resource_id ) ) {
2770 $log = array(
2771 'acting' => 'paypal_card',
2772 'key' => '(empty)',
2773 'result' => 'CREATE ORDER ERROR',
2774 'data' => $post_data,
2775 );
2776 usces_save_order_acting_error( $log );
2777 wp_redirect(
2778 add_query_arg(
2779 array(
2780 'acting' => 'paypal_card',
2781 'acting_return' => 0,
2782 'result' => 0,
2783 ),
2784 USCES_CART_URL
2785 )
2786 );
2787 exit();
2788 }
2789 }
2790
2791 $acting_opts = $this->get_acting_settings();
2792 $cp_intent = ( isset( $acting_opts['intent'] ) ) ? $acting_opts['intent'] : '';
2793 $api_request_url = ( isset( $acting_opts['api_request_url'] ) ) ? $acting_opts['api_request_url'] : '';
2794 $bn_code = self::API_BN_CODE_ACDC;
2795
2796 /* Get Access Token */
2797 $access_token = $this->get_access_token();
2798
2799 $correlation_id = '';
2800 $payment_source = array();
2801
2802 if ( 'vault' === $payment_card && ! empty( $payment_token_id ) ) {
2803 $correlation_id = ( isset( $post_data['correlation_id'] ) ) ? $post_data['correlation_id'] : '';
2804 $payment_source['token'] = array(
2805 'id' => $payment_token_id,
2806 'type' => 'PAYMENT_METHOD_TOKEN',
2807 );
2808 $entry['correlation_id'] = $correlation_id;
2809
2810 /* Create order */
2811 $response_order_data = $this->api_create_order( $access_token, $bn_code, $cp_intent, $tracking_id, $entry, $cart );
2812 if ( isset( $response_order_data['id'] ) && isset( $response_order_data['status'] ) && 'CREATED' === $response_order_data['status'] ) {
2813 $resource_id = $response_order_data['id'];
2814 } else {
2815 $log = array(
2816 'acting' => 'paypal_card',
2817 'key' => $tracking_id,
2818 'result' => 'CREATE ORDER ERROR',
2819 'data' => $response_order_data,
2820 );
2821 usces_save_order_acting_error( $log );
2822 wp_redirect(
2823 add_query_arg(
2824 array(
2825 'acting' => 'paypal_card',
2826 'acting_return' => 0,
2827 'result' => 0,
2828 ),
2829 USCES_CART_URL
2830 )
2831 );
2832 exit();
2833 }
2834 } else {
2835 $currency_code = $usces->get_currency_code();
2836 $cart_count = ( $cart && is_array( $cart ) ) ? count( $cart ) : 0;
2837 $item_total = (float) $entry['order']['total_items_price'];
2838 $shipping = usces_have_shipped( $cart );
2839 $shipping_charge = ( $shipping && isset( $entry['order']['shipping_charge'] ) && 0.0 !== (float) $entry['order']['shipping_charge'] ) ? (float) $entry['order']['shipping_charge'] : 0;
2840 $multiple_shipping = ( defined( 'WCEX_MSA' ) && isset( $entry['delivery']['delivery_flag'] ) && 2 === (int) $entry['delivery']['delivery_flag'] ) ? true : false;
2841 $fee = ( isset( $entry['order']['cod_fee'] ) && 0.0 !== (float) $entry['order']['cod_fee'] ) ? (float) $entry['order']['cod_fee'] : 0;
2842 $tax = ( isset( $entry['order']['tax'] ) && 'exclude' === usces_get_tax_mode() ) ? (float) $entry['order']['tax'] : 0;
2843 $discount = ( isset( $entry['order']['discount'] ) && 0.0 !== (float) $entry['order']['discount'] ) ? ( (float) $entry['order']['discount'] * -1 ) : 0;
2844 if ( usces_is_member_system() && usces_is_member_system_point() && isset( $entry['order']['usedpoint'] ) && 0 !== (int) $entry['order']['usedpoint'] ) {
2845 $discount += (float) $entry['order']['usedpoint'];
2846 }
2847
2848 $body = array();
2849 $body[0]['op'] = 'replace';
2850 $body[0]['path'] = "/purchase_units/@reference_id=='" . $tracking_id . "'";
2851
2852 $purchase_units['reference_id'] = $tracking_id;
2853 $purchase_units['amount']['currency_code'] = $currency_code;
2854 $purchase_units['amount']['value'] = usces_crform( $entry['order']['total_full_price'], false, false, 'return', false );
2855 if ( 0 < $item_total ) {
2856 $breakdown = array();
2857 $breakdown['item_total']['currency_code'] = $currency_code;
2858 $breakdown['item_total']['value'] = usces_crform( $item_total, false, false, 'return', false );
2859 if ( $shipping && 0 < $shipping_charge ) {
2860 $breakdown['shipping']['currency_code'] = $currency_code;
2861 $breakdown['shipping']['value'] = usces_crform( $shipping_charge, false, false, 'return', false );
2862 }
2863 if ( 0 < $fee ) {
2864 $breakdown['handling']['currency_code'] = $currency_code;
2865 $breakdown['handling']['value'] = usces_crform( $fee, false, false, 'return', false );
2866 }
2867 if ( 0 < $tax ) {
2868 $breakdown['tax_total']['currency_code'] = $currency_code;
2869 $breakdown['tax_total']['value'] = usces_crform( $tax, false, false, 'return', false );
2870 }
2871 if ( 0 < $discount ) {
2872 $breakdown['discount']['currency_code'] = $currency_code;
2873 $breakdown['discount']['value'] = usces_crform( $discount, false, false, 'return', false );
2874 }
2875 $purchase_units['amount']['breakdown'] = $breakdown;
2876 $items = array();
2877 for ( $i = 0; $i < $cart_count; $i++ ) {
2878 $cart_row = $cart[ $i ];
2879 $item_name = $usces->getItemName( $cart_row['post_id'] );
2880 if ( 60 < mb_strlen( $item_name, 'UTF-8' ) ) {
2881 $item_name = mb_substr( $item_name, 0, 60, 'UTF-8' ) . '...';
2882 }
2883 $items[ $i ]['name'] = $item_name;
2884 $items[ $i ]['unit_amount']['currency_code'] = $currency_code;
2885 $items[ $i ]['unit_amount']['value'] = usces_crform( $cart_row['price'], false, false, 'return', false );
2886 $items[ $i ]['quantity'] = $cart_row['quantity'];
2887 $options = ( isset( $cart_row['options'] ) && is_array( $cart_row['options'] ) ) ? $cart_row['options'] : array();
2888 if ( 0 < count( $options ) ) {
2889 $description = '';
2890 foreach ( $options as $key => $value ) {
2891 if ( ! empty( $key ) ) {
2892 $key = urldecode( $key );
2893 $value = maybe_unserialize( $value );
2894 if ( is_array( $value ) ) {
2895 $c = '';
2896 $description .= $key . ' : ';
2897 foreach ( $value as $v ) {
2898 $description .= $c . urldecode( $v );
2899 $c = ', ';
2900 }
2901 $description .= "\r\n";
2902 } else {
2903 $description .= $key . ' : ' . urldecode( $value ) . "\r\n";
2904 }
2905 }
2906 }
2907 if ( '' !== $description ) {
2908 if ( 60 < mb_strlen( $description, 'UTF-8' ) ) {
2909 $description = mb_substr( $description, 0, 60, 'UTF-8' ) . '...';
2910 }
2911 $items[ $i ]['description'] = $description;
2912 }
2913 }
2914 $items[ $i ]['sku'] = $usces->getItemCode( $cart_row['post_id'] ) . ' ' . urldecode( $cart_row['sku'] );
2915 }
2916 $purchase_units['items'] = $items;
2917 }
2918
2919 $application_context = array();
2920 if ( $multiple_shipping && false === $array ) {
2921 $msacart = ( isset( $_SESSION['msa_cart'] ) ) ? current( $_SESSION['msa_cart'] ) : array();
2922 if ( isset( $msacart['delivery']['destination_id'] ) ) {
2923 $member = $usces->get_member();
2924 $msadelivery = msa_get_destination( $member['ID'], $msacart['delivery']['destination_id'] );
2925 $name = usces_localized_name( trim( $msadelivery['msa_name'] ), trim( $msadelivery['msa_name2'] ), 'return' );
2926 $purchase_units['shipping']['name']['full_name'] = $name;
2927 $purchase_units['shipping']['address']['address_line_2'] = trim( $msadelivery['msa_address3'] );
2928 $purchase_units['shipping']['address']['address_line_1'] = trim( $msadelivery['msa_address2'] );
2929 $purchase_units['shipping']['address']['admin_area_2'] = trim( $msadelivery['msa_address1'] );
2930 $purchase_units['shipping']['address']['admin_area_1'] = trim( $msadelivery['msa_pref'] );
2931 $purchase_units['shipping']['address']['postal_code'] = trim( mb_convert_kana( $msadelivery['msa_zip'], 'a', 'UTF-8' ) );
2932 $purchase_units['shipping']['address']['country_code'] = 'JP';
2933 $application_context['shipping_preference'] = 'SET_PROVIDED_ADDRESS';
2934 } else {
2935 $application_context['shipping_preference'] = 'NO_SHIPPING';
2936 }
2937 } elseif ( $shipping ) {
2938 $name = usces_localized_name( trim( $entry['delivery']['name1'] ), trim( $entry['delivery']['name2'] ), 'return' );
2939 $country = ( ! empty( $entry['delivery']['country'] ) ) ? $entry['delivery']['country'] : usces_get_base_country();
2940 $country_code = apply_filters( 'usces_filter_paypalcp_shipping_country_code', $country );
2941 $purchase_units['shipping']['name']['full_name'] = $name;
2942 $purchase_units['shipping']['address']['address_line_2'] = trim( $entry['delivery']['address3'] );
2943 $purchase_units['shipping']['address']['address_line_1'] = trim( $entry['delivery']['address2'] );
2944 $purchase_units['shipping']['address']['admin_area_2'] = trim( $entry['delivery']['address1'] );
2945 $purchase_units['shipping']['address']['admin_area_1'] = trim( $entry['delivery']['pref'] );
2946 $purchase_units['shipping']['address']['postal_code'] = trim( mb_convert_kana( $entry['delivery']['zipcode'], 'a', 'UTF-8' ) );
2947 $purchase_units['shipping']['address']['country_code'] = $country_code;
2948 $application_context['shipping_preference'] = 'SET_PROVIDED_ADDRESS';
2949 } else {
2950 $application_context['shipping_preference'] = 'NO_SHIPPING';
2951 }
2952 // $body['application_context'] = $application_context;
2953
2954 $body[0]['value'] = $purchase_units;
2955
2956 /* Update Order */
2957 $params = array(
2958 'method' => 'PATCH',
2959 'headers' => array(
2960 'Content-Type' => 'application/json',
2961 'Authorization' => 'Bearer ' . $access_token['access_token'],
2962 'PayPal-Partner-Attribution-Id' => $bn_code,
2963 'PayPal-Request-Id' => $tracking_id,
2964 ),
2965 'timeout' => self::API_TIMEOUT,
2966 'body' => wp_json_encode( $body, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES ),
2967 );
2968 $response = wp_remote_request( $api_request_url . '/v2/checkout/orders/' . $resource_id, $params );
2969 if ( isset( $response['response']['code'] ) && '204' === (string) $response['response']['code'] ) {
2970 /* A successful request returns the HTTP 204 No Content status code with an empty object in the JSON response body. */
2971 } else {
2972 $log = array(
2973 'acting' => 'paypal_card',
2974 'key' => $tracking_id,
2975 'result' => 'UPDATE ORDER ERROR',
2976 'data' => $response,
2977 );
2978 usces_save_order_acting_error( $log );
2979 wp_redirect(
2980 add_query_arg(
2981 array(
2982 'acting' => 'paypal_card',
2983 'acting_return' => 0,
2984 'result' => 0,
2985 ),
2986 USCES_CART_URL
2987 )
2988 );
2989 exit();
2990 }
2991 }
2992
2993 $usces->error_message = $usces->zaiko_check();
2994 if ( ! empty( $usces->error_message ) || 0 === (int) $usces->cart->num_row() ) {
2995 wp_redirect( USCES_CART_URL );
2996 exit();
2997 }
2998
2999 if ( 'CAPTURE' === $cp_intent ) {
3000 /* Capture */
3001 $response_data = $this->api_capture_order( $access_token, $bn_code, $tracking_id, $resource_id, $correlation_id, $payment_source );
3002 } elseif ( 'AUTHORIZE' === $cp_intent ) {
3003 /* Authorize */
3004 $response_data = $this->api_authorize_order( $access_token, $bn_code, $tracking_id, $resource_id, $correlation_id, $payment_source );
3005 }
3006 if ( isset( $response_data['status'] ) ) {
3007 $status = $response_data['status'];
3008 } else {
3009 $status = $cp_intent . ' ERROR';
3010 }
3011 if ( isset( $response_data['purchase_units'] ) ) {
3012 $purchase_units = $response_data['purchase_units'][0];
3013 if ( isset( $purchase_units['payments']['captures'] ) ) {
3014 $payments = $purchase_units['payments']['captures'][0];
3015 if ( isset( $payments['status'] ) ) {
3016 if ( 'COMPLETED' !== $payments['status'] && 'PENDING' !== $payments['status'] ) {
3017 $status = $payments['status'];
3018 }
3019 }
3020 } elseif ( isset( $purchase_units['payments']['authorizations'] ) ) {
3021 $payments = $purchase_units['payments']['authorizations'][0];
3022 if ( isset( $payments['status'] ) ) {
3023 if ( 'CREATED' !== $payments['status'] && 'PENDING' !== $payments['status'] ) {
3024 $status = $payments['status'];
3025 }
3026 }
3027 }
3028 }
3029 if ( isset( $response_data['id'] ) && 'COMPLETED' === $status ) {
3030 if ( 'vault' === $vault && 'another' === $payment_card && ! empty( $payment_token_id ) ) {
3031 $delete_status = $this->api_delete_payment_tokens( $access_token, $payment_token_id );
3032 }
3033 if ( isset( $acting_opts['card_3ds'] ) && 'on' === $acting_opts['card_3ds'] ) {
3034 $response_data['card_3ds']['liability_shift'] = ( isset( $post_data['liability_shift'] ) ) ? $post_data['liability_shift'] : '';
3035 $response_data['card_3ds']['enrollment_status'] = ( isset( $post_data['enrollment_status'] ) ) ? $post_data['enrollment_status'] : '';
3036 $response_data['card_3ds']['authentication_status'] = ( isset( $post_data['authentication_status'] ) ) ? $post_data['authentication_status'] : '';
3037 }
3038
3039 /* Welcart order data registration */
3040 $res = $usces->order_processing( $response_data );
3041 if ( 'ordercompletion' === $res ) {
3042 wp_redirect(
3043 add_query_arg(
3044 array(
3045 'acting' => 'paypal_card',
3046 'acting_return' => 1,
3047 'result' => 1,
3048 '_nonce' => $post_data['_nonce'],
3049 ),
3050 USCES_CART_URL
3051 )
3052 );
3053 } else {
3054 $log = array(
3055 'acting' => 'paypal_card',
3056 'key' => $tracking_id,
3057 'result' => 'ORDER DATA REGISTRATION ERROR',
3058 'data' => $response_data,
3059 );
3060 usces_save_order_acting_error( $log );
3061 wp_redirect(
3062 add_query_arg(
3063 array(
3064 'acting' => 'paypal_card',
3065 'acting_return' => 0,
3066 'result' => 0,
3067 ),
3068 USCES_CART_URL
3069 )
3070 );
3071 }
3072 exit();
3073 } else {
3074 $log = array(
3075 'acting' => 'paypal_card',
3076 'key' => $tracking_id,
3077 'result' => $cp_intent . ' ERROR',
3078 'data' => $response_data,
3079 );
3080 usces_save_order_acting_error( $log );
3081 wp_redirect(
3082 add_query_arg(
3083 array(
3084 'acting' => 'paypal_card',
3085 'acting_return' => 0,
3086 'retry' => 1,
3087 ),
3088 USCES_CART_URL
3089 )
3090 );
3091 exit();
3092 }
3093 }
3094 }
3095
3096 /**
3097 * Orders API.
3098 * /v2/checkout/orders
3099 *
3100 * @param array $access_token API Access Token.
3101 * @param string $bn_code BN Code.
3102 * @param string $intent AUTHORIZE|CAPTURE.
3103 * @param string $tracking_id Tracking ID.
3104 * @param array $entry Entry data.
3105 * @param array $cart Cart data.
3106 * @param boolean $array Associative true|false.
3107 * @return array|object
3108 */
3109 public function api_create_order( $access_token, $bn_code, $intent, $tracking_id, $entry, $cart, $array = true ) {
3110 global $usces;
3111
3112 $correlation_id = ( ! empty( $entry['correlation_id'] ) ) ? $entry['correlation_id'] : ''; // Purchase using a saved card.
3113 $pre_card_order = ( isset( $entry['pre_card_order'] ) && '' === $correlation_id ) ? true : false; // Pre-Order. Orders will be updated later.
3114
3115 $acting_opts = $this->get_acting_settings();
3116 if ( empty( $intent ) ) {
3117 $intent = ( isset( $acting_opts['intent'] ) ) ? $acting_opts['intent'] : '';
3118 }
3119 $api_request_url = ( isset( $acting_opts['api_request_url'] ) ) ? $acting_opts['api_request_url'] : '';
3120 $currency_code = $usces->get_currency_code();
3121 $cart_count = ( $cart && is_array( $cart ) ) ? count( $cart ) : 0;
3122 if ( 0 < $cart_count && isset( $entry['order']['total_items_price'] ) ) {
3123 $item_total = (float) $entry['order']['total_items_price'];
3124 $shipping = usces_have_shipped( $cart );
3125 $shipping_charge = ( $shipping && isset( $entry['order']['shipping_charge'] ) && 0.0 !== (float) $entry['order']['shipping_charge'] ) ? (float) $entry['order']['shipping_charge'] : 0;
3126 $multiple_shipping = ( defined( 'WCEX_MSA' ) && isset( $entry['delivery']['delivery_flag'] ) && 2 === (int) $entry['delivery']['delivery_flag'] ) ? true : false;
3127 $fee = ( isset( $entry['order']['cod_fee'] ) && 0.0 !== (float) $entry['order']['cod_fee'] ) ? (float) $entry['order']['cod_fee'] : 0;
3128 $tax = ( isset( $entry['order']['tax'] ) && 'exclude' === usces_get_tax_mode() ) ? (float) $entry['order']['tax'] : 0;
3129 $discount = ( isset( $entry['order']['discount'] ) && 0.0 !== (float) $entry['order']['discount'] ) ? ( (float) $entry['order']['discount'] * -1 ) : 0;
3130 if ( usces_is_member_system() && usces_is_member_system_point() && isset( $entry['order']['usedpoint'] ) && 0 !== (int) $entry['order']['usedpoint'] ) {
3131 $discount += (float) $entry['order']['usedpoint'];
3132 }
3133 } else {
3134 $item_total = 0;
3135 $shipping = false;
3136 $multiple_shipping = false;
3137 }
3138
3139 $body = array();
3140 $body['intent'] = $intent;
3141 $purchase_units = array();
3142 $purchase_units['reference_id'] = $tracking_id;
3143 if ( $pre_card_order ) {
3144 $purchase_units['amount']['currency_code'] = $currency_code;
3145 $purchase_units['amount']['value'] = $item_total;
3146 } else {
3147 $purchase_units['amount']['currency_code'] = $currency_code;
3148 $purchase_units['amount']['value'] = usces_crform( $entry['order']['total_full_price'], false, false, 'return', false );
3149 if ( 0 < $item_total ) {
3150 $breakdown = array();
3151 $breakdown['item_total']['currency_code'] = $currency_code;
3152 $breakdown['item_total']['value'] = usces_crform( $item_total, false, false, 'return', false );
3153 if ( $shipping && 0 < $shipping_charge ) {
3154 $breakdown['shipping']['currency_code'] = $currency_code;
3155 $breakdown['shipping']['value'] = usces_crform( $shipping_charge, false, false, 'return', false );
3156 }
3157 if ( 0 < $fee ) {
3158 $breakdown['handling']['currency_code'] = $currency_code;
3159 $breakdown['handling']['value'] = usces_crform( $fee, false, false, 'return', false );
3160 }
3161 if ( 0 < $tax ) {
3162 $breakdown['tax_total']['currency_code'] = $currency_code;
3163 $breakdown['tax_total']['value'] = usces_crform( $tax, false, false, 'return', false );
3164 }
3165 if ( 0 < $discount ) {
3166 $breakdown['discount']['currency_code'] = $currency_code;
3167 $breakdown['discount']['value'] = usces_crform( $discount, false, false, 'return', false );
3168 }
3169 $purchase_units['amount']['breakdown'] = $breakdown;
3170 $items = array();
3171 for ( $i = 0; $i < $cart_count; $i++ ) {
3172 $cart_row = $cart[ $i ];
3173 $item_name = $usces->getItemName( $cart_row['post_id'] );
3174 if ( 60 < mb_strlen( $item_name, 'UTF-8' ) ) {
3175 $item_name = mb_substr( $item_name, 0, 60, 'UTF-8' ) . '...';
3176 }
3177 $items[ $i ]['name'] = $item_name;
3178 $items[ $i ]['unit_amount']['currency_code'] = $currency_code;
3179 $items[ $i ]['unit_amount']['value'] = usces_crform( $cart_row['price'], false, false, 'return', false );
3180 $items[ $i ]['quantity'] = $cart_row['quantity'];
3181 $options = ( isset( $cart_row['options'] ) && is_array( $cart_row['options'] ) ) ? $cart_row['options'] : array();
3182 if ( 0 < count( $options ) ) {
3183 $description = '';
3184 foreach ( $options as $key => $value ) {
3185 if ( ! empty( $key ) ) {
3186 $key = urldecode( $key );
3187 $value = maybe_unserialize( $value );
3188 if ( is_array( $value ) ) {
3189 $c = '';
3190 $description .= $key . ' : ';
3191 foreach ( $value as $v ) {
3192 $description .= $c . urldecode( $v );
3193 $c = ', ';
3194 }
3195 $description .= "\r\n";
3196 } else {
3197 $description .= $key . ' : ' . urldecode( $value ) . "\r\n";
3198 }
3199 }
3200 }
3201 if ( '' !== $description ) {
3202 if ( 60 < mb_strlen( $description, 'UTF-8' ) ) {
3203 $description = mb_substr( $description, 0, 60, 'UTF-8' ) . '...';
3204 }
3205 $items[ $i ]['description'] = $description;
3206 }
3207 }
3208 $items[ $i ]['sku'] = $usces->getItemCode( $cart_row['post_id'] ) . ' ' . urldecode( $cart_row['sku'] );
3209 }
3210 $purchase_units['items'] = $items;
3211 }
3212 }
3213 if ( false === $array ) {
3214 $payer = array();
3215 $name = usces_localized_name( trim( $entry['customer']['name1'] ), trim( $entry['customer']['name2'] ), 'return' );
3216 $phone = str_replace( array( '-', '+', ' ' ), '', mb_convert_kana( $entry['customer']['tel'], 'a', 'UTF-8' ) );
3217 $country = ( ! empty( $entry['customer']['country'] ) ) ? $entry['customer']['country'] : usces_get_base_country();
3218 $country_code = apply_filters( 'usces_filter_paypalcp_customer_country_code', $country );
3219 $payer['name']['given_name'] = trim( $entry['customer']['name2'] );
3220 $payer['name']['surname'] = trim( $entry['customer']['name1'] );
3221 $payer['email_address'] = trim( $entry['customer']['mailaddress1'] );
3222 if ( ! empty( $phone ) ) {
3223 $payer['phone']['phone_number']['national_number'] = ltrim( $phone, '0' );
3224 }
3225 if ( ! empty( $entry['customer']['address1'] ) ) {
3226 $payer['address']['address_line_2'] = trim( $entry['customer']['address3'] );
3227 $payer['address']['address_line_1'] = trim( $entry['customer']['address2'] );
3228 $payer['address']['admin_area_2'] = trim( $entry['customer']['address1'] );
3229 }
3230 if ( ! empty( $entry['customer']['pref'] ) ) {
3231 $payer['address']['admin_area_1'] = trim( $entry['customer']['pref'] );
3232 }
3233 if ( ! empty( $entry['customer']['zipcode'] ) ) {
3234 $payer['address']['postal_code'] = trim( mb_convert_kana( $entry['customer']['zipcode'], 'a', 'UTF-8' ) );
3235 }
3236 $payer['address']['country_code'] = $country_code;
3237 $body['payer'] = $payer;
3238 }
3239 if ( ! $pre_card_order ) {
3240 $application_context = array();
3241 if ( $multiple_shipping && false === $array ) {
3242 $msacart = ( isset( $_SESSION['msa_cart'] ) ) ? current( $_SESSION['msa_cart'] ) : array();
3243 if ( isset( $msacart['delivery']['destination_id'] ) ) {
3244 $member = $usces->get_member();
3245 $msadelivery = msa_get_destination( $member['ID'], $msacart['delivery']['destination_id'] );
3246 $name = usces_localized_name( trim( $msadelivery['msa_name'] ), trim( $msadelivery['msa_name2'] ), 'return' );
3247 $purchase_units['shipping']['name']['full_name'] = $name;
3248 $purchase_units['shipping']['address']['address_line_2'] = trim( $msadelivery['msa_address3'] );
3249 $purchase_units['shipping']['address']['address_line_1'] = trim( $msadelivery['msa_address2'] );
3250 $purchase_units['shipping']['address']['admin_area_2'] = trim( $msadelivery['msa_address1'] );
3251 $purchase_units['shipping']['address']['admin_area_1'] = trim( $msadelivery['msa_pref'] );
3252 $purchase_units['shipping']['address']['postal_code'] = trim( mb_convert_kana( $msadelivery['msa_zip'], 'a', 'UTF-8' ) );
3253 $purchase_units['shipping']['address']['country_code'] = 'JP';
3254 $application_context['shipping_preference'] = 'SET_PROVIDED_ADDRESS';
3255 } else {
3256 $application_context['shipping_preference'] = 'NO_SHIPPING';
3257 }
3258 } elseif ( $shipping ) {
3259 $name = usces_localized_name( trim( $entry['delivery']['name1'] ), trim( $entry['delivery']['name2'] ), 'return' );
3260 $country = ( ! empty( $entry['delivery']['country'] ) ) ? $entry['delivery']['country'] : usces_get_base_country();
3261 $country_code = apply_filters( 'usces_filter_paypalcp_shipping_country_code', $country );
3262 $purchase_units['shipping']['name']['full_name'] = $name;
3263 $purchase_units['shipping']['address']['address_line_2'] = trim( $entry['delivery']['address3'] );
3264 $purchase_units['shipping']['address']['address_line_1'] = trim( $entry['delivery']['address2'] );
3265 $purchase_units['shipping']['address']['admin_area_2'] = trim( $entry['delivery']['address1'] );
3266 $purchase_units['shipping']['address']['admin_area_1'] = trim( $entry['delivery']['pref'] );
3267 $purchase_units['shipping']['address']['postal_code'] = trim( mb_convert_kana( $entry['delivery']['zipcode'], 'a', 'UTF-8' ) );
3268 $purchase_units['shipping']['address']['country_code'] = $country_code;
3269 $application_context['shipping_preference'] = 'SET_PROVIDED_ADDRESS';
3270 } else {
3271 $application_context['shipping_preference'] = 'NO_SHIPPING';
3272 }
3273 }
3274 $body['purchase_units'] = array( $purchase_units );
3275 if ( ! $pre_card_order ) {
3276 $body['application_context'] = $application_context;
3277 }
3278
3279 $params = array(
3280 'method' => 'POST',
3281 'headers' => array(
3282 'Content-Type' => 'application/json;charset=utf-8',
3283 'Authorization' => 'Bearer ' . $access_token['access_token'],
3284 'PayPal-Partner-Attribution-Id' => $bn_code,
3285 'PayPal-Request-Id' => $tracking_id,
3286 ),
3287 'timeout' => self::API_TIMEOUT,
3288 'body' => wp_json_encode( $body ),
3289 );
3290 if ( ! empty( $correlation_id ) ) {
3291 $params['headers']['PayPal-Client-Metadata-Id'] = $correlation_id;
3292 }
3293 $response = wp_remote_post( $api_request_url . '/v2/checkout/orders', $params );
3294 if ( isset( $response['response']['code'] ) && ( '200' === (string) $response['response']['code'] || '201' === (string) $response['response']['code'] ) ) {
3295 $response_data = json_decode( wp_remote_retrieve_body( $response ), $array );
3296 } else {
3297 $response_data = json_decode( wp_remote_retrieve_body( $response ), $array );
3298 if ( is_array( $response_data ) && ! isset( $response_data['status'] ) && ! isset( $response_data['name'] ) ) {
3299 $response_data['name'] = 'CREATE ORDER ERROR';
3300 }
3301 }
3302 return $response_data;
3303 }
3304
3305 /**
3306 * Orders API.
3307 * /v2/checkout/orders/{id}
3308 *
3309 * @param array $access_token API Access Token.
3310 * @param string $bn_code BN Code.
3311 * @param string $tracking_id Tracking ID.
3312 * @param string $resource_id order ID.
3313 * @param boolean $array Associative true|false.
3314 * @return array
3315 */
3316 public function api_get_order( $access_token, $bn_code, $tracking_id, $resource_id, $array = true ) {
3317 $acting_opts = $this->get_acting_settings();
3318 $api_request_url = ( isset( $acting_opts['api_request_url'] ) ) ? $acting_opts['api_request_url'] : '';
3319
3320 $params = array(
3321 'method' => 'GET',
3322 'headers' => array(
3323 'Content-Type' => 'application/json;charset=utf-8',
3324 'Authorization' => 'Bearer ' . $access_token['access_token'],
3325 'PayPal-Partner-Attribution-Id' => $bn_code,
3326 'PayPal-Request-Id' => $tracking_id,
3327 ),
3328 'timeout' => self::API_TIMEOUT,
3329 );
3330 $response = wp_remote_get( $api_request_url . '/v2/checkout/orders/' . $resource_id, $params );
3331 if ( is_wp_error( $response ) ) {
3332 $response_data = array();
3333 } else {
3334 $response_data = json_decode( wp_remote_retrieve_body( $response ), $array );
3335 }
3336 return $response_data;
3337 }
3338
3339 /**
3340 * Orders API.
3341 * /v2/checkout/orders/{id}/capture
3342 *
3343 * @param array $access_token API Access Token.
3344 * @param string $bn_code BN Code.
3345 * @param string $tracking_id Tracking ID.
3346 * @param string $resource_id order ID.
3347 * @param string $correlation_id Correlation ID (CMID, Session Identifier f).
3348 * @param array $payment_source Billing Agreement ID.
3349 * @param boolean $array Associative true|false.
3350 * @return array
3351 */
3352 public function api_capture_order( $access_token, $bn_code, $tracking_id, $resource_id, $correlation_id, $payment_source, $array = true ) {
3353 $acting_opts = $this->get_acting_settings();
3354 $api_request_url = ( isset( $acting_opts['api_request_url'] ) ) ? $acting_opts['api_request_url'] : '';
3355
3356 $params = array(
3357 'method' => 'POST',
3358 'headers' => array(
3359 'Content-Type' => 'application/json;charset=utf-8',
3360 'Authorization' => 'Bearer ' . $access_token['access_token'],
3361 'PayPal-Partner-Attribution-Id' => $bn_code,
3362 'PayPal-Request-Id' => $tracking_id,
3363 ),
3364 'timeout' => self::API_TIMEOUT,
3365 );
3366 if ( ! empty( $correlation_id ) ) {
3367 $params['headers']['PayPal-Client-Metadata-Id'] = $correlation_id;
3368 }
3369 if ( ! empty( $payment_source ) ) {
3370 $body = array();
3371 $body['payment_source'] = $payment_source;
3372 $params['body'] = wp_json_encode( $body );
3373 }
3374 $response = wp_remote_get( $api_request_url . '/v2/checkout/orders/' . $resource_id . '/capture', $params );
3375 if ( is_wp_error( $response ) ) {
3376 $response_data = array();
3377 } else {
3378 if ( isset( $response['response']['code'] ) && ( '200' === (string) $response['response']['code'] || '201' === (string) $response['response']['code'] ) ) {
3379 $response_data = json_decode( wp_remote_retrieve_body( $response ), $array );
3380 } else {
3381 $response_data = json_decode( wp_remote_retrieve_body( $response ), $array );
3382 if ( ! isset( $response_data['status'] ) && ! isset( $response_data['name'] ) ) {
3383 $response_data['name'] = 'CAPTURE ERROR';
3384 }
3385 }
3386 }
3387 return $response_data;
3388 }
3389
3390 /**
3391 * Orders API.
3392 * /v2/checkout/orders/{id}/authorize
3393 *
3394 * @param array $access_token API Access Token.
3395 * @param string $bn_code BN Code.
3396 * @param string $tracking_id Tracking ID.
3397 * @param string $resource_id order ID.
3398 * @param string $correlation_id Correlation ID (CMID, Session Identifier f).
3399 * @param array $payment_source Billing Agreement ID.
3400 * @param boolean $array Associative true|false.
3401 * @return array
3402 */
3403 public function api_authorize_order( $access_token, $bn_code, $tracking_id, $resource_id, $correlation_id, $payment_source, $array = true ) {
3404 $acting_opts = $this->get_acting_settings();
3405 $api_request_url = ( isset( $acting_opts['api_request_url'] ) ) ? $acting_opts['api_request_url'] : '';
3406
3407 $params = array(
3408 'method' => 'POST',
3409 'headers' => array(
3410 'Content-Type' => 'application/json;charset=utf-8',
3411 'Authorization' => 'Bearer ' . $access_token['access_token'],
3412 'PayPal-Partner-Attribution-Id' => $bn_code,
3413 'PayPal-Request-Id' => $tracking_id,
3414 ),
3415 'timeout' => self::API_TIMEOUT,
3416 );
3417 if ( ! empty( $correlation_id ) ) {
3418 $params['headers']['PayPal-Client-Metadata-Id'] = $correlation_id;
3419 }
3420 if ( ! empty( $payment_source ) ) {
3421 $body = array();
3422 $body['payment_source'] = $payment_source;
3423 $params['body'] = wp_json_encode( $body );
3424 }
3425 $response = wp_remote_get( $api_request_url . '/v2/checkout/orders/' . $resource_id . '/authorize', $params );
3426 if ( is_wp_error( $response ) ) {
3427 $response_data = array();
3428 } else {
3429 if ( isset( $response['response']['code'] ) && ( '200' === (string) $response['response']['code'] || '201' === (string) $response['response']['code'] ) ) {
3430 $response_data = json_decode( wp_remote_retrieve_body( $response ), $array );
3431 } else {
3432 $response_data = json_decode( wp_remote_retrieve_body( $response ), $array );
3433 if ( ! isset( $response_data['status'] ) && ! isset( $response_data['name'] ) ) {
3434 $response_data['name'] = 'AUTHORIZE ERROR';
3435 }
3436 }
3437 }
3438 return $response_data;
3439 }
3440
3441 /**
3442 * Orders API.
3443 * /v2/payments/authorizations/{id}/void
3444 *
3445 * @param array $access_token API Access Token.
3446 * @param string $bn_code BN Code.
3447 * @param string $tracking_id Tracking ID.
3448 * @param string $id ID.
3449 * @return string
3450 */
3451 public function api_authorize_void( $access_token, $bn_code, $tracking_id, $id ) {
3452 $acting_opts = $this->get_acting_settings();
3453 $api_request_url = ( isset( $acting_opts['api_request_url'] ) ) ? $acting_opts['api_request_url'] : '';
3454 $void_status = '';
3455
3456 $params = array(
3457 'method' => 'POST',
3458 'headers' => array(
3459 'Content-Type' => 'application/json;charset=utf-8',
3460 'Authorization' => 'Bearer ' . $access_token['access_token'],
3461 'PayPal-Partner-Attribution-Id' => $bn_code,
3462 'PayPal-Request-Id' => $tracking_id,
3463 ),
3464 'timeout' => self::API_TIMEOUT,
3465 );
3466 $response = wp_remote_get( $api_request_url . '/v2/payments/authorizations/' . $id . '/void', $params );
3467 if ( is_wp_error( $response ) ) {
3468 $void_status = 'VOID ERROR';
3469 } elseif ( isset( $response['response']['code'] ) && '204' === (string) $response['response']['code'] ) {
3470 $void_status = 'COMPLETED';
3471 } else {
3472 $response_data = json_decode( wp_remote_retrieve_body( $response ), true );
3473 if ( isset( $response_data['status'] ) ) {
3474 $void_status = $response_data['status'];
3475 } elseif ( isset( $response_data['name'] ) ) {
3476 $void_status = $response_data['name'];
3477 } else {
3478 $void_status = 'VOID ERROR';
3479 }
3480 }
3481 return $void_status;
3482 }
3483
3484 /**
3485 * Payment Method Tokens API.
3486 * /v2/vault/payment-tokens/
3487 * /v2/vault/payment-tokens/{id}
3488 *
3489 * @param array $access_token API Access Token.
3490 * @param string $customer_id Member ID.
3491 * @param string $payment_token_id Payment Token ID.
3492 * @return array
3493 */
3494 public function api_get_payment_tokens( $access_token, $customer_id, $payment_token_id = '' ) {
3495 $acting_opts = $this->get_acting_settings();
3496 $api_request_url = ( isset( $acting_opts['api_request_url'] ) ) ? $acting_opts['api_request_url'] : '';
3497 $card_prefix = ( isset( $acting_opts['card_prefix'] ) ) ? $acting_opts['card_prefix'] : '';
3498
3499 $params = array(
3500 'method' => 'GET',
3501 'headers' => array(
3502 'Content-Type' => 'application/json;charset=utf-8',
3503 'Authorization' => 'Bearer ' . $access_token['access_token'],
3504 'PayPal-Partner-Attribution-Id' => self::API_BN_CODE_ACDC,
3505 ),
3506 'timeout' => self::API_TIMEOUT,
3507 );
3508 if ( ! empty( $payment_token_id ) ) {
3509 $response = wp_remote_get( $api_request_url . '/v2/vault/payment-tokens/' . $payment_token_id, $params );
3510 } else {
3511 $response = wp_remote_get( $api_request_url . '/v2/vault/payment-tokens?customer_id=' . $card_prefix . $customer_id . '&total_required=true', $params );
3512 }
3513 if ( is_wp_error( $response ) ) {
3514 $response_data = array();
3515 } else {
3516 $response_data = json_decode( wp_remote_retrieve_body( $response ), true );
3517 }
3518 return $response_data;
3519 }
3520
3521 /**
3522 * Payment Method Tokens API.
3523 * /v2/vault/payment-tokens/{id}
3524 *
3525 * @param array $access_token API Access Token.
3526 * @param string $payment_token_id Payment Token ID.
3527 * @return string
3528 */
3529 public function api_delete_payment_tokens( $access_token, $payment_token_id ) {
3530 $acting_opts = $this->get_acting_settings();
3531 $api_request_url = ( isset( $acting_opts['api_request_url'] ) ) ? $acting_opts['api_request_url'] : '';
3532 $delete_status = '';
3533
3534 $params = array(
3535 'method' => 'DELETE',
3536 'headers' => array(
3537 'Content-Type' => 'application/json;charset=utf-8',
3538 'Authorization' => 'Bearer ' . $access_token['access_token'],
3539 'PayPal-Partner-Attribution-Id' => self::API_BN_CODE_ACDC,
3540 ),
3541 'timeout' => self::API_TIMEOUT,
3542 );
3543 $response = wp_remote_request( $api_request_url . '/v2/vault/payment-tokens/' . $payment_token_id, $params );
3544 if ( is_wp_error( $response ) ) {
3545 $delete_status = 'DELETE ERROR';
3546 } elseif ( isset( $response['response']['code'] ) && '204' === (string) $response['response']['code'] ) {
3547 $delete_status = 'COMPLETED';
3548 } else {
3549 $response_data = json_decode( wp_remote_retrieve_body( $response ), true );
3550 if ( isset( $response_data['status'] ) ) {
3551 $delete_status = $response_data['status'];
3552 } elseif ( isset( $response_data['name'] ) ) {
3553 $delete_status = $response_data['name'];
3554 } else {
3555 $delete_status = 'DELETE ERROR';
3556 }
3557 }
3558 return $delete_status;
3559 }
3560
3561 /**
3562 * 決済完了ページ制御
3563 * usces_filter_check_acting_return_results
3564 *
3565 * @param array $results Results data.
3566 * @return array
3567 */
3568 public function acting_return( $results ) {
3569 $acting_flg = ( isset( $results['acting'] ) ) ? 'acting_' . $results['acting'] : '';
3570 if ( ! in_array( $acting_flg, $this->pay_method ) ) {
3571 return $results;
3572 }
3573 if ( isset( $results['acting_return'] ) && 1 !== (int) $results['acting_return'] ) {
3574 return $results;
3575 }
3576 if ( ! isset( $results['_nonce'] ) || ! wp_verify_nonce( $results['_nonce'], $acting_flg ) ) {
3577 wp_redirect( home_url() );
3578 exit();
3579 }
3580 $results['reg_order'] = false;
3581 return $results;
3582 }
3583
3584 /**
3585 * 受注データ登録
3586 * Called by usces_reg_orderdata() and usces_new_orderdata().
3587 * usces_action_reg_orderdata
3588 *
3589 * @param array $args Compact array( $cart, $entry, $order_id, $member_id, $payments, $charging_type, $results ).
3590 */
3591 public function register_orderdata( $args ) {
3592 global $usces;
3593 extract( $args );
3594
3595 $acting_flg = $payments['settlement'];
3596 if ( ! in_array( $acting_flg, $this->pay_method ) ) {
3597 return;
3598 }
3599 if ( ! $entry['order']['total_full_price'] ) {
3600 return;
3601 }
3602
3603 if ( isset( $results['id'] ) && isset( $results['purchase_units'] ) ) {
3604 $acting = substr( $acting_flg, 7 );
3605 $usces->set_order_meta_value( 'resource_id', $results['id'], $order_id );
3606 $purchase_units = $results['purchase_units'][0];
3607 if ( isset( $purchase_units['reference_id'] ) ) {
3608 $tracking_id = $purchase_units['reference_id']; /* rand */
3609 $usces->set_order_meta_value( 'reference_id', $tracking_id, $order_id );
3610 }
3611 if ( isset( $purchase_units['payments'] ) ) {
3612 if ( isset( $purchase_units['payments']['captures'] ) ) {
3613 $acting_status = 'CAPTURE';
3614 $payments = $purchase_units['payments']['captures'][0];
3615 } elseif ( isset( $purchase_units['payments']['authorizations'] ) ) {
3616 $acting_status = 'AUTHORIZE';
3617 $payments = $purchase_units['payments']['authorizations'][0];
3618 }
3619 if ( ! empty( $payments['id'] ) ) {
3620 $usces->set_order_meta_value( 'wc_trans_id', $payments['id'], $order_id ); /* 決済ID */
3621 $usces->set_order_meta_value( 'trans_id', $payments['id'], $order_id ); /* 取引ID */
3622 }
3623 if ( isset( $results['status'] ) && ! empty( $tracking_id ) ) {
3624 $results = apply_filters( 'usces_filter_paypal_cp_register_orderdata_log', $results, $args );
3625 $this->save_acting_log( $results, $acting, $acting_status, $results['status'], $entry['order']['total_full_price'], $order_id, $tracking_id );
3626 }
3627 }
3628 $results['acting'] = $acting;
3629 $usces->set_order_meta_value( $acting_flg, usces_serialize( $results ), $order_id );
3630 if ( isset( $results['card_3ds'] ) ) {
3631 $usces->set_order_meta_value( 'liability_shift', $results['card_3ds']['liability_shift'], $order_id );
3632 $usces->set_order_meta_value( 'enrollment_status', $results['card_3ds']['enrollment_status'], $order_id );
3633 $usces->set_order_meta_value( 'authentication_status', $results['card_3ds']['authentication_status'], $order_id );
3634 }
3635 }
3636 }
3637
3638 /**
3639 * ポイント即時付与
3640 * usces_filter_is_complete_settlement
3641 *
3642 * @param boolean $complete Always give points immediately.
3643 * @param string $payment_name Payment name.
3644 * @param string $status Payment status.
3645 * @return boolean
3646 */
3647 public function is_complete_settlement( $complete, $payment_name, $status ) {
3648 $payment = usces_get_payments_by_name( $payment_name );
3649 if ( isset( $payment['settlement'] ) && in_array( $payment['settlement'], $this->pay_method ) ) {
3650 $complete = true;
3651 }
3652 return $complete;
3653 }
3654
3655 /**
3656 * 会員データ削除チェック
3657 * usces_filter_delete_member_check
3658 *
3659 * @param boolean $del Removable|unavailable.
3660 * @param int $member_id Member ID.
3661 * @return boolean
3662 */
3663 public function delete_member_check( $del, $member_id ) {
3664 if ( usces_have_member_continue_order( $member_id ) || usces_have_member_regular_order( $member_id ) ) {
3665 $del = false;
3666 }
3667 return $del;
3668 }
3669
3670 /**
3671 * 会員データ削除
3672 * usces_action_pre_delete_memberdata
3673 *
3674 * @param string $member_id Member ID.
3675 */
3676 public function delete_member( $member_id ) {
3677
3678 /* Get Access Token */
3679 $access_token = $this->get_access_token();
3680
3681 $payment_tokens = $this->api_get_payment_tokens( $access_token, $member_id );
3682 if ( ! empty( $payment_tokens['payment_tokens'] ) && is_array( $payment_tokens['payment_tokens'] ) ) {
3683 foreach ( $payment_tokens['payment_tokens'] as $payment_token ) {
3684 $delete_status = $this->api_delete_payment_tokens( $access_token, $payment_token['id'] );
3685 }
3686 }
3687 }
3688
3689 /**
3690 * クレジットカード登録・変更ページ表示
3691 * usces_filter_template_redirect
3692 */
3693 public function member_update_settlement() {
3694 global $usces;
3695
3696 if ( $usces->is_member_page( $_SERVER['REQUEST_URI'] ) ) {
3697 if ( ! usces_is_membersystem_state() || ! usces_is_login() ) {
3698 return;
3699 }
3700 $acting_opts = $this->get_acting_settings();
3701 if ( 'on' !== $acting_opts['card_vault'] ) {
3702 return;
3703 }
3704 $usces_page = ( isset( $_REQUEST['usces_page'] ) ) ? wp_unslash( $_REQUEST['usces_page'] ) : '';
3705 if ( 'member_update_settlement' === $usces_page ) {
3706 add_filter( 'usces_filter_states_form_js', array( $this, 'states_form_js' ) );
3707 $usces->page = 'member_update_settlement';
3708 $this->member_update_settlement_form();
3709 exit();
3710 } elseif ( 'member_register_settlement' === $usces_page ) {
3711 add_filter( 'usces_filter_states_form_js', array( $this, 'states_form_js' ) );
3712 $usces->page = 'member_register_settlement';
3713 $this->member_update_settlement_form();
3714 exit();
3715 }
3716 }
3717 // return false;
3718 }
3719
3720 /**
3721 * クレジットカード登録・変更ページ表示
3722 * usces_filter_states_form_js
3723 *
3724 * @param string $js Scripts.
3725 * @return string
3726 */
3727 public function states_form_js( $js ) {
3728 return '';
3729 }
3730
3731 /**
3732 * クレジットカード登録・変更ページリンク
3733 * usces_action_member_submenu_list
3734 */
3735 public function e_update_settlement() {
3736 global $usces;
3737
3738 $member = $usces->get_member();
3739 $form = $this->update_settlement( '', $member );
3740 echo $form; // no escape.
3741 }
3742
3743 /**
3744 * クレジットカード登録・変更ページリンク
3745 * usces_filter_member_submenu_list
3746 *
3747 * @param string $form Submenu area of the member page.
3748 * @param array $member Member information.
3749 * @return string
3750 */
3751 public function update_settlement( $form, $member ) {
3752 $acting_opts = $this->get_acting_settings();
3753 if ( isset( $acting_opts['card_activate'] ) && 'off' !== $acting_opts['card_activate'] && 'on' === $acting_opts['card_vault'] && ! empty( $member['ID'] ) ) {
3754
3755 /* Get Access Token */
3756 $access_token = $this->get_access_token();
3757
3758 $payment_tokens = $this->api_get_payment_tokens( $access_token, $member['ID'] );
3759 if ( ! empty( $payment_tokens['payment_tokens'] ) ) {
3760 $update_settlement_url = add_query_arg(
3761 array(
3762 'usces_page' => 'member_update_settlement',
3763 're-enter' => 1,
3764 ),
3765 USCES_MEMBER_URL
3766 );
3767 $form .= '<li class="settlement-update gotoedit"><a href="' . $update_settlement_url . '">' . __( 'Change the credit card is here >>', 'usces' ) . '</a></li>';
3768 } else {
3769 $register_settlement_url = add_query_arg(
3770 array(
3771 'usces_page' => 'member_register_settlement',
3772 're-enter' => 1,
3773 ),
3774 USCES_MEMBER_URL
3775 );
3776 $form .= '<li class="settlement-register gotoedit"><a href="' . $register_settlement_url . '">' . __( 'Credit card registration is here >>', 'usces' ) . '</a></li>';
3777 }
3778 }
3779 return $form;
3780 }
3781
3782 /**
3783 * クレジットカード登録・変更ページ
3784 */
3785 public function member_update_settlement_form() {
3786 global $usces;
3787
3788 $member = $usces->get_member();
3789 $acting_opts = $this->get_acting_settings();
3790
3791 $bn_code = self::API_BN_CODE_ACDC;
3792 $script = '';
3793 $done_message = '';
3794 $update_settlement_url = add_query_arg(
3795 array(
3796 'usces_page' => $usces->page,
3797 'settlement' => 1,
3798 're-enter' => 1,
3799 ),
3800 USCES_MEMBER_URL
3801 );
3802
3803 $update_mode = ( isset( $_POST['update_mode'] ) ) ? wp_unslash( $_POST['update_mode'] ) : '';
3804 if ( 'card-register' === $update_mode ) {
3805 check_admin_referer( 'member_update_settlement', 'wc_nonce' );
3806 $verify_action = wel_verify_update_settlement( $member['ID'] );
3807 if ( ! $verify_action ) {
3808 $usces->error_message .= '<p>' . __( 'Update has been locked. Please contact the store administrator.', 'usces' ) . '</p>';
3809 } else {
3810 $api_request_url = ( isset( $acting_opts['api_request_url'] ) ) ? $acting_opts['api_request_url'] : '';
3811 $tracking_id = ( isset( $_POST['paypal_tracking_id'] ) ) ? wp_unslash( $_POST['paypal_tracking_id'] ) : '';
3812 $resource_id = ( isset( $_POST['paypal_resource_id'] ) ) ? wp_unslash( $_POST['paypal_resource_id'] ) : '';
3813
3814 /* Get Access Token */
3815 $access_token = $this->get_access_token();
3816
3817 $correlation_id = '';
3818 $payment_source = array();
3819
3820 /* Authorize */
3821 $response_data = $this->api_authorize_order( $access_token, $bn_code, $tracking_id, $resource_id, $correlation_id, $payment_source );
3822 if ( isset( $response_data['id'] ) && isset( $response_data['status'] ) && 'COMPLETED' === $response_data['status'] ) {
3823 $done_message = __( 'Successfully registered.', 'usces' );
3824 $purchase_units = $response_data['purchase_units'][0];
3825 if ( isset( $purchase_units['payments']['authorizations'] ) ) {
3826 $payments = $purchase_units['payments']['authorizations'][0];
3827 if ( ! empty( $payments['id'] ) ) {
3828 $void_status = $this->api_authorize_void( $access_token, $bn_code, $tracking_id, $payments['id'] );
3829 }
3830 }
3831 } else {
3832 $done_message = __( 'Registration failed.', 'usces' );
3833 }
3834 }
3835 } elseif ( 'card-update' === $update_mode ) {
3836 check_admin_referer( 'member_update_settlement', 'wc_nonce' );
3837 $verify_action = wel_verify_update_settlement( $member['ID'] );
3838 if ( ! $verify_action ) {
3839 $usces->error_message .= '<p>' . __( 'Update has been locked. Please contact the store administrator.', 'usces' ) . '</p>';
3840 } else {
3841 $api_request_url = ( isset( $acting_opts['api_request_url'] ) ) ? $acting_opts['api_request_url'] : '';
3842 $tracking_id = ( isset( $_POST['paypal_tracking_id'] ) ) ? wp_unslash( $_POST['paypal_tracking_id'] ) : '';
3843 $resource_id = ( isset( $_POST['paypal_resource_id'] ) ) ? wp_unslash( $_POST['paypal_resource_id'] ) : '';
3844 $payment_token_id = ( isset( $_POST['paypal_payment_token_id'] ) ) ? wp_unslash( $_POST['paypal_payment_token_id'] ) : '';
3845 if ( ! empty( $payment_token_id ) ) {
3846
3847 /* Get Access Token */
3848 $access_token = $this->get_access_token();
3849
3850 $delete_status = $this->api_delete_payment_tokens( $access_token, $payment_token_id );
3851 if ( 'COMPLETED' === $delete_status ) {
3852 $correlation_id = '';
3853 $payment_source = array();
3854
3855 /* Authorize */
3856 $response_data = $this->api_authorize_order( $access_token, $bn_code, $tracking_id, $resource_id, $correlation_id, $payment_source );
3857 if ( isset( $response_data['id'] ) && isset( $response_data['status'] ) && 'COMPLETED' === $response_data['status'] ) {
3858 $this->send_update_settlement_mail();
3859 $done_message = __( 'Successfully updated.', 'usces' );
3860 $purchase_units = $response_data['purchase_units'][0];
3861 if ( isset( $purchase_units['payments']['authorizations'] ) ) {
3862 $payments = $purchase_units['payments']['authorizations'][0];
3863 if ( ! empty( $payments['id'] ) ) {
3864 $void_status = $this->api_authorize_void( $access_token, $bn_code, $tracking_id, $payments['id'] );
3865 }
3866 }
3867 } else {
3868 $done_message = __( 'Update failed.', 'usces' );
3869 }
3870 } else {
3871 $done_message = __( 'Update failed.', 'usces' );
3872 }
3873 } else {
3874 $done_message = __( 'Update failed.', 'usces' );
3875 }
3876 }
3877 } elseif ( 'card-delete' === $update_mode ) {
3878 check_admin_referer( 'member_update_settlement', 'wc_nonce' );
3879 $payment_token_id = ( isset( $_POST['paypal_payment_token_id'] ) ) ? wp_unslash( $_POST['paypal_payment_token_id'] ) : '';
3880 if ( ! empty( $payment_token_id ) ) {
3881
3882 /* Get Access Token */
3883 $access_token = $this->get_access_token();
3884
3885 $delete_status = $this->api_delete_payment_tokens( $access_token, $payment_token_id );
3886 if ( 'COMPLETED' === $delete_status ) {
3887 $done_message = __( 'Successfully deleted.', 'usces' );
3888 } else {
3889 $done_message = __( 'Deletion failed.', 'usces' );
3890 }
3891 } else {
3892 $done_message = __( 'Deletion failed.', 'usces' );
3893 }
3894 }
3895
3896 if ( '' !== $done_message ) {
3897 $script .= '
3898 <script type="text/javascript">
3899 jQuery.event.add( window, "load", function() {
3900 alert( "' . $done_message . '" );
3901 });
3902 </script>';
3903 }
3904 $error_message = apply_filters( 'usces_filter_member_update_settlement_error_message', $usces->error_message );
3905
3906 $payment_token_id = '';
3907 $payment_token_last_digits = '';
3908 $payment_token_brand = '';
3909
3910 /* Get Access Token */
3911 $access_token = $this->get_access_token();
3912
3913 $payment_tokens = $this->api_get_payment_tokens( $access_token, $member['ID'] );
3914 if ( ! empty( $payment_tokens['payment_tokens'] ) && is_array( $payment_tokens['payment_tokens'] ) ) {
3915 $tokens_count = count( $payment_tokens['payment_tokens'] );
3916 if ( 1 === $tokens_count ) {
3917 $payment_token = current( $payment_tokens['payment_tokens'] );
3918 $payment_token_id = $payment_token['id'];
3919 $payment_token_last_digits = $payment_token['source']['card']['last_digits'];
3920 $payment_token_brand = $payment_token['source']['card']['brand'];
3921 $register = false;
3922 } else {
3923 foreach ( $payment_tokens['payment_tokens'] as $payment_token ) {
3924 $delete_status = $this->api_delete_payment_tokens( $access_token, $payment_token['id'] );
3925 }
3926 $register = true;
3927 }
3928 } else {
3929 $register = true;
3930 }
3931
3932 $tracking_id = usces_acting_key();
3933
3934 ob_start();
3935 get_header();
3936 if ( '' !== $script ) {
3937 echo $script; // no escape due to script.
3938 }
3939 ?>
3940 <div id="content" class="two-column">
3941 <div class="catbox">
3942 <?php
3943 if ( have_posts() ) :
3944 usces_remove_filter();
3945 ?>
3946 <div class="post" id="wc_member_update_settlement">
3947 <?php if ( $register ) : ?>
3948 <h1 class="member_page_title"><?php esc_html_e( 'Credit card registration', 'usces' ); ?></h1>
3949 <?php else : ?>
3950 <h1 class="member_page_title"><?php esc_html_e( 'Credit card update', 'usces' ); ?></h1>
3951 <?php endif; ?>
3952 <div class="entry">
3953 <div id="memberpages">
3954 <div class="whitebox">
3955 <div id="memberinfo">
3956 <div class="header_explanation"></div>
3957 <div class="error_message"><?php wel_esc_script_e( $error_message ); ?></div>
3958 <form id="member-card-update" name="member_card_update" action="<?php echo esc_url( $update_settlement_url ); ?>" method="post" onKeyDown="if(event.keyCode == 13) {return false;}">
3959 <table class="customer_form" id="paypal_card_form">
3960 <tbody><tr><td>
3961 <?php if ( ! $register ) : ?>
3962 <div class="card_container">
3963 <p><?php esc_html_e( 'Your registered credit card', 'usces' ); ?></p>
3964 <p><?php echo esc_html( $payment_token_brand ); ?> <?php esc_html_e( 'Lower 4 digits', 'usces' ); ?>:<?php echo esc_html( $payment_token_last_digits ); ?></p>
3965 </div>
3966 </td></tr><tr><td>
3967 <?php endif; ?>
3968 <div class="card_container">
3969 <div>
3970 <label for="card-number"><?php esc_html_e( 'Card Number', 'usces' ); ?></label>
3971 <div id="card-number" class="card_field"></div>
3972 </div>
3973 <div>
3974 <label for="expiration-date"><?php esc_html_e( 'Expiration Date', 'usces' ); ?></label>
3975 <div id="expiration-date" class="card_field"></div>
3976 </div>
3977 <div>
3978 <label for="cvv"><?php esc_html_e( 'CVV', 'usces' ); ?></label>
3979 <div id="cvv" class="card_field"></div>
3980 </div>
3981 </div>
3982 </td></tr></tbody>
3983 </table>
3984 <input type="hidden" id="paypal_tracking_id" name="paypal_tracking_id" value="<?php echo esc_attr( $tracking_id ); ?>" />
3985 <input type="hidden" id="paypal_resource_id" name="paypal_resource_id" value="" />
3986 <input type="hidden" id="paypal_payment_token_id " name="paypal_payment_token_id" value="<?php echo esc_attr( $payment_token_id ); ?>" />
3987 <input type="hidden" id="paypal_f" name="paypal_f" value="" />
3988 <div class="send">
3989 <?php if ( $register ) : ?>
3990 <input type="submit" id="card-update" class="card-update" value="<?php esc_attr_e( 'Register', 'usces' ); ?>" />
3991 <input type="hidden" id="update-mode" name="update_mode" value="card-register" />
3992 <?php else : ?>
3993 <input type="submit" id="card-update" class="card-update" value="<?php esc_attr_e( 'Update', 'usces' ); ?>" />
3994 <input type="submit" id="card-delete" class="card-update" value="<?php esc_attr_e( 'Delete', 'usces' ); ?>" />
3995 <input type="hidden" id="update-mode" name="update_mode" value="card-update" />
3996 <?php endif; ?>
3997 <input type="button" name="back" value="<?php esc_attr_e( 'Back to the member page.', 'usces' ); ?>" onclick="location.href='<?php echo esc_url( USCES_MEMBER_URL ); ?>'" />
3998 <input type="button" name="top" value="<?php esc_attr_e( 'Back to the top page.', 'usces' ); ?>" onclick="location.href='<?php echo esc_url( home_url() ); ?>'" />
3999 </div>
4000 <?php wp_nonce_field( 'member_update_settlement', 'wc_nonce' ); ?>
4001 </form>
4002 <div class="footer_explanation"></div>
4003 </div><!-- end of memberinfo -->
4004 </div><!-- end of whitebox -->
4005 </div><!-- end of memberpages -->
4006 </div><!-- end of entry -->
4007 </div><!-- end of post -->
4008 <?php else : ?>
4009 <p><?php esc_html_e( 'Sorry, no posts matched your criteria.', 'usces' ); ?></p>
4010 <?php endif; ?>
4011 </div><!-- end of catbox -->
4012 </div><!-- end of content -->
4013 <?php
4014 $sidebar = apply_filters( 'usces_filter_member_update_settlement_page_sidebar', 'cartmember' );
4015 if ( ! empty( $sidebar ) ) {
4016 get_sidebar( $sidebar );
4017 }
4018 get_footer();
4019 $contents = ob_get_contents();
4020 ob_end_clean();
4021 echo $contents; // no escape.
4022 }
4023
4024 /**
4025 * クレジットカード変更メール
4026 */
4027 public function send_update_settlement_mail() {
4028 global $usces;
4029
4030 $member = $usces->get_member();
4031
4032 $subject = apply_filters( 'usces_filter_send_update_settlement_mail_subject', __( 'Confirmation of credit card update', 'usces' ), $member );
4033 $mail_header = __( 'Your credit card information has been updated on the membership page.', 'usces' ) . "\r\n\r\n";
4034 $mail_footer = get_option( 'blogname' ) . "\r\n";
4035 $name = usces_localized_name( $member['name1'], $member['name2'], 'return' );
4036
4037 $message = '--------------------------------' . "\r\n";
4038 $message .= __( 'Member ID', 'usces' ) . ' : ' . $member['ID'] . "\r\n";
4039 $message .= __( 'Name', 'usces' ) . ' : ' . sprintf( _x( '%s', 'honorific', 'usces' ), $name ) . "\r\n";
4040 $message .= __( 'e-mail adress', 'usces' ) . ' : ' . $member['mailaddress1'] . "\r\n";
4041 $message .= '--------------------------------' . "\r\n\r\n";
4042 $message .= __( 'If you have not requested this email, sorry to trouble you, but please contact us.', 'usces' ) . "\r\n\r\n";
4043 $message = apply_filters( 'usces_filter_send_update_settlement_mail_message', $message, $member );
4044 $message = apply_filters( 'usces_filter_send_update_settlement_mail_message_head', $mail_header, $member ) . $message . apply_filters( 'usces_filter_send_update_settlement_mail_message_foot', $mail_footer, $member ) . "\r\n";
4045 $message = sprintf( __( 'Dear %s', 'usces' ), $name ) . "\r\n\r\n" . $message;
4046
4047 $send_para = array(
4048 'to_name' => sprintf( _x( '%s', 'honorific', 'usces' ), $name ),
4049 'to_address' => $member['mailaddress1'],
4050 'from_name' => get_option( 'blogname' ),
4051 'from_address' => $usces->options['sender_mail'],
4052 'reply_name' => get_option( 'blogname' ),
4053 'reply_to' => usces_get_first_order_mail(),
4054 'return_path' => $usces->options['sender_mail'],
4055 'subject' => $subject,
4056 'message' => do_shortcode( $message ),
4057 );
4058 usces_send_mail( $send_para );
4059
4060 $admin_message = $mail_header;
4061 $admin_message .= '--------------------------------' . "\r\n";
4062 $admin_message .= __( 'Member ID', 'usces' ) . ' : ' . $member['ID'] . "\r\n";
4063 $admin_message .= __( 'Name', 'usces' ) . ' : ' . sprintf( _x( '%s', 'honorific', 'usces' ), $name ) . "\r\n";
4064 $admin_message .= __( 'e-mail adress', 'usces' ) . ' : ' . $member['mailaddress1'] . "\r\n";
4065 $admin_message .= '--------------------------------' . "\r\n\r\n";
4066 $admin_message .=
4067 "\r\n----------------------------------------------------\r\n" .
4068 'REMOTE_ADDR : ' . wp_unslash( $_SERVER['REMOTE_ADDR'] ) .
4069 "\r\n----------------------------------------------------\r\n";
4070
4071 $admin_para = array(
4072 'to_name' => apply_filters( 'usces_filter_bccmail_to_admin_name', 'Shop Admin' ),
4073 'to_address' => $usces->options['order_mail'],
4074 'from_name' => apply_filters( 'usces_filter_bccmail_from_admin_name', 'Welcart Auto BCC' ),
4075 'from_address' => $usces->options['sender_mail'],
4076 'reply_name' => get_option( 'blogname' ),
4077 'reply_to' => usces_get_first_order_mail(),
4078 'return_path' => $usces->options['sender_mail'],
4079 'subject' => $subject . '( ' . sprintf( _x( '%s', 'honorific', 'usces' ), $name ) . ' )',
4080 'message' => do_shortcode( $admin_message ),
4081 );
4082 usces_send_mail( $admin_para );
4083 }
4084
4085 /**
4086 * 管理画面決済処理
4087 * usces_action_admin_ajax
4088 */
4089 public function admin_ajax() {
4090 global $usces;
4091
4092 $mode = wp_unslash( $_POST['mode'] );
4093 $data = array();
4094
4095 switch ( $mode ) {
4096 /* Upfront Onboarding */
4097 case 'upfront_onboarding':
4098 check_admin_referer( 'admin_settlement', 'wc_nonce' );
4099
4100 /* Get Access Token */
4101 $cp_client_id = 'AYbgr2xC4qFsOLqWriUhmUrTLVKQZl_LsO4lLRQ1hkWYaqalZ-DY9n6L4edGt-T0Gnwem9_jKjSgbKc8';
4102 $cp_secret = 'EE_9-oMggvZjksDF2zpdmAafq_v7LZutPWgE1W6yu_KKU4sbeFIswcVjoDUeyA3Szywjs3xQ3qRdTDoj';
4103 $api_request_url = self::API_URL;
4104
4105 $headers = array(
4106 'Content-Type: application/x-www-form-urlencoded;application/json',
4107 'Accept: application/json',
4108 );
4109 $ch = curl_init();
4110 curl_setopt( $ch, CURLOPT_URL, $api_request_url . '/v1/oauth2/token' );
4111 curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
4112 curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
4113 curl_setopt( $ch, CURLOPT_USERPWD, $cp_client_id . ':' . $cp_secret );
4114 curl_setopt( $ch, CURLOPT_POST, true );
4115 curl_setopt( $ch, CURLOPT_POSTFIELDS, 'grant_type=client_credentials' );
4116
4117 $result = curl_exec( $ch );
4118 if ( ! $result ) {
4119 $error = curl_error( $ch );
4120 wp_send_json( json_decode( $error, true ) );
4121 }
4122 unset( $ch );
4123 $access_token = json_decode( $result, true );
4124
4125 $seller_nonce = $this->get_seller_nonce();
4126 $features = array( 'PAYMENT', 'REFUND', 'VAULT' );
4127 $operations = array();
4128 $operations['operation'] = 'API_INTEGRATION';
4129 $operations['api_integration_preference']['rest_api_integration']['integration_method'] = 'PAYPAL';
4130 $operations['api_integration_preference']['rest_api_integration']['integration_type'] = 'FIRST_PARTY';
4131 $operations['api_integration_preference']['rest_api_integration']['first_party_details']['features'] = $features;
4132 $operations['api_integration_preference']['rest_api_integration']['first_party_details']['seller_nonce'] = $seller_nonce;
4133 $products = array( 'EXPRESS_CHECKOUT' );
4134 $legal_consents = array();
4135 $legal_consents['type'] = 'SHARE_DATA_CONSENT';
4136 $legal_consents['granted'] = true;
4137
4138 $body = array();
4139 if ( 'ja' === usces_get_local_language() ) {
4140 $body['preferred_language_code'] = 'ja-JP';
4141 $addresses = array( 'country_code' => 'JP' );
4142 $body['business_entity'] = array( $addresses );
4143 }
4144 $body['operations'] = array( $operations );
4145 $body['products'] = $products;
4146 $body['legal_consents'] = array( $legal_consents );
4147
4148 $params = array(
4149 'method' => 'POST',
4150 'headers' => array(
4151 'Content-Type' => 'application/json;charset=utf-8',
4152 'Authorization' => 'Bearer ' . $access_token['access_token'],
4153 ),
4154 'timeout' => self::API_TIMEOUT,
4155 'body' => wp_json_encode( $body ),
4156 );
4157 $response = wp_remote_post( $api_request_url . '/v2/customer/partner-referrals', $params );
4158 $response_data = json_decode( wp_remote_retrieve_body( $response ), true );
4159
4160 $action_url = '';
4161 $res = '';
4162 if ( isset( $response_data['links'] ) ) {
4163 foreach ( (array) $response_data['links'] as $links ) {
4164 if ( isset( $links['rel'] ) && 'action_url' === $links['rel'] && isset( $links['href'] ) ) {
4165 $action_url = $links['href'];
4166 }
4167 }
4168 }
4169 if ( '' === $action_url && isset( $response_data['name'] ) ) {
4170 $res = $response_data['name'];
4171 }
4172 wp_send_json(
4173 array(
4174 'action_url' => $action_url,
4175 'res' => $res,
4176 'seller_nonce' => $seller_nonce,
4177 )
4178 );
4179 break;
4180
4181 /* 参�
4182 � */
4183 case 'get_paypal_cp':
4184 check_admin_referer( 'order_edit', 'wc_nonce' );
4185 $order_id = ( isset( $_POST['order_id'] ) ) ? wp_unslash( $_POST['order_id'] ) : '';
4186 $order_num = ( isset( $_POST['order_num'] ) ) ? wp_unslash( $_POST['order_num'] ) : 1;
4187 $tracking_id = ( isset( $_POST['tracking_id'] ) ) ? wp_unslash( $_POST['tracking_id'] ) : ''; /* rand|reference_id */
4188 $member_id = ( isset( $_POST['member_id'] ) ) ? wp_unslash( $_POST['member_id'] ) : 0;
4189 $con_id = ( defined( 'WCEX_DLSELLER' ) && isset( $_POST['con_id'] ) ) ? wp_unslash( $_POST['con_id'] ) : 0;
4190 $reg_id = ( defined( 'WCEX_AUTO_DELIVERY' ) && isset( $_POST['reg_id'] ) ) ? wp_unslash( $_POST['reg_id'] ) : 0;
4191 $acting = ( isset( $_POST['acting'] ) ) ? wp_unslash( $_POST['acting'] ) : 'paypal_cp';
4192 if ( empty( $order_id ) || empty( $tracking_id ) ) {
4193 wp_send_json( $data );
4194 break;
4195 }
4196
4197 if ( 1 === (int) $order_num ) {
4198 $resource_id = $usces->get_order_meta_value( 'resource_id', $order_id );
4199 } else {
4200 $resource_id = dlseller_get_continuation_meta_value( 'resource_' . $tracking_id, $con_id );
4201 }
4202
4203 $bn_code = ( 'paypal_card' === $acting ) ? self::API_BN_CODE_ACDC : self::API_BN_CODE_PCP;
4204 $result = '';
4205
4206 /* Get Access Token */
4207 $access_token = $this->get_access_token();
4208
4209 /* Get Order */
4210 $response_data = $this->api_get_order( $access_token, $bn_code, $tracking_id, $resource_id );
4211 if ( $response_data ) {
4212 if ( isset( $response_data['name'] ) && isset( $response_data['message'] ) && isset( $response_data['debug_id'] ) ) {
4213 // $result .= '<div class="paypal-settlement-admin paypal-error">' . $response_data['name'] . '</div>';
4214 $acting_status = $this->get_acting_status( $order_id, $tracking_id );
4215 $class = ' paypal-' . strtolower( $acting_status );
4216 $result .= '<div class="paypal-settlement-admin' . $class . '">' . __( $acting_status, 'usces' ) . '</div>';
4217 $log = $this->get_entry_log( $tracking_id );
4218 if ( isset( $log['entry'] ) && isset( $log['cart'] ) ) {
4219 $amount = $this->get_latest_amount( $order_id, $tracking_id );
4220 $result .= '<table class="paypal-settlement-admin-table">
4221 <tr><th>' . __( 'Transaction amount', 'usces' ) . '</th>
4222 <td><input type="tel" class="settlement-amount" value="' . usces_crform( $amount, false, false, 'return', true ) . '" readonly />' . __( usces_crcode( 'return' ), 'usces' ) . '</td>
4223 </tr>';
4224 if ( empty( $amount ) ) {
4225 if ( defined( 'WCEX_DLSELLER' ) && ! empty( $con_id ) ) {
4226 $amount = $this->get_continuation_amount( $con_id );
4227 } elseif ( defined( 'WCEX_AUTO_DELIVERY' ) && ! empty( $reg_id ) ) {
4228 $amount = $this->get_order_amount( $order_id );
4229 }
4230 }
4231 $result .= '
4232 <tr><th>' . __( 'Settlement amount', 'usces' ) . '</th>
4233 <td><input type="tel" class="settlement-amount amount" id="amount_resettlement" value="' . usces_crform( $amount, false, false, 'return', false ) . '" />' . __( usces_crcode( 'return' ), 'usces' ) . '</td>
4234 </tr>
4235 </table>';
4236 if ( 'paypal_cp' === $acting ) {
4237 $result .= '<div class="paypal-settlement-admin-button">
4238 <input id="re-authorize-settlement" type="button" class="button" value="' . __( 'AUTHORIZE', 'usces' ) . '" />
4239 <input id="re-capture-settlement" type="button" class="button" value="' . __( 'CAPTURE', 'usces' ) . '" />
4240 </div>';
4241 }
4242 }
4243 } else {
4244 $pending = false;
4245 $amount = $this->get_latest_amount( $order_id, $tracking_id );
4246
4247 $latest_log = $this->get_acting_latest_log( $order_id, $tracking_id, 'ALL' );
4248 if ( isset( $latest_log['result'] ) && 'COMPLETED' !== $latest_log['result'] && 'PENDING' !== $latest_log['result'] ) {
4249 $acting_status = $this->get_acting_status( $order_id, $tracking_id );
4250 $class = ' paypal-' . strtolower( $acting_status );
4251 $result .= '<div class="paypal-settlement-admin' . $class . '">' . __( $acting_status, 'usces' ) . '</div>';
4252 $log = $this->get_entry_log( $tracking_id );
4253 if ( isset( $log['entry'] ) && isset( $log['cart'] ) ) {
4254 $result .= '<table class="paypal-settlement-admin-table">
4255 <tr><th>' . __( 'Transaction amount', 'usces' ) . '</th>
4256 <td><input type="tel" class="settlement-amount" value="' . usces_crform( $amount, false, false, 'return', true ) . '" readonly />' . __( usces_crcode( 'return' ), 'usces' ) . '</td>
4257 </tr>';
4258 if ( empty( $amount ) ) {
4259 if ( defined( 'WCEX_DLSELLER' ) && ! empty( $con_id ) ) {
4260 $amount = $this->get_continuation_amount( $con_id );
4261 } elseif ( defined( 'WCEX_AUTO_DELIVERY' ) && ! empty( $reg_id ) ) {
4262 $amount = $this->get_order_amount( $order_id );
4263 }
4264 }
4265 $result .= '
4266 <tr><th>' . __( 'Settlement amount', 'usces' ) . '</th>
4267 <td><input type="tel" class="settlement-amount amount" id="amount_resettlement" value="' . usces_crform( $amount, false, false, 'return', false ) . '" />' . __( usces_crcode( 'return' ), 'usces' ) . '</td>
4268 </tr>
4269 </table>';
4270 if ( 'paypal_cp' === $acting ) {
4271 $result .= '<div class="paypal-settlement-admin-button">
4272 <input id="re-authorize-settlement" type="button" class="button" value="' . __( 'AUTHORIZE', 'usces' ) . '" />
4273 <input id="re-capture-settlement" type="button" class="button" value="' . __( 'CAPTURE', 'usces' ) . '" />
4274 </div>';
4275 }
4276 }
4277 $result .= $this->settlement_history( $order_id, $tracking_id );
4278 $data['result'] = $result;
4279 wp_send_json( $data );
4280 break;
4281 }
4282
4283 if ( isset( $response_data['intent'] ) && isset( $response_data['purchase_units'] ) ) {
4284 $acting_status = $response_data['intent'];
4285 $purchase_units = $response_data['purchase_units'][0];
4286 if ( isset( $purchase_units['payments']['captures'] ) ) {
4287 $payments = $purchase_units['payments']['captures'][0];
4288 $payments_status = ( isset( $payments['status'] ) ) ? $payments['status'] : '';
4289 if ( 'REFUNDED' === $payments_status ) {
4290 $acting_status = 'REFUNDED';
4291 } elseif ( 'COMPLETED' === $payments_status || 'PARTIALLY_REFUNDED' === $payments_status || 'PENDING' === $payments_status ) {
4292 $acting_status = 'CAPTURE';
4293 if ( 'PENDING' === $payments_status ) {
4294 $pending = true;
4295 }
4296 }
4297 } elseif ( isset( $purchase_units['payments']['authorizations'] ) ) {
4298 $payments = $purchase_units['payments']['authorizations'][0];
4299 $payments_status = ( isset( $payments['status'] ) ) ? $payments['status'] : '';
4300 if ( 'VOIDED' === $payments_status ) {
4301 $acting_status = 'VOIDED';
4302 $amount = 0;
4303 } elseif ( 'CREATED' === $payments_status || 'PENDING' === $payments_status ) {
4304 $acting_status = 'AUTHORIZE';
4305 if ( 'PENDING' === $payments_status ) {
4306 $pending = true;
4307 }
4308 }
4309 } else {
4310 $acting_status = $this->get_acting_status( $order_id, $tracking_id );
4311 }
4312 } else {
4313 $acting_status = $this->get_acting_status( $order_id, $tracking_id );
4314 }
4315 $class = ' paypal-' . strtolower( $acting_status );
4316 $acting_pending = ( $pending ) ? ' ' . __( '[ PENDING ]', 'usces' ) : '';
4317 $result .= '<div class="paypal-settlement-admin' . $class . '">' . __( $acting_status, 'usces' ) . $acting_pending . '</div>';
4318 if ( 0 < $amount ) {
4319 if ( 'AUTHORIZE' === $acting_status ) {
4320 $result .= '<table class="paypal-settlement-admin-table">
4321 <tr><th>' . __( 'Transaction amount', 'usces' ) . '</th>
4322 <td><input type="tel" class="settlement-amount" value="' . usces_crform( $amount, false, false, 'return', true ) . '" readonly />' . __( usces_crcode( 'return' ), 'usces' ) . '<input type="hidden" id="amount_original" value="' . usces_crform( $amount, false, false, 'return', false ) . '" /></td>
4323 </tr>
4324 <tr><th>' . __( 'Refund amount', 'usces' ) . '</th>
4325 <td><input type="tel" class="settlement-amount amount" id="amount_refund" value="" />' . __( usces_crcode( 'return' ), 'usces' ) . '</td>
4326 </tr>
4327 </table>';
4328 $result .= '<div class="paypal-settlement-admin-button">
4329 <input id="capture-settlement" type="button" class="button" value="' . __( 'CAPTURE', 'usces' ) . '" />
4330 <input id="void-settlement" type="button" class="button" value="' . __( 'VOID', 'usces' ) . '" />
4331 </div>';
4332 } elseif ( 'CAPTURE' === $acting_status ) {
4333 $result .= '<table class="paypal-settlement-admin-table">
4334 <tr><th>' . __( 'Transaction amount', 'usces' ) . '</th>
4335 <td><input type="tel" class="settlement-amount" value="' . usces_crform( $amount, false, false, 'return', true ) . '" readonly />' . __( usces_crcode( 'return' ), 'usces' ) . '<input type="hidden" id="amount_original" value="' . usces_crform( $amount, false, false, 'return', false ) . '" /></td>
4336 </tr>
4337 <tr><th>' . __( 'Refund amount', 'usces' ) . '</th>
4338 <td><input type="tel" class="settlement-amount amount" id="amount_refund" value="" />' . __( usces_crcode( 'return' ), 'usces' ) . '</td>
4339 </tr>
4340 </table>';
4341 $result .= '<div class="paypal-settlement-admin-button">
4342 <input id="refund-settlement" type="button" class="button" value="' . __( 'REFUND', 'usces' ) . '" />
4343 </div>';
4344 }
4345 } else {
4346 $result .= '<table class="paypal-settlement-admin-table">
4347 <tr><th>' . __( 'Transaction amount', 'usces' ) . '</th>
4348 <td><input type="tel" class="settlement-amount" value="' . usces_crform( $amount, false, false, 'return', true ) . '" readonly />' . __( usces_crcode( 'return' ), 'usces' ) . '</td>
4349 </tr></table>';
4350 }
4351 }
4352 } else {
4353 $acting_status = $this->get_acting_status( $order_id, $tracking_id );
4354 $class = ' paypal-' . strtolower( $acting_status );
4355 $result .= '<div class="paypal-settlement-admin' . $class . '">' . __( $acting_status, 'usces' ) . '</div>';
4356 $log = $this->get_entry_log( $tracking_id );
4357 if ( isset( $log['entry'] ) && isset( $log['cart'] ) ) {
4358 $amount = $this->get_latest_amount( $order_id, $tracking_id );
4359 $result .= '<table class="paypal-settlement-admin-table">
4360 <tr><th>' . __( 'Transaction amount', 'usces' ) . '</th>
4361 <td><input type="tel" class="settlement-amount" value="' . usces_crform( $amount, false, false, 'return', true ) . '" readonly />' . __( usces_crcode( 'return' ), 'usces' ) . '</td>
4362 </tr>';
4363 if ( empty( $amount ) ) {
4364 if ( defined( 'WCEX_DLSELLER' ) && ! empty( $con_id ) ) {
4365 $amount = $this->get_continuation_amount( $con_id );
4366 } elseif ( defined( 'WCEX_AUTO_DELIVERY' ) && ! empty( $reg_id ) ) {
4367 $amount = $this->get_order_amount( $order_id );
4368 }
4369 }
4370 $result .= '
4371 <tr><th>' . __( 'Settlement amount', 'usces' ) . '</th>
4372 <td><input type="tel" class="settlement-amount amount" id="amount_resettlement" value="' . usces_crform( $amount, false, false, 'return', false ) . '" />' . __( usces_crcode( 'return' ), 'usces' ) . '</td>
4373 </tr>
4374 </table>';
4375 if ( 'paypal_cp' === $acting ) {
4376 $result .= '<div class="paypal-settlement-admin-button">
4377 <input id="re-authorize-settlement" type="button" class="button" value="' . __( 'AUTHORIZE', 'usces' ) . '" />
4378 <input id="re-capture-settlement" type="button" class="button" value="' . __( 'CAPTURE', 'usces' ) . '" />
4379 </div>';
4380 }
4381 }
4382 }
4383 $result .= $this->settlement_history( $order_id, $tracking_id );
4384 $data['result'] = $result;
4385 wp_send_json( $data );
4386 break;
4387
4388 /* CAPTURE */
4389 case 'capture_paypal_cp':
4390 check_admin_referer( 'order_edit', 'wc_nonce' );
4391 $order_id = ( isset( $_POST['order_id'] ) ) ? wp_unslash( $_POST['order_id'] ) : '';
4392 $order_num = ( isset( $_POST['order_num'] ) ) ? wp_unslash( $_POST['order_num'] ) : 1;
4393 $tracking_id = ( isset( $_POST['tracking_id'] ) ) ? wp_unslash( $_POST['tracking_id'] ) : '';
4394 $capture_amount = ( isset( $_POST['amount'] ) ) ? wp_unslash( $_POST['amount'] ) : 0;
4395 $con_id = ( defined( 'WCEX_DLSELLER' ) && isset( $_POST['con_id'] ) ) ? wp_unslash( $_POST['con_id'] ) : 0;
4396 $acting = ( isset( $_POST['acting'] ) ) ? wp_unslash( $_POST['acting'] ) : 'paypal_cp';
4397 if ( empty( $order_id ) || empty( $tracking_id ) ) {
4398 wp_send_json( $data );
4399 break;
4400 }
4401
4402 $acting_status = 'CAPTURE';
4403 $acting_opts = $this->get_acting_settings();
4404 $api_request_url = ( isset( $acting_opts['api_request_url'] ) ) ? $acting_opts['api_request_url'] : '';
4405 $bn_code = ( 'paypal_card' === $acting ) ? self::API_BN_CODE_ACDC : self::API_BN_CODE_PCP;
4406 if ( 1 === (int) $order_num ) {
4407 $trans_id = $usces->get_order_meta_value( 'trans_id', $order_id );
4408 } else {
4409 $trans_id = dlseller_get_continuation_meta_value( 'trans_' . $tracking_id, $con_id );
4410 }
4411 $amount = $this->get_latest_amount( $order_id, $tracking_id );
4412 $result = '';
4413
4414 /* Get Access Token */
4415 $access_token = $this->get_access_token();
4416
4417 $params = array(
4418 'method' => 'POST',
4419 'headers' => array(
4420 'Content-Type' => 'application/json;charset=utf-8',
4421 'Authorization' => 'Bearer ' . $access_token['access_token'],
4422 'PayPal-Partner-Attribution-Id' => $bn_code,
4423 'PayPal-Request-Id' => $tracking_id,
4424 ),
4425 'timeout' => self::API_TIMEOUT,
4426 );
4427 if ( $amount > $capture_amount ) {
4428 $amount = $capture_amount;
4429 $body = array();
4430 $body['amount']['currency_code'] = $usces->get_currency_code();
4431 $body['amount']['value'] = $amount;
4432 $body['final_capture'] = true;
4433 $params['body'] = wp_json_encode( $body );
4434 }
4435 $response = wp_remote_get( $api_request_url . '/v2/payments/authorizations/' . $trans_id . '/capture', $params );
4436 $response_data = json_decode( wp_remote_retrieve_body( $response ), true );
4437 if ( isset( $response['response']['code'] ) && '201' === (string) $response['response']['code'] ) {
4438 $status = 'COMPLETED';
4439 } elseif ( isset( $response['response']['code'] ) && '200' === (string) $response['response']['code'] ) {
4440 $status = 'RETRY';
4441 } else {
4442 if ( isset( $response_data['status'] ) ) {
4443 $status = $response_data['status'];
4444 } elseif ( isset( $response_data['name'] ) ) {
4445 $status = $response_data['name'];
4446 } else {
4447 $status = 'CAPTURE ERROR';
4448 }
4449 }
4450 $response_data = apply_filters( 'usces_filter_paypal_cp_capture_log', $response_data, $order_id, $status );
4451 if ( 'COMPLETED' === $status ) {
4452 $this->save_acting_log( $response_data, $acting, $acting_status, $status, $capture_amount, $order_id, $tracking_id );
4453 if ( ! empty( $response_data['id'] ) ) {
4454 if ( 1 === (int) $order_num ) {
4455 $usces->set_order_meta_value( 'wc_trans_id', $response_data['id'], $order_id ); /* 決済ID */
4456 $usces->set_order_meta_value( 'trans_id', $response_data['id'], $order_id ); /* 取引ID */
4457 } else {
4458 dlseller_set_continuation_meta_value( 'trans_' . $tracking_id, $response_data['id'], $con_id ); /* 取引ID */
4459 }
4460 }
4461 } else {
4462 $this->save_acting_log( $response_data, $acting, 'ERROR', $status, 0, $order_id, $tracking_id );
4463 $acting_status = 'AUTHORIZE';
4464 }
4465 $class = ' paypal-' . strtolower( $acting_status );
4466 $result .= '<div class="paypal-settlement-admin' . $class . '">' . __( $acting_status, 'usces' ) . '</div>';
4467 if ( 'AUTHORIZE' === $acting_status ) {
4468 $result .= '<table class="paypal-settlement-admin-table">
4469 <tr><th>' . __( 'Transaction amount', 'usces' ) . '</th>
4470 <td><input type="tel" class="settlement-amount" value="' . usces_crform( $amount, false, false, 'return', true ) . '" readonly />' . __( usces_crcode( 'return' ), 'usces' ) . '<input type="hidden" id="amount_original" value="' . usces_crform( $amount, false, false, 'return', false ) . '" /></td>
4471 </tr>
4472 <tr><th>' . __( 'Refund amount', 'usces' ) . '</th>
4473 <td><input type="tel" class="settlement-amount amount" id="amount_refund" value="" />' . __( usces_crcode( 'return' ), 'usces' ) . '</td>
4474 </tr>
4475 </table>';
4476 $result .= '<div class="paypal-settlement-admin-button">
4477 <input id="capture-settlement" type="button" class="button" value="' . __( 'CAPTURE', 'usces' ) . '" />
4478 <input id="void-settlement" type="button" class="button" value="' . __( 'VOID', 'usces' ) . '" />
4479 </div>';
4480 } elseif ( 'CAPTURE' === $acting_status ) {
4481 $result .= '<table class="paypal-settlement-admin-table">
4482 <tr><th>' . __( 'Transaction amount', 'usces' ) . '</th>
4483 <td><input type="tel" class="settlement-amount" value="' . usces_crform( $amount, false, false, 'return', true ) . '" readonly />' . __( usces_crcode( 'return' ), 'usces' ) . '<input type="hidden" id="amount_original" value="' . usces_crform( $amount, false, false, 'return', false ) . '" /></td>
4484 </tr>
4485 <tr><th>' . __( 'Refund amount', 'usces' ) . '</th>
4486 <td><input type="tel" class="settlement-amount amount" id="amount_refund" value="" />' . __( usces_crcode( 'return' ), 'usces' ) . '</td>
4487 </tr>
4488 </table>';
4489 $result .= '<div class="paypal-settlement-admin-button">
4490 <input id="refund-settlement" type="button" class="button" value="' . __( 'REFUND', 'usces' ) . '" />
4491 </div>';
4492 }
4493 $result .= '</div>';
4494 do_action( 'usces_action_admin_capture_paypal_cp', $response_data, $order_id, $status );
4495 $result .= $this->settlement_history( $order_id, $tracking_id );
4496 $data['result'] = $result;
4497 $data['acting_status'] = '<span class="acting-status' . $class . '">' . __( $acting_status, 'usces' ) . '</span>';
4498 wp_send_json( $data );
4499 break;
4500
4501 /* VOID */
4502 case 'void_paypal_cp':
4503 check_admin_referer( 'order_edit', 'wc_nonce' );
4504 $order_id = ( isset( $_POST['order_id'] ) ) ? wp_unslash( $_POST['order_id'] ) : '';
4505 $order_num = ( isset( $_POST['order_num'] ) ) ? wp_unslash( $_POST['order_num'] ) : 1;
4506 $tracking_id = ( isset( $_POST['tracking_id'] ) ) ? wp_unslash( $_POST['tracking_id'] ) : '';
4507 $con_id = ( defined( 'WCEX_DLSELLER' ) && isset( $_POST['con_id'] ) ) ? wp_unslash( $_POST['con_id'] ) : 0;
4508 $acting = ( isset( $_POST['acting'] ) ) ? wp_unslash( $_POST['acting'] ) : 'paypal_cp';
4509 if ( empty( $order_id ) || empty( $tracking_id ) ) {
4510 wp_send_json( $data );
4511 break;
4512 }
4513
4514 $acting_status = 'VOID';
4515 $acting_opts = $this->get_acting_settings();
4516 $api_request_url = ( isset( $acting_opts['api_request_url'] ) ) ? $acting_opts['api_request_url'] : '';
4517 $bn_code = ( 'paypal_card' === $acting ) ? self::API_BN_CODE_ACDC : self::API_BN_CODE_PCP;
4518 if ( 1 === (int) $order_num ) {
4519 $trans_id = $usces->get_order_meta_value( 'trans_id', $order_id );
4520 } else {
4521 $trans_id = dlseller_get_continuation_meta_value( 'trans_' . $tracking_id, $con_id );
4522 }
4523 $amount = $this->get_latest_amount( $order_id, $tracking_id );
4524 $result = '';
4525
4526 /* Get Access Token */
4527 $access_token = $this->get_access_token();
4528
4529 $params = array(
4530 'method' => 'POST',
4531 'headers' => array(
4532 'Content-Type' => 'application/json;charset=utf-8',
4533 'Authorization' => 'Bearer ' . $access_token['access_token'],
4534 'PayPal-Partner-Attribution-Id' => $bn_code,
4535 'PayPal-Request-Id' => $tracking_id,
4536 ),
4537 'timeout' => self::API_TIMEOUT,
4538 );
4539 $response = wp_remote_get( $api_request_url . '/v2/payments/authorizations/' . $trans_id . '/void', $params );
4540 if ( isset( $response['response']['code'] ) && '204' === (string) $response['response']['code'] ) {
4541 $status = 'COMPLETED';
4542 $response_data = apply_filters( 'usces_filter_paypal_cp_void_log', $response['response'], $order_id, $status );
4543 $this->save_acting_log( $response_data, $acting, $acting_status, $status, $amount * -1, $order_id, $tracking_id );
4544 $acting_status = 'VOIDED';
4545 $amount = 0;
4546 $class = ' paypal-' . strtolower( $acting_status );
4547 $result .= '<div class="paypal-settlement-admin' . $class . '">' . __( $acting_status, 'usces' ) . '</div>';
4548 $result .= '<table class="paypal-settlement-admin-table">
4549 <tr><th>' . __( 'Transaction amount', 'usces' ) . '</th>
4550 <td><input type="tel" class="settlement-amount" value="' . usces_crform( $amount, false, false, 'return', true ) . '" readonly />' . __( usces_crcode( 'return' ), 'usces' ) . '</td>
4551 </tr></table>';
4552 } else {
4553 $response_data = json_decode( wp_remote_retrieve_body( $response ), true );
4554 if ( isset( $response_data['status'] ) ) {
4555 $status = $response_data['status'];
4556 } elseif ( isset( $response_data['name'] ) ) {
4557 $status = $response_data['name'];
4558 } else {
4559 $status = 'VOID ERROR';
4560 }
4561 $this->save_acting_log( $response_data, $acting, 'ERROR', $status, 0, $order_id, $tracking_id );
4562 $acting_status = 'AUTHORIZE';
4563 $class = ' paypal-' . strtolower( $acting_status );
4564 $result .= '<div class="paypal-settlement-admin' . $class . '">' . __( $acting_status, 'usces' ) . '</div>';
4565 $result .= '<table class="paypal-settlement-admin-table">
4566 <tr><th>' . __( 'Transaction amount', 'usces' ) . '</th>
4567 <td><input type="tel" class="settlement-amount" value="' . usces_crform( $amount, false, false, 'return', true ) . '" readonly />' . __( usces_crcode( 'return' ), 'usces' ) . '<input type="hidden" id="amount_original" value="' . usces_crform( $amount, false, false, 'return', false ) . '" /></td>
4568 </tr>
4569 <tr><th>' . __( 'Refund amount', 'usces' ) . '</th>
4570 <td><input type="tel" class="settlement-amount amount" id="amount_refund" value="" />' . __( usces_crcode( 'return' ), 'usces' ) . '</td>
4571 </tr>
4572 </table>';
4573 $result .= '<div class="paypal-settlement-admin-button">
4574 <input id="capture-settlement" type="button" class="button" value="' . __( 'CAPTURE', 'usces' ) . '" />
4575 <input id="void-settlement" type="button" class="button" value="' . __( 'VOID', 'usces' ) . '" />
4576 </div>';
4577 }
4578 do_action( 'usces_action_admin_void_paypal_cp', $response_data, $order_id, $status );
4579 $result .= $this->settlement_history( $order_id, $tracking_id );
4580 $data['result'] = $result;
4581 $data['acting_status'] = '<span class="acting-status' . $class . '">' . __( $acting_status, 'usces' ) . '</span>';
4582 wp_send_json( $data );
4583 break;
4584
4585 /* REFUND */
4586 case 'refund_paypal_cp':
4587 check_admin_referer( 'order_edit', 'wc_nonce' );
4588 $order_id = ( isset( $_POST['order_id'] ) ) ? wp_unslash( $_POST['order_id'] ) : '';
4589 $order_num = ( isset( $_POST['order_num'] ) ) ? wp_unslash( $_POST['order_num'] ) : 1;
4590 $tracking_id = ( isset( $_POST['tracking_id'] ) ) ? wp_unslash( $_POST['tracking_id'] ) : '';
4591 $refund_amount = ( isset( $_POST['amount'] ) ) ? wp_unslash( $_POST['amount'] ) : 0;
4592 $con_id = ( defined( 'WCEX_DLSELLER' ) && isset( $_POST['con_id'] ) ) ? wp_unslash( $_POST['con_id'] ) : 0;
4593 $acting = ( isset( $_POST['acting'] ) ) ? wp_unslash( $_POST['acting'] ) : 'paypal_cp';
4594 if ( empty( $order_id ) || empty( $tracking_id ) || empty( $refund_amount ) ) {
4595 wp_send_json( $data );
4596 break;
4597 }
4598
4599 $acting_status = 'REFUND';
4600 $acting_opts = $this->get_acting_settings();
4601 $api_request_url = ( isset( $acting_opts['api_request_url'] ) ) ? $acting_opts['api_request_url'] : '';
4602 $bn_code = ( 'paypal_card' === $acting ) ? self::API_BN_CODE_ACDC : self::API_BN_CODE_PCP;
4603 if ( 1 === (int) $order_num ) {
4604 $trans_id = $usces->get_order_meta_value( 'trans_id', $order_id );
4605 } else {
4606 $trans_id = dlseller_get_continuation_meta_value( 'trans_' . $tracking_id, $con_id );
4607 }
4608 $currency_code = $usces->get_currency_code();
4609 $request_id = usces_acting_key(); /* Duplication Prevention */
4610 $result = '';
4611
4612 /* Get Access Token */
4613 $access_token = $this->get_access_token();
4614
4615 $body = array();
4616 $body['amount']['value'] = $refund_amount;
4617 $body['amount']['currency_code'] = $currency_code;
4618 $params = array(
4619 'method' => 'POST',
4620 'headers' => array(
4621 'Content-Type' => 'application/json;charset=utf-8',
4622 'Authorization' => 'Bearer ' . $access_token['access_token'],
4623 'PayPal-Partner-Attribution-Id' => $bn_code,
4624 'PayPal-Request-Id' => $request_id,
4625 ),
4626 'timeout' => self::API_TIMEOUT,
4627 'body' => wp_json_encode( $body ),
4628 );
4629 $response = wp_remote_get( $api_request_url . '/v2/payments/captures/' . $trans_id . '/refund', $params );
4630 $response_data = json_decode( wp_remote_retrieve_body( $response ), true );
4631 if ( isset( $response['response']['code'] ) && '201' === (string) $response['response']['code'] ) {
4632 $status = 'COMPLETED';
4633 } elseif ( isset( $response['response']['code'] ) && '200' === (string) $response['response']['code'] ) {
4634 $status = 'RETRY';
4635 } else {
4636 if ( isset( $response_data['status'] ) ) {
4637 $status = $response_data['status'];
4638 } elseif ( isset( $response_data['name'] ) ) {
4639 $status = $response_data['name'];
4640 } else {
4641 $status = 'REFUND ERROR';
4642 }
4643 }
4644 $response_data = apply_filters( 'usces_filter_paypal_cp_refund_log', $response_data, $order_id, $status );
4645 if ( 'COMPLETED' === $status ) {
4646 $this->save_acting_log( $response_data, $acting, $acting_status, $status, $refund_amount * -1, $order_id, $tracking_id );
4647 } else {
4648 $this->save_acting_log( $response_data, $acting, 'ERROR', $status, 0, $order_id, $tracking_id );
4649 }
4650 $amount = $this->get_latest_amount( $order_id, $tracking_id );
4651 if ( 0 < $amount ) {
4652 $acting_status = 'CAPTURE';
4653 $class = ' paypal-' . strtolower( $acting_status );
4654 $result .= '<div class="paypal-settlement-admin' . $class . '">' . __( $acting_status, 'usces' ) . '</div>';
4655 $result .= '<table class="paypal-settlement-admin-table">
4656 <tr><th>' . __( 'Transaction amount', 'usces' ) . '</th>
4657 <td><input type="tel" class="settlement-amount" value="' . usces_crform( $amount, false, false, 'return', true ) . '" readonly />' . __( usces_crcode( 'return' ), 'usces' ) . '<input type="hidden" id="amount_original" value="' . usces_crform( $amount, false, false, 'return', false ) . '" /></td>
4658 </tr>
4659 <tr><th>' . __( 'Refund amount', 'usces' ) . '</th>
4660 <td><input type="tel" class="settlement-amount amount" id="amount_refund" value="" />' . __( usces_crcode( 'return' ), 'usces' ) . '</td>
4661 </tr>
4662 </table>';
4663 $result .= '<div class="paypal-settlement-admin-button">
4664 <input id="refund-settlement" type="button" class="button" value="' . __( 'REFUND', 'usces' ) . '" />
4665 </div>';
4666 } else {
4667 $acting_status = 'REFUNDED';
4668 $class = ' paypal-' . strtolower( $acting_status );
4669 $result .= '<div class="paypal-settlement-admin' . $class . '">' . __( $acting_status, 'usces' ) . '</div>';
4670 $result .= '<table class="paypal-settlement-admin-table">
4671 <tr><th>' . __( 'Transaction amount', 'usces' ) . '</th>
4672 <td><input type="tel" class="settlement-amount" value="' . usces_crform( $amount, false, false, 'return', true ) . '" readonly />' . __( usces_crcode( 'return' ), 'usces' ) . '</td>
4673 </tr></table>';
4674 }
4675 do_action( 'usces_action_admin_refund_paypal_cp', $response_data, $order_id, $status );
4676 $result .= $this->settlement_history( $order_id, $tracking_id );
4677 $data['result'] = $result;
4678 $data['acting_status'] = '<span class="acting-status' . $class . '">' . __( $acting_status, 'usces' ) . '</span>';
4679 wp_send_json( $data );
4680 break;
4681
4682 /* RE-SETTLEMENT */
4683 case 're_settlement_paypal_cp':
4684 check_admin_referer( 'order_edit', 'wc_nonce' );
4685 $order_id = ( isset( $_POST['order_id'] ) ) ? wp_unslash( $_POST['order_id'] ) : '';
4686 $order_num = ( isset( $_POST['order_num'] ) ) ? wp_unslash( $_POST['order_num'] ) : 1;
4687 $tracking_id = ( isset( $_POST['tracking_id'] ) ) ? wp_unslash( $_POST['tracking_id'] ) : '';
4688 $member_id = ( isset( $_POST['member_id'] ) ) ? wp_unslash( $_POST['member_id'] ) : 0;
4689 $amount = ( isset( $_POST['amount'] ) ) ? wp_unslash( $_POST['amount'] ) : 0;
4690 $cp_intent = ( isset( $_POST['intent'] ) ) ? wp_unslash( $_POST['intent'] ) : '';
4691 $con_id = ( defined( 'WCEX_DLSELLER' ) && isset( $_POST['con_id'] ) ) ? wp_unslash( $_POST['con_id'] ) : 0;
4692 $reg_id = ( defined( 'WCEX_AUTO_DELIVERY' ) && isset( $_POST['reg_id'] ) ) ? wp_unslash( $_POST['reg_id'] ) : 0;
4693 $acting = ( isset( $_POST['acting'] ) ) ? wp_unslash( $_POST['acting'] ) : 'paypal_cp';
4694 if ( empty( $order_id ) || empty( $tracking_id ) || empty( $amount ) || empty( $cp_intent ) ) {
4695 wp_send_json( $data );
4696 break;
4697 }
4698
4699 $log = $this->get_entry_log( $tracking_id );
4700 if ( isset( $log['entry'] ) && isset( $log['cart'] ) ) {
4701 $entry = $log['entry'];
4702 $entry['order']['total_items_price'] = 0;
4703 $entry['order']['total_full_price'] = $amount;
4704 $cart = $log['cart'];
4705 } else {
4706 wp_send_json( $data );
4707 break;
4708 }
4709
4710 $result = '';
4711 $pending = false;
4712 if ( defined( 'WCEX_DLSELLER' ) && ! empty( $con_id ) ) {
4713 $ba_id = $this->get_continuation_ba_id( $member_id, $order_id );
4714 } elseif ( defined( 'WCEX_AUTO_DELIVERY' ) && ! empty( $reg_id ) ) {
4715 $ba_id = $this->get_regular_ba_id( $member_id, $reg_id );
4716 } else {
4717 $ba_id = '';
4718 }
4719 if ( ! empty( $ba_id ) ) {
4720 $bn_code = ( 'paypal_card' === $acting ) ? self::API_BN_CODE_ACDC : self::API_BN_CODE_PCP;
4721 $request_id = usces_acting_key(); /* Duplication Prevention */
4722
4723 /* Get Access Token */
4724 $access_token = $this->get_access_token();
4725
4726 $shipping = usces_have_shipped( $cart );
4727 if ( $shipping ) {
4728 $order_data = $usces->get_order_data( $order_id, 'direct' );
4729 $delivery = unserialize( $order_data['order_delivery'] );
4730 $entry['delivery']['name1'] = $delivery['name1'];
4731 $entry['delivery']['name2'] = $delivery['name2'];
4732 $entry['delivery']['country'] = $delivery['country'];
4733 $entry['delivery']['zipcode'] = $delivery['zipcode'];
4734 $entry['delivery']['pref'] = $delivery['pref'];
4735 $entry['delivery']['address1'] = $delivery['address1'];
4736 $entry['delivery']['address2'] = $delivery['address2'];
4737 $entry['delivery']['address3'] = $delivery['address3'];
4738 }
4739
4740 /* Create order */
4741 $response_order_data = $this->api_create_order( $access_token, $bn_code, $cp_intent, $request_id, $entry, $cart );
4742 if ( isset( $response_order_data['id'] ) && isset( $response_order_data['status'] ) && 'CREATED' === $response_order_data['status'] ) {
4743 $resource_id = $response_order_data['id'];
4744 $correlation_id = '';
4745 $payment_source = array();
4746 $payment_source['token'] = array(
4747 'id' => $ba_id,
4748 'type' => 'BILLING_AGREEMENT',
4749 );
4750 dlseller_set_continuation_meta_value( 'resource_' . $tracking_id, $resource_id, $con_id );
4751 if ( 'CAPTURE' === $cp_intent ) {
4752 /* Capture */
4753 $response_data = $this->api_capture_order( $access_token, $bn_code, $request_id, $resource_id, $correlation_id, $payment_source );
4754 } elseif ( 'AUTHORIZE' === $cp_intent ) {
4755 /* Authorize */
4756 $response_data = $this->api_authorize_order( $access_token, $bn_code, $request_id, $resource_id, $correlation_id, $payment_source );
4757 }
4758 if ( isset( $response_data['status'] ) ) {
4759 $status = $response_data['status'];
4760 } elseif ( isset( $response_data['name'] ) ) {
4761 $status = $response_data['name'];
4762 } else {
4763 $status = $cp_intent . ' ERROR';
4764 }
4765 if ( 'paypal_card' === $acting ) {
4766 if ( isset( $response_data['purchase_units'] ) ) {
4767 $purchase_units = $response_data['purchase_units'][0];
4768 if ( isset( $purchase_units['payments']['captures'] ) ) {
4769 $payments = $purchase_units['payments']['captures'][0];
4770 } elseif ( isset( $purchase_units['payments']['authorizations'] ) ) {
4771 $payments = $purchase_units['payments']['authorizations'][0];
4772 }
4773 if ( ! empty( $payments['status'] ) ) {
4774 if ( 'COMPLETED' !== $payments['status'] && 'PENDING' !== $payments['status'] ) {
4775 $status = $payments['status'];
4776 }
4777 }
4778 }
4779 }
4780 $response_data = apply_filters( 'usces_filter_paypal_cp_re_settlement_log', $response_data, $order_id, $status );
4781 if ( 'COMPLETED' === $status && isset( $response_data['purchase_units'] ) ) {
4782 $this->save_acting_log( $response_data, $acting, $cp_intent, $status, $amount, $order_id, $tracking_id );
4783 $purchase_units = $response_data['purchase_units'][0];
4784 if ( isset( $purchase_units['payments']['captures'] ) ) {
4785 $payments = $purchase_units['payments']['captures'][0];
4786 } elseif ( isset( $purchase_units['payments']['authorizations'] ) ) {
4787 $payments = $purchase_units['payments']['authorizations'][0];
4788 }
4789 if ( ! empty( $payments['status'] ) && 'PENDING' === $payments['status'] ) {
4790 $pending = true;
4791 }
4792 if ( $con_id ) {
4793 // dlseller_set_continuation_meta_value( 'resource_' . $tracking_id, $resource_id, $con_id );
4794 if ( ! empty( $payments['id'] ) ) {
4795 dlseller_set_continuation_meta_value( 'trans_' . $tracking_id, $payments['id'], $con_id ); /* 取引ID */
4796 }
4797 } elseif ( $reg_id ) {
4798 $usces->set_order_meta_value( 'resource_id', $resource_id, $order_id );
4799 if ( ! empty( $payments['id'] ) ) {
4800 $usces->set_order_meta_value( 'wc_trans_id', $payments['id'], $order_id ); /* 決済ID */
4801 $usces->set_order_meta_value( 'trans_id', $payments['id'], $order_id ); /* 取引ID */
4802 }
4803 }
4804 $this->del_entry_log( $tracking_id );
4805 } else {
4806 $this->save_acting_log( $response_data, $acting, 'ERROR', $status, 0, $order_id, $tracking_id );
4807 }
4808 } else {
4809 if ( isset( $response_order_data['status'] ) ) {
4810 $status = $response_order_data['status'];
4811 } elseif ( isset( $response_order_data['name'] ) ) {
4812 $status = $response_order_data['name'];
4813 } else {
4814 $status = 'CREATE ORDER ERROR';
4815 }
4816 $this->save_acting_log( $response_order_data, $acting, 'ERROR', $status, 0, $order_id, $tracking_id );
4817 }
4818 } else {
4819 $status = 'RE-SETTLEMENT ERROR';
4820 $this->save_acting_log( array(), $acting, 'ERROR', $status, 0, $order_id, $tracking_id );
4821 }
4822 if ( 'COMPLETED' === $status ) {
4823 $acting_status = $cp_intent;
4824 $class = ' paypal-' . strtolower( $acting_status );
4825 $acting_pending = ( $pending ) ? ' ' . __( '[ PENDING ]', 'usces' ) : '';
4826 $result .= '<div class="paypal-settlement-admin' . $class . '">' . __( $acting_status, 'usces' ) . $acting_pending . '</div>';
4827 if ( 'AUTHORIZE' === $acting_status ) {
4828 $result .= '<table class="paypal-settlement-admin-table">
4829 <tr><th>' . __( 'Transaction amount', 'usces' ) . '</th>
4830 <td><input type="tel" class="settlement-amount" value="' . usces_crform( $amount, false, false, 'return', true ) . '" readonly />' . __( usces_crcode( 'return' ), 'usces' ) . '<input type="hidden" id="amount_original" value="' . usces_crform( $amount, false, false, 'return', false ) . '" /></td>
4831 </tr>
4832 <tr><th>' . __( 'Refund amount', 'usces' ) . '</th>
4833 <td><input type="tel" class="settlement-amount amount" id="amount_refund" value="" />' . __( usces_crcode( 'return' ), 'usces' ) . '</td>
4834 </tr>
4835 </table>';
4836 $result .= '<div class="paypal-settlement-admin-button">
4837 <input id="capture-settlement" type="button" class="button" value="' . __( 'CAPTURE', 'usces' ) . '" />
4838 <input id="void-settlement" type="button" class="button" value="' . __( 'VOID', 'usces' ) . '" />
4839 </div>';
4840 } elseif ( 'CAPTURE' === $acting_status ) {
4841 $result .= '<table class="paypal-settlement-admin-table">
4842 <tr><th>' . __( 'Transaction amount', 'usces' ) . '</th>
4843 <td><input type="tel" class="settlement-amount" value="' . usces_crform( $amount, false, false, 'return', true ) . '" readonly />' . __( usces_crcode( 'return' ), 'usces' ) . '<input type="hidden" id="amount_original" value="' . usces_crform( $amount, false, false, 'return', false ) . '" /></td>
4844 </tr>
4845 <tr><th>' . __( 'Refund amount', 'usces' ) . '</th>
4846 <td><input type="tel" class="settlement-amount amount" id="amount_refund" value="" />' . __( usces_crcode( 'return' ), 'usces' ) . '</td>
4847 </tr>
4848 </table>';
4849 $result .= '<div class="paypal-settlement-admin-button">
4850 <input id="refund-settlement" type="button" class="button" value="' . __( 'REFUND', 'usces' ) . '" />
4851 </div>';
4852 }
4853 } else {
4854 // $amount = 0;
4855 $acting_status = 'ERROR';
4856 $class = ' paypal-' . strtolower( $acting_status );
4857 $result .= '<div class="paypal-settlement-admin paypal-error">' . __( $acting_status, 'usces' ) . '</div>';
4858 $result .= '<table class="paypal-settlement-admin-table">
4859 <tr><th>' . __( 'Transaction amount', 'usces' ) . '</th>
4860 <td><input type="tel" class="settlement-amount" value="' . usces_crform( $amount, false, false, 'return', true ) . '" readonly />' . __( usces_crcode( 'return' ), 'usces' ) . '</td>
4861 </tr>
4862 <tr><th>' . __( 'Settlement amount', 'usces' ) . '</th>
4863 <td><input type="tel" class="settlement-amount amount" id="amount_resettlement" value="" />' . __( usces_crcode( 'return' ), 'usces' ) . '</td>
4864 </tr>
4865 </table>';
4866 if ( 'paypal_cp' === $acting ) {
4867 $result .= '<div class="paypal-settlement-admin-button">
4868 <input id="re-authorize-settlement" type="button" class="button" value="' . __( 'AUTHORIZE', 'usces' ) . '" />
4869 <input id="re-capture-settlement" type="button" class="button" value="' . __( 'CAPTURE', 'usces' ) . '" />
4870 </div>';
4871 }
4872 }
4873 if ( defined( 'WCEX_DLSELLER' ) && ! empty( $con_id ) ) {
4874 do_action( 'usces_action_admin_re_settlement_paypal_cp', $response_data, $order_id, $status, $con_id );
4875 } elseif ( defined( 'WCEX_AUTO_DELIVERY' ) && ! empty( $reg_id ) ) {
4876 do_action( 'usces_action_admin_re_settlement_paypal_cp', $response_data, $order_id, $status, $reg_id );
4877 } else {
4878 do_action( 'usces_action_admin_re_settlement_paypal_cp', $response_data, $order_id, $status );
4879 }
4880 $result .= $this->settlement_history( $order_id, $tracking_id );
4881 $data['result'] = $result;
4882 $data['acting_status'] = '<span class="acting-status' . $class . '">' . __( $acting_status, 'usces' ) . '</span>';
4883 wp_send_json( $data );
4884 break;
4885
4886 /* 継続課金�
4887 報更新 */
4888 case 'continuation_update':
4889 check_admin_referer( 'order_edit', 'wc_nonce' );
4890 $order_id = ( isset( $_POST['order_id'] ) ) ? wp_unslash( $_POST['order_id'] ) : '';
4891 $member_id = ( isset( $_POST['member_id'] ) ) ? wp_unslash( $_POST['member_id'] ) : '';
4892 $contracted_year = ( isset( $_POST['contracted_year'] ) ) ? wp_unslash( $_POST['contracted_year'] ) : '';
4893 $contracted_month = ( isset( $_POST['contracted_month'] ) ) ? wp_unslash( $_POST['contracted_month'] ) : '';
4894 $contracted_day = ( isset( $_POST['contracted_day'] ) ) ? wp_unslash( $_POST['contracted_day'] ) : '';
4895 $charged_year = ( isset( $_POST['charged_year'] ) ) ? wp_unslash( $_POST['charged_year'] ) : '';
4896 $charged_month = ( isset( $_POST['charged_month'] ) ) ? wp_unslash( $_POST['charged_month'] ) : '';
4897 $charged_day = ( isset( $_POST['charged_day'] ) ) ? wp_unslash( $_POST['charged_day'] ) : '';
4898 $price = ( isset( $_POST['price'] ) ) ? wp_unslash( $_POST['price'] ) : 0;
4899 $status = ( isset( $_POST['status'] ) ) ? wp_unslash( $_POST['status'] ) : '';
4900
4901 $continue_data = $this->get_continuation_data( $member_id, $order_id );
4902 if ( ! $continue_data ) {
4903 $data['status'] = 'NG';
4904 wp_send_json( $data );
4905 break;
4906 }
4907
4908 /* 継続中→停止 */
4909 if ( 'continuation' === $continue_data['status'] && 'cancellation' === $status ) {
4910 $this->update_continuation_data( $member_id, $order_id, $continue_data, true );
4911 } else {
4912 if ( ! empty( $contracted_year ) && ! empty( $contracted_month ) && ! empty( $contracted_day ) ) {
4913 $contracted_date = ( empty( $continue_data['contractedday'] ) ) ? dlseller_next_contracting( $order_id ) : $continue_data['contractedday'];
4914 if ( $contracted_date ) {
4915 $new_contracted_date = $contracted_year . '-' . $contracted_month . '-' . $contracted_day;
4916 if ( ! $this->isdate( $new_contracted_date ) ) {
4917 $data['status'] = 'NG';
4918 $data['message'] = __( 'Next contract renewal date is incorrect.', 'dlseller' );
4919 wp_send_json( $data );
4920 }
4921 }
4922 } else {
4923 $new_contracted_date = '';
4924 }
4925 $new_charged_date = $charged_year . '-' . $charged_month . '-' . $charged_day;
4926 if ( ! $this->isdate( $new_charged_date ) ) {
4927 $data['status'] = 'NG';
4928 $data['message'] = __( 'Next settlement date is incorrect.', 'dlseller' );
4929 wp_send_json( $data );
4930 }
4931 $tomorrow = date_i18n( 'Y-m-d', strtotime( '+1 day' ) );
4932 if ( $new_charged_date < $tomorrow ) {
4933 $data['status'] = 'NG';
4934 $data['message'] = sprintf( __( 'The next settlement date must be after %s.', 'dlseller' ), $tomorrow );
4935 wp_send_json( $data );
4936 }
4937 $continue_data['contractedday'] = $new_contracted_date;
4938 $continue_data['chargedday'] = $new_charged_date;
4939 $continue_data['price'] = usces_crform( $price, false, false, 'return', false );
4940 $continue_data['status'] = $status;
4941 $this->update_continuation_data( $member_id, $order_id, $continue_data );
4942 }
4943 $data['status'] = 'OK';
4944 wp_send_json( $data );
4945 break;
4946 }
4947 }
4948
4949 /**
4950 * 受注データから取得する決済�
4951 報のキー
4952 * usces_filter_settle_info_field_meta_keys
4953 *
4954 * @param array $keys Settlement information keys.
4955 * @return array
4956 */
4957 public function settlement_info_field_meta_keys( $keys ) {
4958 $keys = array_merge( $keys, array( 'reference_id', 'trans_id', 'liability_shift', 'enrollment_status', 'authentication_status' ) );
4959 return $keys;
4960 }
4961
4962 /**
4963 * 受注編集画面に表示する決済�
4964 報のキー
4965 * usces_filter_settle_info_field_keys
4966 *
4967 * @param array $keys Settlement information keys.
4968 * @param array $fields Settlement information fields.
4969 * @return array
4970 */
4971 public function settlement_info_field_keys( $keys, $fields ) {
4972 if ( isset( $fields['acting'] ) && ( 'paypal_cp' === $fields['acting'] || 'paypal_card' === $fields['acting'] ) ) {
4973 $keys = array( 'acting', 'reference_id', 'trans_id', 'liability_shift', 'enrollment_status', 'authentication_status' );
4974 }
4975 return $keys;
4976 }
4977
4978 /**
4979 * 受注編集画面に表示する決済�
4980 報の値整形
4981 * usces_filter_settle_info_field_value
4982 *
4983 * @param string $value Value.
4984 * @param string $key Key.
4985 * @param string $acting Acting type.
4986 * @return string
4987 */
4988 public function settlement_info_field_value( $value, $key, $acting ) {
4989 if ( 'acting' === $key ) {
4990 if ( 'paypal_cp' === $value ) {
4991 $value = $this->acting_name;
4992 } elseif ( 'paypal_card' === $value ) {
4993 $value = __( 'PayPal(CreditCard)', 'usces' );
4994 }
4995 }
4996 return $value;
4997 }
4998
4999 /**
5000 * 決済状況
5001 * usces_filter_orderlist_detail_value
5002 *
5003 * @param string $detail HTML.
5004 * @param string $value Value.
5005 * @param string $key Key.
5006 * @param int $order_id Order number.
5007 * @return array
5008 */
5009 public function orderlist_settlement_status( $detail, $value, $key, $order_id ) {
5010 global $usces;
5011
5012 if ( 'wc_trans_id' !== $key || empty( $value ) ) {
5013 return $detail;
5014 }
5015
5016 $acting_flg = $this->get_order_acting_flg( $order_id );
5017 if ( in_array( $acting_flg, $this->pay_method ) ) {
5018 $tracking_id = $usces->get_order_meta_value( 'reference_id', $order_id );
5019 $acting_status = $this->get_acting_status( $order_id, $tracking_id );
5020 if ( ! empty( $acting_status ) ) {
5021 $class = ' paypal-' . strtolower( $acting_status );
5022 $detail = '<td>' . $value . '<span class="acting-status' . $class . '">' . __( $acting_status, 'usces' ) . '</span></td>';
5023 }
5024 }
5025 return $detail;
5026 }
5027
5028 /**
5029 * 受注編集画面【ステータス】
5030 * usces_action_order_edit_form_status_block_middle
5031 *
5032 * @param array $data Order data.
5033 * @param array $cscs_meta Custom field data.
5034 * @param array $action_args Compact array( $order_action, $order_id, $cart ).
5035 */
5036 public function settlement_status( $data, $cscs_meta, $action_args ) {
5037 global $usces;
5038 extract( $action_args );
5039
5040 if ( 'new' !== $order_action && ! empty( $order_id ) ) {
5041 $payment = usces_get_payments_by_name( $data['order_payment_name'] );
5042 $acting_flg = ( isset( $payment['settlement'] ) ) ? $payment['settlement'] : '';
5043 if ( in_array( $acting_flg, $this->pay_method ) ) {
5044 $tracking_id = $usces->get_order_meta_value( 'reference_id', $order_id );
5045 $acting_status = $this->get_acting_status( $order_id, $tracking_id );
5046 if ( ! empty( $acting_status ) ) {
5047 $class = ' paypal-' . strtolower( $acting_status );
5048 if ( ! empty( $acting_status ) ) {
5049 echo '
5050 <tr>
5051 <td class="label status">' . esc_html__( 'Settlement status', 'usces' ) . '</td>
5052 <td class="col1 status"><span id="settlement-status-1"><span class="acting-status' . esc_attr( $class ) . '">' . esc_html__( $acting_status, 'usces' ) . '</span></span></td>
5053 </tr>';
5054 }
5055 }
5056 }
5057 }
5058 }
5059
5060 /**
5061 * 受注編集画面【支払�
5062 報】
5063 * usces_action_order_edit_form_settle_info
5064 *
5065 * @param array $data Order data.
5066 * @param array $action_args Compact array( $order_action, $order_id, $cart ).
5067 */
5068 public function settlement_information( $data, $action_args ) {
5069 global $usces;
5070 extract( $action_args );
5071
5072 if ( 'new' !== $order_action && ! empty( $order_id ) ) {
5073 $payment = usces_get_payments_by_name( $data['order_payment_name'] );
5074 if ( in_array( $payment['settlement'], $this->pay_method ) ) {
5075 $tracking_id = $usces->get_order_meta_value( 'reference_id', $order_id );
5076 echo '<input type="button" class="button settlement-information" id="settlement-information-' . esc_attr( $tracking_id ) . '" data-tracking_id="' . esc_attr( $tracking_id ) . '" data-num="1" value="' . esc_html__( 'Settlement info', 'usces' ) . '">';
5077 }
5078 }
5079 }
5080
5081 /**
5082 * 決済�
5083 報ダイアログ
5084 * usces_action_endof_order_edit_form
5085 *
5086 * @param array $data Order data.
5087 * @param array $action_args Compact array( $order_action, $order_id, $cart ).
5088 */
5089 public function settlement_dialog( $data, $action_args ) {
5090 global $usces;
5091 extract( $action_args );
5092
5093 if ( 'new' !== $order_action && ! empty( $order_id ) ) :
5094 $payment = usces_get_payments_by_name( $data['order_payment_name'] );
5095 if ( in_array( $payment['settlement'], $this->pay_method ) ) :
5096 $acting = substr( $payment['settlement'], 7 );
5097 if ( defined( 'WCEX_AUTO_DELIVERY' ) ) {
5098 $reg_id = $this->get_regular_id( $order_id );
5099 } else {
5100 $reg_id = '';
5101 }
5102 ?>
5103 <div id="settlement_dialog" title="">
5104 <div id="settlement-response-loading"></div>
5105 <fieldset>
5106 <div id="settlement-response"></div>
5107 <input type="hidden" id="order_num">
5108 <input type="hidden" id="tracking_id">
5109 <input type="hidden" id="acting" value="<?php echo esc_attr( $acting ); ?>">
5110 <?php if ( ! empty( $reg_id ) ) : ?>
5111 <input type="hidden" id="reg_id" value="<?php echo esc_attr( $reg_id ); ?>">
5112 <?php endif; ?>
5113 </fieldset>
5114 </div>
5115 <?php
5116 endif;
5117 endif;
5118 }
5119
5120 /**
5121 * 会員データ編集画面
5122 * usces_action_admin_member_info
5123 *
5124 * @param array $member Member data.
5125 * @param array $member_metas Member meta data.
5126 * @param array $usces_member_history Member history data.
5127 */
5128 public function admin_member_info( $member, $member_metas, $usces_member_history ) {
5129 if ( empty( $member['ID'] ) ) {
5130 return;
5131 }
5132 if ( 0 < count( $member_metas ) ) :
5133 if ( defined( 'WCEX_DLSELLER' ) ) :
5134 if ( dlseller_have_member_continue_order( $member['ID'] ) ) :
5135 ?>
5136 <tr>
5137 <td class="label"><?php esc_html_e( '[ Automatic recurring billing ]', 'usces' ); ?></td>
5138 <td><div class="rod_left shortm"><?php esc_html_e( 'Registered', 'usces' ); ?></div></td>
5139 </tr>
5140 <?php
5141 endif;
5142 endif;
5143 if ( defined( 'WCEX_AUTO_DELIVERY' ) ) :
5144 if ( wcad_have_member_regular_order( $member['ID'] ) ) :
5145 ?>
5146 <tr>
5147 <td class="label"><?php esc_html_e( '[ Regular Purchase ]', 'usces' ); ?></td>
5148 <td><div class="rod_left shortm"><?php esc_html_e( 'Registered', 'usces' ); ?></div></td>
5149 </tr>
5150 <?php
5151 endif;
5152 endif;
5153 endif;
5154
5155 $acting_opts = $this->get_acting_settings();
5156 if ( $this->is_validity_acting( 'card' ) ) :
5157 /* Get Access Token */
5158 $access_token = $this->get_access_token();
5159
5160 $payment_tokens = $this->api_get_payment_tokens( $access_token, $member['ID'] );
5161 if ( ! empty( $payment_tokens['payment_tokens'] ) && is_array( $payment_tokens['payment_tokens'] ) ) :
5162 $tokens_count = count( $payment_tokens['payment_tokens'] );
5163 if ( 1 === $tokens_count ) :
5164 $payment_token = current( $payment_tokens['payment_tokens'] );
5165 $payment_token_id = $payment_token['id'];
5166 $payment_token_last_digits = $payment_token['source']['card']['last_digits'];
5167 $payment_token_brand = $payment_token['source']['card']['brand'];
5168 ?>
5169 <tr>
5170 <td class="label"><?php esc_html_e( 'Last 4 digits of registered card', 'usces' ); ?></td>
5171 <td><div class="rod_left shortm"><?php echo esc_html( $payment_token_last_digits ); ?></div></td>
5172 </tr>
5173 <tr>
5174 <td class="label"><input type="hidden" name="paypal_payment_token_id" value="<?php echo esc_attr( $payment_token_id ); ?>" /></td>
5175 <td><input type="checkbox" name="paypal_card_delete" id="paypal_card_delete" value="delete"><label for="paypal_card_delete"><?php esc_html_e( 'Credit Card Deletion', 'usces' ); ?></label></td>
5176 </tr>
5177 <?php
5178 endif;
5179 endif;
5180 endif;
5181 }
5182
5183 /**
5184 * 会員データ編集画面 カード�
5185 報登録解除
5186 * usces_action_post_update_memberdata
5187 *
5188 * @param int $member_id Member ID.
5189 * @param boolean $res Result.
5190 */
5191 public function admin_update_memberdata( $member_id, $res ) {
5192 if ( ! $this->is_validity_acting( 'card' ) || false === $res ) {
5193 return;
5194 }
5195
5196 if ( isset( $_POST['paypal_card_delete'] ) && 'delete' === wp_unslash( $_POST['paypal_card_delete'] ) && isset( $_POST['paypal_payment_token_id'] ) ) {
5197 /* Get Access Token */
5198 $access_token = $this->get_access_token();
5199 $delete_status = $this->api_delete_payment_tokens( $access_token, wp_unslash( $_POST['paypal_payment_token_id'] ) );
5200 }
5201 }
5202
5203 /**
5204 * 利用可能な支払方法(継続課金・定期購�
5205 �)
5206 * dlseller_filter_the_payment_method_restriction
5207 * wcad_filter_the_payment_method_restriction
5208 *
5209 * @param array $payments_restriction Payment method.
5210 * @param string $value Input value.
5211 * @return array
5212 */
5213 public function payment_method_restriction( $payments_restriction, $value ) {
5214 if ( ( usces_have_regular_order() || usces_have_continue_charge() ) && usces_is_login() ) {
5215 $paypal_cp = false;
5216 foreach ( (array) $payments_restriction as $key => $payment ) {
5217 if ( 'acting_paypal_cp' === $payment['settlement'] ) {
5218 $paypal_cp = true;
5219 }
5220 }
5221 if ( ! $paypal_cp ) {
5222 $payments = usces_get_system_option( 'usces_payment_method', 'settlement' );
5223 $payments_restriction[] = $payments['acting_paypal_cp'];
5224 }
5225 $sort = array();
5226 foreach ( (array) $payments_restriction as $key => $payment ) {
5227 $sort[ $key ] = $payment['sort'];
5228 }
5229 array_multisort( $sort, SORT_ASC, $payments_restriction );
5230 }
5231 return $payments_restriction;
5232 }
5233
5234 /**
5235 * 利用可能な支払方法
5236 * usces_filter_the_continue_payment_method
5237 *
5238 * @param array $payment_method Payment method.
5239 * @return array
5240 */
5241 public function continuation_payment_method( $payment_method ) {
5242 if ( ! array_key_exists( 'acting_paypal_cp', $payment_method ) ) {
5243 $payment_method[] = 'acting_paypal_cp';
5244 }
5245 return $payment_method;
5246 }
5247
5248 /**
5249 * 自動継続課金データ登録
5250 * dlseller_action_reg_continuationdata
5251 *
5252 * @param array $args Compact array( $cart, $entry, $order_id, $member_id, $payments, $charging_type, $results, $con_id ).
5253 */
5254 public function register_continuationdata( $args ) {
5255 global $usces;
5256 extract( $args );
5257
5258 if ( ! empty( $order_id ) && ! empty( $member_id ) && ! empty( $results['ba_id'] ) ) {
5259 $this->set_continuation_ba_id( $member_id, $order_id, $results['ba_id'] );
5260 }
5261 }
5262
5263 /**
5264 * 「初回引落し日」
5265 * dlseller_filter_first_charging
5266 *
5267 * @param object $time Datetime.
5268 * @param int $post_id Post ID.
5269 * @param array $usces_item Item data.
5270 * @param int $order_id Order number.
5271 * @param array $continue_data Continuation data.
5272 * @return object
5273 */
5274 public function first_charging_date( $time, $post_id, $usces_item, $order_id, $continue_data ) {
5275 if ( 99 === (int) $usces_item['item_chargingday'] ) {
5276 if ( empty( $order_id ) ) {
5277 $today = date_i18n( 'Y-m-d', current_time( 'timestamp' ) );
5278 list( $year, $month, $day ) = explode( '-', $today );
5279 $time = mktime( 0, 0, 0, (int) $month, (int) $day, (int) $year );
5280 }
5281 }
5282 return $time;
5283 }
5284
5285 /**
5286 * 継続課金会員リスト「契約」
5287 * dlseller_filter_continue_member_list_continue_status
5288 *
5289 * @param string $status Continuation status.
5290 * @param int $member_id Member ID.
5291 * @param int $order_id Order number.
5292 * @param array $meta_data Continuation data.
5293 * @return string
5294 */
5295 public function continue_member_list_continue_status( $status, $member_id, $order_id, $meta_data ) {
5296 return $status;
5297 }
5298
5299 /**
5300 * 継続課金会員リスト Column
5301 * dlseller_filter_continue_memberlist_column
5302 *
5303 * @param string $columns Continuation condition.
5304 * @param object $continuationlist Continuation list class.
5305 * @return string
5306 */
5307 public function continue_memberlist_column( $columns, $continuationlist ) {
5308 $columns['ba_id'] = __( 'Billing Agreement ID', 'usces' );
5309 return $columns;
5310 }
5311
5312 /**
5313 * 継続課金会員リスト Join table
5314 * dlseller_filter_continue_memberlist_sql_jointable
5315 *
5316 * @param string $join Join query.
5317 * @param object $continuationlist Continuation list class.
5318 * @return string
5319 */
5320 public function continue_memberlist_sql_jointable( $join, $continuationlist ) {
5321 global $wpdb;
5322
5323 $join .= "LEFT JOIN {$wpdb->prefix}usces_member_meta AS `mm1` ON con_member_id = mm1.member_id AND mm1.meta_key = concat( 'paypal_continuation_', con_order_id ) ";
5324 return $join;
5325 }
5326
5327 /**
5328 * 継続課金会員リスト Select query
5329 * dlseller_filter_continue_memberlist_sql_select
5330 *
5331 * @param string $query Select query.
5332 * @param string $csod Custom query.
5333 * @param object $continuationlist Continuation list class.
5334 * @return string
5335 */
5336 public function continue_memberlist_sql_select( $query, $csod, $continuationlist ) {
5337 global $wpdb;
5338
5339 $query = "SELECT
5340 `con_id` AS `ID`,
5341 `con_order_id` AS `order_id`,
5342 om.meta_value AS `deco_id`,
5343 `con_member_id` AS `mem_id`,
5344 mem.mem_email AS `email`,
5345 mem.mem_name1 AS `name1`,
5346 mem.mem_name2 AS `name2`,
5347 mem.mem_name3 AS `name3`,
5348 mem.mem_name4 AS `name4`,
5349 IFNULL( mm.meta_value, '' ) AS `limitofcard`,
5350 `con_price` AS `price`,
5351 `con_acting` AS `acting`,
5352 ord.order_payment_name AS `payment_name`,
5353 DATE_FORMAT( ord.order_date, '%Y-%m-%d' ) AS `orderdate`,
5354 DATE_FORMAT( con_startdate, '%Y-%m-%d' ) AS `startdate`,
5355 DATE_FORMAT( con_next_contracting, '%Y-%m-%d' ) AS `contractedday`,
5356 DATE_FORMAT( con_next_charging, '%Y-%m-%d' ) AS `chargedday`,
5357 `con_status` AS `status`,
5358 `con_condition` AS `condition`
5359 {$csod},
5360 IFNULL( mm1.meta_value, '' ) AS `ba_id`
5361 FROM {$continuationlist->table} ";
5362 return $query;
5363 }
5364
5365 /**
5366 * 継続課金会員リスト「状�
5367 �」
5368 * dlseller_filter_continue_member_list_condition
5369 *
5370 * @param string $condition Continuation condition.
5371 * @param int $member_id Member ID.
5372 * @param int $order_id Order number.
5373 * @param array $continue_data Continuation data.
5374 * @return string
5375 */
5376 public function continue_member_list_condition( $condition, $member_id, $order_id, $continue_data ) {
5377 global $usces;
5378
5379 $acting_flg = $this->get_order_acting_flg( $order_id );
5380 if ( 'acting_paypal_cp' === $acting_flg ) {
5381 $log_data = $this->get_acting_log( $order_id, 0, 'ALL' );
5382 if ( $log_data ) {
5383 $url = admin_url( 'admin.php?page=usces_continue&continue_action=settlement_paypal_cp&member_id=' . esc_attr( $member_id ) . '&order_id=' . esc_attr( $order_id ) );
5384 $condition = '<a href="' . $url . '">' . __( 'Detail', 'usces' ) . '</a>';
5385 if ( 'continuation' === $continue_data['status'] ) {
5386 $latest_log = $this->get_acting_latest_log( $order_id, 0, 'ALL' );
5387 if ( isset( $latest_log['status'] ) && 'ERROR' === $latest_log['status'] ) {
5388 $condition .= '<div class="acting-status paypal-error">' . __( 'Settlement error', 'usces' ) . '</div>';
5389 }
5390 }
5391 }
5392 }
5393 return $condition;
5394 }
5395
5396 /**
5397 * 継続課金会員決済状況ページ表示
5398 * dlseller_action_continue_member_list_page
5399 *
5400 * @param string $continue_action Continuation action.
5401 */
5402 public function continue_member_list_page( $continue_action ) {
5403 if ( 'settlement_paypal_cp' === $continue_action ) {
5404 $member_id = ( isset( $_GET['member_id'] ) ) ? wp_unslash( $_GET['member_id'] ) : '';
5405 $order_id = ( isset( $_GET['order_id'] ) ) ? wp_unslash( $_GET['order_id'] ) : '';
5406 if ( ! empty( $member_id ) && ! empty( $order_id ) ) {
5407 $this->continue_member_settlement_info_page( $member_id, $order_id );
5408 exit();
5409 }
5410 }
5411 }
5412
5413 /**
5414 * 継続課金会員決済状況ページ
5415 *
5416 * @param int $member_id Member ID.
5417 * @param int $order_id Order number.
5418 */
5419 public function continue_member_settlement_info_page( $member_id, $order_id ) {
5420 global $usces;
5421
5422 $order_data = $usces->get_order_data( $order_id, 'direct' );
5423 if ( ! $order_data ) {
5424 return;
5425 }
5426
5427 $payment = $usces->getPayments( $order_data['order_payment_name'] );
5428 if ( 'acting_paypal_cp' !== $payment['settlement'] ) {
5429 return;
5430 }
5431
5432 $continue_data = $this->get_continuation_data( $member_id, $order_id );
5433 $con_id = $continue_data['con_id'];
5434 $curent_url = esc_url( $_SERVER['REQUEST_URI'] );
5435 $navibutton = '<a href="' . esc_url( $_SERVER['HTTP_REFERER'] ) . '" class="back-list"><span class="dashicons dashicons-list-view"></span>' . __( 'Back to the continue members list', 'dlseller' ) . '</a>';
5436
5437 $member_info = $usces->get_member_info( $member_id );
5438 $name = usces_localized_name( $member_info['mem_name1'], $member_info['mem_name2'], 'return' );
5439
5440 $contracted_date = ( empty( $continue_data['contractedday'] ) ) ? dlseller_next_contracting( $order_id ) : $continue_data['contractedday'];
5441 if ( ! empty( $contracted_date ) ) {
5442 list( $contracted_year, $contracted_month, $contracted_day ) = explode( '-', $contracted_date );
5443 } else {
5444 $contracted_year = 0;
5445 $contracted_month = 0;
5446 $contracted_day = 0;
5447 }
5448 $charged_date = ( empty( $continue_data['chargedday'] ) ) ? dlseller_next_charging( $order_id ) : $continue_data['chargedday'];
5449 if ( ! empty( $charged_date ) ) {
5450 list( $charged_year, $charged_month, $charged_day ) = explode( '-', $charged_date );
5451 } else {
5452 $charged_year = 0;
5453 $charged_month = 0;
5454 $charged_day = 0;
5455 }
5456 $this_year = substr( date_i18n( 'Y', current_time( 'timestamp' ) ), 0, 4 );
5457
5458 $log_data = $this->get_acting_log( $order_id, 0, 'ALL' );
5459 $num = ( $log_data ) ? count( $log_data ) : 1;
5460 ?>
5461 <div class="wrap">
5462 <div class="usces_admin">
5463 <h1>Welcart Management <?php esc_html_e( 'Continuation charging member information', 'dlseller' ); ?></h1>
5464 <p class="version_info">Version <?php echo esc_html( WCEX_DLSELLER_VERSION ); ?></p>
5465 <?php usces_admin_action_status(); ?>
5466 <div class="edit_pagenav"><?php wel_esc_script_e( $navibutton ); ?></div>
5467 <div id="datatable">
5468 <div id="tablesearch" class="usces_tablesearch">
5469 <div id="searchBox" style="display:block">
5470 <table class="search_table">
5471 <tr>
5472 <td class="label"><?php esc_html_e( 'Continuation charging information', 'dlseller' ); ?></td>
5473 <td>
5474 <table class="order_info">
5475 <tr>
5476 <th><?php esc_html_e( 'Member ID', 'dlseller' ); ?></th>
5477 <td><?php echo esc_html( $member_id ); ?></td>
5478 <th><?php esc_html_e( 'Contractor name', 'dlseller' ); ?></th>
5479 <td><?php echo esc_html( $name ); ?></td>
5480 </tr>
5481 <tr>
5482 <th><?php esc_html_e( 'Order ID', 'dlseller' ); ?></th>
5483 <td><?php echo esc_html( $order_id ); ?></td>
5484 <th><?php esc_html_e( 'Application Date', 'dlseller' ); ?></th>
5485 <td><?php echo esc_html( $order_data['order_date'] ); ?></td>
5486 </tr>
5487 <tr>
5488 <th><?php esc_html_e( 'Renewal Date', 'dlseller' ); ?></th>
5489 <td>
5490 <?php
5491 echo '<select id="contracted-year">';
5492 echo '<option value="0"' . selected( $contracted_year, 0, false ) . '></option>';
5493 for ( $i = 0; $i <= 10; $i++ ) {
5494 $year = (int) $this_year + $i;
5495 echo '<option value="' . esc_html( $year ) . '"' . selected( $contracted_year, $year, false ) . '>' . esc_html( $year ) . '</option>';
5496 }
5497 echo '</select>-<select id="contracted-month">';
5498 echo '<option value="0"' . selected( $contracted_month, 0, false ) . '></option>';
5499 for ( $i = 1; $i <= 12; $i++ ) {
5500 $month = sprintf( '%02d', $i );
5501 echo '<option value="' . esc_html( $month ) . '"' . selected( $contracted_month, $month, false ) . '>' . esc_html( $month ) . '</option>';
5502 }
5503 echo '</select>-<select id="contracted-day">';
5504 echo '<option value="0"' . selected( $contracted_day, 0, false ) . '></option>';
5505 for ( $i = 1; $i <= 31; $i++ ) {
5506 $day = sprintf( '%02d', $i );
5507 echo '<option value="' . esc_html( $day ) . '"' . selected( $contracted_day, $day, false ) . '>' . esc_html( $day ) . '</option>';
5508 }
5509 echo '</select>';
5510 ?>
5511 </td>
5512 <th><?php esc_html_e( 'Next Withdrawal Date', 'dlseller' ); ?></th>
5513 <td>
5514 <?php
5515 echo '<select id="charged-year">';
5516 echo '<option value="0"' . selected( $charged_year, 0, false ) . '></option>';
5517 echo '<option value="' . esc_html( $this_year ) . '"' . selected( $charged_year, $this_year, false ) . '>' . esc_html( $this_year ) . '</option>';
5518 $next_year = (int) $this_year + 1;
5519 echo '<option value="' . esc_html( $next_year ) . '"' . selected( $charged_year, $next_year, false ) . '>' . esc_html( $next_year ) . '</option>';
5520 echo '</select>-<select id="charged-month">';
5521 echo '<option value="0"' . selected( $charged_month, 0, false ) . '></option>';
5522 for ( $i = 1; $i <= 12; $i++ ) {
5523 $month = sprintf( '%02d', $i );
5524 echo '<option value="' . esc_html( $month ) . '"' . selected( $charged_month, $month, false ) . '>' . esc_html( $month ) . '</option>';
5525 }
5526 echo '</select>-<select id="charged-day">';
5527 echo '<option value="0"' . selected( $charged_day, 0, false ) . '></option>';
5528 for ( $i = 1; $i <= 31; $i++ ) {
5529 $day = sprintf( '%02d', $i );
5530 echo '<option value="' . esc_html( $day ) . '"' . selected( $charged_day, $day, false ) . '>' . esc_html( $day ) . '</option>';
5531 }
5532 echo '</select>';
5533 ?>
5534 </td>
5535 </tr>
5536 <tr>
5537 <th><?php esc_html_e( 'Amount on order', 'usces' ); ?></th>
5538 <td><?php usces_crform( $continue_data['order_price'], false ); ?></td>
5539 <th><?php esc_html_e( 'Transaction amount', 'usces' ); ?></th>
5540 <td><input type="text" class="amount" id="price" style="text-align: right;" value="<?php usces_crform( $continue_data['price'], false, false, '', false ); ?>"><?php usces_crcode(); ?></td>
5541 </tr>
5542 <tr>
5543 <th><?php esc_html_e( 'Billing Agreement ID', 'usces' ); ?></th>
5544 <?php $ba_id = $this->get_continuation_ba_id( $member_id, $order_id ); ?>
5545 <td colspan="3"><?php echo esc_html( $ba_id ); ?></td>
5546 </tr>
5547 <tr>
5548 <th><?php esc_html_e( 'Status', 'dlseller' ); ?></th>
5549 <td><select id="dlseller-status">
5550 <?php ob_start(); ?>
5551 <?php if ( 'continuation' === $continue_data['status'] ) : ?>
5552 <option value="continuation" selected><?php esc_html_e( 'Continuation', 'dlseller' ); ?></option>
5553 <option value="cancellation"><?php esc_html_e( 'Stop', 'dlseller' ); ?></option>
5554 <?php else : ?>
5555 <option value="cancellation" selected><?php esc_html_e( 'Cancellation', 'dlseller' ); ?></option>
5556 <option value="continuation"><?php esc_html_e( 'Resumption', 'dlseller' ); ?></option>
5557 <?php endif; ?>
5558 <?php
5559 $dlseller_status_options = ob_get_contents();
5560 ob_end_clean();
5561 $dlseller_status_options = apply_filters( 'usces_filter_continuation_charging_status_options', $dlseller_status_options, $continue_data );
5562 wel_esc_script_e( $dlseller_status_options );
5563 ?>
5564 </select></td>
5565 <td colspan="2"><input id="continuation-update" type="button" class="button button-primary" value="<?php esc_attr_e( 'Update' ); ?>" /></td>
5566 </tr>
5567 </table>
5568 <?php do_action( 'usces_action_continuation_charging_information', $continue_data, $member_id, $order_id ); ?>
5569 </td>
5570 </tr>
5571 </table>
5572 </div><!-- searchBox -->
5573 </div><!-- tablesearch -->
5574 <table id="mainDataTable" class="new-table order-new-table">
5575 <thead>
5576 <tr>
5577 <th scope="col">&nbsp;</th>
5578 <th scope="col"><?php esc_html_e( 'Processing date', 'usces' ); ?></th>
5579 <th scope="col"><?php esc_html_e( 'Trans ID', 'usces' ); ?></th>
5580 <th scope="col"><?php esc_html_e( 'Transaction amount', 'usces' ); ?></th>
5581 <th scope="col"><?php esc_html_e( 'Process', 'usces' ); ?></th>
5582 <th scope="col">&nbsp;</th>
5583 </tr>
5584 </thead>
5585 <?php
5586 foreach ( (array) $log_data as $data ) :
5587 $tracking_id = ( isset( $data['tracking_id'] ) ) ? $data['tracking_id'] : '';
5588 $latest_log = $this->get_acting_latest_log( $order_id, $tracking_id, 'ALL' );
5589 if ( $latest_log ) :
5590 $acting_status = $this->get_acting_status( $order_id, $tracking_id );
5591 if ( 'COMPLETED' === $latest_log['result'] ) {
5592 $class = ' paypal-' . strtolower( $acting_status );
5593 $amount = usces_crform( $this->get_latest_amount( $order_id, $tracking_id ), false, true, 'return', true );
5594 } else {
5595 $class = ' paypal-error';
5596 $amount = '';
5597 }
5598 $log = usces_unserialize( $latest_log['log'] );
5599 $id = ( isset( $log['id'] ) ) ? $log['id'] : '';
5600 if ( isset( $log['purchase_units'] ) ) {
5601 $purchase_units = $log['purchase_units'][0];
5602 if ( isset( $purchase_units['payments'] ) ) {
5603 if ( isset( $purchase_units['payments']['captures'] ) ) {
5604 $payments = $purchase_units['payments']['captures'][0];
5605 } elseif ( isset( $purchase_units['payments']['authorizations'] ) ) {
5606 $payments = $purchase_units['payments']['authorizations'][0];
5607 } elseif ( isset( $purchase_units['payments']['refunds'] ) ) {
5608 $payments = $purchase_units['payments']['refunds'][0];
5609 }
5610 if ( isset( $payments['id'] ) ) {
5611 $id = $payments['id'];
5612 }
5613 }
5614 }
5615 ?>
5616 <tbody>
5617 <tr>
5618 <td><?php echo esc_html( $num ); ?></td>
5619 <td><?php echo esc_html( $data['datetime'] ); ?></td>
5620 <td><?php echo esc_html( $id ); ?></td>
5621 <td class="amount"><?php echo esc_html( $amount ); ?></td>
5622 <td><span id="settlement-status-<?php echo esc_attr( $num ); ?>"><span class="acting-status<?php echo esc_attr( $class ); ?>"><?php echo esc_html__( $acting_status, 'usces' ); ?></span></span></td>
5623 <td>
5624 <input type="button" class="button settlement-information" data-tracking_id="<?php echo esc_attr( $tracking_id ); ?>" data-num="<?php echo esc_attr( $num ); ?>" value="<?php esc_attr_e( 'Settlement info', 'usces' ); ?>">
5625 </td>
5626 </tr>
5627 </tbody>
5628 <?php
5629 $num--;
5630 endif;
5631 endforeach;
5632 ?>
5633 </table>
5634 </div><!--datatable-->
5635 <input name="member_id" type="hidden" id="member_id" value="<?php echo esc_attr( $member_id ); ?>" />
5636 <input name="order_id" type="hidden" id="order_id" value="<?php echo esc_attr( $order_id ); ?>" />
5637 <input name="con_id" type="hidden" id="con_id" value="<?php echo esc_attr( $con_id ); ?>" />
5638 <input name="usces_referer" type="hidden" id="usces_referer" value="<?php echo urlencode( $curent_url ); ?>" />
5639 <?php wp_nonce_field( 'order_edit', 'wc_nonce' ); ?>
5640 </div><!--usces_admin-->
5641 </div><!--wrap-->
5642 <?php
5643 $order_action = 'edit';
5644 $cart = array();
5645 $action_args = compact( 'order_action', 'order_id', 'cart' );
5646 $this->settlement_dialog( $order_data, $action_args );
5647 include ABSPATH . 'wp-admin/admin-footer.php';
5648 }
5649
5650 /**
5651 * 自動継続課金処理
5652 * dlseller_action_do_continuation_charging
5653 *
5654 * @param string $today Today.
5655 * @param int $member_id Member ID.
5656 * @param int $order_id Order number.
5657 * @param array $continue_data Continuation data.
5658 */
5659 public function auto_continuation_charging( $today, $member_id, $order_id, $continue_data ) {
5660 global $usces;
5661
5662 if ( ! usces_is_membersystem_state() ) {
5663 return;
5664 }
5665
5666 if ( 0 >= $continue_data['price'] ) {
5667 return;
5668 }
5669
5670 $order_data = $usces->get_order_data( $order_id, 'direct' );
5671 if ( ! $order_data || $usces->is_status( 'cancel', $order_data['order_status'] ) ) {
5672 return;
5673 }
5674
5675 $payment = $usces->getPayments( $order_data['order_payment_name'] );
5676 if ( 'acting_paypal_cp' !== $payment['settlement'] || 'acting_paypal_cp' !== $continue_data['acting'] ) {
5677 return;
5678 }
5679
5680 $log_data = $this->get_acting_log( $order_id, 0, 'ALL' );
5681 if ( ! $log_data ) {
5682 return;
5683 }
5684
5685 $bn_code = self::API_BN_CODE_PCP;
5686 $ba_id = $this->get_continuation_ba_id( $member_id, $order_id );
5687 $tracking_id = usces_acting_key();
5688 if ( ! empty( $ba_id ) ) {
5689 $acting_opts = $this->get_acting_settings();
5690 $cp_intent = ( isset( $acting_opts['autobilling_intent'] ) ) ? $acting_opts['autobilling_intent'] : '';
5691
5692 /* Get Access Token */
5693 $access_token = $this->get_access_token();
5694
5695 $entry = array();
5696 $entry['order']['total_full_price'] = $continue_data['price'];
5697 $cart = usces_get_ordercartdata( $order_id );
5698 $shipping = usces_have_shipped( $cart );
5699 if ( $shipping ) {
5700 $delivery = unserialize( $order_data['order_delivery'] );
5701 $entry['delivery']['name1'] = $delivery['name1'];
5702 $entry['delivery']['name2'] = $delivery['name2'];
5703 $entry['delivery']['country'] = $delivery['country'];
5704 $entry['delivery']['zipcode'] = $delivery['zipcode'];
5705 $entry['delivery']['pref'] = $delivery['pref'];
5706 $entry['delivery']['address1'] = $delivery['address1'];
5707 $entry['delivery']['address2'] = $delivery['address2'];
5708 $entry['delivery']['address3'] = $delivery['address3'];
5709 }
5710
5711 /* Create order */
5712 $response_order_data = $this->api_create_order( $access_token, $bn_code, $cp_intent, $tracking_id, $entry, $cart );
5713 if ( isset( $response_order_data['id'] ) && isset( $response_order_data['status'] ) && 'CREATED' === $response_order_data['status'] ) {
5714 $resource_id = $response_order_data['id'];
5715 $correlation_id = '';
5716 $payment_source = array();
5717 $payment_source['token'] = array(
5718 'id' => $ba_id,
5719 'type' => 'BILLING_AGREEMENT',
5720 );
5721 dlseller_set_continuation_meta_value( 'resource_' . $tracking_id, $resource_id, $continue_data['con_id'] );
5722 if ( 'CAPTURE' === $cp_intent ) {
5723 /* Capture */
5724 $response_data = $this->api_capture_order( $access_token, $bn_code, $tracking_id, $resource_id, $correlation_id, $payment_source );
5725 } elseif ( 'AUTHORIZE' === $cp_intent ) {
5726 /* Authorize */
5727 $response_data = $this->api_authorize_order( $access_token, $bn_code, $tracking_id, $resource_id, $correlation_id, $payment_source );
5728 }
5729 if ( isset( $response_data['status'] ) ) {
5730 $status = $response_data['status'];
5731 } elseif ( isset( $response_data['name'] ) ) {
5732 $status = $response_data['name'];
5733 } else {
5734 $status = $cp_intent . ' ERROR';
5735 }
5736 $response_data = apply_filters( 'usces_filter_paypal_cp_auto_continuation_charging_log', $response_data, $order_id, $status );
5737 if ( 'COMPLETED' === $status && isset( $response_data['purchase_units'] ) ) {
5738 $this->save_acting_log( $response_data, 'paypal_cp', $cp_intent, $status, $continue_data['price'], $order_id, $tracking_id );
5739 // dlseller_set_continuation_meta_value( 'resource_' . $tracking_id, $resource_id, $continue_data['con_id'] );
5740 $purchase_units = $response_data['purchase_units'][0];
5741 if ( isset( $purchase_units['payments']['captures'] ) ) {
5742 $payments = $purchase_units['payments']['captures'][0];
5743 } elseif ( isset( $purchase_units['payments']['authorizations'] ) ) {
5744 $payments = $purchase_units['payments']['authorizations'][0];
5745 }
5746 if ( ! empty( $payments['id'] ) ) {
5747 dlseller_set_continuation_meta_value( 'trans_' . $tracking_id, $payments['id'], $continue_data['con_id'] ); /* 取引ID */
5748 }
5749 $response_data['acting'] = $this->paymod_id;
5750 $this->autobilling_email( $member_id, $order_id, $response_data, $continue_data );
5751 } else {
5752 $this->save_acting_log( $response_data, 'paypal_cp', 'ERROR', $status, 0, $order_id, $tracking_id );
5753 $this->save_entry_log( $tracking_id, $continue_data['price'], $cart, $entry );
5754 $log = array(
5755 'acting' => $this->paymod_id,
5756 'key' => $tracking_id,
5757 'result' => $status,
5758 'data' => $response_data,
5759 );
5760 usces_save_order_acting_error( $log );
5761 $this->autobilling_error_email( $member_id, $order_id, $response_data, $response_data );
5762 }
5763 do_action( 'usces_action_auto_continuation_charging', $member_id, $order_id, $continue_data, $response_data );
5764 } else {
5765 $this->save_entry_log( $tracking_id, $continue_data['price'], $cart, $entry );
5766 if ( isset( $response_order_data['status'] ) ) {
5767 $status = $response_order_data['status'];
5768 } elseif ( isset( $response_order_data['name'] ) ) {
5769 $status = $response_order_data['name'];
5770 } else {
5771 $status = 'CREATE ORDER ERROR';
5772 }
5773 $log = array(
5774 'acting' => $this->paymod_id,
5775 'key' => $tracking_id,
5776 'result' => $status,
5777 'data' => $response_order_data,
5778 );
5779 usces_save_order_acting_error( $log );
5780 $this->save_acting_log( $response_order_data, 'paypal_cp', 'ERROR', $status, 0, $order_id, $tracking_id );
5781 $this->autobilling_error_email( $member_id, $order_id, $response_order_data, $continue_data );
5782 do_action( 'usces_action_auto_continuation_charging', $member_id, $order_id, $continue_data, $response_order_data );
5783 }
5784 } else {
5785 $this->save_entry_log( $tracking_id, $continue_data['price'], $cart, $entry );
5786 $logdata = array();
5787 $log = array(
5788 'acting' => $this->paymod_id,
5789 'key' => $member_id,
5790 'result' => 'MEMBER ERROR',
5791 'data' => $logdata,
5792 );
5793 usces_save_order_acting_error( $log );
5794 $this->save_acting_log( $logdata, 'paypal_cp', 'ERROR', 'MEMBER ERROR', 0, $order_id, $tracking_id );
5795 $this->autobilling_error_email( $member_id, $order_id, $logdata, $continue_data );
5796 do_action( 'usces_action_auto_continuation_charging', $member_id, $order_id, $continue_data, $logdata );
5797 }
5798 }
5799
5800 /**
5801 * 自動継続課金処理メール(正常)
5802 *
5803 * @param int $member_id Member ID.
5804 * @param int $order_id Order number.
5805 * @param array $response_data Response data.
5806 * @param array $continue_data Continuation data.
5807 */
5808 public function autobilling_email( $member_id, $order_id, $response_data, $continue_data ) {
5809 global $usces;
5810
5811 $acting_opts = $this->get_acting_settings();
5812 $order_data = $usces->get_order_data( $order_id, 'direct' );
5813 $mail_body = $this->autobilling_message( $member_id, $order_id, $order_data, $response_data, $continue_data, false );
5814
5815 if ( 'on' === $acting_opts['autobilling_email'] ) {
5816 $subject = apply_filters( 'usces_filter_paypal_cp_autobilling_email_subject', __( 'Announcement of automatic continuing charging process', 'usces' ), $member_id, $order_id, $order_data, $response_data, $continue_data );
5817 $member_info = $usces->get_member_info( $member_id );
5818 $name = usces_localized_name( $member_info['mem_name1'], $member_info['mem_name2'], 'return' );
5819 $mail_data = usces_mail_data();
5820 $mail_header = '';
5821 if ( isset( $usces->options['put_customer_name'] ) && 1 === (int) $usces->options['put_customer_name'] ) {
5822 $mail_header .= sprintf( __( 'Dear %s', 'usces' ), $name ) . "\r\n\r\n";
5823 }
5824 $mail_header .= __( 'We will report automated accounting process was carried out as follows.', 'usces' ) . "\r\n\r\n";
5825 $mail_footer = __( 'If you have any questions, please contact us.', 'usces' ) . "\r\n\r\n" . $mail_data['footer']['thankyou'];
5826 $message = apply_filters( 'usces_filter_paypal_cp_autobilling_email_header', $mail_header, $member_id, $order_id, $order_data, $response_data, $continue_data ) .
5827 apply_filters( 'usces_filter_paypal_cp_autobilling_email_body', $mail_body, $member_id, $order_id, $order_data, $response_data, $continue_data ) .
5828 apply_filters( 'usces_filter_paypal_cp_autobilling_email_footer', $mail_footer, $member_id, $order_id, $order_data, $response_data, $continue_data );
5829 $headers = apply_filters( 'usces_filter_paypal_cp_autobilling_email_headers', '' );
5830 $to_customer = array(
5831 'to_name' => sprintf( _x( '%s', 'honorific', 'usces' ), $name ),
5832 'to_address' => $member_info['mem_email'],
5833 'from_name' => get_option( 'blogname' ),
5834 'from_address' => $usces->options['sender_mail'],
5835 'reply_name' => get_option( 'blogname' ),
5836 'reply_to' => usces_get_first_order_mail(),
5837 'return_path' => $usces->options['sender_mail'],
5838 'subject' => $subject,
5839 'message' => do_shortcode( $message ),
5840 'headers' => $headers,
5841 );
5842 usces_send_mail( $to_customer );
5843 }
5844
5845 $ok = ( empty( $this->continuation_charging_mail['OK'] ) ) ? 0 : $this->continuation_charging_mail['OK'];
5846 $this->continuation_charging_mail['OK'] = $ok + 1;
5847 $this->continuation_charging_mail['mail'][] = $mail_body;
5848 }
5849
5850 /**
5851 * 自動継続課金処理メール(エラー)
5852 *
5853 * @param int $member_id Member ID.
5854 * @param int $order_id Order number.
5855 * @param array $response_data Response data.
5856 * @param array $continue_data Continuation data.
5857 */
5858 public function autobilling_error_email( $member_id, $order_id, $response_data, $continue_data ) {
5859 global $usces;
5860
5861 $acting_opts = $this->get_acting_settings();
5862 $order_data = $usces->get_order_data( $order_id, 'direct' );
5863 $mail_body = $this->autobilling_message( $member_id, $order_id, $order_data, $response_data, $continue_data, false );
5864
5865 if ( 'on' === $acting_opts['autobilling_email'] ) {
5866 $subject = apply_filters( 'usces_filter_paypal_cp_autobilling_error_email_subject', __( 'Announcement of automatic continuing charging process', 'usces' ), $member_id, $order_id, $order_data, $response_data, $continue_data );
5867 $member_info = $usces->get_member_info( $member_id );
5868 $name = usces_localized_name( $member_info['mem_name1'], $member_info['mem_name2'], 'return' );
5869 $mail_data = usces_mail_data();
5870 $mail_header = '';
5871 if ( isset( $usces->options['put_customer_name'] ) && 1 === (int) $usces->options['put_customer_name'] ) {
5872 $mail_header .= sprintf( __( 'Dear %s', 'usces' ), $name ) . "\r\n\r\n";
5873 }
5874 $mail_header .= __( 'We will reported that an error occurred in automated accounting process.', 'usces' ) . "\r\n\r\n";
5875 $mail_footer = __( 'If you have any questions, please contact us.', 'usces' ) . "\r\n\r\n" . $mail_data['footer']['thankyou'];
5876 $message = apply_filters( 'usces_filter_paypal_cp_autobilling_error_email_header', $mail_header, $member_id, $order_id, $order_data, $response_data, $continue_data ) .
5877 apply_filters( 'usces_filter_paypal_cp_autobilling_error_email_body', $mail_body, $member_id, $order_id, $order_data, $response_data, $continue_data ) .
5878 apply_filters( 'usces_filter_paypal_cp_autobilling_error_email_footer', $mail_footer, $member_id, $order_id, $order_data, $response_data, $continue_data );
5879 $headers = apply_filters( 'usces_filter_paypal_cp_autobilling_error_email_headers', '' );
5880 $to_customer = array(
5881 'to_name' => sprintf( _x( '%s', 'honorific', 'usces' ), $name ),
5882 'to_address' => $member_info['mem_email'],
5883 'from_name' => get_option( 'blogname' ),
5884 'from_address' => $usces->options['sender_mail'],
5885 'reply_name' => get_option( 'blogname' ),
5886 'reply_to' => usces_get_first_order_mail(),
5887 'return_path' => $usces->options['sender_mail'],
5888 'subject' => $subject,
5889 'message' => do_shortcode( $message ),
5890 'headers' => $headers,
5891 );
5892 usces_send_mail( $to_customer );
5893 }
5894
5895 $error = ( empty( $this->continuation_charging_mail['NG'] ) ) ? 0 : $this->continuation_charging_mail['NG'];
5896 $this->continuation_charging_mail['NG'] = $error + 1;
5897 $this->continuation_charging_mail['mail'][] = $mail_body;
5898 }
5899
5900 /**
5901 * 自動継続課金処理メール本文
5902 *
5903 * @param int $member_id Member ID.
5904 * @param int $order_id Order number.
5905 * @param array $order_data Order data.
5906 * @param array $response_data Response data.
5907 * @param array $continue_data Continuation data.
5908 * @param boolean $html true|false.
5909 * @return string
5910 */
5911 public function autobilling_message( $member_id, $order_id, $order_data, $response_data, $continue_data, $html = true ) {
5912 global $usces;
5913
5914 if ( usces_is_html_mail() && $html ) {
5915 $message = $this->autobilling_message_htmlbody( $member_id, $order_id, $order_data, $response_data, $continue_data );
5916 } else {
5917 $member_info = $usces->get_member_info( $member_id );
5918 $name = usces_localized_name( $member_info['mem_name1'], $member_info['mem_name2'], 'return' );
5919 $contracted_date = ( isset( $continue_data['contractedday'] ) ) ? $continue_data['contractedday'] : '';
5920 $charged_date = ( isset( $continue_data['chargedday'] ) ) ? $continue_data['chargedday'] : '';
5921
5922 $message = usces_mail_line( 2 ); // --------------------
5923 $message .= __( 'Order ID', 'dlseller' ) . ' : ' . $order_id . "\r\n";
5924 $message .= __( 'Application Date', 'dlseller' ) . ' : ' . $order_data['order_date'] . "\r\n";
5925 $message .= __( 'Member ID', 'dlseller' ) . ' : ' . $member_id . "\r\n";
5926 $message .= __( 'Contractor name', 'dlseller' ) . ' : ' . sprintf( _x( '%s', 'honorific', 'usces' ), $name ) . "\r\n";
5927
5928 $cart = usces_get_ordercartdata( $order_id );
5929 $cart_row = current( $cart );
5930 $item_name = $usces->getCartItemName_byOrder( $cart_row );
5931 $options = ( empty( $cart_row['options'] ) ) ? array() : $cart_row['options'];
5932 $message .= __( 'Items', 'usces' ) . ' : ' . $item_name . "\r\n";
5933 if ( is_array( $options ) && 0 < count( $options ) ) {
5934 $optstr = '';
5935 foreach ( $options as $key => $value ) {
5936 if ( ! empty( $key ) ) {
5937 $key = urldecode( $key );
5938 $value = maybe_unserialize( $value );
5939 if ( is_array( $value ) ) {
5940 $c = '';
5941 $optstr .= '( ' . $key . ' : ';
5942 foreach ( $value as $v ) {
5943 $optstr .= $c . rawurldecode( $v );
5944 $c = ', ';
5945 }
5946 $optstr .= " )\r\n";
5947 } else {
5948 $optstr .= '( ' . $key . ' : ' . rawurldecode( $value ) . " )\r\n";
5949 }
5950 }
5951 }
5952 $message .= $optstr;
5953 }
5954
5955 $message .= __( 'Settlement amount', 'usces' ) . ' : ' . usces_crform( $continue_data['price'], true, false, 'return' ) . "\r\n";
5956 if ( isset( $response_data['reminder'] ) ) {
5957 if ( ! empty( $charged_date ) ) {
5958 $message .= __( 'Next Withdrawal Date', 'dlseller' ) . ' : ' . $charged_date . "\r\n";
5959 }
5960 if ( ! empty( $contracted_date ) ) {
5961 $message .= __( 'Renewal Date', 'dlseller' ) . ' : ' . $contracted_date . "\r\n";
5962 }
5963 } else {
5964 if ( ! empty( $charged_date ) ) {
5965 $message .= __( 'Next Withdrawal Date', 'dlseller' ) . ' : ' . $charged_date . "\r\n";
5966 }
5967 if ( ! empty( $contracted_date ) ) {
5968 $message .= __( 'Renewal Date', 'dlseller' ) . ' : ' . $contracted_date . "\r\n";
5969 }
5970 $message .= "\r\n";
5971 if ( isset( $response_data['status'] ) && 'COMPLETED' === $response_data['status'] ) {
5972 $message .= __( 'Result', 'usces' ) . ' : ' . __( 'Normal done', 'usces' ) . "\r\n";
5973 } elseif ( isset( $response_data['name'] ) ) {
5974 $message .= __( 'Result', 'usces' ) . ' : ' . $response_data['name'] . "\r\n";
5975 } else {
5976 $message .= __( 'Result', 'usces' ) . ' : ' . __( 'Error', 'usces' ) . "\r\n";
5977 }
5978 }
5979
5980 $ba_id = $this->get_continuation_ba_id( $member_id, $order_id );
5981 if ( ! empty( $ba_id ) ) {
5982 $message .= "\r\n" . __( 'Billing Agreement ID', 'usces' ) . ' : ' . $ba_id . "\r\n\r\n";
5983 }
5984
5985 $message .= usces_mail_line( 2 ) . "\r\n"; // --------------------
5986 }
5987 return $message;
5988 }
5989
5990 /**
5991 * Automatic renewal billing process email body html
5992 *
5993 * @param int $member_id Member ID.
5994 * @param int $order_id Order number.
5995 * @param array $order_data Order data.
5996 * @param array $response_data Response data.
5997 * @param array $continue_data Continuation data.
5998 * @return string
5999 */
6000 public function autobilling_message_htmlbody( $member_id, $order_id, $order_data, $response_data, $continue_data ) {
6001 global $usces;
6002
6003 $member_info = $usces->get_member_info( $member_id );
6004 $name = usces_localized_name( $member_info['mem_name1'], $member_info['mem_name2'], 'return' );
6005 $contracted_date = ( isset( $continue_data['contractedday'] ) ) ? $continue_data['contractedday'] : '';
6006 $charged_date = ( isset( $continue_data['chargedday'] ) ) ? $continue_data['chargedday'] : '';
6007
6008 $message = '<table style="font-size: 14px; margin-bottom: 30px; width: 100%; border-collapse: collapse; border: 1px solid #ddd;"><tbody>';
6009 $message .= '<tr><td style="background-color: #f9f9f9; padding: 12px; width: 33%; border: 1px solid #ddd; text-align: left;">';
6010 $message .= __( 'Order ID', 'dlseller' );
6011 $message .= '</td><td style="padding: 12px; width: 67%; border: 1px solid #ddd;">';
6012 $message .= $order_id;
6013 $message .= '</td></tr>';
6014
6015 $message .= '<tr><td style="background-color: #f9f9f9; padding: 12px; width: 33%; border: 1px solid #ddd; text-align: left;">';
6016 $message .= __( 'Application Date', 'dlseller' );
6017 $message .= '</td><td style="padding: 12px; width: 67%; border: 1px solid #ddd;">';
6018 $message .= $order_data['order_date'];
6019 $message .= '</td></tr>';
6020
6021 $message .= '<tr><td style="background-color: #f9f9f9; padding: 12px; width: 33%; border: 1px solid #ddd; text-align: left;">';
6022 $message .= __( 'Member ID', 'dlseller' );
6023 $message .= '</td><td style="padding: 12px; width: 67%; border: 1px solid #ddd;">';
6024 $message .= $member_id;
6025 $message .= '</td></tr>';
6026
6027 $message .= '<tr><td style="background-color: #f9f9f9; padding: 12px; width: 33%; border: 1px solid #ddd; text-align: left;">';
6028 $message .= __( 'Contractor name', 'dlseller' );
6029 $message .= '</td><td style="padding: 12px; width: 67%; border: 1px solid #ddd;">';
6030 $message .= sprintf( _x( '%s', 'honorific', 'usces' ), $name );
6031 $message .= '</td></tr>';
6032
6033 $cart = usces_get_ordercartdata( $order_id );
6034 $cart_row = current( $cart );
6035 $item_name = $usces->getCartItemName_byOrder( $cart_row );
6036 $options = ( empty( $cart_row['options'] ) ) ? array() : $cart_row['options'];
6037 $message .= '<tr><td style="background-color: #f9f9f9; padding: 12px; width: 33%; border: 1px solid #ddd; text-align: left;">';
6038 $message .= __( 'Items', 'usces' );
6039 $message .= '</td><td style="padding: 12px; width: 67%; border: 1px solid #ddd;">';
6040 $message .= $item_name;
6041 if ( is_array( $options ) && 0 < count( $options ) ) {
6042 $optstr = '';
6043 foreach ( $options as $key => $value ) {
6044 if ( ! empty( $key ) ) {
6045 $key = urldecode( $key );
6046 $value = maybe_unserialize( $value );
6047 if ( is_array( $value ) ) {
6048 $c = '';
6049 $optstr .= '( ' . $key . ' : ';
6050 foreach ( $value as $v ) {
6051 $optstr .= $c . rawurldecode( $v );
6052 $c = ', ';
6053 }
6054 $optstr .= ' )<br>';
6055 } else {
6056 $optstr .= '( ' . $key . ' : ' . rawurldecode( $value ) . ' )<br>';
6057 }
6058 }
6059 }
6060 $message .= $optstr;
6061 }
6062 $message .= '</td></tr>';
6063
6064 $message .= '<tr><td style="background-color: #f9f9f9; padding: 12px; width: 33%; border: 1px solid #ddd; text-align: left;">';
6065 $message .= __( 'Settlement amount', 'usces' );
6066 $message .= '</td><td style="padding: 12px; width: 67%; border: 1px solid #ddd;">';
6067 $message .= usces_crform( $continue_data['price'], true, false, 'return' );
6068 $message .= '</td></tr>';
6069 if ( isset( $response_data['reminder'] ) ) {
6070 if ( ! empty( $charged_date ) ) {
6071 $message .= '<tr><td style="background-color: #f9f9f9; padding: 12px; width: 33%; border: 1px solid #ddd; text-align: left;">';
6072 $message .= __( 'Next Withdrawal Date', 'dlseller' );
6073 $message .= '</td><td style="padding: 12px; width: 67%; border: 1px solid #ddd;">';
6074 $message .= $charged_date;
6075 $message .= '</td></tr>';
6076 }
6077 if ( ! empty( $contracted_date ) ) {
6078 $message .= '<tr><td style="background-color: #f9f9f9; padding: 12px; width: 33%; border: 1px solid #ddd; text-align: left;">';
6079 $message .= __( 'Renewal Date', 'dlseller' );
6080 $message .= '</td><td style="padding: 12px; width: 67%; border: 1px solid #ddd;">';
6081 $message .= $contracted_date;
6082 $message .= '</td></tr>';
6083 }
6084 } else {
6085 if ( ! empty( $charged_date ) ) {
6086 $message .= '<tr><td style="background-color: #f9f9f9; padding: 12px; width: 33%; border: 1px solid #ddd; text-align: left;">';
6087 $message .= __( 'Next Withdrawal Date', 'dlseller' );
6088 $message .= '</td><td style="padding: 12px; width: 67%; border: 1px solid #ddd;">';
6089 $message .= $charged_date;
6090 $message .= '</td></tr>';
6091 }
6092 if ( ! empty( $contracted_date ) ) {
6093 $message .= '<tr><td style="background-color: #f9f9f9; padding: 12px; width: 33%; border: 1px solid #ddd; text-align: left;">';
6094 $message .= __( 'Renewal Date', 'dlseller' );
6095 $message .= '</td><td style="padding: 12px; width: 67%; border: 1px solid #ddd;">';
6096 $message .= $contracted_date;
6097 $message .= '</td></tr>';
6098 }
6099 if ( isset( $response_data['status'] ) && 'COMPLETED' === $response_data['status'] ) {
6100 $message .= '<tr><td style="background-color: #f9f9f9; padding: 12px; width: 33%; border: 1px solid #ddd; text-align: left;">';
6101 $message .= __( 'Result', 'usces' );
6102 $message .= '</td><td style="padding: 12px; width: 67%; border: 1px solid #ddd;">';
6103 $message .= __( 'Normal done', 'usces' );
6104 $message .= '</td></tr>';
6105 } elseif ( isset( $response_data['name'] ) ) {
6106 $message .= '<tr><td style="background-color: #f9f9f9; padding: 12px; width: 33%; border: 1px solid #ddd; text-align: left;">';
6107 $message .= __( 'Result', 'usces' );
6108 $message .= '</td><td style="padding: 12px; width: 67%; border: 1px solid #ddd;">';
6109 $message .= $response_data['name'];
6110 $message .= '</td></tr>';
6111 } else {
6112 $message .= '<tr><td style="background-color: #f9f9f9; padding: 12px; width: 33%; border: 1px solid #ddd; text-align: left;">';
6113 $message .= __( 'Result', 'usces' );
6114 $message .= '</td><td style="padding: 12px; width: 67%; border: 1px solid #ddd;">';
6115 $message .= __( 'Error', 'usces' );
6116 $message .= '</td></tr>';
6117 }
6118 }
6119
6120 $ba_id = $this->get_continuation_ba_id( $member_id, $order_id );
6121 if ( ! empty( $ba_id ) ) {
6122 $message .= '<tr><td style="background-color: #f9f9f9; padding: 12px; width: 33%; border: 1px solid #ddd; text-align: left;">';
6123 $message .= __( 'Billing Agreement ID', 'usces' );
6124 $message .= '</td><td style="padding: 12px; width: 67%; border: 1px solid #ddd;">';
6125 $message .= $ba_id;
6126 $message .= '</td></tr>';
6127 }
6128 $message .= '</tbody></table>';
6129 return $message;
6130 }
6131
6132 /**
6133 * 自動継続課金処理
6134 * dlseller_action_do_continuation
6135 *
6136 * @param string $today Today.
6137 * @param array $todays_charging Charged data.
6138 */
6139 public function do_auto_continuation( $today, $todays_charging ) {
6140 global $usces;
6141
6142 if ( empty( $todays_charging ) ) {
6143 return;
6144 }
6145
6146 $ok = ( empty( $this->continuation_charging_mail['OK'] ) ) ? 0 : $this->continuation_charging_mail['OK'];
6147 $error = ( empty( $this->continuation_charging_mail['NG'] ) ) ? 0 : $this->continuation_charging_mail['NG'];
6148 $admin_subject = apply_filters( 'usces_filter_paypal_cp_autobilling_email_admin_subject', __( 'Automatic Continuing Charging Process Result', 'usces' ) . ' ' . $today, $today );
6149 $admin_footer = apply_filters( 'usces_filter_paypal_cp_autobilling_email_admin_mail_footer', __( 'For details, please check on the administration panel > Continuous charge member list > Continuous charge member information.', 'usces' ) );
6150 $admin_message = __( 'Report that automated accounting process has been completed.', 'usces' ) . "\r\n\r\n"
6151 . __( 'Processing date', 'usces' ) . ' : ' . date_i18n( 'Y-m-d H:i:s', current_time( 'timestamp' ) ) . "\r\n"
6152 . __( 'Normal done', 'usces' ) . ' : ' . $ok . "\r\n"
6153 . __( 'Abnormal done', 'usces' ) . ' : ' . $error . "\r\n\r\n";
6154 foreach ( (array) $this->continuation_charging_mail['mail'] as $mail ) {
6155 $admin_message .= $mail . "\r\n";
6156 }
6157 $admin_message .= $admin_footer . "\r\n";
6158
6159 $to_admin = array(
6160 'to_name' => apply_filters( 'usces_filter_bccmail_to_admin_name', 'Shop Admin' ),
6161 'to_address' => $usces->options['order_mail'],
6162 'from_name' => apply_filters( 'usces_filter_bccmail_from_admin_name', 'Welcart Auto BCC' ),
6163 'from_address' => $usces->options['sender_mail'],
6164 'reply_name' => get_option( 'blogname' ),
6165 'reply_to' => usces_get_first_order_mail(),
6166 'return_path' => $usces->options['sender_mail'],
6167 'subject' => $admin_subject,
6168 'message' => do_shortcode( $admin_message ),
6169 );
6170 usces_send_mail( $to_admin );
6171 unset( $this->continuation_charging_mail );
6172 }
6173
6174 /**
6175 * 課金日通知メール
6176 * dlseller_filter_reminder_mail_body
6177 *
6178 * @param string $mail_body Message body.
6179 * @param int $order_id Order number.
6180 * @param array $continue_data Continuation data.
6181 * @return string
6182 */
6183 public function reminder_mail_body( $mail_body, $order_id, $continue_data ) {
6184 global $usces;
6185
6186 $member_id = $continue_data['member_id'];
6187 $order_id = $continue_data['order_id'];
6188 $order_data = $usces->get_order_data( $order_id, 'direct' );
6189 $response_data = array( 'reminder' => 'reminder' );
6190 $mail_body = $this->autobilling_message( $member_id, $order_id, $order_data, $response_data, $continue_data );
6191 return $mail_body;
6192 }
6193
6194 /**
6195 * 契約更新日通知メール
6196 * dlseller_filter_contract_renewal_mail_body
6197 *
6198 * @param string $mail_body Message body.
6199 * @param int $order_id Order number.
6200 * @param array $continue_data Continuation data.
6201 * @return string
6202 */
6203 public function contract_renewal_mail_body( $mail_body, $order_id, $continue_data ) {
6204 global $usces;
6205
6206 $member_id = $continue_data['member_id'];
6207 $order_data = $usces->get_order_data( $order_id, 'direct' );
6208 $response_data = array( 'reminder' => 'contract_renewal' );
6209 $mail_body = $this->autobilling_message( $member_id, $order_id, $order_data, $response_data, $continue_data );
6210 return $mail_body;
6211 }
6212
6213 /**
6214 * メール支払方法
6215 * usces_filter_order_confirm_mail_payment
6216 * usces_filter_send_order_mail_payment
6217 *
6218 * @param string $msg_payment Message body.
6219 * @param int $order_id Order number.
6220 * @param array $payment Payment data.
6221 * @param array $cart Cart data.
6222 * @return string
6223 */
6224 public function continue_order_mail_payment( $msg_payment, $order_id, $payment, $cart ) {
6225 global $usces;
6226
6227 $post_id = $cart[0]['post_id'];
6228 $charging_type = $usces->getItemChargingType( $post_id );
6229 if ( 'continue' !== $charging_type ) {
6230 return $msg_payment;
6231 }
6232
6233 $acting_flg = ( isset( $payment['settlement'] ) ) ? $payment['settlement'] : '';
6234 if ( 'acting_paypal_cp' !== $acting_flg ) {
6235 return $msg_payment;
6236 }
6237
6238 $member_id = $this->get_member_id( $order_id );
6239 $ba_id = $this->get_continuation_ba_id( $member_id, $order_id );
6240 if ( ! empty( $ba_id ) ) {
6241 $msg_payment .= __( 'Billing Agreement ID', 'usces' ) . ' : ' . $ba_id . "\r\n\r\n";
6242 }
6243 return $msg_payment;
6244 }
6245
6246 /**
6247 * 継続課金会員データ取得
6248 *
6249 * @param int $member_id Member ID.
6250 * @param int $order_id Order number.
6251 * @return array
6252 */
6253 private function get_continuation_data( $member_id, $order_id ) {
6254 global $wpdb;
6255
6256 $query = $wpdb->prepare(
6257 "SELECT
6258 `con_id` AS `con_id`,
6259 `con_acting` AS `acting`,
6260 `con_order_price` AS `order_price`,
6261 `con_price` AS `price`,
6262 `con_next_charging` AS `chargedday`,
6263 `con_next_contracting` AS `contractedday`,
6264 `con_startdate` AS `startdate`,
6265 `con_status` AS `status`
6266 FROM {$wpdb->prefix}usces_continuation
6267 WHERE `con_order_id` = %d AND `con_member_id` = %d",
6268 $order_id,
6269 $member_id
6270 );
6271 $data = $wpdb->get_row( $query, ARRAY_A );
6272 return $data;
6273 }
6274
6275 /**
6276 * 継続課金会員データ更新
6277 *
6278 * @param int $member_id Member ID.
6279 * @param int $order_id Order number.
6280 * @param array $data Continuation data.
6281 * @param boolean $stop Stop continuous billing.
6282 * @return boolean
6283 */
6284 private function update_continuation_data( $member_id, $order_id, $data, $stop = false ) {
6285 global $wpdb;
6286
6287 if ( $stop ) {
6288 $query = $wpdb->prepare(
6289 "UPDATE {$wpdb->prefix}usces_continuation SET
6290 `con_status` = 'cancellation'
6291 WHERE `con_order_id` = %d AND `con_member_id` = %d",
6292 $order_id,
6293 $member_id
6294 );
6295 } else {
6296 $query = $wpdb->prepare(
6297 "UPDATE {$wpdb->prefix}usces_continuation SET
6298 `con_price` = %f,
6299 `con_next_charging` = %s,
6300 `con_next_contracting` = %s,
6301 `con_status` = %s
6302 WHERE `con_order_id` = %d AND `con_member_id` = %d",
6303 $data['price'],
6304 $data['chargedday'],
6305 $data['contractedday'],
6306 $data['status'],
6307 $order_id,
6308 $member_id
6309 );
6310 }
6311 $res = $wpdb->query( $query );
6312 return $res;
6313 }
6314
6315 /**
6316 * 継続課金 ba_id 保存
6317 *
6318 * @param int $member_id Member ID.
6319 * @param int $order_id Order number.
6320 * @param string $ba_id Billing Agreement ID.
6321 */
6322 private function set_continuation_ba_id( $member_id, $order_id, $ba_id ) {
6323 global $usces;
6324
6325 $usces->set_member_meta_value( 'paypal_continuation_' . $order_id, $ba_id, $member_id );
6326 }
6327
6328 /**
6329 * 継続課金 ba_id 取得
6330 *
6331 * @param int $member_id Member ID.
6332 * @param int $order_id Order number.
6333 * @return string
6334 */
6335 private function get_continuation_ba_id( $member_id, $order_id = 0 ) {
6336 global $usces, $wpdb;
6337
6338 if ( empty( $order_id ) ) {
6339 $member_meta_table = usces_get_tablename( 'usces_member_meta' );
6340 $query = $wpdb->prepare( "SELECT COUNT(*) FROM {$member_meta_table} WHERE `member_id` = %d AND `meta_key` LIKE %s", $member_id, 'paypal_continuation_%' );
6341 $ba_id = $wpdb->get_var( $query );
6342 } else {
6343 $ba_id = $usces->get_member_meta_value( 'paypal_continuation_' . $order_id, $member_id );
6344 }
6345 return $ba_id;
6346 }
6347
6348 /**
6349 * 継続課金決済金額取得
6350 *
6351 * @param int $con_id Continuation ID.
6352 * @return float
6353 */
6354 private function get_continuation_amount( $con_id ) {
6355 global $wpdb;
6356
6357 $amount = $wpdb->get_var( $wpdb->prepare( "SELECT `con_order_price` FROM {$wpdb->prefix}usces_continuation WHERE `con_id` = %d", $con_id ) );
6358 return $amount;
6359 }
6360
6361 /**
6362 * 定期購�
6363 �データ登録
6364 * wcad_action_reg_regulardata
6365 *
6366 * @param array $args Compact array( $cart, $entry, $order_id, $member_id, $payments, $charging_type, $results、$reg_id ).
6367 */
6368 public function register_regulardata( $args ) {
6369 global $usces;
6370 extract( $args );
6371
6372 if ( ! empty( $reg_id ) && ! empty( $member_id ) && ! empty( $results['ba_id'] ) ) {
6373 $this->set_regular_ba_id( $member_id, $reg_id, $results['ba_id'] );
6374 }
6375 }
6376
6377 /**
6378 * 発送�
6379 �リスト利用可能決済
6380 * wcad_filter_shippinglist_acting
6381 *
6382 * @param string $acting Payment method.
6383 * @return string
6384 */
6385 public function set_shippinglist_acting( $acting ) {
6386 $acting = 'acting_paypal_cp';
6387 return $acting;
6388 }
6389
6390 /**
6391 * 管理画面利用可能決済メッセージ
6392 * wcad_filter_available_regular_payment_method
6393 *
6394 * @param array $payment_method Payment method.
6395 * @return array
6396 */
6397 public function available_regular_payment_method( $payment_method ) {
6398 $payment_method[] = 'acting_paypal_cp';
6399 return $payment_method;
6400 }
6401
6402 /**
6403 * 定期購�
6404 �決済処理
6405 * wcad_action_reg_auto_orderdata
6406 *
6407 * @param array $args Compact array( $cart, $entry, $order_id, $member_id, $payments, $charging_type, $total_amount, $reg_id, $order_date ).
6408 */
6409 public function register_auto_orderdata( $args ) {
6410 global $usces;
6411 extract( $args );
6412
6413 $acting_flg = $payments['settlement'];
6414 if ( 'acting_paypal_cp' !== $payments['settlement'] ) {
6415 return;
6416 }
6417
6418 if ( ! usces_is_membersystem_state() ) {
6419 return;
6420 }
6421
6422 if ( 0 >= $total_amount ) {
6423 return;
6424 }
6425
6426 $bn_code = self::API_BN_CODE_PCP;
6427 $ba_id = $this->get_regular_ba_id( $member_id, $reg_id );
6428 $tracking_id = usces_acting_key();
6429 $usces->set_order_meta_value( 'reference_id', $tracking_id, $order_id );
6430 $settltment_errmsg = '';
6431
6432 if ( ! empty( $ba_id ) ) {
6433 $acting_opts = $this->get_acting_settings();
6434 $cp_intent = ( isset( $acting_opts['intent'] ) ) ? $acting_opts['intent'] : '';
6435
6436 /* Get Access Token */
6437 $access_token = $this->get_access_token();
6438
6439 $shipping = usces_have_shipped( $cart );
6440 if ( $shipping ) {
6441 $regular_data = $this->get_regular_data( $member_id, $reg_id );
6442 $delivery = (array) unserialize( $regular_data['reg_delivery'] );
6443 $entry['delivery']['name1'] = $delivery['name1'];
6444 $entry['delivery']['name2'] = $delivery['name2'];
6445 $entry['delivery']['country'] = $delivery['country'];
6446 $entry['delivery']['zipcode'] = $delivery['zipcode'];
6447 $entry['delivery']['pref'] = $delivery['pref'];
6448 $entry['delivery']['address1'] = $delivery['address1'];
6449 $entry['delivery']['address2'] = $delivery['address2'];
6450 $entry['delivery']['address3'] = $delivery['address3'];
6451 }
6452 $entry['order']['total_full_price'] = usces_crform( $total_amount, false, false, 'return', false );
6453
6454 /* Create order */
6455 $response_order_data = $this->api_create_order( $access_token, $bn_code, $cp_intent, $tracking_id, $entry, $cart );
6456 if ( isset( $response_order_data['id'] ) && isset( $response_order_data['status'] ) && 'CREATED' === $response_order_data['status'] ) {
6457 $resource_id = $response_order_data['id'];
6458 $correlation_id = '';
6459 $payment_source = array();
6460 $payment_source['token'] = array(
6461 'id' => $ba_id,
6462 'type' => 'BILLING_AGREEMENT',
6463 );
6464 if ( 'CAPTURE' === $cp_intent ) {
6465 /* Capture */
6466 $response_data = $this->api_capture_order( $access_token, $bn_code, $tracking_id, $resource_id, $correlation_id, $payment_source );
6467 } elseif ( 'AUTHORIZE' === $cp_intent ) {
6468 /* Authorize */
6469 $response_data = $this->api_authorize_order( $access_token, $bn_code, $tracking_id, $resource_id, $correlation_id, $payment_source );
6470 }
6471 if ( isset( $response_data['status'] ) ) {
6472 $status = $response_data['status'];
6473 } elseif ( isset( $response_data['name'] ) ) {
6474 $status = $response_data['name'];
6475 } else {
6476 $status = $cp_intent . ' ERROR';
6477 }
6478 $response_data = apply_filters( 'usces_filter_paypal_cp_register_auto_orderdata_log', $response_data, $status, $args );
6479 if ( 'COMPLETED' === $status && isset( $response_data['purchase_units'] ) ) {
6480 $this->save_acting_log( $response_data, 'paypal_cp', $cp_intent, $status, $total_amount, $order_id, $tracking_id );
6481 $usces->set_order_meta_value( 'resource_id', $resource_id, $order_id );
6482 $purchase_units = $response_data['purchase_units'][0];
6483 if ( isset( $purchase_units['payments']['captures'] ) ) {
6484 $payments = $purchase_units['payments']['captures'][0];
6485 } elseif ( isset( $purchase_units['payments']['authorizations'] ) ) {
6486 $payments = $purchase_units['payments']['authorizations'][0];
6487 }
6488 if ( ! empty( $payments['id'] ) ) {
6489 $usces->set_order_meta_value( 'wc_trans_id', $payments['id'], $order_id ); /* 決済ID */
6490 $usces->set_order_meta_value( 'trans_id', $payments['id'], $order_id ); /* 取引ID */
6491 }
6492 $response_data['acting'] = $this->paymod_id;
6493 $usces->set_order_meta_value( $acting_flg, usces_serialize( $response_data ), $order_id );
6494 } else {
6495 $this->save_acting_log( $response_data, 'paypal_cp', 'ERROR', $status, 0, $order_id, $tracking_id );
6496 $this->save_entry_log( $tracking_id, $total_amount, $cart, $entry );
6497 $settltment_errmsg = __( '[Regular purchase] Settlement was not completed.', 'autodelivery' );
6498 $log = array(
6499 'acting' => $this->paymod_id,
6500 'key' => $tracking_id,
6501 'result' => $status,
6502 'data' => $response_data,
6503 );
6504 usces_save_order_acting_error( $log );
6505 }
6506 do_action( 'usces_action_register_auto_orderdata', $args, $response_data );
6507 } else {
6508 $this->save_entry_log( $tracking_id, $total_amount, $cart, $entry );
6509 $settltment_errmsg = __( '[Regular purchase] Settlement was not completed.', 'autodelivery' );
6510 if ( isset( $response_order_data['status'] ) ) {
6511 $status = $response_order_data['status'];
6512 } elseif ( isset( $response_order_data['name'] ) ) {
6513 $status = $response_order_data['name'];
6514 } else {
6515 $status = 'CREATE ORDER ERROR';
6516 }
6517 $log = array(
6518 'acting' => $this->paymod_id,
6519 'key' => $tracking_id,
6520 'result' => $status,
6521 'data' => $response_order_data,
6522 );
6523 usces_save_order_acting_error( $log );
6524 $this->save_acting_log( $response_order_data, 'paypal_cp', 'ERROR', $status, 0, $order_id, $tracking_id );
6525 do_action( 'usces_action_register_auto_orderdata', $args, $response_order_data );
6526 }
6527 } else {
6528 $this->save_entry_log( $tracking_id, $total_amount, $cart, $entry );
6529 $settltment_errmsg = __( '[Regular purchase] Member information acquisition error.', 'autodelivery' );
6530 $logdata = array( 'ba_id' => $ba_id );
6531 $log = array(
6532 'acting' => $this->paymod_id,
6533 'key' => $member_id,
6534 'result' => 'MEMBER ERROR',
6535 'data' => $logdata,
6536 );
6537 usces_save_order_acting_error( $log );
6538 $this->save_acting_log( $logdata, 'paypal_cp', 'ERROR', 'MEMBER ERROR', 0, $order_id, $tracking_id );
6539 do_action( 'usces_action_register_auto_orderdata', $args, $log );
6540 }
6541 if ( '' !== $settltment_errmsg ) {
6542 $settlement = array(
6543 'settltment_status' => __( 'Failure', 'autodelivery' ),
6544 'settltment_errmsg' => $settltment_errmsg,
6545 );
6546 $usces->set_order_meta_value( $acting_flg, usces_serialize( $settlement ), $order_id );
6547 wcad_settlement_error_mail( $order_id, $settltment_errmsg );
6548 }
6549 }
6550
6551 /**
6552 * 自動受注決済エラーメールヘッダー
6553 * wcad_filter_send_settlement_error_mail_message_head
6554 *
6555 * @param string $mail_header Message header.
6556 * @param array $order_data Order data.
6557 * @return string
6558 */
6559 public function settlement_error_mail_message_header( $mail_header, $order_data ) {
6560 $payment = usces_get_payments_by_name( $order_data['order_payment_name'] );
6561 $acting_flg = ( isset( $payment['settlement'] ) ) ? $payment['settlement'] : '';
6562 if ( 'acting_paypal_cp' === $acting_flg ) {
6563 $mail_header = __( 'Settlement error has occurred in the regular purchase. Please check your PayPal account for any problems.', 'usces' ) . "\r\n\r\n";
6564 }
6565 return $mail_header;
6566 }
6567
6568 /**
6569 * 自動受注決済エラーメール本文
6570 * wcad_filter_send_settlement_error_mail_message
6571 *
6572 * @param string $mail_body Message body.
6573 * @param array $order_data Order data.
6574 * @param string $errmsg Error message.
6575 * @return string
6576 */
6577 public function settlement_error_mail_message( $mail_body, $order_data, $errmsg ) {
6578 $payment = usces_get_payments_by_name( $order_data['order_payment_name'] );
6579 $acting_flg = ( isset( $payment['settlement'] ) ) ? $payment['settlement'] : '';
6580 if ( 'acting_paypal_cp' === $acting_flg ) {
6581 $name = usces_localized_name( $order_data['order_name1'], $order_data['order_name2'], 'return' );
6582 $order_number = usces_get_deco_order_id( $order_data['ID'] );
6583 if ( usces_is_html_mail() ) {
6584 $hook_args = compact( 'order_data', 'name', 'order_number', 'errmsg' );
6585 $mail_body = $this->settlement_error_mail_message_htmlbody( $hook_args );
6586 } else {
6587 $mail_body = '--------------------------------' . "\r\n";
6588 $mail_body .= __( 'Member ID', 'usces' ) . ' : ' . $order_data['mem_id'] . "\r\n";
6589 $mail_body .= __( 'Name', 'usces' ) . ' : ' . sprintf( _x( '%s', 'honorific', 'usces' ), $name ) . "\r\n";
6590 $mail_body .= __( 'e-mail adress', 'usces' ) . ' : ' . $order_data['order_email'] . "\r\n";
6591 $mail_body .= __( 'Automatic order number', 'autodelivery' ) . ' : ' . $order_number . "\r\n";
6592 $mail_body .= $errmsg . "\r\n";
6593 $mail_body .= '--------------------------------' . "\r\n\r\n";
6594 $mail_body .= __( 'If you have not requested this email, sorry to trouble you, but please contact us.', 'usces' ) . "\r\n\r\n";
6595 }
6596 }
6597 return $mail_body;
6598 }
6599
6600 /**
6601 * Generate html for the body of settlement error mail
6602 *
6603 * @param array $hook_args : compact( 'order_data', 'name', 'order_number', 'errmsg' ).
6604 *
6605 * @return text : mail body
6606 */
6607 public function settlement_error_mail_message_htmlbody( $hook_args ) {
6608 extract( $hook_args );
6609
6610 $mail_body = '<table style="font-size: 14px; margin-bottom: 40px; width: 100%; border-collapse: collapse; border: 1px solid #ddd;">';
6611 $mail_body .= '<caption style="background-color: #111; margin-bottom: 20px; padding: 15px; color: #fff; font-size: 15px; font-weight: 700; text-align: left;">';
6612 $mail_body .= remove_query_arg( 'uscesid', usces_url( 'login', 'return' ) );
6613 $mail_body .= '</caption><tbody>';
6614 $mail_body .= '<tr>';
6615 $mail_body .= '<td style="background-color: #f9f9f9; padding: 12px; width: 33%; border: 1px solid #ddd; text-align: left;">' . __( 'Member ID', 'usces' ) . '</td>';
6616 $mail_body .= '<td style="padding: 12px; width: 67%; border: 1px solid #ddd;">' . esc_attr( $order_data['mem_id'] ) . '</td>';
6617 $mail_body .= '</tr>';
6618 $mail_body .= '<tr>';
6619 $mail_body .= '<td style="background-color: #f9f9f9; padding: 12px; width: 33%; border: 1px solid #ddd; text-align: left;">' . __( 'Name', 'usces' ) . '</td>';
6620 $mail_body .= '<td style="padding: 12px; width: 67%; border: 1px solid #ddd;">' . sprintf( _x( '%s', 'honorific', 'usces' ), $name ) . '</td>';
6621 $mail_body .= '</tr>';
6622 $mail_body .= '<tr>';
6623 $mail_body .= '<td style="background-color: #f9f9f9; padding: 12px; width: 33%; border: 1px solid #ddd; text-align: left;">' . __( 'e-mail adress', 'usces' ) . '</td>';
6624 $mail_body .= '<td style="padding: 12px; width: 67%; border: 1px solid #ddd;">' . esc_attr( $order_data['order_email'] ) . '</td>';
6625 $mail_body .= '</tr>';
6626 $mail_body .= '<tr>';
6627 $mail_body .= '<td style="background-color: #f9f9f9; padding: 12px; width: 33%; border: 1px solid #ddd; text-align: left;">' . __( 'Automatic order number', 'autodelivery' ) . '</td>';
6628 $mail_body .= '<td style="padding: 12px; width: 67%; border: 1px solid #ddd;">' . esc_attr( $order_number ) . '</td>';
6629 $mail_body .= '</tr>';
6630 if ( ! empty( $errmsg ) ) {
6631 $mail_body .= '<tr>';
6632 $mail_body .= '<td style="background-color: #f9f9f9; padding: 12px; width: 33%; border: 1px solid #ddd; text-align: left;">' . __( 'Settlement error message', 'usces' ) . '</td>';
6633 $mail_body .= '<td style="padding: 12px; width: 67%; border: 1px solid #ddd;">' . esc_html( $errmsg ) . '</td>';
6634 $mail_body .= '</tr>';
6635 }
6636 $mail_body .= '</tbody></table>';
6637
6638 $mail_body .= '<table style="font-size:15px; margin-bottom: 10px;" border="0" width="540" cellspacing="0" cellpadding="0" align="center" bgcolor="#ffffff">';
6639 $mail_body .= '<tbody><tr>';
6640 $mail_body .= '<td>' . __( 'If you have not requested this email, sorry to trouble you, but please contact us.', 'usces' ) . '</td>';
6641 $mail_body .= '</tr>';
6642 $mail_body .= '</tbody></table>';
6643 $mail_body .= '<hr style="margin: 50px 0 0; border-style: none; border-top: 3px solid #777;" />';
6644
6645 return $mail_body;
6646 }
6647
6648 /**
6649 * 定期購�
6650 �データ編集画面
6651 * wcad_action_regular_information_edit_form
6652 *
6653 * @param array $regular_order Regular data.
6654 */
6655 public function regular_information_edit_form( $regular_order ) {
6656 $payment = usces_get_payments_by_name( $regular_order['reg_payment_name'] );
6657 $acting_flg = ( isset( $payment['settlement'] ) ) ? $payment['settlement'] : '';
6658 if ( 'acting_paypal_cp' !== $acting_flg ) {
6659 return;
6660 }
6661
6662 $ba_id = $this->get_regular_ba_id( $regular_order['reg_mem_id'], $regular_order['reg_id'] );
6663 ?>
6664 <div class="inside">
6665 <table class="mem_info">
6666 <tr><th class="label"><?php esc_html_e( 'Billing Agreement ID', 'usces' ); ?></th><td class="col1"><?php echo esc_attr( $ba_id ); ?></td></tr>
6667 </table>
6668 </div>
6669 <?php
6670 }
6671
6672 /**
6673 * メール支払方法
6674 * usces_filter_order_confirm_mail_payment
6675 * usces_filter_send_order_mail_payment
6676 * wcad_filter_order_confirm_mail_payment
6677 *
6678 * @param string $msg_payment Message body.
6679 * @param int $order_id Order number.
6680 * @param array $payment Payment data.
6681 * @param array $cart Cart data.
6682 * @return string
6683 */
6684 public function regular_order_mail_payment( $msg_payment, $order_id, $payment, $cart ) {
6685 global $usces;
6686
6687 if ( ! wcad_have_regular_order( $cart ) ) {
6688 return $msg_payment;
6689 }
6690
6691 $acting_flg = ( isset( $payment['settlement'] ) ) ? $payment['settlement'] : '';
6692 if ( 'acting_paypal_cp' !== $acting_flg ) {
6693 return $msg_payment;
6694 }
6695
6696 $member_id = $this->get_member_id( $order_id );
6697 $reg_id = $this->get_regular_id( $order_id );
6698 if ( ! empty( $reg_id ) ) {
6699 $ba_id = $this->get_regular_ba_id( $member_id, $reg_id );
6700 if ( ! empty( $ba_id ) ) {
6701 $msg_payment .= __( 'Billing Agreement ID', 'usces' ) . ' : ' . $ba_id . "\r\n\r\n";
6702 }
6703 }
6704 return $msg_payment;
6705 }
6706
6707 /**
6708 * 定期購�
6709 �ID 取得
6710 *
6711 * @param int $order_id Order number.
6712 * @return int
6713 */
6714 private function get_regular_id( $order_id ) {
6715 global $wpdb, $usces;
6716
6717 $query = $wpdb->prepare( "SELECT `reg_id` FROM {$wpdb->prefix}usces_regular WHERE `reg_order_id` = %d ", $order_id );
6718 $reg_id = $wpdb->get_var( $query );
6719 if ( empty( $reg_id ) ) {
6720 $reg_id = $usces->get_order_meta_value( 'regular_id', $order_id );
6721 }
6722 return $reg_id;
6723 }
6724
6725 /**
6726 * 定期購�
6727 �データ取得
6728 *
6729 * @param int $member_id Member ID.
6730 * @param int $reg_id Regular ID.
6731 * @return array
6732 */
6733 private function get_regular_data( $member_id, $reg_id ) {
6734 global $wpdb;
6735
6736 $query = $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}usces_regular WHERE `reg_id` = %d AND `reg_mem_id` = %d", $reg_id, $member_id );
6737 $data = $wpdb->get_row( $query, ARRAY_A );
6738 return $data;
6739 }
6740
6741 /**
6742 * 定期購�
6743 � ba_id 保存
6744 *
6745 * @param int $member_id Member ID.
6746 * @param int $reg_id Regular ID.
6747 * @param string $ba_id Billing Agreement ID.
6748 */
6749 private function set_regular_ba_id( $member_id, $reg_id, $ba_id ) {
6750 global $usces;
6751
6752 $usces->set_member_meta_value( 'paypal_regular_' . $reg_id, $ba_id, $member_id );
6753 }
6754
6755 /**
6756 * 定期購�
6757 � ba_id 取得
6758 *
6759 * @param int $member_id Member ID.
6760 * @param int $reg_id Regular ID.
6761 * @return string
6762 */
6763 private function get_regular_ba_id( $member_id, $reg_id = 0 ) {
6764 global $usces, $wpdb;
6765
6766 if ( empty( $reg_id ) ) {
6767 $member_meta_table = usces_get_tablename( 'usces_member_meta' );
6768 $query = $wpdb->prepare( "SELECT COUNT(*) FROM {$member_meta_table} WHERE `member_id` = %d AND `meta_key` LIKE %s", $member_id, 'paypal_regular_%' );
6769 $ba_id = $wpdb->get_var( $query );
6770 } else {
6771 $ba_id = $usces->get_member_meta_value( 'paypal_regular_' . $reg_id, $member_id );
6772 }
6773 return $ba_id;
6774 }
6775
6776 /**
6777 * 定期購�
6778 �受注金額取得
6779 *
6780 * @param int $order_id Order ID.
6781 * @return float
6782 */
6783 private function get_order_amount( $order_id ) {
6784 global $wpdb;
6785
6786 $amount = $wpdb->get_var( $wpdb->prepare( "SELECT ( `order_item_total_price` - `order_usedpoint` + `order_discount` + `order_shipping_charge` + `order_cod_fee` + `order_tax` ) AS `total_price` FROM {$wpdb->prefix}usces_order WHERE `ID` = %d", $order_id ) );
6787 return $amount;
6788 }
6789
6790 /**
6791 * 会員ID取得
6792 *
6793 * @param int $order_id Order number.
6794 * @return int
6795 */
6796 private function get_member_id( $order_id ) {
6797 global $wpdb;
6798
6799 $query = $wpdb->prepare( "SELECT `mem_id` FROM {$wpdb->prefix}usces_order WHERE `ID` = %d", $order_id );
6800 $member_id = $wpdb->get_var( $query );
6801 return $member_id;
6802 }
6803
6804 /**
6805 * 決済ログ保存
6806 *
6807 * @param string $log Log data.
6808 * @param string $acting Acting type.
6809 * @param string $status Status.
6810 * @param string $result Result.
6811 * @param float $amount Amount.
6812 * @param int $order_id Order number.
6813 * @param string $tracking_id Tracking ID.
6814 * @return array
6815 */
6816 private function save_acting_log( $log, $acting, $status, $result, $amount, $order_id, $tracking_id ) {
6817 global $wpdb;
6818
6819 $query = $wpdb->prepare(
6820 "INSERT INTO {$wpdb->prefix}usces_acting_log ( `datetime`, `log`, `acting`, `status`, `result`, `amount`, `order_id`, `tracking_id` ) VALUES ( %s, %s, %s, %s, %s, %f, %d, %s )",
6821 current_time( 'mysql' ),
6822 usces_serialize( $log ),
6823 $acting,
6824 $status,
6825 $result,
6826 $amount,
6827 $order_id,
6828 $tracking_id
6829 );
6830 $res = $wpdb->query( $query );
6831 return $res;
6832 }
6833
6834 /**
6835 * 決済ログ取得
6836 *
6837 * @param int $order_id Order number.
6838 * @param string $tracking_id Tracking ID.
6839 * @param string $result Result.
6840 * @return array
6841 */
6842 public function get_acting_log( $order_id = 0, $tracking_id = 0, $result = 'COMPLETED' ) {
6843 global $wpdb;
6844
6845 if ( empty( $order_id ) ) {
6846 if ( empty( $tracking_id ) ) {
6847 return array();
6848 }
6849 if ( 'COMPLETED' === $result ) {
6850 $query = $wpdb->prepare(
6851 "SELECT * FROM {$wpdb->prefix}usces_acting_log WHERE`tracking_id` = %s AND `result` IN ( 'COMPLETED', 'PENDING' ) ORDER BY `ID` DESC, datetime DESC",
6852 $tracking_id
6853 );
6854 } else {
6855 $query = $wpdb->prepare(
6856 "SELECT * FROM {$wpdb->prefix}usces_acting_log WHERE `tracking_id` = %s ORDER BY `ID` DESC, datetime DESC",
6857 $tracking_id
6858 );
6859 }
6860 } else {
6861 if ( empty( $tracking_id ) ) {
6862 if ( 'COMPLETED' === $result ) {
6863 $query = $wpdb->prepare(
6864 "SELECT * FROM {$wpdb->prefix}usces_acting_log WHERE `datetime` IN( SELECT MAX( `datetime` ) FROM {$wpdb->prefix}usces_acting_log WHERE `order_id` = %d GROUP BY `tracking_id` ) AND `order_id` = %d AND `result` IN ( 'COMPLETED', 'PENDING' ) ORDER BY `ID` DESC, datetime DESC",
6865 $order_id,
6866 $order_id
6867 );
6868 } else {
6869 $query = $wpdb->prepare(
6870 "SELECT * FROM {$wpdb->prefix}usces_acting_log WHERE `datetime` IN( SELECT MAX( `datetime` ) FROM {$wpdb->prefix}usces_acting_log WHERE `order_id` = %d GROUP BY `tracking_id` ) AND `order_id` = %d ORDER BY `ID` DESC, datetime DESC",
6871 $order_id,
6872 $order_id
6873 );
6874 }
6875 } else {
6876 if ( 'COMPLETED' === $result ) {
6877 $query = $wpdb->prepare(
6878 "SELECT * FROM {$wpdb->prefix}usces_acting_log WHERE `order_id` = %d AND `tracking_id` = %s AND `result` IN ( 'COMPLETED', 'PENDING' ) ORDER BY `ID` DESC, datetime DESC",
6879 $order_id,
6880 $tracking_id
6881 );
6882 } else {
6883 $query = $wpdb->prepare(
6884 "SELECT * FROM {$wpdb->prefix}usces_acting_log WHERE `order_id` = %d AND `tracking_id` = %s ORDER BY `ID` DESC, datetime DESC",
6885 $order_id,
6886 $tracking_id
6887 );
6888 }
6889 }
6890 }
6891 $log_data = $wpdb->get_results( $query, ARRAY_A );
6892 return $log_data;
6893 }
6894
6895 /**
6896 * 自動決済ログ保存
6897 *
6898 * @param string $tracking_id Tracking ID.
6899 * @param float $amount Amount.
6900 * @param array $cart Cart data.
6901 * @param array $entry Entry data.
6902 * @return array
6903 */
6904 private function save_entry_log( $tracking_id, $amount, $cart, $entry ) {
6905 global $wpdb;
6906
6907 $log = array(
6908 'cart' => $cart,
6909 'entry' => $entry,
6910 );
6911 $query = $wpdb->prepare(
6912 "INSERT INTO {$wpdb->prefix}usces_acting_log ( `datetime`, `log`, `acting`, `result`, `amount`, `tracking_id` ) VALUES ( %s, %s, %s, %s, %f, %s )",
6913 current_time( 'mysql' ),
6914 usces_serialize( $log ),
6915 $this->paymod_id,
6916 'ENTRY',
6917 $amount,
6918 $tracking_id
6919 );
6920 $res = $wpdb->query( $query );
6921 return $res;
6922 }
6923
6924 /**
6925 * 自動決済ログ取得
6926 *
6927 * @param string $tracking_id Tracking ID.
6928 * @return array
6929 */
6930 public function get_entry_log( $tracking_id ) {
6931 global $wpdb;
6932
6933 $query = $wpdb->prepare(
6934 "SELECT `log` FROM {$wpdb->prefix}usces_acting_log WHERE `acting` = %s AND `result` = %s AND `tracking_id` = %s",
6935 $this->paymod_id,
6936 'ENTRY',
6937 $tracking_id
6938 );
6939 $log_data = $wpdb->get_var( $query );
6940 $log = ( $log_data ) ? usces_unserialize( $log_data ) : array();
6941 return $log;
6942 }
6943
6944 /**
6945 * 自動決済ログ削除
6946 *
6947 * @param string $tracking_id Tracking ID.
6948 * @return array
6949 */
6950 public function del_entry_log( $tracking_id ) {
6951 global $wpdb;
6952
6953 $query = $wpdb->prepare(
6954 "DELETE FROM {$wpdb->prefix}usces_acting_log WHERE `acting` = %s AND `result` = %s AND `tracking_id` = %s",
6955 $this->paymod_id,
6956 'ENTRY',
6957 $tracking_id
6958 );
6959 $res = $wpdb->query( $query );
6960 return $res;
6961 }
6962
6963 /**
6964 * 最新処理取得
6965 *
6966 * @param int $order_id Order number.
6967 * @param string $tracking_id Tracking ID.
6968 * @param string $result Result.
6969 * @return array
6970 */
6971 public function get_acting_latest_log( $order_id, $tracking_id, $result = 'COMPLETED' ) {
6972 $log_data = $this->get_acting_log( $order_id, $tracking_id, $result );
6973 if ( $log_data ) {
6974 $data = current( $log_data );
6975 $latest_log = array(
6976 'log' => usces_unserialize( $data['log'] ),
6977 'acting' => $data['acting'],
6978 'status' => $data['status'],
6979 'result' => $data['result'],
6980 'amount' => $data['amount'],
6981 'order_id' => $data['order_id'],
6982 'tracking_id' => $data['tracking_id'],
6983 );
6984 } else {
6985 $latest_log = array();
6986 }
6987 return $latest_log;
6988 }
6989
6990 /**
6991 * 最新決済金額取得
6992 *
6993 * @param int $order_id Order number.
6994 * @param string $tracking_id Tracking ID.
6995 * @return int $amount Amount.
6996 */
6997 private function get_latest_amount( $order_id, $tracking_id ) {
6998 $amount = 0;
6999 $refund = 0;
7000 $log_data = $this->get_acting_log( $order_id, $tracking_id );
7001 if ( $log_data ) {
7002 foreach ( (array) $log_data as $data ) {
7003 if ( 'CAPTURE' === $data['status'] ) {
7004 $amount = $data['amount'];
7005 } elseif ( 'AUTHORIZE' === $data['status'] ) {
7006 if ( 0.0 === (float) $amount ) {
7007 $amount = $data['amount'];
7008 }
7009 } elseif ( 'REFUND' === $data['status'] || 'VOID' === $data['status'] ) {
7010 $refund += $data['amount']; /* minus value */
7011 }
7012 }
7013 }
7014 return $amount + $refund;
7015 }
7016
7017 /**
7018 * 決済処理取得
7019 *
7020 * @param int $order_id Order number.
7021 * @param string $tracking_id Tracking ID.
7022 * @return string
7023 */
7024 private function get_acting_status( $order_id, $tracking_id ) {
7025 global $wpdb;
7026
7027 $acting_status = '';
7028 $latest_log = $this->get_acting_latest_log( $order_id, $tracking_id, 'ALL' );
7029 if ( isset( $latest_log['status'] ) && isset( $latest_log['result'] ) ) {
7030 if ( 'COMPLETED' === $latest_log['result'] ) {
7031 $amount = $this->get_latest_amount( $order_id, $tracking_id );
7032 if ( 'VOID' === $latest_log['status'] ) {
7033 $acting_status = 'VOIDED';
7034 } elseif ( 'REFUND' === $latest_log['status'] && 0 < $amount ) {
7035 $acting_status = 'CAPTURE';
7036 } elseif ( 'REFUND' === $latest_log['status'] && 0.0 === (float) $amount ) {
7037 $acting_status = 'REFUNDED';
7038 } else {
7039 $acting_status = $latest_log['status'];
7040 }
7041 } else {
7042 $acting_status = $latest_log['status'];
7043 }
7044 }
7045 return $acting_status;
7046 }
7047
7048 /**
7049 * 決済履歴
7050 *
7051 * @param int $order_id Order number.
7052 * @param string $tracking_id Tracking ID.
7053 * @return string
7054 */
7055 private function settlement_history( $order_id, $tracking_id ) {
7056 $html = '';
7057 $log_data = $this->get_acting_log( $order_id, $tracking_id, 'ALL' );
7058 if ( $log_data ) {
7059 $num = count( $log_data );
7060 $html = '<table class="settlement-history">
7061 <thead class="settlement-history-head">
7062 <tr><th></th><th>' . __( 'Processing date', 'usces' ) . '</th><th>' . __( 'Trans ID', 'usces' ) . '</th><th>' . __( 'Process', 'usces' ) . '</th><th>' . __( 'Amount', 'usces' ) . '</th><th>' . __( 'Status', 'usces' ) . '</th></tr>
7063 </thead>
7064 <tbody class="settlement-history-body">';
7065 foreach ( (array) $log_data as $data ) {
7066 $log = usces_unserialize( $data['log'] );
7067 $id = ( isset( $log['id'] ) ) ? $log['id'] : '';
7068 $issue = '';
7069 if ( 'COMPLETED' !== $data['result'] ) {
7070 $class = ' error';
7071 $amount = '';
7072 if ( isset( $log['details'][0]['issue'] ) && '' !== $log['details'][0]['issue'] ) {
7073 $issue = ' (' . $log['details'][0]['issue'] . ')';
7074 }
7075 } else {
7076 $class = '';
7077 $amount = ( isset( $data['amount'] ) ) ? usces_crform( $data['amount'], false, true, 'return', true ) : '';
7078 }
7079 if ( isset( $log['purchase_units'] ) ) {
7080 $purchase_units = $log['purchase_units'][0];
7081 if ( isset( $purchase_units['payments'] ) ) {
7082 if ( isset( $purchase_units['payments']['captures'] ) ) {
7083 $payments = $purchase_units['payments']['captures'][0];
7084 } elseif ( isset( $purchase_units['payments']['authorizations'] ) ) {
7085 $payments = $purchase_units['payments']['authorizations'][0];
7086 } elseif ( isset( $purchase_units['payments']['refunds'] ) ) {
7087 $payments = $purchase_units['payments']['refunds'][0];
7088 }
7089 if ( isset( $payments['id'] ) ) {
7090 $id = $payments['id'];
7091 }
7092 }
7093 }
7094 $html .= '<tr>
7095 <td class="num">' . $num . '</td>
7096 <td class="datetime">' . $data['datetime'] . '</td>
7097 <td class="transactionid">' . $id . '</td>
7098 <td class="status">' . $data['status'] . '</td>
7099 <td class="amount">' . $amount . '</td>
7100 <td class="result' . $class . '">' . $data['result'] . $issue . '</td>
7101 </tr>';
7102 $num--;
7103 }
7104 $html .= '</tbody>
7105 </table>';
7106 }
7107 return $html;
7108 }
7109
7110 /**
7111 * 受注データ支払方法取得
7112 *
7113 * @param int $order_id Order number.
7114 * @return string
7115 */
7116 private function get_order_acting_flg( $order_id ) {
7117 global $wpdb;
7118
7119 $query = $wpdb->prepare( "SELECT `order_payment_name` FROM {$wpdb->prefix}usces_order WHERE `ID` = %d", $order_id );
7120 $order_payment_name = $wpdb->get_var( $query );
7121 $payment = usces_get_payments_by_name( $order_payment_name );
7122 $acting_flg = ( isset( $payment['settlement'] ) ) ? $payment['settlement'] : '';
7123 return $acting_flg;
7124 }
7125
7126 /**
7127 * 決済オプション取得
7128 *
7129 * @return array
7130 */
7131 public function get_acting_settings() {
7132 global $usces;
7133
7134 $acting_settings = ( isset( $usces->options['acting_settings'][ $this->paymod_id ] ) ) ? $usces->options['acting_settings'][ $this->paymod_id ] : array();
7135 return $acting_settings;
7136 }
7137
7138 /**
7139 * Date validity check.
7140 *
7141 * @param string $date Date to check.
7142 * @return boolean
7143 */
7144 private function isdate( $date ) {
7145 if ( empty( $date ) ) {
7146 return false;
7147 }
7148 try {
7149 new DateTime( $date );
7150 list( $year, $month, $day ) = explode( '-', $date );
7151 $res = checkdate( (int) $month, (int) $day, (int) $year );
7152 return $res;
7153 } catch ( Exception $e ) {
7154 return false;
7155 }
7156 }
7157
7158 /**
7159 * Get PayPal locale.
7160 *
7161 * @return string
7162 */
7163 private function get_locale() {
7164 global $usces;
7165
7166 $front_lang = ( isset( $usces->options['system']['front_lang'] ) && ! empty( $usces->options['system']['front_lang'] ) ) ? $usces->options['system']['front_lang'] : usces_get_local_language();
7167 switch ( $front_lang ) {
7168 case 'en':
7169 $locale = 'en_US';
7170 break;
7171 case 'ja':
7172 $locale = 'ja_JP';
7173 break;
7174 case 'th':
7175 $locale = 'th_TH';
7176 break;
7177 default:
7178 $locale = get_locale();
7179 }
7180 return $locale;
7181 }
7182
7183 /**
7184 * Check prefix.
7185 *
7186 * @param string $prefix Prefix.
7187 * @return bool
7188 */
7189 private function check_prefix( $prefix ) {
7190 if ( 0 === strlen( $prefix ) ) {
7191 return true;
7192 }
7193 $check = false;
7194 if ( 1 > strlen( $prefix ) || 4 < strlen( $prefix ) ) {
7195 return $check;
7196 }
7197 if ( preg_match( '/[A-Z0-9]/', $prefix ) ) {
7198 $check = true;
7199 }
7200 return $check;
7201 }
7202 }
7203