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 / classes / order / payment-info.php

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

153 lines 4.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 declare( strict_types=1 );
3
4 namespace StoreEngine\Classes\Order;
5
6 use StoreEngine\Classes\AbstractOrder;
7 use StoreEngine\Utils\Fs;
8 use StoreEngine\Utils\Helper;
9 use StoreEngine\Utils\StringUtil;
10
11 /**
12 * Class PaymentInfo.
13 */
14 class PaymentInfo {
15 /**
16 * This array must contain all the names of the files in the CardIcons directory (without extension),
17 * except 'unknown'.
18 */
19 private const KNOWN_CARD_BRANDS = array(
20 'amex',
21 'diners',
22 'discover',
23 'interac',
24 'jcb',
25 'mastercard',
26 'visa',
27 );
28
29 /**
30 * Get info about the card used for payment on an order.
31 *
32 * @param AbstractOrder $order The order in question.
33 *
34 * @return array
35 */
36 public static function get_card_info( AbstractOrder $order ): array {
37 $method = $order->get_payment_method();
38
39 // @XXX support for wc-payment plugin.
40 if ( 'woocommerce_payments' === $method ) {
41 $info = self::get_wcpay_card_info( $order );
42 } else {
43 /**
44 * Filter to allow payment gateways to provide payment card info for an order.
45 *
46 * @param array|null $info The card info.
47 * @param AbstractOrder $order The order.
48 */
49 $info = apply_filters( 'storeengine/order_payment_card_info', [], $order );
50
51 if ( ! is_array( $info ) ) {
52 $info = [];
53 }
54 }
55
56 $defaults = [
57 'payment_method' => $method,
58 'brand' => '',
59 'icon' => '',
60 'last4' => '',
61 ];
62 $info = wp_parse_args( $info, $defaults );
63
64 if ( empty( $info['icon'] ) ) {
65 $info['icon'] = self::get_card_icon( $info['brand'] );
66 }
67
68 return $info;
69 }
70
71 /**
72 * Generate a CSS-compatible SVG icon of a card brand.
73 *
74 * @param string|null $brand The brand of the card.
75 *
76 * @return string
77 */
78 private static function get_card_icon( ?string $brand ): string {
79 $brand = strtolower( (string) $brand );
80
81 if ( ! in_array( $brand, self::KNOWN_CARD_BRANDS, true ) ) {
82 $brand = 'unknown';
83 }
84
85 return base64_encode( Fs::get_contents( __DIR__ . "/card-icons/{$brand}.svg" ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode
86 }
87
88 /**
89 * Get info about the card used for payment on an order, when the payment gateway is WooPayments.
90 * This adds support for woocommerce-payments plugin.
91 *
92 * @see https://docs.stripe.com/api/charges/object#charge_object-payment_method_details
93 *
94 * @param AbstractOrder $order The order in question.
95 *
96 * @return array
97 */
98 private static function get_wcpay_card_info( AbstractOrder $order ): array {
99 if ( 'woocommerce_payments' !== $order->get_payment_method() ) {
100 return [];
101 }
102
103 // For testing purposes: if WooCommerce Payments development mode is enabled, an order meta item with
104 // key '_wcpay_payment_details' will be used if it exists as a replacement for the call to the Stripe
105 // API's 'get intent' endpoint. The value must be the JSON encoding of an array simulating the
106 // "payment_details" part of the response from the endpoint.
107 $stored_payment_details = defined( 'WCPAY_DEV_MODE' ) ? $order->get_meta( '_wcpay_payment_details' ) : '';
108 $payment_details = json_decode( $stored_payment_details, true );
109
110 if ( ! $payment_details ) {
111 if ( ! class_exists( '\WC_Payments' ) ) {
112 return [];
113 }
114
115 $payment_method_id = $order->get_meta( '_payment_method_id' );
116 if ( ! $payment_method_id ) {
117 return [];
118 }
119
120 try {
121 $payment_details = \WC_Payments::get_payments_api_client()->get_payment_method( $payment_method_id );
122 } catch ( \Throwable $ex ) {
123 Helper::log_error( $ex );
124
125 return [];
126 }
127 }
128
129 $card_info = [];
130
131 if ( isset( $payment_details['type'], $payment_details[ $payment_details['type'] ] ) ) {
132 $details = $payment_details[ $payment_details['type'] ];
133 switch ( $payment_details['type'] ) {
134 case 'card':
135 default:
136 $card_info['brand'] = $details['brand'] ?? '';
137 $card_info['last4'] = $details['last4'] ?? '';
138 break;
139 case 'card_present':
140 case 'interac_present':
141 $card_info['brand'] = $details['brand'] ?? '';
142 $card_info['last4'] = $details['last4'] ?? '';
143 $card_info['account_type'] = $details['receipt']['account_type'] ?? '';
144 $card_info['aid'] = $details['receipt']['dedicated_file_name'] ?? '';
145 $card_info['app_name'] = $details['receipt']['application_preferred_name'] ?? '';
146 break;
147 }
148 }
149
150 return array_map( 'sanitize_text_field', $card_info );
151 }
152 }
153