Skip to content

Commit

Permalink
New classes to store Stripe information
Browse files Browse the repository at this point in the history
  • Loading branch information
wjrosa committed Sep 12, 2024
1 parent b34de09 commit 925cf38
Show file tree
Hide file tree
Showing 3 changed files with 279 additions and 74 deletions.
97 changes: 97 additions & 0 deletions includes/class-wc-stripe-payment-information.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}

/**
* Class WC_Stripe_Payment_Information
*
* Represents a payment information object.
*/
class WC_Stripe_Payment_Information {
/**
* @var string The payment method.
*/
private $payment_method;

/**
* @var object The payment method details.
*/
private $payment_method_details;

/**
* @var string The selected payment type.
*/
private $selected_payment_type;

/**
* @var string The customer ID.
*/
private $customer;

/**
* @var boolean Whether the customer is using a saved payment method.
*/
private $is_using_saved_payment_method;

/**
* @var WC_Payment_Token|false
*/
private $token;


/**
* Constructor
*
* @param $payment_information array The payment information object.
*/
public function __construct( $payment_information ) {
$this->payment_method = $payment_information['payment_method'];
$this->payment_method_details = $payment_information['payment_method_details'];
$this->selected_payment_type = $payment_information['selected_payment_type'];
$this->customer = $payment_information['customer'];
$this->is_using_saved_payment_method = $payment_information['is_using_saved_payment_method'];

if ( $payment_information['token'] ) {
$this->token = $payment_information['token'];
} else {
$this->token = false;
}
}

/**
* @return string The selected payment method type.
*/
public function get_selected_payment_type() {
return $this->selected_payment_type;
}

/**
* @return string The payment method ID.
*/
public function get_payment_method() {
return $this->payment_method;
}

/**
* @return object The payment method details.
*/
public function get_payment_method_details() {
return $this->payment_method_details;
}

/**
* @return string The customer ID.
*/
public function get_customer() {
return $this->customer;
}

/**
* @return WC_Payment_Token|false The payment token.
*/
public function get_token() {
return $this->token;
}
}

139 changes: 139 additions & 0 deletions includes/class-wc-stripe-payment-intent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}

/**
* Class WC_Stripe_Payment_Intent
*
* Represents a Stripe PaymentIntent object.
*/
class WC_Stripe_Payment_Intent {
/**
* @var string The client secret.
*/
private string $client_secret;

/**
* @var string The payment intent status.
*/
private string $status;

/**
* @var array The payment method types.
*/
private array $payment_method_types;

/**
* @var object The next action object.
*/
private object $next_action;

/**
* @var object The charges object.
*/
private object $latest_charge;

/**
* Constructor
*
* @param $payment_intent_response object The payment intent response object.
*/
public function __construct( $payment_intent_response ) {
$this->client_secret = $payment_intent_response->client_secret;
$this->status = $payment_intent_response->status;
$this->payment_method_types = $payment_intent_response->payment_method_types;

if ( isset( $payment_intent_response->next_action ) ) {
$this->next_action = $payment_intent_response->next_action;
}

if ( ! empty( $payment_intent_response->charges->data ) ) {
$this->latest_charge = end( $payment_intent_response->charges->data );
} elseif ( ! empty( $payment_intent_response->latest_charge ) ) {
$this->latest_charge = $this->get_charge_object( $payment_intent_response->latest_charge );
}
}

/**
* @return string The client secret.
*/
public function get_client_secret() {
return $this->client_secret;
}

/**
* @return string The payment intent status.
*/
public function get_status() {
return $this->status;
}

/**
* @return string The next action type.
*/
public function get_next_action_type_url() {
return isset( $this->next_action->type )
&& in_array( $this->next_action->type, [ 'redirect_to_url', 'alipay_handle_redirect' ], true )
&& ! empty( $this->next_action->{$this->next_action->type}->url ) ? $this->next_action->{$this->next_action->type}->url : '';
}

/**
* @return bool Whether the payment methods contain a voucher or wallet payment method.
*/
public function contains_voucher_or_wallet_payment() {
return $this->contains_voucher_payment() || $this->contains_wallet_payment();
}

/**
* @return bool Whether the payment methods contain a voucher payment method.
*/
public function contains_voucher_payment() {
return count( array_intersect( [ 'boleto', 'oxxo', 'multibanco' ], $this->payment_method_types ) ) !== 0;
}

/**
* @return bool Whether the payment methods contain a wallet payment method.
*/
public function contains_wallet_payment() {
return count( array_intersect( [ 'wechat_pay', 'cashapp' ], $this->payment_method_types ) ) !== 0;
}

/**
* Get latest charge object from payment intent.
*
* Since API version 2022-11-15, the `charges` property was replaced with `latest_charge`.
* We can remove this method once we drop support for API versions prior to 2022-11-15.
*
* @since 7.0.2
*
* @return string|object
*/
public function get_latest_charge() {
return $this->latest_charge;
}

/**
* Get charge object by charge ID.
*
* @since 7.0.2
* @param string $charge_id The charge ID to get charge object for.
* @param array $params The parameters to pass to the request.
*
* @throws WC_Stripe_Exception Error while retrieving charge object.
* @return string|object
*/
public function get_charge_object( $charge_id = '', $params = [] ) {
if ( empty( $charge_id ) ) {
return '';
}

$charge_object = WC_Stripe_API::request( $params, 'charges/' . $charge_id, 'GET' );

if ( ! empty( $charge_object->error ) ) {
throw new WC_Stripe_Exception( print_r( $charge_object, true ), $charge_object->error->message );
}

return $charge_object;
}
}
Loading

0 comments on commit 925cf38

Please sign in to comment.