Skip to content

Commit

Permalink
Tidy up
Browse files Browse the repository at this point in the history
  • Loading branch information
nicdwilson committed Jan 10, 2024
1 parent 67dd9f6 commit 80052e3
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 93 deletions.
26 changes: 11 additions & 15 deletions includes/class-customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

use Exception;
use Faker\Factory;
use http\Message;
use WP_Error;

if ( ! defined( 'ABSPATH' ) ) {
Expand All @@ -21,9 +20,6 @@ class Customer {
*/
private array $settings;


private string $user_id;

/**
* Customer The instance of Customer
*
Expand Down Expand Up @@ -168,11 +164,11 @@ public function get_random_user() {
}

if ( is_array( $users ) && ! empty( $users[0] ) ) {
$this->user_id = $users[0];
$user_id = $users[0];
} else {
$this->user_id = $this->create_user();
$user_id = $this->create_user();
}
return $this->user_id;
return $user_id;
}

public function get_id() {
Expand All @@ -182,9 +178,9 @@ public function get_id() {
/**
* Get the customer Stripe ID. This is stored as a user option, to be multisite compatible.
*
* @return mixed
* @return string
*/
public function get_stripe_customer_id() {
public function get_stripe_customer_id(): string {
return get_user_meta( $this->ID, 'wp__stripe_customer_id', true );
}

Expand All @@ -200,9 +196,9 @@ public function get_customer_ip(): string {
/**
* Create a password
*
* @return mixed|null
* @return string
*/
private function get_password() {
private function get_password(): string {
return wp_generate_password();
}

Expand All @@ -226,9 +222,9 @@ private function get_customer_firstname( $faker ): string {
* @param $faker
* @param $user_id
*
* @return mixed|string
* @return string
*/
private function get_customer_lastname( $faker, $user_id ) {
private function get_customer_lastname( $faker, $user_id ): string {

if ( 'faker' === $this->settings['customer_naming_convention'] ) {
$last_name = $faker->lastName;
Expand Down Expand Up @@ -257,7 +253,7 @@ private function get_customer_email( $faker, $user_id ): string {
break;
default:
$email = $faker->email;
};
}

return $email;
}
Expand Down Expand Up @@ -374,7 +370,7 @@ private function get_faker_countries(): array {
foreach ( $locale_list as $key => $value ) {
if ( 'd' === ( $value['type'] ) ) {
// we currently use only en locales because special characters are giving us trouble
if( 'en' !== substr( $value['name'], 0, 2 ) ) {
if( str_starts_with( $value['name'], 'en' ) ) {
continue;
}
// Extract the country code from the locale
Expand Down
62 changes: 38 additions & 24 deletions includes/class-gateway-integration-stripe.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,12 @@

namespace Happy_Order_Generator;

use AutomateWoo\Log;
use Exception;
use http\Env\Request;
use WC_Gateway_Stripe;
use WC_Payment_Token_CC;
use WC_Stripe_API;
use WC_Stripe_Exception;
use WC_Stripe_Helper;
use WC_Stripe_Payment_Gateway;
use function Crontrol\Schedule\add;

if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
Expand Down Expand Up @@ -63,7 +59,7 @@ class Gateway_Integration_Stripe {
*
* Ensures only one instance of Gateway is loaded or can be loaded.
*
* @return Gateway_Stripe_Integration instance
* @return Gateway_Integration_Stripe instance
* @since 1.0.0
* @static
*/
Expand All @@ -85,7 +81,10 @@ public function __construct() {
/**
* Failed orders going through Stripe are handled by Happy Order Generator, so we intercept them here
*/
add_action( 'woocommerce_rest_checkout_process_payment_with_context', array( $this, 'do_failure_check' ), 10, 2 );
add_action( 'woocommerce_rest_checkout_process_payment_with_context', array(
$this,
'do_failure_check'
), 10, 2 );

/**
* Handle generated failed orders
Expand All @@ -100,14 +99,14 @@ public function __construct() {
* - is listed as available
* - is in test mode
*
* @param $supported_gateways
* @param array $supported_gateways
*
* @return mixed
* @return array The supported gateways
*/
public function add_stripe_support( $supported_gateways ) {
public function add_stripe_support( array $supported_gateways ): array {

/**
* Is Stripe plugin active? Doublecheck because there are clones out there
* Is Stripe plugin active? Double-check because there are clones out there
*/
if ( ! is_plugin_active( 'woocommerce-gateway-stripe/woocommerce-gateway-stripe.php' ) || ! class_exists( WC_Gateway_Stripe::class ) ) {
return $supported_gateways;
Expand Down Expand Up @@ -204,7 +203,7 @@ public function do_checkout_fail( $order, $options ): void {
}

/**
* We always just fail anyway
* We always just fail anyway. That's why we're here.
*/
$order->update_status( 'failed' );
}
Expand Down Expand Up @@ -253,7 +252,7 @@ public function get_payment_data( $user_id = '', $final_status = true ) {

$payment_method = $this->get_payment_method( $final_status );

if ( false !== strpos( $payment_method->id, 'cus_', 0 ) ) {
if ( str_contains( $payment_method->id, 'cus_' ) ) {
$payment_method_id = $payment_method->default_source;
if ( $stripe_customer_id !== $payment_method->id ) {
$stripe_customer_id = $payment_method->id;
Expand All @@ -265,7 +264,7 @@ public function get_payment_data( $user_id = '', $final_status = true ) {

/**
* If the payment method failed, mark the order as failed
* and bail immediately, otherwise setup a payment intent
* and bail immediately, otherwise set up a payment intent
*/
if ( isset( $payment_method_id ) && ! empty( $payment_method_id ) ) {
$output['stripe_source_id'] = $payment_method_id;
Expand Down Expand Up @@ -316,7 +315,7 @@ public function pay_generated_order( $order, $final_status = true ) {

$payment_method = $this->get_payment_method( $final_status );

if ( false !== strpos( $payment_method->id, 'cus_', 0 ) ) {
if ( str_contains( $payment_method->id, 'cus_' ) ) {
$payment_method_id = $payment_method->default_source;
if ( $stripe_customer_id !== $payment_method->id ) {
$stripe_customer_id = $payment_method->id;
Expand All @@ -329,7 +328,7 @@ public function pay_generated_order( $order, $final_status = true ) {

/**
* If the payment method failed, mark the order as failed
* and bail immediately, otherwise setup a payment intent
* and bail immediately, otherwise set up a payment intent
*/
if ( isset( $payment_method_id ) && ! empty( $payment_method_id ) ) {
Logger::log( 'Stripe order successfully paid' );
Expand All @@ -344,7 +343,7 @@ public function pay_generated_order( $order, $final_status = true ) {
$order->update_meta_data( '_stripe_source_id', $payment_method_id );
$payment_intent = $this->setup_payment_intent( $payment_method_id, $stripe_customer_id, $order );

if ( isset( $payment_intent ) && ! empty( $payment_intent ) ) {
if ( ! empty( $payment_intent ) ) {
Logger::log( 'Stripe payment intent ' . $payment_intent->id );
} else {
Logger::log( 'There was a problem with the Stripe payment intent.' );
Expand Down Expand Up @@ -418,21 +417,36 @@ public function create_stripe_customer(): string|bool {
}


/**
* Confirm the payment intent
*
* @param $payment_intent_id
* @param $payment_method_id
*
* //todo tidy this up??
* @return array|false|\stdClass
*/
public function confirm_payment_intent( $payment_intent_id, $payment_method_id ) {

$request = array(
'payment_method' => $payment_method_id
);

$response = WC_Stripe_API::request( $request, 'payment_intents/' . $payment_intent_id . '/confirm' );
try {
$response = WC_Stripe_API::request( $request, 'payment_intents/' . $payment_intent_id . '/confirm' );
} catch ( Exception $ex ) {
Logger::log( 'Confirming the Stripe payment intent failed with ' . $ex->getMessage() );
return false;
}

return $response;
}

/**
* Setup the payment intent.
* Set up the payment intent.
*
* @param $payment_method_id
* @param $stripe_customer_id
* @param $order
*
* @return object|bool
Expand Down Expand Up @@ -493,12 +507,12 @@ public function setup_payment_intent( $payment_method_id, $stripe_customer_id, $
/**
* Submit card details to get back the payment method.
*
* @param $final_status
* @param string $final_status
*
* @return array|
* @return object|bool The payment method object or false if it fails
* @throws WC_Stripe_Exception
*/
function get_payment_method( $final_status = 'processing' ): object|bool {
function get_payment_method( string $final_status = 'processing' ): object|bool {

if ( $final_status != 'failed' ) {
$card_number = '4242424242424242';
Expand All @@ -518,7 +532,7 @@ function get_payment_method( $final_status = 'processing' ): object|bool {

$response = WC_Stripe_API::request( $request, 'payment_methods' );

if ( strpos( $response->id, 'pm_' ) !== false ) {
if ( str_contains( $response->id, 'pm_' ) ) {
return $response;
}

Expand All @@ -531,7 +545,7 @@ function get_payment_method( $final_status = 'processing' ): object|bool {
Logger::log( 'Creating the Stripe payment method failed with ' . $ex->getMessage() );
}

if ( isset( $response->id ) && ! empty( $response->id ) ) {
if ( ! empty( $response->id ) ) {
return $response;
} else {
return false;
Expand All @@ -546,7 +560,7 @@ function get_payment_method( $final_status = 'processing' ): object|bool {
*
* @return bool
*/
public function has_subscription( $order_id ) {
public function has_subscription( $order_id ): bool {
return ( function_exists( 'wcs_order_contains_subscription' ) && ( wcs_order_contains_subscription( $order_id ) || wcs_is_subscription( $order_id ) || wcs_order_contains_renewal( $order_id ) ) );
}
}
Loading

0 comments on commit 80052e3

Please sign in to comment.