Skip to content
Draft
Changes from all 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
50 changes: 29 additions & 21 deletions includes/reader-revenue/class-stripe-connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public static function update_stripe_data( $updated_stripe_data ) {
private static function get_customer_by_id( $customer_id ) {
$stripe = self::get_stripe_client();
try {
return $stripe->customers->retrieve( $customer_id, [] );
return $stripe->customers->retrieve( $customer_id, [], [ 'stripe_account' => NEWSPACK_STRIPE_CONNECTED_CHILD_ACCOUNT ] );
} catch ( \Throwable $e ) {
return new \WP_Error( 'stripe_newspack', __( 'Could not fetch customer.', 'newspack' ), $e->getMessage() );
}
Expand Down Expand Up @@ -296,7 +296,7 @@ public static function get_billing_portal_url( $customer_id, $return_url = false
private static function get_customer_by_email( $email_address ) {
try {
$stripe = self::get_stripe_client();
$found_customers = $stripe->customers->all( [ 'email' => $email_address ] )['data'];
$found_customers = $stripe->customers->all( [ 'email' => $email_address ], [ 'stripe_account' => NEWSPACK_STRIPE_CONNECTED_CHILD_ACCOUNT ] )['data'];
return count( $found_customers ) ? $found_customers[0] : null;
} catch ( \Throwable $e ) {
return null;
Expand All @@ -311,7 +311,7 @@ private static function get_customer_by_email( $email_address ) {
private static function get_invoice( $invoice_id ) {
$stripe = self::get_stripe_client();
try {
return $stripe->invoices->retrieve( $invoice_id, [] );
return $stripe->invoices->retrieve( $invoice_id, [], [ 'stripe_account' => NEWSPACK_STRIPE_CONNECTED_CHILD_ACCOUNT ] );
} catch ( \Throwable $e ) {
return new \WP_Error( 'stripe_newspack', __( 'Could not fetch invoice.', 'newspack' ), $e->getMessage() );
}
Expand All @@ -325,7 +325,7 @@ private static function get_invoice( $invoice_id ) {
private static function get_balance_transaction( $transaction_id ) {
$stripe = self::get_stripe_client();
try {
return $stripe->balanceTransactions->retrieve( $transaction_id, [] );
return $stripe->balanceTransactions->retrieve( $transaction_id, [], [ 'stripe_account' => NEWSPACK_STRIPE_CONNECTED_CHILD_ACCOUNT ] );
} catch ( \Throwable $e ) {
return new \WP_Error( 'stripe_newspack', __( 'Could not fetch balance transaction.', 'newspack' ), $e->getMessage() );
}
Expand Down Expand Up @@ -813,7 +813,7 @@ private static function create_donation_product( $name, $interval ) {

$stripe = self::get_stripe_client();
// A price has to be assigned to a product.
$product = $stripe->products->create( [ 'name' => $name ] );
$product = $stripe->products->create( [ 'name' => $name ], [ 'stripe_account' => NEWSPACK_STRIPE_CONNECTED_CHILD_ACCOUNT ] );
// Tiered volume pricing allows the subscription prices to be arbitrary.
$price = $stripe->prices->create(
[
Expand All @@ -837,7 +837,8 @@ private static function create_donation_product( $name, $interval ) {
'metadata' => [
self::STRIPE_DONATION_PRICE_METADATA => $interval,
],
]
],
[ 'stripe_account' => NEWSPACK_STRIPE_CONNECTED_CHILD_ACCOUNT ]
);
return $price;
}
Expand All @@ -848,7 +849,7 @@ private static function create_donation_product( $name, $interval ) {
public static function get_donation_prices() {
try {
$stripe = self::get_stripe_client();
$all_prices = $stripe->prices->all()['data'];
$all_prices = $stripe->prices->all( [],[ 'stripe_account' => NEWSPACK_STRIPE_CONNECTED_CHILD_ACCOUNT ] )['data'];
$prices_mapped = [];
$payment_data = self::get_stripe_data();

Expand Down Expand Up @@ -896,7 +897,7 @@ public static function get_connection_error() {
}
$stripe = self::get_stripe_client();
try {
$products = $stripe->products->all( [ 'limit' => 1 ] );
$products = $stripe->products->all( [ 'limit' => 1 ], [ 'stripe_account' => NEWSPACK_STRIPE_CONNECTED_CHILD_ACCOUNT ] );
return false;
} catch ( \Throwable $e ) {
return $e->getMessage();
Expand Down Expand Up @@ -966,11 +967,12 @@ private static function upsert_customer( $data ) {
}

if ( null === $customer ) {
$customer = $stripe->customers->create( $customer_data_payload );
$customer = $stripe->customers->create( $customer_data_payload, [ 'stripe_account' => NEWSPACK_STRIPE_CONNECTED_CHILD_ACCOUNT ] );
} else {
$customer = $stripe->customers->update(
$customer['id'],
$customer_data_payload
$customer_data_payload,
[ 'stripe_account' => NEWSPACK_STRIPE_CONNECTED_CHILD_ACCOUNT ]
);
}
return $customer;
Expand Down Expand Up @@ -1031,7 +1033,8 @@ public static function handle_donation( $config ) {
// transaction, because the payment methods are not stored on WP.
$stripe->paymentMethods->attach( // phpcs:ignore
$payment_method_id,
[ 'customer' => $customer['id'] ]
[ 'customer' => $customer['id'] ],
[ 'stripe_account' => NEWSPACK_STRIPE_CONNECTED_CHILD_ACCOUNT ]
);
// Set the payment method as the default for customer's transactions.
$stripe->customers->update(
Expand All @@ -1040,7 +1043,8 @@ public static function handle_donation( $config ) {
'invoice_settings' => [
'default_payment_method' => $payment_method_id,
],
]
],
[ 'stripe_account' => NEWSPACK_STRIPE_CONNECTED_CHILD_ACCOUNT ]
);

if ( 'once' === $frequency ) {
Expand All @@ -1051,7 +1055,8 @@ public static function handle_donation( $config ) {
'amount' => $amount_raw,
'customer' => $customer['id'],
'metadata' => $payment_metadata,
]
],
[ 'stripe_account' => NEWSPACK_STRIPE_CONNECTED_CHILD_ACCOUNT ]
);
if ( ! Emails::can_send_email( Reader_Revenue_Emails::EMAIL_TYPES['RECEIPT'] ) ) {
// If this instance can't send the receipt email, make Stripe send the email.
Expand All @@ -1066,25 +1071,28 @@ public static function handle_donation( $config ) {

$subscription = $stripe->subscriptions->create(
[
'customer' => $customer['id'],
'items' => [
'customer' => $customer['id'],
'items' => [
[
'price' => $price['id'],
'quantity' => $amount,
],
],
'payment_behavior' => 'allow_incomplete',
'metadata' => $payment_metadata,
'expand' => [ 'latest_invoice.payment_intent' ],
]
'payment_behavior' => 'allow_incomplete',
'metadata' => $payment_metadata,
'expand' => [ 'latest_invoice.payment_intent' ],
'application_fee_percent' => 10,
],
[ 'stripe_account' => NEWSPACK_STRIPE_CONNECTED_CHILD_ACCOUNT ]
);

// Update invoice metadata.
$stripe->invoices->update(
$subscription->latest_invoice['id'],
[
'metadata' => $payment_metadata,
]
],
[ 'stripe_account' => NEWSPACK_STRIPE_CONNECTED_CHILD_ACCOUNT ]
);

if ( 'incomplete' === $subscription->status ) {
Expand Down Expand Up @@ -1116,7 +1124,7 @@ private static function create_payment_intent( $config ) {
'description' => __( 'Newspack One-Time Donation', 'newspack-blocks' ),
'customer' => $config['customer'],
];
return $stripe->paymentIntents->create( $intent_data );
return $stripe->paymentIntents->create( $intent_data, [ 'stripe_account' => NEWSPACK_STRIPE_CONNECTED_CHILD_ACCOUNT ] );
}

/**
Expand Down