Skip to content

Commit

Permalink
Move Stripe logic to core plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisfromthelc committed Nov 15, 2024
1 parent cef2288 commit d3b7af8
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 8 deletions.
35 changes: 35 additions & 0 deletions src/inc/payment-gateways/lib/stripe.php
Original file line number Diff line number Diff line change
Expand Up @@ -308,4 +308,39 @@ public static function convert_dispute_reason_to_sift_chargeback_reason( string
return null;
}
}

public static function send_chargeback_to_sift( $event ): void {
// Extract the charge ID and the original dispute reason.
$charge_id = $event->charge ?? null;
$dispute_reason = $event->reason ?? null;

if ( ! $charge_id || ! $dispute_reason ) {
wc_get_logger()->error( 'Missing charge ID or dispute reason in the Stripe dispute event.' );
return;
}

// Get the order ID from the Stripe charge ID using the helper method from the Stripe class.
$order_id = self::get_order_from_charge_id( $charge_id );
if ( ! $order_id ) {
wc_get_logger()->error( 'Order ID not found for the Stripe charge ID: ' . esc_html( $charge_id ) );
return;
}

// Get the WooCommerce order object.
$order = wc_get_order( $order_id );
if ( ! $order instanceof WC_Order ) {
wc_get_logger()->error( 'WooCommerce order not found for Order ID: ' . esc_html( $order_id ) );
return;
}

// Convert the Stripe dispute reason to the Sift chargeback reason using the helper method from the Stripe class.
$chargeback_reason = self::convert_dispute_reason_to_sift_chargeback_reason( $dispute_reason );
if ( ! $chargeback_reason ) {
wc_get_logger()->error( 'Unable to convert Stripe dispute reason to Sift chargeback reason: ' . esc_html( $dispute_reason ) );
return;
}

// Call the action to handle the chargeback event.
do_action( 'sift_for_woocommerce_chargeback', $order_id, $order, $chargeback_reason );
}
}
53 changes: 45 additions & 8 deletions src/inc/woocommerce-actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public static function hooks() {
add_action( 'woocommerce_update_order', array( static::class, 'update_order' ), 100, 2 );
add_action( 'woocommerce_order_applied_coupon', array( static::class, 'add_promotion' ), 100, 2 );
add_action( 'sift_for_woocommerce_chargeback', array( __CLASS__, 'chargeback' ), 100, 3 );
add_action( 'woocommerce_payments_before_webhook_delivery', __NAMESPACE__ . '\process_before_event_delivery', 100, 2 );

/**
* We need to break this out into separate actions so we have the $status_transition available.
Expand Down Expand Up @@ -955,14 +956,14 @@ public static function get_order_payment_methods( \WC_Order $order ): array {
}

/**
* Return the amount of transaction "micros"
*
* @link https://developers.sift.com/docs/curl/events-api/reserved-events/transaction in the $amount
*
* @param float $price The price to format.
*
* @return integer
*/
* Return the amount of transaction "micros"
*
* @link https://developers.sift.com/docs/curl/events-api/reserved-events/transaction in the $amount
*
* @param float $price The price to format.
*
* @return integer
*/
public static function get_transaction_micros( float $price ) {
$currencies_without_decimals = array( 'JPY' );

Expand All @@ -975,4 +976,40 @@ public static function get_transaction_micros( float $price ) {
// For currencies with decimals
return intval( $price * 10000 );
}

/**
* Processes events (from Stripe and more) before sending the data elsewhere.
*
* @param string $event_type The type of event.
* @param mixed $event_body The body of the event.
*
* @return void
*/
public static function process_before_event_delivery( string $event_type, $event_body ) {
$result = null;

// Using a switch case since we'll likely handle more future event types
switch ( $event_type ) {
case 'charge.dispute.created':
$result = Stripe::send_chargeback_to_sift( $event_body );
break;
default:
$result = new \WP_Error(
'sift-for-woocommerce-invalid-event-type',
'Sift For WooCommerce Webhook Forwarding: No matching event type found'
);
}

// Error handling
if ( is_wp_error( $result ) ) {
wc_get_logger()->error(
'Sift For WooCommerce Webhook Forwarding: Error processing event',
array(
'event_type' => $event_type,
'event_body' => $event_body,
'error' => $result,
)
);
}
}
}

0 comments on commit d3b7af8

Please sign in to comment.