| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\App\Modules\Payments\PaymentMethods\Stripe\API; |
| 4 |
|
| 5 |
use FluentForm\App\Modules\Payments\PaymentMethods\Stripe\StripeSettings; |
| 6 |
|
| 7 |
if (!defined('ABSPATH')) { |
| 8 |
exit; |
| 9 |
} |
| 10 |
|
| 11 |
class Customer |
| 12 |
{ |
| 13 |
use RequestProcessor; |
| 14 |
|
| 15 |
public static function createCustomer($customerArgs, $formId) |
| 16 |
{ |
| 17 |
$errors = static::validate($customerArgs); |
| 18 |
|
| 19 |
if ($errors) { |
| 20 |
return static::errorHandler('validation_failed', __('Payment data validation failed', 'fluentform'), $errors); |
| 21 |
} |
| 22 |
|
| 23 |
try { |
| 24 |
$secretKey = apply_filters_deprecated( |
| 25 |
'fluentform-payment_stripe_secret_key', |
| 26 |
[ |
| 27 |
StripeSettings::getSecretKey($formId), |
| 28 |
$formId |
| 29 |
], |
| 30 |
FLUENTFORM_FRAMEWORK_UPGRADE, |
| 31 |
'fluentform/payment_stripe_secret_key', |
| 32 |
'Use fluentform/payment_stripe_secret_key instead of fluentform-payment_stripe_secret_key.' |
| 33 |
); |
| 34 |
$secretKey = apply_filters('fluentform/payment_stripe_secret_key', $secretKey, $formId); |
| 35 |
|
| 36 |
ApiRequest::set_secret_key($secretKey); |
| 37 |
|
| 38 |
$response = ApiRequest::request($customerArgs, 'customers'); |
| 39 |
|
| 40 |
$response = static::processResponse($response); |
| 41 |
|
| 42 |
do_action_deprecated( |
| 43 |
'fluentform_stripe_customer_created', |
| 44 |
[ |
| 45 |
$response, |
| 46 |
$customerArgs |
| 47 |
], |
| 48 |
FLUENTFORM_FRAMEWORK_UPGRADE, |
| 49 |
'fluentform/stripe_customer_created', |
| 50 |
'Use fluentform/stripe_customer_created instead of fluentform_stripe_customer_created.' |
| 51 |
); |
| 52 |
|
| 53 |
do_action('fluentform/stripe_customer_created', $response, $customerArgs); |
| 54 |
|
| 55 |
return $response; |
| 56 |
} catch (\Exception $e) { |
| 57 |
// Something else happened, completely unrelated to Stripe |
| 58 |
return static::errorHandler('non_stripe', esc_html__('General Error', 'fluentform') . ': ' . $e->getMessage()); |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
public static function validate($args) |
| 63 |
{ |
| 64 |
$errors = []; |
| 65 |
|
| 66 |
if (empty($args['source']) && empty($args['payment_method'])) { |
| 67 |
$errors['source'] = __('Stripe token/payment_method is required', 'fluentform'); |
| 68 |
} |
| 69 |
|
| 70 |
return $errors; |
| 71 |
} |
| 72 |
} |
| 73 |
|