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 Subscriptions to WC subscriptions migrator #2298

Merged
merged 6 commits into from
Mar 1, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
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
26 changes: 14 additions & 12 deletions includes/reader-revenue/class-woocommerce-connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,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 @@ -393,7 +393,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 @@ -634,14 +634,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 @@ -705,8 +707,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