Skip to content

Commit

Permalink
Additional getters and setters
Browse files Browse the repository at this point in the history
  • Loading branch information
wjrosa committed Dec 26, 2024
1 parent 2f4dac8 commit 9ac5a55
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 25 deletions.
14 changes: 8 additions & 6 deletions includes/abstracts/abstract-wc-stripe-payment-gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,8 @@ public function generate_payment_request( $order, $prepared_payment_method ) {
/**
* Store extra meta data for an order from a Stripe Response.
*
* @param object $response The Stripe response.
* @param WC_Stripe_Order $order The order object.
* @throws WC_Stripe_Exception
*/
public function process_response( $response, $order ) {
Expand All @@ -542,7 +544,7 @@ public function process_response( $response, $order ) {
}

if ( isset( $response->payment_method_details->card->mandate ) ) {
$order->update_meta_data( '_stripe_mandate_id', $response->payment_method_details->card->mandate );
$order->set_mandate_id( $response->payment_method_details->card->mandate );
}

if ( isset( $response->payment_method, $response->payment_method_details ) ) {
Expand Down Expand Up @@ -1200,7 +1202,7 @@ public function process_refund( $order_id, $amount = null, $reason = '' ) {
}
}

$order->update_meta_data( '_stripe_refund_id', $response->id );
$order->set_refund_id( $response->id );

if ( isset( $response->balance_transaction ) ) {
$this->update_fees( $order, $response->balance_transaction );
Expand Down Expand Up @@ -1592,7 +1594,7 @@ public function confirm_intent( $intent, $order, $prepared_source ) {
* Saves intent to order.
*
* @since 3.2.0
* @param WC_Order $order For to which the source applies.
* @param WC_Stripe_Order $order For to which the source applies.
* @param stdClass $intent Payment intent information.
*/
public function save_intent_to_order( $order, $intent ) {
Expand All @@ -1608,7 +1610,7 @@ public function save_intent_to_order( $order, $intent ) {
$charge = $this->get_latest_charge_from_intent( $intent );

if ( isset( $charge->payment_method_details->card->mandate ) ) {
$order->update_meta_data( '_stripe_mandate_id', $charge->payment_method_details->card->mandate );
$order->set_mandate_id( $charge->payment_method_details->card->mandate );
}
} elseif ( 'setup_intent' === $intent->object ) {
$order->update_meta_data( '_stripe_setup_intent', $intent->id );
Expand Down Expand Up @@ -1672,7 +1674,7 @@ private function get_intent( $intent_type, $intent_id ) {
* Locks an order for payment intent processing for 5 minutes.
*
* @since 4.2
* @param WC_Order $order The order that is being paid.
* @param WC_Stripe_Order $order The order that is being paid.
* @param stdClass $intent The intent that is being processed.
* @return bool A flag that indicates whether the order is already locked.
*/
Expand All @@ -1694,7 +1696,7 @@ public function lock_order_payment( $order, $intent = null ) {

$new_lock = ( time() + 5 * MINUTE_IN_SECONDS ) . ( isset( $intent->id ) ? '|' . $intent->id : '' );

$order->update_meta_data( '_stripe_lock_payment', $new_lock );
$order->set_lock_payment( $new_lock );
$order->save_meta_data();

return false;
Expand Down
76 changes: 76 additions & 0 deletions includes/class-wc-stripe-order.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,82 @@
* Wrapper for the original WC_Order class to allow custom getters and setter with the extension's specific metadata.
*/
class WC_Stripe_Order extends WC_Order {
/**
* Set the mandate ID.
*
* @param $mandate_id string The mandate ID to set.
* @return void
*/
public function set_mandate_id( $mandate_id ) {
$this->update_meta_data( '_stripe_mandate_id', $mandate_id );
}

/**
* Get the mandate ID.
*
* @return string
*/
public function get_mandate_id() {
return $this->get_meta( '_stripe_mandate_id' );
}

/**
* Set the lock payment time.
*
* @param $time int The time to set.
* @return void
*/
public function set_lock_payment( $time ) {
$this->update_meta_data( '_stripe_lock_payment', $time );
}

/**
* Get the lock payment time.
*
* @return int
*/
public function get_lock_payment() {
return $this->get_meta( '_stripe_lock_payment' );
}

/**
* Set the refund ID.
*
* @param $refund_id string The refund ID to set.
* @return void
*/
public function set_refund_id( $refund_id ) {
$this->update_meta_data( '_stripe_refund_id', $refund_id );
}

/**
* Get the refund ID.
*
* @return string
*/
public function get_refund_id() {
return $this->get_meta( '_stripe_refund_id' );
}

/**
* Set the Multibanco data.
*
* @param $data array The Multibanco data to set.
* @return void
*/
public function set_multibanco_data( $data ) {
$this->update_meta_data( '_stripe_multibanco', $data );
}

/**
* Get the Multibanco data.
*
* @return array
*/
public function get_multibanco_data() {
return $this->get_meta( '_stripe_multibanco' );
}

/**
* Set the Stripe intent ID.
*
Expand Down
2 changes: 1 addition & 1 deletion includes/class-wc-stripe-webhook-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,7 @@ public function process_webhook_refund( $notification ) {
WC_Stripe_Logger::log( $refund->get_error_message() );
}

$order->update_meta_data( '_stripe_refund_id', $refund_object->id );
$order->set_refund_id( $refund_object->id );

if ( isset( $refund_object->balance_transaction ) ) {
$this->update_fees( $order, $refund_object->balance_transaction );
Expand Down
10 changes: 4 additions & 6 deletions includes/payment-methods/class-wc-gateway-stripe-multibanco.php
Original file line number Diff line number Diff line change
Expand Up @@ -238,14 +238,14 @@ public function email_instructions( $order, $sent_to_admin, $plain_text = false
*
* @since 4.1.0
* @version 4.1.0
* @param int|WC_Order $order
* @param int|WC_Stripe_Order $order
*/
public function get_instructions( $order, $plain_text = false ) {
if ( true === is_int( $order ) ) {
$order = wc_get_order( $order );
}

$data = $order->get_meta( '_stripe_multibanco' );
$data = $order->get_multibanco_data();

if ( $plain_text ) {
esc_html_e( 'MULTIBANCO INFORMAÇÕES DE ENCOMENDA:', 'woocommerce-gateway-stripe' ) . "\n\n";
Expand Down Expand Up @@ -284,7 +284,7 @@ public function get_instructions( $order, $plain_text = false ) {
*
* @since 4.1.0
* @version 4.1.0
* @param object $order
* @param WC_Stripe_Order $order
* @param object $source_object
*/
public function save_instructions( $order, $source_object ) {
Expand All @@ -294,9 +294,7 @@ public function save_instructions( $order, $source_object ) {
'reference' => $source_object->multibanco->reference,
];

$order_id = $order->get_id();

$order->update_meta_data( '_stripe_multibanco', $data );
$order->set_multibanco_data( $data );
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ public function email_instructions( $order, $sent_to_admin, $plain_text = false
/**
* Gets Multibanco payment instructions for the customer.
*
* @param WC_Order $order
* @param WC_Stripe_Order $order
* @param bool $plain_text
*/
public function get_instructions( $order, $plain_text = false ) {
$data = $order->get_meta( '_stripe_multibanco' );
$data = $order->get_multibanco_data();
if ( ! $data ) {
return;
}
Expand Down Expand Up @@ -111,7 +111,7 @@ public function get_instructions( $order, $plain_text = false ) {
/**
* Saves Multibanco information to the order meta for later use.
*
* @param object $order
* @param WC_Stripe_Order $order
* @param object $payment_intent. The PaymentIntent object.
*/
public function save_instructions( $order, $payment_intent ) {
Expand All @@ -125,7 +125,7 @@ public function save_instructions( $order, $payment_intent ) {
'reference' => $payment_intent->next_action->multibanco_display_details->reference,
];

$order->update_meta_data( '_stripe_multibanco', $data );
$order->set_multibanco_data( $data );
}

/**
Expand Down
6 changes: 3 additions & 3 deletions tests/phpunit/test-class-wc-stripe-upe-payment-gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ public function test_process_payment_returns_valid_response() {
$currency = $order->get_currency();
$order_id = $order->get_id();

$order->update_meta_data( '_stripe_intent_id', $payment_intent_id );
$order->set_intent_id( $payment_intent_id );
$order->set_upe_payment_type( '' );
$order->update_meta_data( '_stripe_upe_waiting_for_redirect', true );
$order->save();
Expand Down Expand Up @@ -1769,7 +1769,7 @@ public function test_subscription_renewal_is_successful() {

list( $amount, $description, $metadata ) = $this->get_order_details( $order );
$order->set_payment_method( WC_Stripe_UPE_Payment_Gateway::ID );
$order->update_meta_data( '_stripe_lock_payment', ( time() + MINUTE_IN_SECONDS ) ); // To assist with comparing expected order objects, set an existing lock.
$order->set_lock_payment( ( time() + MINUTE_IN_SECONDS ) ); // To assist with comparing expected order objects, set an existing lock.
$order->save();

$order = wc_get_order( $order_id );
Expand Down Expand Up @@ -1863,7 +1863,7 @@ public function test_subscription_renewal_checks_payment_method_authorization()

list( $amount, $description, $metadata ) = $this->get_order_details( $order );
$order->set_payment_method( WC_Stripe_UPE_Payment_Gateway::ID );
$order->update_meta_data( '_stripe_lock_payment', ( time() + MINUTE_IN_SECONDS ) ); // To assist with comparing expected order objects, set an existing lock.
$order->set_lock_payment( ( time() + MINUTE_IN_SECONDS ) ); // To assist with comparing expected order objects, set an existing lock.
$order->save();

$order = wc_get_order( $order_id );
Expand Down
8 changes: 4 additions & 4 deletions tests/phpunit/test-wc-stripe-payment-gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -578,8 +578,8 @@ public function test_render_subscription_payment_method() {
$mock_subscription = WC_Helper_Order::create_order(); // We can use an order as a subscription.
$mock_subscription->set_payment_method( 'stripe' );

$mock_subscription->update_meta_data( '_stripe_source_id', 'src_mock' );
$mock_subscription->update_meta_data( '_stripe_customer_id', 'cus_mock' );
$mock_subscription->set_source_id( 'src_mock' );
$mock_subscription->set_stripe_customer_id( 'cus_mock' );
$mock_subscription->save();

// This is the key the customer's payment methods are stored under in the transient.
Expand Down Expand Up @@ -655,11 +655,11 @@ public function test_lock_order_payment() {

// test expired locks.
$order_3 = WC_Helper_Order::create_order();
$order_3->update_meta_data( '_stripe_lock_payment', time() - 1 );
$order_3->set_lock_payment( time() - 1 );
$order_3->save_meta_data();

$locked = $this->gateway->lock_order_payment( $order_3, $intent_id );
$current_lock = $order_3->get_meta( '_stripe_lock_payment' );
$current_lock = $order_3->get_lock_payment();

$this->assertFalse( $locked );
$this->assertEqualsWithDelta( (int) $current_lock, ( time() + 5 * MINUTE_IN_SECONDS ), 3 );
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/test-wc-stripe-webhook-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ public function test_process_webhook_dispute( $order_status, $order_status_final
$order->set_status( $order_status );
$order->set_transaction_id( $charge_id );
if ( $order_status_final ) {
$order->update_meta_data( '_stripe_status_final', true );
$order->set_status_final( true );
}
$order->save();

Expand Down

0 comments on commit 9ac5a55

Please sign in to comment.