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

Adds environment metadata to Stripe Billing subscription and invoice payment intents #7190

Merged
merged 11 commits into from
Sep 14, 2023
4 changes: 4 additions & 0 deletions changelog/fix-6529-add-stripe-billing-context
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: add

Record the subscriptions environment context in transaction meta when Stripe Billing payments are handled.
4 changes: 4 additions & 0 deletions changelog/fix-6529-add-stripe-billing-context-2
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: add

Record the source (Woo Subscriptions or WCPay Subscriptions) when a Stripe Billing subscription is created.
4 changes: 4 additions & 0 deletions changelog/fix-payment-intent-meta-for-recurring-transactions
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: dev

Fix payment context and subscription payment metadata stored on subscription recurring transactions.
4 changes: 2 additions & 2 deletions includes/class-wc-payment-gateway-wcpay.php
Original file line number Diff line number Diff line change
Expand Up @@ -1615,9 +1615,9 @@ protected function get_metadata_from_order( $order, $payment_type ) {
'subscription_payment' => 'no',
];

if ( 'recurring' === (string) $payment_type && function_exists( 'wcs_order_contains_subscription' ) && wcs_order_contains_subscription( $order ) ) {
if ( 'recurring' === (string) $payment_type && function_exists( 'wcs_order_contains_subscription' ) && wcs_order_contains_subscription( $order, 'any' ) ) {
$metadata['subscription_payment'] = wcs_order_contains_renewal( $order ) ? 'renewal' : 'initial';
$metadata['payment_context'] = $this->is_subscriptions_plugin_active() ? 'regular_subscription' : 'wcpay_subscription';
$metadata['payment_context'] = WC_Payments_Features::should_use_stripe_billing() ? 'wcpay_subscription' : 'regular_subscription';
}
return apply_filters( 'wcpay_metadata_from_order', $metadata, $order, $payment_type );
}
Expand Down
14 changes: 14 additions & 0 deletions includes/subscriptions/class-wc-payments-invoice-service.php
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,20 @@ public function get_and_attach_intent_info_to_order( $order, $intent_id ) {
);
}

/**
* Sends a request to server to record the store's context for an invoice payment.
*
* @param string $invoice_id The subscription invoice ID.
*/
public function record_subscription_payment_context( string $invoice_id ) {
$this->payments_api_client->update_invoice(
$invoice_id,
[
'subscription_context' => class_exists( 'WC_Subscriptions' ) && WC_Payments_Features::is_stripe_billing_enabled() ? 'stripe_billing' : 'legacy_wcpay_subscription',
]
);
}

/**
* Sets the subscription last invoice ID meta for WC subscription.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,8 @@ public function create_subscription( WC_Subscription $subscription ) {
$subscription_data = $this->prepare_wcpay_subscription_data( $wcpay_customer_id, $subscription );
$this->validate_subscription_data( $subscription_data );

$subscription_data['metadata']['subscription_source'] = $this->is_subscriptions_plugin_active() ? 'woo_subscriptions' : 'wcpay_subscriptions';

$response = $this->payments_api_client->create_subscription( $subscription_data );

$this->set_wcpay_subscription_id( $subscription, $response['id'] );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,9 @@ public function handle_invoice_paid( array $body ) {

// Remove pending invoice ID in case one was recorded for previous failed renewal attempts.
$this->invoice_service->mark_pending_invoice_paid_for_subscription( $subscription );

// Record the store's Stripe Billing environment context on the payment intent.
$this->invoice_service->record_subscription_payment_context( $wcpay_invoice_id );
}

/**
Expand Down Expand Up @@ -248,6 +251,9 @@ public function handle_invoice_payment_failed( array $body ) {

// Record invoice ID so we can trigger repayment on payment method update.
$this->invoice_service->mark_pending_invoice_for_subscription( $subscription, $wcpay_invoice_id );

// Record the store's Stripe Billing environment context on the payment intent.
$this->invoice_service->record_subscription_payment_context( $wcpay_invoice_id );
}

/**
Expand Down
17 changes: 17 additions & 0 deletions includes/wc-payment-api/class-wc-payments-api-client.php
Original file line number Diff line number Diff line change
Expand Up @@ -1122,6 +1122,23 @@ public function charge_invoice( string $invoice_id, array $data = [] ) {
);
}

/**
* Updates an invoice.
*
* @param string $invoice_id ID of the invoice to update.
* @param array $data Parameters to send to the invoice endpoint. Optional. Default is an empty array.
* @return array
*
* @throws API_Exception Error updating the invoice.
*/
public function update_invoice( string $invoice_id, array $data = [] ) {
return $this->request(
$data,
self::INVOICES_API . '/' . $invoice_id,
self::POST
);
}

/**
* Fetch a WCPay subscription.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,9 @@ public function test_create_subscription() {
],
],
],
'metadata' => [
'subscription_source' => 'woo_subscriptions',
],
];

$this->assertNotEquals( $mock_subscription->get_meta( self::SUBSCRIPTION_ID_META_KEY ), $mock_wcpay_subscription_id );
Expand Down