Skip to content

Commit

Permalink
feat: Stripe Subscriptions to WC subscriptions migrator (#2298)
Browse files Browse the repository at this point in the history
As it turns out, not all changes from #2251 are
necessary – Stripe Payment Gateway creates sources,
but will handle a PaymentMethod ID if set as the source
ID in the subscription metadata.
  • Loading branch information
adekbadek authored Mar 1, 2023
1 parent 4c83e72 commit 6904356
Show file tree
Hide file tree
Showing 3 changed files with 241 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,9 @@ function( $gateway ) {
*/
public function stripe_data() {
$gateways = self::get_payment_gateways();
$defaults = Stripe_Connection::get_default_stripe_data();
if ( ! isset( $gateways['stripe'] ) ) {
return Stripe_Connection::get_default_stripe_data();
return $defaults;
}
$stripe = $gateways['stripe'];
$stripe_data = [
Expand All @@ -120,7 +121,7 @@ public function stripe_data() {
'testPublishableKey' => $stripe->get_option( 'test_publishable_key', '' ),
'testSecretKey' => $stripe->get_option( 'test_secret_key', '' ),
];
return $stripe_data;
return \wp_parse_args( $stripe_data, $defaults );
}

/**
Expand Down
28 changes: 15 additions & 13 deletions includes/reader-revenue/class-woocommerce-connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ private static function find_order_by_transaction_id( $transaction_id ) {
* @param string $stripe_subscription_id Stripe Subscription ID.
* @return WC_Subscription|false Subscription object or false.
*/
private static function get_subscription_by_stripe_subscription_id( $stripe_subscription_id ) {
public static function get_subscription_by_stripe_subscription_id( $stripe_subscription_id ) {
if ( ! function_exists( 'wcs_get_subscription' ) ) {
return false;
}
Expand Down Expand Up @@ -394,7 +394,7 @@ private static function add_universal_order_data( $order, $order_data ) {
$order->set_billing_email( $order_data['email'] );
$order->set_billing_first_name( $order_data['name'] );

if ( $order_data['subscribed'] ) {
if ( isset( $order_data['subscribed'] ) && $order_data['subscribed'] ) {
$order->add_order_note( __( 'Donor has opted-in to your newsletter.', 'newspack' ) );
}

Expand Down Expand Up @@ -444,7 +444,7 @@ public static function update_order( $order_id, $update ) {
* @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 ) {
public static function add_wc_stripe_gateway_metadata( $order, $metadata ) {
$order->set_payment_method( 'stripe' );

if ( isset( $metadata['stripe_id'] ) ) {
Expand Down Expand Up @@ -643,14 +643,16 @@ public static function create_transaction( $order_data ) {
}
$order->save();
}
$subscription = \wcs_create_subscription(
[
'start_date' => self::convert_timestamp_to_date( $order_data['date'] ),
'order_id' => $order->get_id(),
'billing_period' => $frequency,
'billing_interval' => 1, // Every billing period (not e.g. every *second* month).
]
);
$subscription_creation_payload = [
'start_date' => self::convert_timestamp_to_date( $order_data['date'] ),
'order_id' => $order->get_id(),
'billing_period' => $frequency,
'billing_interval' => 1, // Every billing period (not e.g. every *second* month).
];
if ( isset( $order_data['wc_subscription_status'] ) ) {
$subscription_creation_payload['status'] = $order_data['wc_subscription_status'];
}
$subscription = \wcs_create_subscription( $subscription_creation_payload );

if ( is_wp_error( $subscription ) ) {
Logger::error( 'Error creating WC subscription: ' . $subscription->get_error_message() );
Expand Down Expand Up @@ -716,8 +718,8 @@ public static function create_transaction( $order_data ) {
}

return [
'order_id' => $order ? $order->get_id() : false,
'subscription_id' => $subscription ? $subscription->get_id() : false,
'order_id' => ( ! \is_wp_error( $order ) && $order ) ? $order->get_id() : false,
'subscription_id' => ( ! \is_wp_error( $subscription ) && $subscription ) ? $subscription->get_id() : false,
];
}

Expand Down
Loading

0 comments on commit 6904356

Please sign in to comment.