Home > OS >  Trying to charge before or after Subscription Schedule is created Stripe API
Trying to charge before or after Subscription Schedule is created Stripe API

Time:08-02

I am developing a wordpress website for a client. He needs different types of packages. For most of these packages I developed a simple Stripe checkout webpage, using its documentation. The problem is that I need this workflow:

  • first month x dollars
  • second month x dollars
  • after one year subscription y dollars

I've already done this using Subscription Schedule. But it needs a customer ofc. How can I charge before and after charging create this Subscription Schedule? I don't know how to deal with this, how to charge using Stripe checkout simple already built page or do I need to create one by myself, where user needs to add his card, pay, and get the customer_id?

    function checkout3() {
    // Set your secret key. Remember to switch to your live secret key in production.
    // See your keys here: https://dashboard.stripe.com/apikeys
    \Stripe\Stripe::setApiKey('sk_test_51e7DRPLRnISGb5vSFxnvvuDx1GzhlBIFeazcmpEevsUFf29jHXJ1YgE2xaJ1lGfzjtKzE8uoN0eR9Klaq00CnMFWvfB');
    
    // The price ID passed from the front end.
    //   $priceId = $_POST['priceId'];
    $priceId = 'price_1LPahmIne7DRPLRnFXV6Uz34';
    
   $futureDate= strtotime(date('Y-m-d', strtotime(' 1 year')));

   $customer = \Stripe\Customer::create(
       [
          'description' => 'My First Test Customer (created for API docs at https://www.stripe.com/docs/api)',
          
        ]
       );
       
 
   $session = \Stripe\SubscriptionSchedule::create([
      'customer' => $customer["id"],
      'start_date' => 'now',
      'end_behavior' => 'release',
      'phases' => [
        [
          'items' => [
            [
              'price' => 'price_1LRF5CIne7DRPLRnwuLVE2pu',
              'quantity' => 1,
            ],
          ],
          //'end_date' => $futureDate,
          'iterations' => 1,
        ],
        [
          'items' => [
            [
              'price' => 'price_1LRF5cIne7DRPLRngevvIZiw',
              'quantity' => 1,
            ],
          ],
          'iterations' => 1,
        ],
        [
          'items' => [
            [
              'price' => 'price_1LPujQIne7DRPLRnj3EOweJN',
              'quantity' => 1,
            ],
          ],
        ],
      ],
    ]);
    
    // Redirect to the URL returned on the Checkout Session.
    // With vanilla PHP, you can redirect with:
       //header("HTTP/1.1 303 See Other");
       //header("Location: " . '$session->url');
}

So right now, the subscription schedule is added to the Stripe dashboard, the page it's keep loading infinitely, but without charging... How to deal with this?

  public static function firebase_checkout3_func() {
        $html = "";
        $html .= "<form id='firebase-checkout' action='/wp-json/api/checkout2' method='POST'>
                    <button type='submit' id='checkout-button'>Începe acum</button>
                </form>";
        return $html;
    }

CodePudding user response:

  1. Use Stripe hosted Checkout Page with setup mode to collect a Customer's SetupIntent
  2. Retrieve the Payment Method inside the SetupIntent
  3. Set it as the customer's invoice_settings.default_payment_method
  4. Create a Subscription Schedule with that Customer Id as normal
  • Related