Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stripe: create native WC Subscriptions (if a flag is set) #2251

Merged
merged 15 commits into from
Feb 13, 2023
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions includes/class-donations.php
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,24 @@ public static function is_recurring( $frequency ) {
return 'once' !== $frequency;
}

/**
* Map donation frequency code to a human readable string.
*
* @param string $frequency Frequency code.
*/
public static function get_donation_name_by_frequency( $frequency ) {
switch ( $frequency ) {
case 'once':
return __( 'One-Time Donation', 'newspack' );
case 'month':
return __( 'Monthly Donation', 'newspack' );
case 'year':
return __( 'Yearly Donation', 'newspack' );
default:
return __( 'Donation', 'newspack' );
}
}

/**
* Create missing donations products.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,35 @@ public function country_state_fields() {
return $location_info;
}

/**
* Retrieve configured payment gateways.
*
* @param bool $only_enabled Whether to return only the enabled gateways.
* @return Array Array of payment gateways.
*/
public static function get_payment_gateways( $only_enabled = false ) {
if ( ! class_exists( '\WC_Payment_Gateways' ) ) {
return [];
}
$gateways = \WC_Payment_Gateways::instance()->payment_gateways();
if ( $only_enabled ) {
return array_filter(
$gateways,
function( $gateway ) {
return 'yes' === $gateway->enabled;
}
);
}
return $gateways;
}

/**
* Retrieve Stripe data
*
* @return Array Array of Stripe data.
*/
public function stripe_data() {
if ( ! class_exists( 'WC_Payment_Gateways' ) ) {
return Stripe_Connection::get_default_stripe_data();
}
$gateways = WC_Payment_Gateways::instance()->payment_gateways();
$gateways = self::get_payment_gateways();
if ( ! isset( $gateways['stripe'] ) ) {
return Stripe_Connection::get_default_stripe_data();
}
Expand Down
140 changes: 119 additions & 21 deletions includes/reader-revenue/class-woocommerce-connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,67 @@ private static function add_universal_order_data( $order, $order_data ) {
$order->set_created_via( self::CREATED_VIA_NAME );
}

/**
* Update an order.
*
* @param int $order_id Order ID.
* @param array $update Update data.
*/
public static function update_order( $order_id, $update ) {
$order = \wc_get_order( $order_id );
if ( false === $order ) {
Logger::error( 'Could not find WC order with id: ' . $order_id );
} else {
if ( isset( $update['status'] ) ) {
$order->set_status( $update['status'] );
}
self::add_wc_stripe_gateway_metadata( $order, $update );
$order->save();
Logger::log( 'Updated WC order with id: ' . $order->get_id() );
}
}

/**
* Update WC Stripe Gateway related metadata to an order or subscription.
* The order has to be saved afterwards.
*
* @param WC_Order $order Order object. Can be a subscription or an order.
* @param array $metadata Metadata.
*/
private static function add_wc_stripe_gateway_metadata( $order, $metadata ) {
$order->set_payment_method( 'stripe' );

if ( isset( $metadata['stripe_id'] ) ) {
$order->set_transaction_id( $metadata['stripe_id'] );
}
if ( isset( $metadata['stripe_fee'] ) ) {
$order->add_meta_data( '_stripe_fee', $metadata['stripe_fee'] );
}
if ( isset( $metadata['stripe_net'] ) ) {
$order->add_meta_data( '_stripe_net', $metadata['stripe_net'] );
}
if ( isset( $metadata['payment_method_title'] ) ) {
$order->set_payment_method_title( $metadata['payment_method_title'] );
} else {
$order->set_payment_method_title( __( 'Stripe via Newspack', 'newspack' ) );
}
if ( isset( $metadata['stripe_customer_id'] ) ) {
$order->add_meta_data( '_stripe_customer_id', $metadata['stripe_customer_id'] );
}
if ( isset( $metadata['currency'] ) ) {
$order->add_meta_data( '_stripe_currency', $metadata['currency'] );
}
if ( isset( $metadata['stripe_source_id'] ) ) {
$order->add_meta_data( '_stripe_source_id', $metadata['stripe_source_id'] );
}
if ( isset( $metadata['stripe_intent_id'] ) ) {
$order->add_meta_data( '_stripe_intent_id', $metadata['stripe_intent_id'] );
}
if ( 'completed' === $order->get_status() ) {
$order->add_meta_data( '_stripe_charge_captured', 'yes' );
}
}

/**
* Create a WooCommerce order.
*
Expand All @@ -403,17 +464,15 @@ private static function create_order( $order_data, $item ) {

$order->add_item( $item );
$order->calculate_totals();
$order->set_status( 'completed' );

$status = 'completed';
if ( isset( $order_data['status'] ) && is_string( $order_data['status'] ) ) {
$status = $order_data['status'];
}
$order->set_status( $status );

// Metadata for woocommerce-gateway-stripe plugin.
$order->set_payment_method( 'stripe' );
$order->set_payment_method_title( __( 'Stripe via Newspack', 'newspack' ) );
$order->set_transaction_id( $order_data['stripe_id'] );
$order->add_meta_data( '_stripe_customer_id', $order_data['stripe_customer_id'] );
$order->add_meta_data( '_stripe_charge_captured', 'yes' );
$order->add_meta_data( '_stripe_fee', $order_data['stripe_fee'] );
$order->add_meta_data( '_stripe_net', $order_data['stripe_net'] );
$order->add_meta_data( '_stripe_currency', $order_data['currency'] );
self::add_wc_stripe_gateway_metadata( $order, $order_data );

if ( ! empty( $order_data['client_id'] ) ) {
/**
Expand Down Expand Up @@ -451,16 +510,19 @@ private static function convert_timestamp_to_date( $timestamp ) {
* @param object $order_data Order data.
*/
public static function create_transaction( $order_data ) {
$transaction_id = $order_data['stripe_id'];

$found_order = self::find_order_by_transaction_id( $transaction_id );
if ( ! empty( $found_order ) ) {
Logger::log( 'NOT creating an order, it was already synced.' );
return;
if ( isset( $order_data['stripe_id'] ) ) {
$found_order = self::find_order_by_transaction_id( $order_data['stripe_id'] );
if ( ! empty( $found_order ) ) {
Logger::log( 'NOT creating an order, it was already synced.' );
return;
}
}

$frequency = $order_data['frequency'];
$stripe_subscription_id = $order_data['stripe_subscription_id'];
$stripe_subscription_id = false;
if ( isset( $order_data['stripe_subscription_id'] ) ) {
$stripe_subscription_id = $order_data['stripe_subscription_id'];
}

// Match the Stripe product to WC product.
$item = self::get_donation_order_item( $frequency, $order_data['amount'] );
Expand All @@ -469,6 +531,9 @@ public static function create_transaction( $order_data ) {
}

$subscription_status = 'none';
if ( isset( $order_data['subscription_status'] ) ) {
$subscription_status = $order_data['subscription_status'];
}

if (
Donations::is_recurring( $frequency )
Expand Down Expand Up @@ -519,6 +584,16 @@ public static function create_transaction( $order_data ) {
* Handle WooCommerce Subscriptions - new subscription.
*/
if ( 'created' === $subscription_status ) {
// A subscription needs a valid user. If not user ID is provided, attribute the
// subscription to the user with the supplied email address.
if ( empty( $order_data['user_id'] ) ) {
$user = \get_user_by( 'email', $order_data['email'] );
if ( ! $user || \is_wp_error( $user ) ) {
Logger::error( 'Could not find user by email.' );
}
$order->set_customer_id( $user->ID );
$order->save();
}
$subscription = \wcs_create_subscription(
[
'start_date' => self::convert_timestamp_to_date( $order_data['date'] ),
Expand All @@ -531,16 +606,39 @@ public static function create_transaction( $order_data ) {
if ( is_wp_error( $subscription ) ) {
Logger::error( 'Error creating WC subscription: ' . $subscription->get_error_message() );
} else {
$subscription_id = $subscription->get_id();

$wc_subscription_payload = [
'stripe_customer_id' => $order_data['stripe_customer_id'],
];
if ( isset( $order_data['stripe_source_id'] ) ) {
$wc_subscription_payload['stripe_source_id'] = $order_data['stripe_source_id'];
}
self::add_wc_stripe_gateway_metadata( $subscription, $wc_subscription_payload );
self::add_universal_order_data( $subscription, $order_data );
/* translators: %s - donation frequency */
$subscription->add_order_note( sprintf( __( 'Newspack subscription with frequency: %s. The recurring payment and the subscription will be handled in Stripe, so you\'ll see "Manual renewal" as the payment method in WooCommerce.', 'newspack' ), $frequency ) );
$subscription->update_status( 'active' ); // Settings status via method (not in wcs_create_subscription), to make WCS recalculate dates.
if ( false === $stripe_subscription_id ) {
$subscription->add_order_note( __( 'This subscription was created via Newspack.', 'newspack' ) );
} else {
/* translators: %s - donation frequency */
$subscription->add_order_note( sprintf( __( 'Newspack subscription with frequency: %s. The recurring payment and the subscription will be handled in Stripe, so you\'ll see "Manual renewal" as the payment method in WooCommerce.', 'newspack' ), $frequency ) );
$subscription->update_status( 'active' ); // Settings status via method (not in wcs_create_subscription), to make WCS recalculate dates.
}
$subscription->add_item( $item );
$subscription->calculate_totals();

if ( false === $stripe_subscription_id ) {
$subscription->set_payment_method( 'stripe' );
if ( isset( $order_data['payment_method_title'] ) ) {
$subscription->set_payment_method_title( $order_data['payment_method_title'] );
} else {
$subscription->set_payment_method_title( __( 'Stripe', 'newspack' ) );
}
} else {
update_post_meta( $subscription_id, self::SUBSCRIPTION_STRIPE_ID_META_KEY, $stripe_subscription_id );
}

$subscription->save();
$subscription_id = $subscription->get_id();

update_post_meta( $subscription_id, self::SUBSCRIPTION_STRIPE_ID_META_KEY, $stripe_subscription_id );
Logger::log( 'Created WC subscription with id: ' . $subscription_id );

if ( class_exists( 'WC_Memberships_Integration_Subscriptions_User_Membership' ) && self::$created_membership_id ) {
Expand Down
Loading