I am trying to integrate the Payment Request Button in the stripe. For that, I follow the document(https://stripe.com/docs/stripe-js/elements/payment-request-button#complete-payment-intents). I got the button in my chrome. At the time of payment completion
paymentRequest.on('paymentmethod', function(ev) {
stripe.confirmPaymentIntent(clientSecret, {
payment_method: ev.paymentMethod.id,
}).then(function(confirmResult) {
if (confirmResult.error) {
console.log("error")
ev.complete('fail');
} else {
ev.complete('success');
stripe.handleCardPayment(clientSecret).then(function(result) {
if (result.error) {
console.log(result.error)
} else {
console.log("Success")
}
});
}
});
});
I got an error clientSecret is not defined. I have a doubt from where can I get the client secret
paymentmethod API did not provide the clientSecret
While I call the source API I got the clientSecret
But can't able to run both source and paymentMethod API
Please guide me on the proper way to finish the integration.
Thanks in advance
From Stripe Docs
clientSecret: The client secret of this PaymentIntent. Used for client-side retrieval using a publishable key.
The client secret can be used to complete a payment from your frontend. It should not be stored, logged, embedded in URLs, or exposed to anyone other than the customer. Make sure that you have TLS enabled on any page that includes the client secret.
So clientSecret it's defined as property of paymentintent object.
paymentintent object (sample):
{
"id": "pi_1FpUmEKZaRsxp2y4c9OPoTjM",
"object": "payment_intent",
"amount": 3000,
"amount_capturable": 0,
amount_received": 0,
"application": null,
"application_fee_amount": null,
"canceled_at": null,
"cancellation_reason": null,
"capture_method": "automatic",
"charges": {
"object": "list",
"data": [],
"has_more": false,
"url": "/v1/charges?payment_intent=pi_1FpUmEKZaRsxp2y4c9OPoTjM"
},
"client_secret": "pi_1FpUmEKZaRsxp2y4c9OPoTjM_secret_tv9tsgAQbAlNRYqm8MAzmYPuE",
"confirmation_method": "automatic",
"created": 1576307458,
"currency": "eur",
"customer": null,
"description": null,
"invoice": null,
"last_payment_error": null,
"livemode": false,
"metadata": {},
"next_action": null,
"on_behalf_of": null,
"payment_method": null,
"payment_method_options": {
"card": {
"installments": null,
"request_three_d_secure": "automatic"
}
},
"payment_method_types": [
"card"
],
"receipt_email": null,
"review": null,
"setup_future_usage": null,
"shipping": null,
"statement_descriptor": null,
"statement_descriptor_suffix": null,
"status": "requires_payment_method",
"transfer_data": null,
"transfer_group": null
}
As step 2 in documentationyou can retrieve your clientSecret as paymentintent.client_secret. To do in your code use:
const clientSecret = paymentRequest.client_secret
If others are using php don't forget to use it as a string and not as variable:
submitButton.addEventListener('click', function(ev) {
stripe.confirmCardPayment('<?php echo ($intent->client_secret);?>', {
payment_method: {card: card}
}).then(function(result) {
if (result.error) {
// Show error to your customer (e.g., insufficient funds)
console.log(result.error.message);
} else {
// The payment has been processed!
if (result.paymentIntent.status === 'succeeded') {
// Show a success message to your customer
// There's a risk of the customer closing the window before callback
// execution. Set up a webhook or plugin to listen for the
// payment_intent.succeeded event that handles any business critical
// post-payment actions.
}
}
});
});
Related
I want to use stripe payment link system, The payment method can be Card/Wallet
I dont want to use checkout button system, since the payment isnt dynamic
Once the payment is successful (auto subscription or manual checkout) I need to send the activation code to the users email. The next year when the charge is auto debited from card I need to generate the activation key again and send the same for the next year.
I see there are many events in the Stripe payment webhooks, I have used charge.succeeded event listener and it does send me the object (pasted below).
I have one issue here. If I rely on this charge.succeeded object I find no information on which product the purchase is made. There are many products in my system
The response have user supplied email but there is no way I product details in the charge.succeeded object. I have supplied the metadata info in the payment link page as below. I have also supplied the metadata in products as well as metadata in the every pricings
I use this link - https://buy.stripe.com/test_28o3cn6hC5bgdoIcMM
Test card number: 4242 4242 4242 4242
and any dates and CVV number would work.
Webhook responses are captured here - https://docs.google.com/spreadsheets/d/1RjnFnjHvs9ca8tIPoRiNHFUph_npm5pVK2S15wVllzI/edit?usp=sharing
Any help is greatly appreciated
{
"id": "evt_1KWzCbHpIo9Nhh5aYEv7XlY8",
"object": "event",
"api_version": "2017-12-14",
"created": 1645777861,
"data": {
"object": {
"id": "ch_1KWzCaHpIo9Nhh5atK09dpaO",
"object": "charge",
"amount": 100,
"amount_captured": 100,
"amount_refunded": 0,
"application": null,
"application_fee": null,
"application_fee_amount": null,
"balance_transaction": "txn_1KWzCaHpIo9Nhh5anEebLs4X",
"billing_details": {
"address": {
"city": null,
"country": "IN",
"line1": null,
"line2": null,
"postal_code": null,
"state": null
},
"email": "te...#email.com",
"name": "CARDNAME",
"phone": null
},
"calculated_statement_descriptor": "XXXXXXXXXXX",
"captured": true,
"created": 1645777860,
"currency": "inr",
"customer": "cus_LDQ2DBhL2VkPOH",
"description": "Subscription creation",
"destination": null,
"dispute": null,
"disputed": false,
"failure_code": null,
"failure_message": null,
"fraud_details": {
},
"invoice": "in_1KWzCYHpIo9Nhh5ammBkFvU1",
"livemode": false,
"metadata": {
},
"on_behalf_of": null,
"order": null,
"outcome": {
"network_status": "approved_by_network",
"reason": null,
"risk_level": "normal",
"risk_score": 58,
"seller_message": "Payment complete.",
"type": "authorized"
},
"paid": true,
"payment_intent": "pi_1KWzCYHpIo9Nhh5aj6Xgl3tS",
"payment_method": "pm_1KWzCXHpIo9Nhh5aADMKyWPc",
"payment_method_details": {
"card": {
"brand": "visa",
"checks": {
"address_line1_check": null,
"address_postal_code_check": null,
"cvc_check": "pass"
},
"country": "US",
"exp_month": 2,
"exp_year": 2022,
"fingerprint": "MxtsbEBU2BmJbOn4",
"funding": "credit",
"installments": null,
"last4": "4242",
"network": "visa",
"three_d_secure": null,
"wallet": null
},
"type": "card"
},
"receipt_email": null,
"receipt_number": null,
"receipt_url": "https://pay.stripe.com/receipts/acct_1BhpF1HpIo9Nhh5a/ch_1KWzCaHpIo9Nhh5atK09dpaO/rcpt_LDQ2FDPK6fwsEyBOISiDCItSv8JeNbl",
"refunded": false,
"refunds": {
"object": "list",
"data": [
],
"has_more": false,
"total_count": 0,
"url": "/v1/charges/ch_1KWzCaHpIo9Nhh5atK09dpaO/refunds"
},
"review": null,
"shipping": null,
"source": null,
"source_transfer": null,
"statement_descriptor": null,
"statement_descriptor_suffix": null,
"status": "succeeded",
"transfer_data": null,
"transfer_group": null
}
},
"livemode": false,
"pending_webhooks": 1,
"request": {
"id": "req_QzrolSFU0OA7D7",
"idempotency_key": "228c5670-85ea-4047-9f9f-9d1e519ffc2c"
},
"type": "charge.succeeded"
}
a Checkout Session will be generated under the hood when your customer opens a Payment Link. Therefore, you should listen to the checkout.session.completed events, and get the product data from the line_items property of the Checkout Session object.
I have had a similar problem when using a webhook solution in Stripe. Although I had defined meta data in the product it was not shipped, i.e. metadata in JSON was empty.
In my case the solution has been to use the meta data of the price instead (which is defined within the product). For this, just click price section on product page on Stripe website...
...and define meta data on the price page.
There is another interesting post https://stackoverflow.com/a/69117489/10849985 showing that it is event-depending if meta data is provided or not, i.e. some events linked to the webhook inlcude it, others not.
For me the issue was that I was merging the price and products objects together which was overwriting the metadata on the product with the empty metadata of the price object.
To fix this I just made sure the metadata was receiving the values from the product obj
const product = await stripe.products.list();
const price = await stripe.prices.list();
const productData = product.data.map((productData, index) => {
return {
...productData,
...price.data[index],
quantity: 1,
addedToCart: false,
metadata: productData.metadata };
});
I am using Sripe PHP SDK from this link
When I am trying to use any api of Stripe Payment it gets successful but the response it returns is empty
I am following API Instructions from Strip API Documentations
For example I am using following code snippet to create a payment
$stripe = new \Stripe\StripeClient(
'SECRET_KEY'
);
$stripe->paymentIntents->create([
'amount' => 1200,
'currency' => 'usd',
'payment_method_types' => ['card']
]);
// after successfull payment, you can store payment related information into your database
$this->response([ 'success' =>'1' ,'status' => 'success', 'message' => $stripe], REST_Controller::HTTP_OK);
But it is not returning any response, instead the return object is empty.
Here is the response
{"success":"1","status":"success","message":{}}
It should return an object like this in message
{
"id": "pi_1HkSRrKUkp6sNA1QWNFwUOLd",
"object": "payment_intent",
"amount": 2000,
"amount_capturable": 0,
"amount_received": 0,
"application": null,
"application_fee_amount": null,
"canceled_at": null,
"cancellation_reason": null,
"capture_method": "automatic",
"charges": {
"object": "list",
"data": [],
"has_more": false,
"url": "/v1/charges?payment_intent=pi_1HkSRrKUkp6sNA1QWNFwUOLd"
},
"client_secret": "SECRET_KEY",
"confirmation_method": "automatic",
"created": 1604659539,
"currency": "usd",
"customer": null,
"description": "Created by stripe.com/docs demo",
"invoice": null,
"last_payment_error": null,
"livemode": false,
"metadata": {},
"next_action": null,
"on_behalf_of": null,
"payment_method": null,
"payment_method_options": {
"card": {
"installments": null,
"network": null,
"request_three_d_secure": "automatic"
}
},
"payment_method_types": [
"card"
],
"receipt_email": null,
"review": null,
"setup_future_usage": null,
"shipping": null,
"statement_descriptor": null,
"statement_descriptor_suffix": null,
"status": "requires_payment_method",
"transfer_data": null,
"transfer_group": null
}
Where exactly are you getting that example from? It looks incomplete.
You should just save the output of the API call to some variable, then return the client_secret for example:
$intent = $stripe->paymentIntents->create([
'amount' => 1200,
'currency' => 'usd',
'payment_method_types' => ['card']
]);
$this->response([ 'success' =>'1' ,'status' => 'success', 'message' => $intent->client_secret], REST_Controller::HTTP_OK);
I'm using Intent API(currency, amount only) and confirmCardPayment and Stripe rendered HTML input to checkout.
Now I want to implement saved credit cards, I can save it, but I don't know how to use it to pass to confirmCardPayment.
My currently working confirmCardPayment code is this
Stripe.confirmCardPayment(intent.client_secret, {
payment_method: {
/**
* I'm using Vue here
* If I'm using a saved card, what should I pass here?
**/
card: this.$refs.stripeCardRef.cardNumberElement,
},
setup_future_usage: "off_session",
});
If I'm using a saved card, what should I pass to confirmCardPayment, and also Intent API(I'm using currency, amount only right now)?
So I can use stripe.paymentMethods.list to get a customer's all saved credit cards, and return it to the front-end like this:
// Server
export async function listPaymentMethods(userId: string) {
const customer = await getOrCreateCustomer(userId);
return stripe.paymentMethods.list({
customer: customer.id,
type: "card",
});
}
Then, my front-end get the response like this
{
"object": "list",
"data": [
{
"id": "pm_1GuBTiICxYQTNr22LwKB6rfy",
"object": "payment_method",
"billing_details": {
"address": {
"city": null,
"country": null,
"line1": null,
"line2": null,
"postal_code": null,
"state": null
},
"email": null,
"name": null,
"phone": null
},
"card": {
"brand": "visa",
"checks": {
"address_line1_check": null,
"address_postal_code_check": null,
"cvc_check": "pass"
},
"country": "US",
"exp_month": 1,
"exp_year": 2023,
"fingerprint": "riI755UKjfafxa3C",
"funding": "credit",
"generated_from": null,
"last4": "4242",
"networks": {
"available": ["visa"],
"preferred": null
},
"three_d_secure_usage": {
"supported": true
},
"wallet": null
},
"created": 1592201250,
"customer": "cus_HPOXLjeqF24rBn",
"livemode": false,
"metadata": [],
"type": "card"
}
],
"has_more": false,
"url": "/v1/payment_methods"
}
How can I use this response data to confirmCardPayment?
Should my intent API need to be changed also?
This flow is covered in Stripe's documentation here: https://stripe.com/docs/payments/save-and-reuse#web-create-payment-intent-off-session
The idea is that you pass the existing PaymentMethod id pm_12345 client-side when you confirm the PaymentIntent like this:
stripe.confirmCardPayment(
intent.client_secret,
{
payment_method: intent.last_payment_error.payment_method.id
}
).then(function(result) {
if (result.error) {
// Show error to your customer
console.log(result.error.message);
} else {
if (result.paymentIntent.status === 'succeeded') {
// The payment is complete!
}
}
});
I am trying to get customer_id of a stripe customer from the response of of charge entity. But response is not providing the customer id in return.
&stripe.Charge JSON: {
"id": "ch_1AxWbTFytruJp2FXW6iuRd1X",
"object": "charge",
"amount": 100,
"amount_refunded": 0,
"application": null,
"application_fee": null,
"balance_transaction": "txn_17JOXKFytruJp2FXS4XNisQd",
"captured": false,
"created": 1504339423,
"currency": "usd",
"customer": null,
"description": "My First Test Charge (created for API docs)",
"destination": null,
"dispute": null,
"failure_code": null,
"failure_message": null,
"fraud_details": {
},
"invoice": null,
"livemode": false,
"metadata": {
},
"on_behalf_of": null,
"order": null,
"outcome": null,
"paid": true,
"receipt_email": null,
"receipt_number": null,
"refunded": false,
"refunds": {
"object": "list",
"data": [
],
"has_more": false,
"total_count": 0,
"url": "/v1/charges/ch_1AxWbTFytruJp2FXW6iuRd1X/refunds"
},
"review": null,
"shipping": null,
"source": {
"id": "card_1AxWPmFytruJp2FXw4m0V0fN",
"object": "card",
"address_city": null,
"address_country": null,
"address_line1": null,
"address_line1_check": null,
"address_line2": null,
"address_state": null,
"address_zip": "10001",
"address_zip_check": "unchecked",
"brand": "Visa",
"country": "US",
"customer": null,
"cvc_check": "unchecked",
"dynamic_last4": null,
"exp_month": 4,
"exp_year": 2024,
"fingerprint": "sNtyd9sZ2vA6o4IM",
"funding": "credit",
"last4": "4242",
"metadata": {
},
"name": "Mandeep",
"tokenization_method": null
},
"source_transfer": null,
"statement_descriptor": null,
"status": "succeeded",
"transfer_group": null
}
But it has a customer field inside the object which is null. Can anyone please tell me why am I getting this null?
What I am trying to do is to make a system where customer can book anonymously on site and while creating the booking the customer gets registered and charged for the total amount of the booking. I need to keep track of the customer's stripe account id and card id. So the problem is if I am creating a customer then I am not able to get its card id but when I am charging the customer then I am not able to get the customer id.
Customer Response:
&stripe.Customer JSON: {
"id": "cus_BKAxGZre2HCNIU",
"object": "customer",
"account_balance": 0,
"created": 1504339424,
"currency": "usd",
"default_source": null,
"delinquent": false,
"description": null,
"discount": null,
"email": null,
"livemode": false,
"metadata": {
},
"shipping": null,
"sources": {
"object": "list",
"data": [
],
"has_more": false,
"total_count": 0,
"url": "/v1/customers/cus_BKAxGZre2HCNIU/sources"
},
"subscriptions": {
"object": "list",
"data": [
],
"has_more": false,
"total_count": 0,
"url": "/v1/customers/cus_BKAxGZre2HCNIU/subscriptions"
}
}
When you create a charge using only the credit card token, no Stripe customer is created, you only create a payment with no associated customer.
So it's normal that you the API returns customer: null.
Instead of charging a credit card, I think you should charge a new customer.
In your backend code, you could handle the payment in 2 steps:
STEP 1: create a new customer, passing the credit card token to store
the customer's card
STEP 2: charge the customer, using the customer ID returned by
STEP 1 API call.
Doing this, you charge the customer with the credit card that is stored in customer's data.
For more details, check here: https://stripe.com/docs/quickstart#saving-card-information
Does it make sense?
You can access card_id while creating new customer using following code (in Golang):
for _, data := range customer.Sources.Values{
card_id = data.ID
}
fmt.Println(card_id)
I spend almost 1 day to figure it out. Actually in customer structure(under stripe package) there are some fields which are having embedded types and such fields are further connected to some other structure in different files. So there is hierarchy to access structure fields like above.
Hope this will solve your problem.
Thanks!
I am using stripe to manage my transactions.
I have a need to add the customer (if not already present) and then add the card to that customer.
Now i have to get token for both processes which i did by
stripe.tokens.create({
card: {
"number": "4242424242424242",
"exp_month": "1",
"exp_year": "2019",
"cvc": "123"
}
}, function (err, token) {
// asynchronously called
if (token != null) {
//Do something
}
else {
//Do something
}
});
Now i am calling this method twice to get new token every time to add customer if not present and to add card to that customer. But every time i get same response as i am sending same information of the card.
1St
{
"id": "tok_16v4mvBR7e7FUB557PD3hOZc",
"object": "token",
"card": {
"id": "card_16v4mvBR7e7FUB55KD06kqJo",
"object": "card",
"address_city": null,
"address_country": null,
"address_line1": null,
"address_line1_check": null,
"address_line2": null,
"address_state": null,
"address_zip": null,
"address_zip_check": null,
"brand": "Diners Club",
"country": null,
"cvc_check": "unchecked",
"dynamic_last4": null,
"exp_month": 10,
"exp_year": 2021,
"fingerprint": "iE2uiiDxUFHRgpt1",
"funding": "credit",
"last4": "5904",
"metadata": {},
"name": null,
"tokenization_method": null
},
"client_ip": "51.25.52.52",
"created": 1444650501,
"livemode": false,
"type": "card",
"used": false
}
2nd
{
"id": "tok_16v4mvBR7e7FUB557PD3hOZc",
"object": "token",
"card": {
"id": "card_16v4mvBR7e7FUB55KD06kqJo",
"object": "card",
"address_city": null,
"address_country": null,
"address_line1": null,
"address_line1_check": null,
"address_line2": null,
"address_state": null,
"address_zip": null,
"address_zip_check": null,
"brand": "Diners Club",
"country": null,
"cvc_check": "unchecked",
"dynamic_last4": null,
"exp_month": 10,
"exp_year": 2021,
"fingerprint": "iE2uiiDxUFHRgpt1",
"funding": "credit",
"last4": "5904",
"metadata": {},
"name": null,
"tokenization_method": null
},
"client_ip": "55.55.55.55",
"created": 1444650501,
"livemode": false,
"type": "card",
"used": false
}
Check every field is same as first.
But if i want to use the same token which i get first time i am getting error
Stripe Error 400 - Cannot use stripe token more than once
So my question is if i am getting the same token again then why it is giving me error in using the token returned first time for adding customer and to add card for that customer.
This could increase my site speed if i do not have to get token again.
From the comments of #Matthew Arkin
I have a need to add the customer (if not already present) and then add the card to that customer.
The Business logic of stipe is such that when you ask user for credit card detail only then it will add customer so they are not different but same procedures.
The stripe Token is a way to encode the following things:
1) A bank account
2) A credit card detail
To get a token you should pass Card information for credit card like here.
Now you can get the same token using stripe.js. the good thing for using stripejs is the a token will be sent instead of a card information.
To add a customer you should supply either the token from stripe.js (or from api) or the card information. using token is what i think is best method.
When you do call stripe using token you automatically create customer with a card. hence customer cannot be created without the card information supplied to stripe.
If you want to add card to existing customer then do following:
**1) Retrieve customer passing customer id **
2) Check customer for null and then add the card for that customer obtaining a token from stripe.js