| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
die( 'You are not allowed to call this page directly.' ); |
| 4 |
} |
| 5 |
|
| 6 |
/** |
| 7 |
* The majority of Stripe Connect logic lies in FrmStrpLiteConnectHelper |
| 8 |
* The purpose of this Adapter is to mirror FrmStrpApiHelper's interface |
| 9 |
*/ |
| 10 |
class FrmStrpLiteConnectApiAdapter { |
| 11 |
|
| 12 |
/** |
| 13 |
* @return bool |
| 14 |
*/ |
| 15 |
public static function initialize_api() { |
| 16 |
return FrmStrpLiteConnectHelper::stripe_connect_is_setup(); |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* @param string $sub_id |
| 21 |
* |
| 22 |
* @return bool |
| 23 |
*/ |
| 24 |
public static function cancel_subscription( $sub_id ) { |
| 25 |
if ( current_user_can( 'administrator' ) ) { |
| 26 |
$customer_id = false; |
| 27 |
} else { |
| 28 |
$customer = self::get_customer_by_id( get_current_user_id() ); |
| 29 |
|
| 30 |
if ( ! is_object( $customer ) ) { |
| 31 |
return false; |
| 32 |
} |
| 33 |
|
| 34 |
$customer_id = $customer->id; |
| 35 |
} |
| 36 |
|
| 37 |
return FrmStrpLiteConnectHelper::cancel_subscription( $sub_id, $customer_id ); |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Cancel a subscription without checking that it belongs to the current user. |
| 42 |
* Use this when there is no logged in user, like when a webhook event is processed. |
| 43 |
* The customer check in self::cancel_subscription would always fail there because the current user ID is 0. |
| 44 |
* |
| 45 |
* @since 6.35 |
| 46 |
* |
| 47 |
* @param string $sub_id |
| 48 |
* |
| 49 |
* @return bool |
| 50 |
*/ |
| 51 |
public static function cancel_subscription_without_customer_check( $sub_id ) { |
| 52 |
return FrmStrpLiteConnectHelper::cancel_subscription( $sub_id ); |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* @param string $payment_id |
| 57 |
* |
| 58 |
* @return bool |
| 59 |
*/ |
| 60 |
public static function refund_payment( $payment_id ) { |
| 61 |
return FrmStrpLiteConnectHelper::refund_payment( $payment_id ); |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Get the payment intent from Stripe |
| 66 |
* |
| 67 |
* @param string $payment_id |
| 68 |
* |
| 69 |
* @return mixed |
| 70 |
*/ |
| 71 |
public static function get_intent( $payment_id ) { |
| 72 |
return FrmStrpLiteConnectHelper::get_intent( $payment_id ); |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* @return array |
| 77 |
*/ |
| 78 |
public static function get_customer_subscriptions() { |
| 79 |
return FrmStrpLiteConnectHelper::get_customer_subscriptions(); |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* @param array $options |
| 84 |
* |
| 85 |
* @return object|string |
| 86 |
*/ |
| 87 |
public static function get_customer( $options = array() ) { |
| 88 |
$user_id = ! empty( $options['user_id'] ) ? $options['user_id'] : get_current_user_id(); |
| 89 |
$meta_name = FrmStrpLiteAppHelper::get_customer_id_meta_name(); |
| 90 |
$customer_id_error_message = ''; |
| 91 |
|
| 92 |
if ( $user_id ) { |
| 93 |
$customer_id = get_user_meta( $user_id, $meta_name, true ); |
| 94 |
|
| 95 |
if ( ! isset( $options['email'] ) ) { |
| 96 |
$user_info = get_userdata( $user_id ); |
| 97 |
|
| 98 |
if ( ! empty( $user_info->user_email ) ) { |
| 99 |
$options['email'] = $user_info->user_email; |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
if ( $customer_id ) { |
| 104 |
$options['customer_id'] = $customer_id; |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
if ( isset( $options['user_id'] ) ) { |
| 109 |
unset( $options['user_id'] ); |
| 110 |
} |
| 111 |
|
| 112 |
$a_customer_id_value_was_previously_set = ! empty( $customer_id ); |
| 113 |
$customer_id = FrmStrpLiteConnectHelper::get_customer_id( $options ); |
| 114 |
|
| 115 |
if ( $customer_id ) { |
| 116 |
$customer_id_is_actually_an_error_message = ! str_contains( $customer_id, 'cus_' ); |
| 117 |
|
| 118 |
if ( $customer_id_is_actually_an_error_message ) { |
| 119 |
$customer_id_error_message = $customer_id; |
| 120 |
$customer_id = false; |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
if ( ! $customer_id ) { |
| 125 |
if ( $a_customer_id_value_was_previously_set ) { |
| 126 |
delete_user_meta( $user_id, $meta_name ); |
| 127 |
} |
| 128 |
|
| 129 |
if ( $customer_id_error_message ) { |
| 130 |
return $customer_id_error_message; |
| 131 |
} |
| 132 |
|
| 133 |
return __( 'Unable to retrieve customer through Stripe Connect.', 'formidable' ); |
| 134 |
} |
| 135 |
|
| 136 |
if ( $user_id ) { |
| 137 |
update_user_meta( $user_id, $meta_name, $customer_id ); |
| 138 |
} |
| 139 |
|
| 140 |
return self::create_decoy_customer( $customer_id ); |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* @param string $customer_id |
| 145 |
* |
| 146 |
* @return object |
| 147 |
*/ |
| 148 |
private static function create_decoy_customer( $customer_id ) { |
| 149 |
$decoy_object = new stdClass(); |
| 150 |
$decoy_object->id = $customer_id; |
| 151 |
$decoy_object->type = 'customer'; |
| 152 |
return $decoy_object; |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* @param int $user_id |
| 157 |
* |
| 158 |
* @return mixed |
| 159 |
*/ |
| 160 |
public static function get_customer_by_id( $user_id ) { |
| 161 |
$meta_name = FrmStrpLiteAppHelper::get_customer_id_meta_name(); |
| 162 |
$customer_id = get_user_meta( $user_id, $meta_name, true ); |
| 163 |
|
| 164 |
if ( ! $customer_id ) { |
| 165 |
return false; |
| 166 |
} |
| 167 |
|
| 168 |
if ( ! FrmStrpLiteConnectHelper::validate_customer( $customer_id ) ) { |
| 169 |
delete_user_meta( $user_id, $meta_name ); |
| 170 |
return false; |
| 171 |
} |
| 172 |
|
| 173 |
return self::create_decoy_customer( $customer_id ); |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* @param string $event_id |
| 178 |
* |
| 179 |
* @return mixed |
| 180 |
*/ |
| 181 |
public static function get_event( $event_id ) { |
| 182 |
return FrmStrpLiteConnectHelper::get_event( $event_id ); |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* @param array $plan |
| 187 |
* |
| 188 |
* @return mixed |
| 189 |
*/ |
| 190 |
public static function maybe_create_plan( $plan ) { |
| 191 |
return FrmStrpLiteConnectHelper::maybe_create_plan( $plan ); |
| 192 |
} |
| 193 |
|
| 194 |
/** |
| 195 |
* @param array $new_charge |
| 196 |
* |
| 197 |
* @return false|object|string |
| 198 |
*/ |
| 199 |
public static function create_subscription( $new_charge ) { |
| 200 |
return FrmStrpLiteConnectHelper::create_subscription( $new_charge ); |
| 201 |
} |
| 202 |
|
| 203 |
/** |
| 204 |
* @param array $new_charge |
| 205 |
* |
| 206 |
* @return mixed |
| 207 |
*/ |
| 208 |
public static function create_intent( $new_charge ) { |
| 209 |
return FrmStrpLiteConnectHelper::create_intent( $new_charge ); |
| 210 |
} |
| 211 |
|
| 212 |
/** |
| 213 |
* @param string $intent_id |
| 214 |
* @param array $data |
| 215 |
* |
| 216 |
* @return mixed |
| 217 |
*/ |
| 218 |
public static function update_intent( $intent_id, $data ) { |
| 219 |
return FrmStrpLiteConnectHelper::update_intent( $intent_id, $data ); |
| 220 |
} |
| 221 |
|
| 222 |
/** |
| 223 |
* Create a setup intent for a Stripe link recurring payment. |
| 224 |
* This is called when a form is loaded. |
| 225 |
* |
| 226 |
* @since 6.5, introduced in v3.0 of the Stripe add on. |
| 227 |
* |
| 228 |
* @param string $customer_id Customer ID beginning with cus_. |
| 229 |
* @param array|false $payment_method_types If false the types will defaults to array( 'card', 'link' ). |
| 230 |
* |
| 231 |
* @return false|object|string |
| 232 |
*/ |
| 233 |
public static function create_setup_intent( $customer_id, $payment_method_types = false ) { |
| 234 |
return FrmStrpLiteConnectHelper::create_setup_intent( $customer_id, $payment_method_types ); |
| 235 |
} |
| 236 |
|
| 237 |
/** |
| 238 |
* Get a setup intent (used for Stripe link recurring payments). |
| 239 |
* |
| 240 |
* @since 6.5, introduced in v3.0 of the Stripe add on. |
| 241 |
* |
| 242 |
* @param string $setup_id |
| 243 |
* |
| 244 |
* @return false|object|string |
| 245 |
*/ |
| 246 |
public static function get_setup_intent( $setup_id ) { |
| 247 |
return FrmStrpLiteConnectHelper::get_setup_intent( $setup_id ); |
| 248 |
} |
| 249 |
} |
| 250 |
|