#tarunmv30
1 messages · Page 1 of 1 (latest)
Im trying to implement direct payments in my swiftui app
ill show you my code and the error poppingup
👍
const total = items.reduce((previous, current) => {
return previous + current.price
}, 0)
const fee = Math.round(total * 0.1);
const transferAmount = Math.round(total * 0.9);
return {
total: total,
fee: fee,
transferAmount: transferAmount
};
};
app.post("/create-payment-intent", async (req, res) => {
const items = req.body;
// Calculate the order amount, fee, and transfer amount
const { total, fee, transferAmount } = calculateOrderAmount(items);
// Create a PaymentIntent with the order amount and currency
const paymentIntent = await stripe.paymentIntents.create({
amount: total,
currency: 'usd',
automatic_payment_methods: {enabled: true},
application_fee_amount: fee,
}, {
stripeAccount: '{acct_1MsyJPCWAlRghLO7}',
});
// Generate an ephemeral key for the customer
const customer = await stripe.customers.create();
const ephemeralKey = await stripe.ephemeralKeys.create(
{ customer: customer.id },
{ apiVersion: '2020-08-27' }
);
// Return the necessary information to the client
res.send({
customer: customer.id,
ephemeralKey: ephemeralKey.secret,
paymentIntent: paymentIntent.client_secret,
publishableKey: 'pk_test_51MSCbfJMYkCh6QmLjYSQLK5gPtmLvaUMn0KrXsbFVnqVROCRere3kLrAQnKOT2Uepb5fRqv34vjgNAArl8wzPwWd00OoOZlDFj' // replace with your own publishable key
});
});
exports.stripe = functions.https.onRequest(app);```
This is my server code
let jsonData = try? JSONEncoder().encode(cart)
var request = URLRequest(url: backendCheckoutUrl)
request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.httpBody = jsonData
let task = URLSession.shared.dataTask(
with: request,
completionHandler: { (data, _, _) in
guard let data = data,
let json = try? JSONSerialization.jsonObject(with: data, options: [])
as? [String: Any],
let customerId = json["customer"] as? String,
let customerEphemeralKeySecret = json["ephemeralKey"] as? String,
let paymentIntentClientSecret = json["paymentIntent"] as? String,
let publishableKey = json["publishableKey"] as? String
else {
print("There was an errir with the data")
return
}
STPAPIClient.shared.publishableKey = publishableKey
var configuration = PaymentSheet.Configuration()
configuration.merchantDisplayName = "Example, Inc."
configuration.applePay = .init(
merchantId: "com.foo.example", merchantCountryCode: "US")
configuration.customer = .init(
id: customerId, ephemeralKeySecret: customerEphemeralKeySecret)
configuration.returnURL = "payments-example://stripe-redirect"
configuration.allowsDelayedPaymentMethods = true
DispatchQueue.main.async {
self.paymentSheet = PaymentSheet(
paymentIntentClientSecret: paymentIntentClientSecret,
configuration: configuration)
}
})
task.resume()
}```
I can't really see anything from that screenshot. What is the specific error you see?
the error is hitting the print statement "there was an error with the data"
so i think it has to do from the response coming back from the server
this is the error logged from my server
Ah okay
Can you share the request ID for that error from your server. You can find the request ID via: https://support.stripe.com/questions/finding-the-id-for-an-api-request
for some reason my logs are empty in the stripe dashboard
Can you make sure you click "more" and toggle on "outgoing Connect requests"
still no luck
I was able to succesfull process payments however whenever I try to do the application fee / integrate connect it doenst work
Are you sure you are using the correct platform API key here that is connected to that Connected Account (acct_1MsyJPCWAlRghLO7)
Like it looks like that account is connected to a platform, so if you are using the right keys then you shouldn't see this error
Ah ah ah
I missed it
You are only passing the Stripe Account header for the PaymentIntent creation
You need to pass that for all 3 requests
youre good lol i know the screenshot dont help
The Customer and Ephemeral Key requests as well
So like: const customer = await stripe.customers.create({ stripeAccount: 'acct_1MsyJPCWAlRghLO7', });
Yep that looks good
Try that
sweet give me a moment
Oh also please redact that secret key
And you may want to consider rolling it
As this is a public server
Even with it being a test key it can give access to your account
in the code tho I dont know where to put the transfer amount tho
const total = items.reduce((previous, current) => {
return previous + current.price
}, 0)
const fee = Math.round(total * 0.1);
const transferAmount = Math.round(total * 0.9);
return {
total: total,
fee: fee,
transferAmount: transferAmount
};
};
app.post("/create-payment-intent", async (req, res) => {
const items = req.body;
// Calculate the order amount, fee, and transfer amount
const { total, fee, transferAmount } = calculateOrderAmount(items);
// Create a PaymentIntent with the order amount and currency
const paymentIntent = await stripe.paymentIntents.create({
amount: total,
currency: 'usd',
application_fee_amount: fee,
}, {
stripeAccount: 'acct_1MsyJPCWAlRghLO7',
});
// Generate an ephemeral key for the customer
const customer = await stripe.customers.create({
email: 'customer@example.com'
}, {
stripeAccount: 'acct_1MsyJPCWAlRghLO7',
});
const ephemeralKey = await stripe.ephemeralKeys.create(
{ customer: customer.id },
{ apiVersion: '2020-08-27', stripeAccount: 'acct_1MsyJPCWAlRghLO7' }
);
// Return the necessary information to the client
res.send({
customer: customer.id,
ephemeralKey: ephemeralKey.secret,
paymentIntent: paymentIntent.client_secret,
publishableKey: 'pk_test_51MSCbfJMYkCh6QmLjYSQLK5gPtmLvaUMn0KrXsbFVnqVROCRere3kLrAQnKOT2Uepb5fRqv34vjgNAArl8wzPwWd00OoOZlDFj' // replace with your own publishable key
});
});
exports.stripe = functions.https.onRequest(app);
You are using Standard Connected Accounts so you don't use a transfer amount
gotcha
This will create the Charge directly on the Connected Account
Then you take your "commission" via the application fee
sorry this is the error im getting now
ERROR 2023-04-04T17:33:40.795156Z [resource.labels.functionName: stripe] [labels.executionId: ycxd1y64ov0x] Error: Amount must be at least $0.50 usd at Function.generate (/workspace/node_modules/stripe/cjs/Error.js:10:20) at res.toJSON.then.Error_js_1.StripeAPIError.message (/workspace/node_modules/stripe/cjs/RequestSender.js:105:54) at processTicksAndRejections (node:internal/process/task_queues:96:5)
It sounds like you're hitting the minimum amount error for the transaction: https://stripe.com/docs/currencies#minimum-and-maximum-charge-amounts
the charge was for $10
i think the application fee is too small? is there a lmit on that
Do you have a request ID for the request that failed with that error message?
Here's how you can find a request ID: https://support.stripe.com/questions/finding-the-id-for-an-api-request
Find help and support for Stripe. Our support center provides answers on all types of situations, including account information, charges and refunds, and subscriptions information. Get your questions answered and find international support for Stripe.
Error: Amount must be at least $0.50 usd
at Function.generate (/workspace/node_modules/stripe/cjs/Error.js:10:20)
at res.toJSON.then.Error_js_1.StripeAPIError.message (/workspace/node_modules/stripe/cjs/RequestSender.js:105:54)
at processTicksAndRejections (node:internal/process/task_queues:96:
still getting the same error and my logs are empty for some reason
im only able to see the log for it on my server
Try using a larger amount for the application fee and (if it works) then you have your answer
thats not the issue because .10 *10 is a dollar
its triggering that error incorrectly
const express = require("express");
const cors = require('cors');
const app = express();
app.use(cors({ origin: true }));
app.use(express.static("public"));
app.use(express.json());
const stripe = require("stripe")('sk_test_51MSCbfJMYkCh6QmL1XTvPCddMuGAqUyLNbPf5JUUgptgGz6TksSpsAbGXqCtFfBJml87zA9hzIWWdazmkO14nFXC00AdQGwasx');
const calculateOrderAmount = items => {
const total = items.reduce((previous, current) => {
return previous + current.price
}, 0)
const fee = Math.round(total * 0.3);
return {
total: Math.round(total),
fee: fee
};
};
app.post("/create-payment-intent", async (req, res) => {
const items = req.body;
// Calculate the order amount, fee, and transfer amount
const { total, fee } = calculateOrderAmount(items);
// Create a PaymentIntent with the order amount and currency
const paymentIntent = await stripe.paymentIntents.create({
amount: total,
currency: 'usd',
application_fee_amount: fee,
}, {
stripeAccount: 'acct_1MsyJPCWAlRghLO7',
});
// Generate an ephemeral key for the customer
const customer = await stripe.customers.create({
email: 'customer@example.com'
}, {
stripeAccount: 'acct_1MsyJPCWAlRghLO7',
});
const ephemeralKey = await stripe.ephemeralKeys.create(
{ customer: customer.id },
{ apiVersion: '2020-08-27', stripeAccount: 'acct_1MsyJPCWAlRghLO7' }
);
// Return the necessary information to the client
res.send({
customer: customer.id,
ephemeralKey: ephemeralKey.secret,
paymentIntent: paymentIntent.client_secret,
publishableKey: 'pk_test_51MSCbfJMYkCh6QmLjYSQLK5gPtmLvaUMn0KrXsbFVnqVROCRere3kLrAQnKOT2Uepb5fRqv34vjgNAArl8wzPwWd00OoOZlDFj' // replace with your own publishable key
});
});
exports.stripe = functions.https.onRequest(app);