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