TC_Guy
TC_Guy

Reputation: 137

Can't retrieve Customer from Stripe Checkout Session

I am working with an ASP.NET webforms application and have a Stripe Checkout page set up. A webhook is triggered by Stripe, and everything works EXCEPT that I can't retrieve the Customer from the Checkout.Session.Completed event. Here is the JSON from Stripe showing the event:

{
  "id": "evt_1OTx6dJqbmj5op6EbTwFDGjI",
  "object": "event",
  "api_version": "2023-10-16",
  "created": 1704160163,
  "data": {
    "object": {
      "id": "cs_test_c1Bo8DeRxy9jIJICpbdXaxG4zwki2caw6ph8qenMDpbXTsujxaNzh9ZQdT",
      "object": "checkout.session",
      "after_expiration": null,
      "allow_promotion_codes": null,
      "amount_subtotal": null,
      "amount_total": null,
      "automatic_tax": {
        "enabled": false,
        "status": null
      },
      "billing_address_collection": null,
      "cancel_url": null,
      "client_reference_id": null,
      "client_secret": null,
      "consent": {
        "promotions": null,
        "terms_of_service": "accepted"
      },
      "consent_collection": {
        "payment_method_reuse_agreement": {
          "position": "hidden"
        },
        "promotions": "none",
        "terms_of_service": "required"
      },
      "created": 1704160119,
      "currency": null,
      "currency_conversion": null,
      "custom_fields": [
      ],
      "custom_text": {
        "after_submit": null,
        "shipping_address": null,
        "submit": null,
        "terms_of_service_acceptance": {
          "message": "I agree to the  [Terms of Service](https://test.com/terms.aspx)"
        }
      },
      "customer": "cus_PGt0WkKRwNskR5",
      "customer_creation": null,
      "customer_details": {
        "address": null,
        "email": "[email protected]",
        "name": null,
        "phone": null,
        "tax_exempt": null,
        "tax_ids": null
      },
      "customer_email": null,
      "expires_at": 1704246519,
      "invoice": null,
      "invoice_creation": null,
      "livemode": false,
      "locale": null,
      "metadata": {
      },
      "mode": "setup",
      "payment_intent": null,
      "payment_link": null,
      "payment_method_collection": "always",
      "payment_method_configuration_details": null,
      "payment_method_options": {
      },
      "payment_method_types": [
        "card"
      ],
      "payment_status": "no_payment_required",
      "phone_number_collection": {
        "enabled": false
      },
      "recovered_from": null,
      "redirect_on_completion": "always",
      "return_url": "https://test.com/done.aspx?session_id={CHECKOUT_SESSION_ID}",
      "setup_intent": "seti_1OTx5vJqbmj5op6EqHc02Tae",
      "shipping_address_collection": null,
      "shipping_cost": null,
      "shipping_details": null,
      "shipping_options": [
      ],
      "status": "complete",
      "submit_type": null,
      "subscription": null,
      "success_url": null,
      "total_details": null,
      "ui_mode": "embedded",
      "url": null
    }
  },
  "livemode": false,
  "pending_webhooks": 3,
  "request": {
    "id": null,
    "idempotency_key": null
  },
  "type": "checkout.session.completed"
}

As you can see, there IS a value for "customer", and I am using the following code to try retrieving it:

        try {

            var stripeEvent=EventUtility.ConstructEvent(
                json,
                signature,
                endpointSecret
            );
            switch (stripeEvent.Type) {
                case Events.CheckoutSessionCompleted:
                    var objCheckout=stripeEvent.Data.Object as Session;
                    var info2=$"Hit Checkout Session ID '{objCheckout.Id}' customer '{objCheckout.Customer?.ToString()}'";
                    MailSvc.SendMail ( "[email protected]", "Session Checkout Complete", false, "[email protected]", info2 );
                    break;
                default:
                    var info3=$"Missed this here - {stripeEvent.Type}";
                    MailSvc.SendMail ( "[email protected]", "Webhook event error", false, "[email protected]", info3 );
                    break;
            }
        }
        catch (StripeException ex) {
            Console.WriteLine ( ex.Message );
            throw ( ex );
        }

All of the code works fine, except I am unable to retrieve the value of "customer" so that I can process the transaction. I can't find any code to help me with this. Any ideas?

Upvotes: 0

Views: 156

Answers (1)

qichuan
qichuan

Reputation: 2011

In your webhook handling code, you need to call the Retrieve a customer API to retrieve the customer object, so that you can use the full customer data to process the order.

Upvotes: 0

Related Questions