user1310969
user1310969

Reputation: 201

Stripe This customer has no attached payment source

I have a problem in my wordpress, i use stripe and i have this error message : "This customer has no attached payment source " but i don't understand why. In dev environment i had no problem and now in prod i have this issue. For information i use the card number 4242 4242 4242 4242 for testing.

Thanks a lot for your help

if(isset($_POST['action']) && $_POST['action'] == 'stripe' && wp_verify_nonce($_POST['stripe_nonce'], 'stripe-nonce')) {

    global $stripe_options, $post;


    // load the stripe libraries
    require_once(STRIPE_BASE_DIR . '/init.php');

    // retrieve the token generated by stripe.js
    $token = $_POST['stripeToken'];
    $amount = base64_decode($_POST['amount'])*100;
    $email = $_POST['email'];
    $plan_nickname = $_POST['plan_nickname'];
    $plan_id = $_POST['plan_id'];
    $nom = $_POST['name'];
    $prenom = $_POST['prenom'];
    $adresse = $_POST['address-line1'];
    $ville = $_POST['address-city'];
    $zip = $_POST['address-zip'];



    // check if we are using test mode
    if(isset($stripe_options['test_mode']) && $stripe_options['test_mode']) {
        $secret_key = $stripe_options['test_secret_key'];
    } else {
        $secret_key = $stripe_options['live_secret_key'];
    }

    // attempt to charge the customer's card
    try {

        \Stripe\Stripe::setApiKey($secret_key);

        $product = \Stripe\Product::create([
            'name' => $stripe_options['product_name'],
            'type' => 'service',
        ]);

        $plan = \Stripe\Plan::create([
            'product' => $stripe_options['product_key'],
            'nickname' => $plan_nickname,
            'interval' => 'month',
            'currency' => 'eur',
            'amount' => $amount,
          ]);

        $customer = \Stripe\Customer::create([
            'email' => $email,
            'source' => $token,
            'description' => $plan_nickname,
        ]);

        $subscription = \Stripe\Subscription::create([
            'customer' => $customer->id,
            'items' => [['plan' => $plan_id]],
        ]);

        // redirect on successful payment
        $redirect = add_query_arg('payment', 'paid', $_POST['redirect']);

    } catch (Exception $e) {
        // redirect on failed payment
        //$redirect = add_query_arg('payment', 'failed', $_POST['redirect_failed']);
        var_dump($e);

    }

    // redirect back to our previous page with the added query variable
    wp_redirect($redirect); exit;
}

}

Upvotes: 3

Views: 7206

Answers (1)

Durim
Durim

Reputation: 73

Check the plan on Stripe. If the plan has a trial period you need to create the subscription with the trial period. I am using laravel cashier and react-stripe-checkout and this worked for me:

$user->newSubscription('main', 'basic')
            ->trialDays(30)
            ->create($request->token);

Upvotes: 2

Related Questions