swissChili | f0cbdc3 | 2023-01-05 17:21:38 -0500 | [diff] [blame^] | 1 | <?php |
| 2 | if ( ! defined( 'ABSPATH' ) ) { |
| 3 | exit; |
| 4 | } |
| 5 | |
| 6 | /** |
| 7 | * Link Payment Method class extending UPE base class |
| 8 | */ |
| 9 | class WC_Stripe_UPE_Payment_Method_Link extends WC_Stripe_UPE_Payment_Method { |
| 10 | |
| 11 | const STRIPE_ID = 'link'; |
| 12 | |
| 13 | /** |
| 14 | * Constructor for Link payment method |
| 15 | */ |
| 16 | public function __construct() { |
| 17 | parent::__construct(); |
| 18 | $this->stripe_id = self::STRIPE_ID; |
| 19 | $this->title = __( 'Pay with Link', 'woocommerce-gateway-stripe' ); |
| 20 | $this->is_reusable = true; |
| 21 | $this->supported_currencies = [ 'USD' ]; |
| 22 | $this->label = __( 'Stripe Link', 'woocommerce-gateway-stripe' ); |
| 23 | $this->description = __( |
| 24 | 'Link is a payment method that allows customers to save payment information and use the payment details |
| 25 | for further payments.', |
| 26 | 'woocommerce-gateway-stripe' |
| 27 | ); |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * Return if Stripe Link is enabled |
| 32 | * |
| 33 | * @return bool |
| 34 | */ |
| 35 | public static function is_link_enabled() { |
| 36 | |
| 37 | // Assume Link is disabled if UPE is disabled. |
| 38 | if ( ! WC_Stripe_Feature_Flags::is_upe_checkout_enabled() ) { |
| 39 | return false; |
| 40 | } |
| 41 | |
| 42 | return in_array( |
| 43 | self::STRIPE_ID, |
| 44 | woocommerce_gateway_stripe()->get_main_stripe_gateway()->get_upe_enabled_payment_method_ids(), |
| 45 | true |
| 46 | ); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Returns string representing payment method type |
| 51 | * to query to retrieve saved payment methods from Stripe. |
| 52 | */ |
| 53 | public function get_retrievable_type() { |
| 54 | return $this->get_id(); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Create new WC payment token and add to user. |
| 59 | * |
| 60 | * @param int $user_id WP_User ID |
| 61 | * @param object $payment_method Stripe payment method object |
| 62 | * |
| 63 | * @return WC_Payment_Token_Link |
| 64 | */ |
| 65 | public function create_payment_token_for_user( $user_id, $payment_method ) { |
| 66 | $token = new WC_Payment_Token_Link(); |
| 67 | $token->set_email( $payment_method->link->email ); |
| 68 | $token->set_gateway_id( WC_Stripe_UPE_Payment_Gateway::ID ); |
| 69 | $token->set_token( $payment_method->id ); |
| 70 | $token->set_payment_method_type( $this->get_id() ); |
| 71 | $token->set_user_id( $user_id ); |
| 72 | $token->save(); |
| 73 | return $token; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Returns true if the UPE method is available. |
| 78 | * |
| 79 | * @return bool |
| 80 | */ |
| 81 | public function is_available() { |
| 82 | //if merchant is outside US, Link payment method should not be available |
| 83 | $cached_account_data = WC_Stripe::get_instance()->account->get_cached_account_data(); |
| 84 | $account_country = $cached_account_data['country'] ?? null; |
| 85 | |
| 86 | return 'US' === $account_country && parent::is_available(); |
| 87 | } |
| 88 | } |