PluginProbe
StoreEngine — Complete eCommerce Solution with Memberships, Licensing, Affiliates & More / 2.2.0
StoreEngine — Complete eCommerce Solution with Memberships, Licensing, Affiliates & More v2.2.0
2.3.0 2.2.0 2.1.1 2.1.0 2.0.0 1.10.0 1.9.1 1.9.0 1.2.1 1.2.2 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.6.0 All 59 releases
storeengine / includes / hooks / payment.php

payment.php in StoreEngine — Complete eCommerce Solution with Memberships, Licensing, Affiliates & More 2.2.0, at includes/hooks/payment.php

163 lines 6.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace StoreEngine\Hooks;
4
5 use StoreEngine\Classes\Order;
6 use StoreEngine\Classes\PaymentTokens\PaymentTokens;
7 use StoreEngine\Classes\Refund;
8 use StoreEngine\Utils\Helper;
9
10 if ( ! defined( 'ABSPATH' ) ) {
11 exit;
12 }
13
14 class Payment {
15
16 public static function init() {
17 $self = new self();
18
19 add_action( 'storeengine/order/status_changed', [ $self, 'update_customer' ], 10, 4 );
20 add_action( 'storeengine/order/refund_created', [ $self, 'update_customer_refund' ] );
21
22 add_filter( 'storeengine/saved_payment_methods_list', [ __CLASS__, 'get_account_saved_payment_methods_list' ], 10, 2 );
23 add_action( 'storeengine_dashboard_handle_delete-payment-method_request', [ __CLASS__, 'delete_payment_method_action' ], 20 );
24 add_action( 'storeengine_dashboard_handle_set-default-payment-method_request', [ __CLASS__, 'set_default_payment_method_action' ], 20 );
25 }
26
27 public function update_customer( $order_id, string $old_status, string $new_status, Order $order ) {
28 $paid_statuses = Helper::get_order_paid_statuses();
29
30 $old_status_exists = in_array( $old_status, $paid_statuses, true );
31 $new_status_exists = in_array( $new_status, $paid_statuses, true );
32
33 if ( ( ! $old_status_exists && ! $new_status_exists ) || ( $old_status_exists && $new_status_exists ) ) {
34 return;
35 }
36
37 $customer = $order->get_customer();
38 if ( ! $customer ) {
39 return;
40 }
41
42 $order_total = $order->get_total() - $order->get_total_refunded();
43 if ( $old_status_exists && ! $new_status_exists ) {
44 $customer->set_total_orders( $customer->get_total_orders() - 1 );
45 $total_spent = (float) ( $customer->get_total_spent() - $order_total );
46 $customer->set_total_spent( $total_spent );
47 }
48
49 if ( ! $old_status_exists && $new_status_exists ) {
50 $customer->set_total_orders( $customer->get_total_orders() + 1 );
51 $total_spent = (float) ( $customer->get_total_spent() + $order_total );
52 $customer->set_total_spent( $total_spent );
53 }
54
55 $customer->save();
56 }
57
58 public function update_customer_refund( Refund $refund ) {
59 $customer = $refund->get_customer();
60 $order = Helper::get_order( $refund->get_parent_order_id() );
61
62 if ( ! $customer || ! in_array( $order->get_status(), Helper::get_order_paid_statuses(), true ) ) {
63 return;
64 }
65
66 $total_spent = (float) $customer->get_total_spent();
67 $total_spent = $total_spent - (float) $refund->get_total();
68 $customer->set_total_spent( $total_spent );
69 $customer->save();
70 }
71
72 public static function get_account_saved_payment_methods_list( $list, $customer_id ): array {
73 $payment_tokens = PaymentTokens::get_customer_tokens( $customer_id, '', - 1 );
74 foreach ( $payment_tokens as $payment_token ) {
75 $delete_url = Helper::get_account_endpoint_url( 'delete-payment-method', $payment_token->get_id() );
76 $delete_url = wp_nonce_url( $delete_url, 'delete-payment-method-' . $payment_token->get_id() );
77 $actions = [
78 'delete' => [
79 'url' => $delete_url,
80 'name' => esc_html__( 'Delete', 'storeengine' ),
81 'icon' => 'trash',
82 'styles' => '--text-color: var(--storeengine-error-color)',
83 'priority' => 100,
84 ],
85 ];
86
87 if ( ! $payment_token->is_default() ) {
88 $set_default_url = Helper::get_account_endpoint_url( 'set-default-payment-method', $payment_token->get_id() );
89 $set_default_url = wp_nonce_url( $set_default_url, 'set-default-payment-method-' . $payment_token->get_id() );
90 $actions['default'] = [
91 'url' => $set_default_url,
92 'name' => esc_html__( 'Make default', 'storeengine' ),
93 'icon' => 'star',
94 'styles' => [ '--icon-color' => 'var(--storeengine-text-color)' ],
95 'priority' => 10,
96 ];
97 }
98
99 $type = strtolower( $payment_token->get_type() );
100 $item = [
101 'id' => $payment_token->get_id(),
102 'method' => [ 'gateway' => $payment_token->get_gateway_id() ],
103 'expires' => esc_html__( 'N/A', 'storeengine' ),
104 'is_default' => $payment_token->is_default(),
105 'actions' => $actions,
106 ];
107
108 if ( 'cc' === $type ) {
109 //$item['method']['brand'] = ( ! empty( $card_type ) ? ucwords( str_replace( '_', ' ', $card_type ) ) : esc_html__( 'Credit card', 'storeengine' ) );
110 $card_type = $payment_token->get_card_type();
111 $item['method']['last4'] = $payment_token->get_last4();
112 $item['method']['brand'] = $card_type ?: 'credit_card';
113 $item['expires'] = $payment_token->get_expiry_month() . '/' . substr( $payment_token->get_expiry_year(), - 2 );
114 }
115
116 if ( 'echeck' === $type ) {
117 $item['method']['last4'] = $payment_token->get_last4();
118 $item['method']['brand'] = 'echeck';
119 }
120
121 $list[ $type ][] = apply_filters( 'storeengine/payment_methods_list_item', $item, $payment_token );
122 }
123
124 return $list;
125 }
126
127 /**
128 * Process the delete payment method form.
129 */
130 public static function delete_payment_method_action( $token_id ) {
131 $token = PaymentTokens::get_token( absint( $token_id ) );
132
133 if ( is_null( $token ) || get_current_user_id() !== $token->get_user_id() || ! isset( $_REQUEST['_wpnonce'] ) || false === wp_verify_nonce( wp_unslash( $_REQUEST['_wpnonce'] ), 'delete-payment-method-' . $token_id ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
134 wp_die( esc_html__( 'Invalid payment method.', 'storeengine' ), esc_html__( 'Invalid payment method.', 'storeengine' ), [ 'back_link' => true ] );
135 } else {
136 PaymentTokens::delete( $token_id );
137 // @TODO notification handler.
138 //__( 'Payment method deleted.', 'storeengine' )
139 }
140
141 wp_safe_redirect( Helper::get_account_endpoint_url( 'payment-methods' ) );
142 exit();
143 }
144
145 /**
146 * Process the delete payment method form.
147 */
148 public static function set_default_payment_method_action( $token_id ) {
149 $token = PaymentTokens::get_token( absint( $token_id ) );
150
151 if ( is_null( $token ) || get_current_user_id() !== $token->get_user_id() || ! isset( $_REQUEST['_wpnonce'] ) || false === wp_verify_nonce( wp_unslash( $_REQUEST['_wpnonce'] ), 'set-default-payment-method-' . $token_id ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
152 wp_die( esc_html__( 'Invalid payment method.', 'storeengine' ), esc_html__( 'Invalid payment method.', 'storeengine' ), [ 'back_link' => true ] );
153 } else {
154 PaymentTokens::set_users_default( $token->get_user_id(), intval( $token_id ) );
155 // @TODO notification handler.
156 // __( 'This payment method was successfully set as your default.', 'storeengine' )
157 }
158
159 wp_safe_redirect( Helper::get_account_endpoint_url( 'payment-methods' ) );
160 exit();
161 }
162 }
163