henry sneed
henry sneed

Reputation: 526

Stripe Customer Null in Test Mode

While Stripe is in test mode, the customer field of request payloads is null when the mode is payment and not subscription. Creating the customers in Stripe before submitting the payment (with test credit cards) also doesn't have any affect on the customer field in payment mode.

Is there any way to get Stripe to include customer ids in the request payloads when using the payment mode with test mode? The assumption seems to be that developers don't need to create accounts for customers only making one time payments and not subscribing to a service but this is not the case for my application.

Using Subscription Mode (customer: cus_PIpLq6wFgjP2W3)

{
  "id": "evt_1OUDgfDcghvJaUoz0kK03Z13",
  "object": "event",
  "api_version": "2022-11-15",
  "created": 1704223901,
  "data": {
    "object": {
      "id": "cs_test_a1PGOU8a6sLHxvrBnuFeFArDdcXN8E1rSqeAAfjB6kqVK2sBFOy6dWq4q4",
      "object": "checkout.session",
      "after_expiration": null,
      "allow_promotion_codes": null,
      "amount_subtotal": 999,
      "amount_total": 999,
      "automatic_tax": {
        "enabled": false,
        "status": null
      },
      "billing_address_collection": null,
      "cancel_url": "http://localhost:3000/cancel",
      "client_reference_id": null,
      "client_secret": null,
      "consent": null,
      "consent_collection": null,
      "created": 1704223859,
      "currency": "usd",
      "currency_conversion": null,
      "custom_fields": [
      ],
      "custom_text": {
        "after_submit": null,
        "shipping_address": null,
        "submit": null,
        "terms_of_service_acceptance": null
      },
      "customer": "cus_PIpLq2wFgpP2W3",
      "customer_creation": null,
      "customer_details": {
        "address": {
          "city": null,
          "country": "US",
          "line1": null,
          "line2": null,
          "postal_code": "28036",
          "state": null
        },
        "email": "[email protected]",
        "name": "test1 test2",
        "phone": null,
        "tax_exempt": "none",
        "tax_ids": [
        ]
      },
      "customer_email": null,
      "expires_at": 1704310259,
      "invoice": "in_1OUDgbDcghvJaUozYJCmJyo7",
      "invoice_creation": null,
      "livemode": false,
      "locale": null,
      "metadata": {
      },
      "mode": "subscription",
      "payment_intent": null,
      "payment_link": null,
      "payment_method_collection": null,
      "payment_method_configuration_details": null,
      "payment_method_options": null,
      "payment_method_types": [
        "card"
      ],
      "payment_status": "paid",
      "phone_number_collection": {
        "enabled": false
      },
      "recovered_from": null,
      "setup_intent": null,
      "shipping_address_collection": null,
      "shipping_cost": null,
      "shipping_details": null,
      "shipping_options": [
      ],
      "status": "complete",
      "submit_type": null,
      "subscription": "sub_1OUDgbDcghvJaUozli1YZwLQ",
      "success_url": "http://localhost:3000/login",
      "total_details": {
        "amount_discount": 0,
        "amount_shipping": 0,
        "amount_tax": 0
      },
      "ui_mode": "hosted",
      "url": null
    }
  },
  "livemode": false,
  "pending_webhooks": 1,
  "request": {
    "id": null,
    "idempotency_key": null
  },
  "type": "checkout.session.completed"
}

Using Payment Mode (customer: null)

{
  "id": "evt_1OTwhHDcghvJaUozf9VptPDi",
  "object": "event",
  "api_version": "2022-11-15",
  "created": 1704158591,
  "data": {
    "object": {
      "id": "cs_test_a1ryh8MDmMh1q8mEgX7kExp4m2Ovx5h2WxbKUNFVK6IQhjMhIXDMLsP3BQ",
      "object": "checkout.session",
      "after_expiration": null,
      "allow_promotion_codes": null,
      "amount_subtotal": 2399,
      "amount_total": 2399,
      "automatic_tax": {
        "enabled": false,
        "status": null
      },
      "billing_address_collection": null,
      "cancel_url": "http://localhost:3000/cancel",
      "client_reference_id": null,
      "client_secret": null,
      "consent": null,
      "consent_collection": null,
      "created": 1704158556,
      "currency": "usd",
      "currency_conversion": null,
      "custom_fields": [
      ],
      "custom_text": {
        "after_submit": null,
        "shipping_address": null,
        "submit": null,
        "terms_of_service_acceptance": null
      },
      "customer": null,
      "customer_creation": null,
      "customer_details": {
        "address": {
          "city": null,
          "country": "US",
          "line1": null,
          "line2": null,
          "postal_code": "28036",
          "state": null
        },
        "email": "[email protected]",
        "name": "Testfirst Testlast",
        "phone": null,
        "tax_exempt": "none",
        "tax_ids": [
        ]
      },
      "customer_email": null,
      "expires_at": 1704244956,
      "invoice": null,
      "invoice_creation": null,
      "livemode": false,
      "locale": null,
      "metadata": {
      },
      "mode": "payment",
      "payment_intent": "pi_3OEmgiDcghvJaUoz1Q1TFagq",
      "payment_link": null,
      "payment_method_collection": null,
      "payment_method_configuration_details": null,
      "payment_method_options": {
      },
      "payment_method_types": [
        "card"
      ],
      "payment_status": "paid",
      "phone_number_collection": {
        "enabled": false
      },
      "recovered_from": null,
      "setup_intent": null,
      "shipping_address_collection": null,
      "shipping_cost": null,
      "shipping_details": null,
      "shipping_options": [
      ],
      "status": "complete",
      "submit_type": null,
      "subscription": null,
      "success_url": "http://localhost:3000/login",
      "total_details": {
        "amount_discount": 0,
        "amount_shipping": 0,
        "amount_tax": 0
      },
      "ui_mode": "hosted",
      "url": null
    }
  },
  "livemode": false,
  "pending_webhooks": 1,
  "request": {
    "id": null,
    "idempotency_key": null
  },
  "type": "checkout.session.completed"
}

loadStripe Code

async function handleClick() {
        const stripe = await loadStripe(   'pk_test_2fsdf979sdeUozQvee95j3F88MFXPMFrPGPpjlklmm4Y4oh2Uob7uiRdJCjlkozZIyAayjth3vv3fds898sfe2g'
        )
        const { error } = await stripe.redirectToCheckout({
            lineItems: [
                {
                    price: 'price_10FEPIMIDfsdfozETytrkq2',
                    quantity: 1,
                }
            ],
            mode: 'subscription',
            successUrl: 'http://localhost:3000/login',
            cancelUrl: 'http://localhost:3000/cancel'
        })
    }

I'm using a stripe hosted page so I don't have as much flexibility to hardcode customer ids into transactions.

Upvotes: 0

Views: 433

Answers (1)

Pompey
Pompey

Reputation: 1354

It sounds like you need to set customer_creation="always" when creating your payment mode Checkout Sessions[1]. By default, the setting is if_required. Subscriptions do require a Customer object to function, so Checkout will create a Customer for them. For one time payments, a Customer object is not required so no Customer is created unless you specify customer_creation="always".

[1] https://stripe.com/docs/api/checkout/sessions/create#create_checkout_session-customer_creation

Upvotes: 0

Related Questions