PluginProbe
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments / 2.25.2
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments v2.25.2
4.7.2 4.7.1 4.7.0 4.6.6 4.6.5 4.6.4 4.6.3 4.6.2 4.6.1 4.6.0 4.5.1 4.5.0 4.4.2 4.4.1 4.4.0 4.3.3 4.3.2 4.3.1 4.3.0 4.2.3 4.2.2 4.2.1 1.0.3 1.0.4 1.0.5 All 281 releases
surecart / packages / blocks / Controllers / PaymentMethodController.php

PaymentMethodController.php in SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments 2.25.2, at packages/blocks/Controllers/PaymentMethodController.php

252 lines 8.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace SureCartBlocks\Controllers;
3
4 use SureCart\Models\Component;
5 use SureCart\Models\Customer;
6 use SureCart\Models\Processor;
7 use SureCart\Models\User;
8
9 /**
10 * Payment method block controller class.
11 */
12 class PaymentMethodController extends BaseController {
13 /**
14 * List all payment methods.
15 *
16 * @param array $attributes Block attributes.
17 * @param string $content Block content.
18 *
19 * @return function
20 */
21 public function index( $attributes, $content ) {
22 if ( ! is_user_logged_in() ) {
23 return false;
24 }
25
26 return wp_kses_post(
27 Component::tag( 'sc-payment-methods-list' )
28 ->id( 'sc-customer-payment-methods-list' )
29 ->with(
30 [
31 'isCustomer' => User::current()->isCustomer(),
32 'query' => [
33 'customer_ids' => array_values( User::current()->customerIds() ),
34 'page' => 1,
35 'per_page' => 100,
36 'reusable' => true,
37 ],
38 ]
39 )->render( $attributes['title'] ? "<span slot='heading'>" . $attributes['title'] . '</span>' : '' )
40 );
41 }
42
43 public function getProcessors() {
44 return array_values(
45 array_filter(
46 Processor::get() ?? [],
47 function( $processor ) {
48 return $processor->live_mode === $this->isLiveMode() && $processor->recurring_enabled && $processor->enabled;
49 }
50 )
51 );
52 }
53
54 /**
55 * Get the processor by type.
56 *
57 * @param string $type The processor type.
58 * @param array $processors Array of processors.
59 *
60 * @return \SureCart/Models/Processor|null;
61 */
62 protected function getProcessorByType( $type ) {
63 $processors = $this->getProcessors();
64 if ( empty( $processors ) ) {
65 return null;
66 }
67 $key = array_search( $type, array_column( (array) $processors, 'processor_type' ), true );
68 if ( ! is_int( $key ) ) {
69 return null;
70 }
71 return $processors[ $key ] ?? null;
72 }
73
74 /**
75 * Get the success url.
76 *
77 * @param array $attributes The block attributes.
78 *
79 * @return string
80 */
81 public function getSuccessUrl( $attributes = [] ) {
82 // attribute.
83 if ( ! empty( $attributes['success_url'] ) ) {
84 return esc_url( $attributes['success_url'] );
85 }
86
87 // url parameter.
88 if ( ! empty( $_GET['success_url'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
89 return esc_url( $_GET['success_url'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
90 }
91
92 // default.
93 return home_url( add_query_arg( [ 'tab' => $this->getTab() ], remove_query_arg( array_keys( $_GET ) ) ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
94 }
95
96 /**
97 * Show a view to add a payment method.
98 *
99 * @param array $attributes The block attributes.
100 *
101 * @return string
102 */
103 public function create( $attributes = [] ) {
104 // get the success url.
105 $success_url = $this->getSuccessUrl( $attributes );
106
107 if ( empty( User::current()->customerId( $this->isLiveMode() ? 'live' : 'test' ) ) ) {
108 ob_start(); ?>
109 <sc-alert type="info" open>
110 <?php if ( $this->isLiveMode() && ! empty( User::current()->customerId( 'test' ) ) ) { ?>
111 <?php esc_html_e( 'You are not a live mode customer.', 'surecart' ); ?>
112 <div style="margin-top:0.5em;">
113 <sc-button href="<?php echo esc_url( add_query_arg( [ 'live_mode' => 'false' ] ) ); ?>" type="info" size="small">
114 <?php esc_html_e( 'Switch to test mode.', 'surecart' ); ?>
115 </sc-button>
116 </div>
117 <?php } elseif ( ! $this->isLiveMode() && ! empty( User::current()->customerId( 'live' ) ) ) { ?>
118 <?php esc_html_e( 'You are not a test mode customer.', 'surecart' ); ?>
119 <div style="margin-top:0.5em;">
120 <sc-button href="<?php echo esc_url( add_query_arg( [ 'live_mode' => false ] ) ); ?>" type="info" size="small">
121 <?php esc_html_e( 'Switch to live mode.', 'surecart' ); ?>
122 </sc-button>
123 </div>
124 <?php } else { ?>
125 <?php esc_html_e( 'You are not currently a customer.', 'surecart' ); ?>
126 <?php } ?>
127 </sc-alert>
128 <?php
129 return ob_get_clean();
130 }
131
132 $processor_names = array_filter(
133 array_values(
134 array_map(
135 function( $processor ) {
136 return $processor->processor_type;
137 },
138 $this->getProcessors() ?? []
139 )
140 )
141 );
142
143 if ( empty( $processor_names ) ) {
144 return '<sc-alert type="info" open>' . __( 'You cannot currently add a payment method. Please contact us for support.', 'surecart' ) . '</sc-alert>';
145 }
146
147 ob_start();
148 ?>
149
150 <sc-spacing style="--spacing: var(--sc-spacing-large)">
151 <sc-breadcrumbs>
152 <sc-breadcrumb href="<?php echo esc_url( add_query_arg( [ 'tab' => $this->getTab() ], remove_query_arg( array_keys( $_GET ) ) ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended ?>">
153 <?php esc_html_e( 'Dashboard', 'surecart' ); ?>
154 </sc-breadcrumb>
155 <sc-breadcrumb href="
156 <?php
157 echo esc_url(
158 add_query_arg(
159 [
160 'tab' => $this->getTab(),
161 'model' => 'customer',
162 'action' => 'show',
163 'id' => User::current()->customerId( $this->isLiveMode() ? 'live' : 'test' ),
164 ],
165 remove_query_arg( array_keys( $_GET ) ) // phpcs:ignore WordPress.Security.NonceVerification.Recommended
166 )
167 );
168 ?>
169 ">
170 <?php esc_html_e( 'Billing', 'surecart' ); ?>
171 </sc-breadcrumb>
172 <sc-breadcrumb>
173 <?php esc_html_e( 'Add Payment Method', 'surecart' ); ?>
174 </sc-breadcrumb>
175 </sc-breadcrumbs>
176
177 <sc-heading>
178 <?php esc_html_e( 'Add Payment Method', 'surecart' ); ?>
179 <?php echo ! $this->isLiveMode() ? '<sc-tag type="warning" slot="end">' . esc_html__( 'Test Mode', 'surecart' ) . '</sc-tag>' : ''; ?>
180 </sc-heading>
181
182 <?php
183 $mollie = $this->getProcessorByType( 'mollie' );
184 if ( $mollie ) :
185 ?>
186 <?php $customer = Customer::with( [ 'shipping_address' ] )->find( User::current()->customerId( $this->isLiveMode() ? 'live' : 'test' ) ); ?>
187 <sc-mollie-add-method
188 processor-id="<?php echo esc_attr( $mollie->id ); ?>"
189 success-url="<?php echo esc_url( $success_url ); ?>"
190 live-mode="<?php echo esc_attr( $this->isLiveMode() ? 'true' : 'false' ); ?>"
191 country="<?php echo esc_attr( $customer->shipping_address->country ?? '' ); ?>"
192 currency="<?php echo esc_attr( \SureCart::account()->currency ); ?>"
193 customer-id="<?php echo esc_attr( User::current()->customerId( $this->isLiveMode() ? 'live' : 'test' ) ); ?>">
194 </sc-mollie-add-method>
195 <?php else : ?>
196
197 <sc-toggles collapsible="false" theme="container" accordion>
198 <?php if ( in_array( 'stripe', $processor_names ) ) : ?>
199 <sc-toggle class="sc-stripe-toggle" show-control shady borderless>
200 <span slot="summary" class="sc-payment-toggle-summary">
201 <sc-flex>
202 <sc-icon name="creditcard" style="font-size:24px"></sc-icon>
203 <span><?php esc_html_e( 'Credit Card', 'surecart' ); ?></span>
204 </sc-flex>
205 </span>
206 <sc-stripe-add-method
207 success-url="<?php echo esc_url( $success_url ); ?>"
208 live-mode="<?php echo esc_attr( $this->isLiveMode() ? 'true' : 'false' ); ?>"
209 customer-id="<?php echo esc_attr( User::current()->customerId( $this->isLiveMode() ? 'live' : 'test' ) ); ?>">
210 </sc-stripe-add-method>
211 </sc-toggle>
212 <?php endif; ?>
213
214 <?php if ( in_array( 'paypal', $processor_names ) ) : ?>
215 <sc-toggle class="sc-paypal-toggle" show-control shady borderless>
216 <span slot="summary" class="sc-payment-toggle-summary">
217 <sc-icon name="paypal" style="width: 80px; font-size: 24px"></sc-icon>
218 </span>
219 <sc-paypal-add-method
220 success-url="<?php echo esc_url( $success_url ); ?>"
221 live-mode="<?php echo esc_attr( $this->isLiveMode() ? 'true' : 'false' ); ?>"
222 currency="<?php echo esc_attr( \SureCart::account()->currency ); ?>"
223 customer-id="<?php echo esc_attr( User::current()->customerId( $this->isLiveMode() ? 'live' : 'test' ) ); ?>">
224 </sc-paypal-add-method>
225 </sc-toggle>
226 <?php endif; ?>
227
228 <?php if ( in_array( 'paystack', $processor_names ) && ! in_array( 'stripe', $processor_names ) ) : ?>
229 <sc-toggle class="sc-paystack-toggle" show-control shady borderless>
230 <span slot="summary" class="sc-payment-toggle-summary">
231 <sc-flex>
232 <sc-icon name="creditcard" style="font-size:24px"></sc-icon>
233 <span><?php esc_html_e( 'Credit Card', 'surecart' ); ?></span>
234 </sc-flex>
235 </span>
236 <sc-paystack-add-method
237 success-url="<?php echo esc_url( $success_url ); ?>"
238 live-mode="<?php echo esc_attr( $this->isLiveMode() ? 'true' : 'false' ); ?>"
239 currency="<?php echo esc_attr( \SureCart::account()->currency ); ?>"
240 customer-id="<?php echo esc_attr( User::current()->customerId( $this->isLiveMode() ? 'live' : 'test' ) ); ?>">
241 </sc-paystack-add-method>
242 </sc-toggle>
243 <?php endif; ?>
244 </sc-toggles>
245
246 <?php endif; ?>
247 </sc-spacing>
248 <?php
249 return ob_get_clean();
250 }
251 }
252