新手求助:基于ParseServer/Bitnami在Android应用中接入Stripe支付
Hey there! I totally get how tricky it can be to set up Stripe payments when you don't have formal courses or mentor guidance—let's break this down step by step for your Android app with Parse Server (Bitnami) setup. I'll keep things practical and easy to follow, since you're new to this.
1. Android端:基础Stripe配置
First, let's get the Android app ready to interact with Stripe:
- Add the Stripe SDK dependency to your module-level
build.gradlefile:dependencies { implementation 'com.stripe:stripe-android:20.17.0' // Check Stripe's official docs for the latest version } - Initialize Stripe in your app (either in your Application class or the payment screen's
onCreatemethod):
Grab your publishable key from your Stripe Dashboard under the API Keys section.val stripe = Stripe(applicationContext, "pk_test_YOUR_PUBLISHABLE_KEY")
2. Parse Server (Bitnami):配置Stripe云函数
Critical note: You can't create a PaymentIntent directly on the Android client—it's unsafe and violates Stripe's compliance rules. We'll use a Parse Cloud Function to handle this server-side:
- SSH into your Bitnami server, navigate to the Parse cloud code directory (usually
/opt/bitnami/apps/parse/htdocs/cloud), then install the Stripe Node.js library:npm install stripe --save - Open
main.jsand add this cloud function (replace with your Stripe secret key):const stripe = require('stripe')('sk_test_YOUR_SECRET_KEY'); Parse.Cloud.define("createPaymentIntent", async (request) => { const { amount, currency } = request.params; try { const paymentIntent = await stripe.paymentIntents.create({ amount: amount, // Amount in cents (e.g., 100 = $1.00) currency: currency || 'usd', }); return { clientSecret: paymentIntent.client_secret }; } catch (error) { throw new Parse.Error(Parse.Error.INTERNAL_SERVER_ERROR, error.message); } }); - Restart Parse Server to apply the changes:
sudo /opt/bitnami/ctlscript.sh restart parse
3. Android端:调用云函数并完成支付
Now, let's tie everything together to let users complete payments:
- Call the Parse Cloud Function to get the PaymentIntent's client secret:
val paymentParams = hashMapOf( "amount" to 100, // $1.00 example "currency" to "usd" ) ParseCloud.callFunctionInBackground<String>("createPaymentIntent", paymentParams) { clientSecret, error -> if (error == null && clientSecret != null) { // Launch Stripe's payment flow with the client secret startStripePaymentFlow(clientSecret) } else { Toast.makeText(this, "Failed to prepare payment: ${error?.message}", Toast.LENGTH_LONG).show() } } - Implement the payment flow using Stripe's PaymentSheet (the simplest, most user-friendly option):
private fun startStripePaymentFlow(clientSecret: String) { val paymentSheet = PaymentSheet(this, ::onPaymentSheetResult) paymentSheet.presentWithPaymentIntent( clientSecret, PaymentSheet.Configuration( merchantDisplayName = "Your App Name", // Add optional config like customer info if needed ) ) } private fun onPaymentSheetResult(result: PaymentSheetResult) { when (result) { is PaymentSheetResult.Completed -> { Toast.makeText(this, "Payment successful!", Toast.LENGTH_LONG).show() // Optional: Call another Parse Cloud Function to record the payment in your database } is PaymentSheetResult.Canceled -> { // User canceled the payment } is PaymentSheetResult.Failed -> { Toast.makeText(this, "Payment failed: ${result.error.message}", Toast.LENGTH_LONG).show() } } }
4. Key Things to Remember
- Security first: Never hardcode your Stripe secret key in the Android app—always handle secret-key operations in Parse Cloud Functions.
- Bitnami permissions: If you run into issues installing npm packages, use
sudobut make sure file ownership is set correctly so Parse Server can access the installed library. - Test first: Use Stripe's test card (4242 4242 4242 4242, any future expiration date, any CVC) to test the flow before going live.
- Error handling: Add detailed error messages in your cloud function and client code to make debugging easier when things go wrong.
内容的提问来源于stack exchange,提问作者MarvyP
相关产品推荐
相关产品推荐

