swissChili | f0cbdc3 | 2023-01-05 17:21:38 -0500 | [diff] [blame^] | 1 | <?php |
| 2 | if ( ! defined( 'ABSPATH' ) ) { |
| 3 | exit; |
| 4 | } |
| 5 | |
| 6 | /** |
| 7 | * Class WC_Stripe_UPE_Payment_Method_CC |
| 8 | */ |
| 9 | |
| 10 | /** |
| 11 | * Credit card Payment Method class extending UPE base class |
| 12 | */ |
| 13 | class WC_Stripe_UPE_Payment_Method_CC extends WC_Stripe_UPE_Payment_Method { |
| 14 | |
| 15 | const STRIPE_ID = 'card'; |
| 16 | |
| 17 | const LPM_GATEWAY_CLASS = WC_Gateway_Stripe::class; |
| 18 | |
| 19 | /** |
| 20 | * Constructor for card payment method |
| 21 | */ |
| 22 | public function __construct() { |
| 23 | parent::__construct(); |
| 24 | $this->stripe_id = self::STRIPE_ID; |
| 25 | $this->title = __( 'Pay with credit card / debit card', 'woocommerce-gateway-stripe' ); |
| 26 | $this->is_reusable = true; |
| 27 | $this->label = __( 'Credit card / debit card', 'woocommerce-gateway-stripe' ); |
| 28 | $this->description = __( |
| 29 | 'Let your customers pay with major credit and debit cards without leaving your store.', |
| 30 | 'woocommerce-gateway-stripe' |
| 31 | ); |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Returns payment method title |
| 36 | * |
| 37 | * @param array|bool $payment_details Optional payment details from charge object. |
| 38 | * |
| 39 | * @return string |
| 40 | */ |
| 41 | public function get_title( $payment_details = false ) { |
| 42 | if ( ! $payment_details ) { |
| 43 | return $this->title; |
| 44 | } |
| 45 | |
| 46 | $details = $payment_details[ $this->stripe_id ]; |
| 47 | $funding_types = [ |
| 48 | 'credit' => __( 'credit', 'woocommerce-gateway-stripe' ), |
| 49 | 'debit' => __( 'debit', 'woocommerce-gateway-stripe' ), |
| 50 | 'prepaid' => __( 'prepaid', 'woocommerce-gateway-stripe' ), |
| 51 | 'unknown' => __( 'unknown', 'woocommerce-gateway-stripe' ), |
| 52 | ]; |
| 53 | |
| 54 | return sprintf( |
| 55 | // Translators: %1$s card brand, %2$s card funding (prepaid, credit, etc.). |
| 56 | __( '%1$s %2$s card', 'woocommerce-gateway-stripe' ), |
| 57 | ucfirst( $details->network ), |
| 58 | $funding_types[ $details->funding ] |
| 59 | ); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Returns string representing payment method type |
| 64 | * to query to retrieve saved payment methods from Stripe. |
| 65 | */ |
| 66 | public function get_retrievable_type() { |
| 67 | return $this->get_id(); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Create and return WC payment token for user. |
| 72 | * |
| 73 | * This will be used from the WC_Stripe_Payment_Tokens service |
| 74 | * as opposed to WC_Stripe_UPE_Payment_Gateway. |
| 75 | * |
| 76 | * @param string $user_id WP_User ID |
| 77 | * @param object $payment_method Stripe payment method object |
| 78 | * |
| 79 | * @return WC_Payment_Token_CC |
| 80 | */ |
| 81 | public function create_payment_token_for_user( $user_id, $payment_method ) { |
| 82 | $token = new WC_Payment_Token_CC(); |
| 83 | $token->set_expiry_month( $payment_method->card->exp_month ); |
| 84 | $token->set_expiry_year( $payment_method->card->exp_year ); |
| 85 | $token->set_card_type( strtolower( $payment_method->card->brand ) ); |
| 86 | $token->set_last4( $payment_method->card->last4 ); |
| 87 | $token->set_gateway_id( WC_Stripe_UPE_Payment_Gateway::ID ); |
| 88 | $token->set_token( $payment_method->id ); |
| 89 | $token->set_user_id( $user_id ); |
| 90 | $token->save(); |
| 91 | return $token; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Returns boolean dependent on whether capability |
| 96 | * for site account is enabled for payment method. |
| 97 | * |
| 98 | * @return bool |
| 99 | */ |
| 100 | public function is_capability_active() { |
| 101 | return true; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * The Credit Card method allows automatic capture. |
| 106 | * |
| 107 | * @return bool |
| 108 | */ |
| 109 | public function requires_automatic_capture() { |
| 110 | return false; |
| 111 | } |
| 112 | } |