1

My client has requested the addition of a questionnaire that, in the end, results in the order of a subscription. All of this takes place outside the normal checkout method. Currently, after all the data has been collected I am creating user (customer) for them and logging them in, creating the order with the gathered information, passing it to the Stripe gateway to “process_order” and then, upon success creating a subscription linked to the order. When I omit the gateway all of this works. I know how to apply the gateway to the order and pass the order to the gateway but I can’t figure out how to associate the credit card info to the order or customer so that it will be successful.

  • So I don't like the idea of bumps but it was suggested that I post here from WooCommerce support for assistance but I'm getting the impression no one knows how the Stripe extension works. – Jeremy May 10 '19 at 17:38
  • And here I am, coming back to this question after significant amount of time searching and trial and error - and still without a clue! 1.) I'll definitely post my answer once I find a solution, for future reference, and 2.) @Jeremy would do someone a favor with this question, even if it's not answered, if he posted some code with it (and not to mention that maybe it would be answered because of that). – dev_masta Apr 8 at 20:31
  • Why not use a subscription plugin??? – Tofandel Apr 10 at 7:57
  • Not sure about Jeremy, but my issue comes from a fact that the requirement is that the Sales Floor should be able to process an order for the customer, while on the phone with them. So, they are logged in with their username in WP, but they are creating the order and subscription for another WP user, with customer's credit card. – dev_masta Apr 11 at 21:23
0

Well, I could go find a nulled version of the WooCommerce Stripe & give you the snippet, but in theory, what you want to do, is pretty simple, you want to grab the same data that is used upon checkout, if the credit card is saved, then the next best thing is the Stripe CID ( Customer ID ).

  1. Take the WooCommerce Stripe Integration.
  2. Find the code that integrates with the Checkout to output the credit card list.
  3. You'll find something related to the current user_id, same function / class, you want to run in your own script to retrieve the credit cards, and simply do an array_shift or something to take the first one.
  4. Charge the credit card. ? That's pretty much it.

Based on how Stripe works, you'll actually get in a "bit of trouble" because the WooCommerce integration does not use the native stripe subscriptions ( to my knowledge ), and you'll actually have to emulate an fully fledged order in WooCommerce to have it done properly.

A different trick is to rely on the email address, and use the Stripe API to do a search for the CID.

Here's a code sample I'm using to get all subscriptions registered in stripe ( not woocommerce ), the CID and a lot of "juicy" data is available there, simply do a var_dump( $customer ) in the loop and an exit.

$customer_search = \Stripe\Customer::all( [
  'email' => $email_address,
  'limit' => 200
] );

if( empty( $customer_search->data ) )
  return [];

$response = [];

foreach( $customer_search->data as $customer ) {
  if( !isset( $customer->subscriptions )
      || empty( $customer->subscriptions->data  ) )
    continue;

  foreach( $customer->subscriptions->data as $subscription ) {
    if( is_string( $status ) && $subscription->status != $status )
      continue;
    if( is_array( $status ) && !in_array( $subscription->status, $status ) )
      continue;

    $response[] = $subscription;
  }
}


return $response;
| improve this answer | |
  • I thought I'll be able to create an order and subscription with PHP, for one (customer's) WP user while logged in as another user, and process the payment - all with WooCommerce methods and functions. Seems like I'll need to create a charge directly in Stripe with their API and attach transaction ID to the order (yes, I know the source ID and customer ID already). – dev_masta Apr 11 at 21:25
  • And, it seems that the Woo Subscription plugin is not actually creating subscriptions in Stripe, only single charges. – dev_masta Apr 11 at 21:25
  • You can create subscriptions for which ever user you want, there's going to be a few security things you need to take into account. wp_set_current_user allows you to emulate a currently logged in user somewhere in the request, and you can use it to switch. And yes, the subscription is in WooCommerce, not Stripe, Stripe is strictly handling the charges. – Robert Apr 12 at 22:50
  • I'm wondering, maybe WC()->cart (WC_Cart) is the way to go? Programmatically add products/variations to cart, apply coupons and process the cart with WC_Checkout? That should trigger all the relevant hooks which should trigger order creation, subscription creation, emailing... – dev_masta Apr 13 at 22:31
0

Because we only got 1 answer that didn't really answer the question, let me just say that in the end I went with a solution where I:

  1. Add a product (in my case, luckily, only 1 product per order is available) and coupons to the WC()->cart object.
  2. Get the amount that needs to be paid today with WC()->cart->get_totals()["total"]
  3. Charge Stripe manually via their API, since I already have customer's source id and customer id
  4. Create an order and save the transaction information to it's meta data, while updating Stripe transaction with reference to the order
  5. Create a Subscription that is connected to the order

How will that work is yet to be seen 'cause I'm not half way done with testing. Hope this will help someone and I'll do my best to update this answer later on.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.