Search code examples
phpwordpresswoocommercestripe-paymentshook-woocommerce

WooCommerce Stripe Payment - Cloning PaymentMethod Issue


I am using WooCommerce Stripe Payment Plugin and cannot get any answers from the support so hoping to try on here.

I am basically modifying a few setting to make a Stripe Direct Charge on a connected account.

Here are my changes:

  1. added the connected account id in the header
add_filter('woocommerce_stripe_request_headers', 'vnmBug_addToStripeHeaders', 20, 1);
function vnmBug_addToStripeHeaders($headersArray) {
    $stripeUserID = 'acct_';
    $headersArray['Stripe-Account'] = $stripeUserID;
    return $headersArray;
}
  1. Modified the stripe.js file to include the connected account id
var stripe = Stripe( wc_stripe_params.key, {
    stripeAccount: 'acct_',
} );
  1. Added the application_fee in the stripe request body
add_filter( 'woocommerce_stripe_request_body', 'add_application_fee', 20, 3 );
function add_application_fee( $request, $api ) {

    //Try to retrieve the order ID from the metadata
    $order = null;
    if (isset($request['metadata']['order_id'])){
        
        $orderID = $request['metadata']['order_id'];
        $order = wc_get_order($orderID);
    }
    
    //This filter is hit multiple times, so the order ID might not be available. If not, just return the request.
    
    if (!$order) {
        return $request;
    }
    
    $applicationFee = 99;   //  <- Use this for testing
    
    if ($applicationFee > 0) {
        $request['application_fee_amount'] = $applicationFee;
    }
    
    return $request;
}

Then when I put through a test order I receive the following error response.

No such PaymentMethod: 'pm_'; OAuth key or Stripe-Account header was used but API request was provided with a platform-owned payment method ID. Please ensure that the provided payment method matches the specified account.

I read that I need to clone the payment method https://stripe.com/docs/payments/payment-methods/connect#cloning-payment-methods but not sure how to add this part in the code? Also i am not 100% sure if this is the solution either?

Any help would be appreciated.

I tried modifying a few different hooks in the Stripe API but with no luck.


Solution

  • If feel like you removed a lot of your code - maybe for the sake of clarity - but some of it is more confusing now.

    For instance, your error:

    No such PaymentMethod: 'pm_'; OAuth key...

    Is the error actually giving 'pm_' ? or did you remove the ID for length ?
    If not that error means you literally passed 'pm_' as a string.

    I'll explain how payment method cloning works, and maybe that will help:

    1 - Get a Customer and attached Payment Method objects on your Platform.
    How you get there isn't very important, there are multiple ways to do it.
    We'll call the objects cus_platform and pm_platform.

    2 - Clone the Payment Method onto your connected account.
    This call is pretty confusing, because it's a PaymentMethod.create() call where you're setting the connected account ID as stripe-account header, but use cus_platform and pm_platform as values.
    In any other situation, a call authenticated as a connected account containing platform objects would not work.

    What this call does is create a copy of the Payment Method on the connected account - this has a different ID, and doesn't have a related Customer yet. We'll call it pm_clone.

    3 - Optionally, attach this pm_clone to a Customer on the connected account (cus_clone).

    This is optional, but without it you'll only be able to process one payment with pm_clone.

    At the end of this set up, you would have 2 pairs of objects:
    -cus_platform and pm_platform, used to process payments on your Platform.
    -cus_clone and pm_clone, used to process payments on behalf of the connected account they exist on (with the stripe-account header)