英文:
How to implement stripe accordion layout with react js?
问题
以下是翻译好的部分:
我想要实现Stripe支付布局的手风琴样式。但是无法实现这一点。我已经实现了选项卡布局,但无法实现手风琴。我无法确定在哪里使用 `paymentElement`。
客户端:
我已经尝试过这个,但没有成功。
```javascript
const paymentElement = elements.create('payment', {
layout: {
type: 'accordion',
defaultCollapsed: false,
radios: true,
spacedAccordionItems: false
}
});
return (
<form id="payment-form" onSubmit={handleSubmit}>
<PaymentElement id="payment-element" options={{ paymentElement }}/>
<button disabled={isProcessing || !stripe || !elements} id="submit">
<span id="button-text">
{isProcessing ? "Processing ..." : "Pay now"}
</span>
</button>
{/* 显示任何错误或成功消息 */}
{message && <div id="payment-message">{message}</div>}
</form>
);
希望这有所帮助。如果您有其他问题,请随时提出。
英文:
I want to implement the stipe Accordion payment layout. But couldn't implement this. I have implemented the Tab layout. But couldn't do the Accordion. I'm not getting to the point where I should use the paymentElement
.
const express = require("express");
const app = express();
const { resolve } = require("path");
// Replace if using a different env file or config
const env = require("dotenv").config({ path: "./.env" });
const stripe = require("stripe")(process.env.STRIPE_SECRET_KEY, {
apiVersion: "2022-08-01",
});
app.use(express.static(process.env.STATIC_DIR));
app.get("/", (req, res) => {
const path = resolve(process.env.STATIC_DIR + "/index.html");
res.sendFile(path);
});
app.get("/config", (req, res) => {
res.send({
publishableKey: process.env.STRIPE_PUBLISHABLE_KEY,
});
});
app.post("/create-payment-intent", async (req, res) => {
try {
const paymentIntent = await stripe.paymentIntents.create({
currency: "EUR",
amount: 1999,
automatic_payment_methods: { enabled: true },
});
// Send publishable key and PaymentIntent details to client
res.send({
clientSecret: paymentIntent.client_secret,
});
} catch (e) {
return res.status(400).send({
error: {
message: e.message,
},
});
}
});
app.listen(5252, () =>
console.log(`Node server listening at http://localhost:5252`)
);
Client:
import { useEffect, useState } from "react";
import { Elements } from "@stripe/react-stripe-js";
import CheckoutForm from "./CheckoutForm";
import { loadStripe } from "@stripe/stripe-js";
function Payment() {
const [stripePromise, setStripePromise] = useState(null);
const [clientSecret, setClientSecret] = useState("");
useEffect(() => {
fetch("/config").then(async (r) => {
const { publishableKey } = await r.json();
setStripePromise(loadStripe(publishableKey));
});
}, []);
useEffect(() => {
fetch("/create-payment-intent", {
method: "POST",
body: JSON.stringify({}),
}).then(async (result) => {
var { clientSecret } = await result.json();
setClientSecret(clientSecret);
});
}, []);
return (
<>
<h1>React Stripe and the Payment Element</h1>
{clientSecret && stripePromise && (
<Elements stripe={stripePromise} options={{ clientSecret }}>
<CheckoutForm />
</Elements>
)}
</>
);
}
export default Payment;
checout:
import { PaymentElement } from "@stripe/react-stripe-js";
import { useState } from "react";
import { useStripe, useElements } from "@stripe/react-stripe-js";
export default function CheckoutForm() {
const stripe = useStripe();
const elements = useElements();
const [message, setMessage] = useState(null);
const [isProcessing, setIsProcessing] = useState(false);
const handleSubmit = async (e) => {
e.preventDefault();
if (!stripe || !elements) {
// Stripe.js has not yet loaded.
// Make sure to disable form submission until Stripe.js has loaded.
return;
}
setIsProcessing(true);
const { error } = await stripe.confirmPayment({
elements,
confirmParams: {
// Make sure to change this to your payment completion page
return_url: `${window.location.origin}/completion`,
},
});
if (error.type === "card_error" || error.type === "validation_error") {
setMessage(error.message);
} else {
setMessage("An unexpected error occured.");
}
setIsProcessing(false);
};
const paymentElement = elements.create('payment', {
layout: {
type: 'accordion',
defaultCollapsed: false,
radios: true,
spacedAccordionItems: false
}
});
return (
<form id="payment-form" onSubmit={handleSubmit}>
<PaymentElement id="payment-element"/>
<button disabled={isProcessing || !stripe || !elements} id="submit">
<span id="button-text">
{isProcessing ? "Processing ... " : "Pay now"}
</span>
</button>
{/* Show any error or success messages */}
{message && <div id="payment-message">{message}</div>}
</form>
);
}
I have tried this. But it's not working.
const paymentElement = elements.create('payment', {
layout: {
type: 'accordion',
defaultCollapsed: false,
radios: true,
spacedAccordionItems: false
}
});
return (
<form id="payment-form" onSubmit={handleSubmit}>
<PaymentElement id="payment-element" options={{paymentElement }}/>
<button disabled={isProcessing || !stripe || !elements} id="submit">
<span id="button-text">
{isProcessing ? "Processing ... " : "Pay now"}
</span>
</button>
{/* Show any error or success messages */}
{message && <div id="payment-message">{message}</div>}
</form>
);
答案1
得分: 2
在当前这一行的地方:
<PaymentElement id="payment-element" options={{paymentElement }}/>
你需要更新它并在options
中包括你想要的付款元素选项,大致看起来会像这样:
<PaymentElement id="payment-element" options={{layout: {type: "accordion", ... }}/>
或者,你可以将你想要的选项存储在一个变量中,然后将其传递给options
:
const paymentElementOptions = {
layout: "accordion"
}
return (
<form id="payment-form" onSubmit={handleSubmit}>
<PaymentElement id="payment-element" options={paymentElementOptions} />
...
)
英文:
Where you currently have this line:
<PaymentElement id="payment-element" options={{paymentElement }}/>`
You'll want to update that and include your desired Payment Element options in options
there, so that would look roughly like this:
<PaymentElement id="payment-element" options={{layout: {type: "accordion", ... }}/>`
Alternatively you can store your desired options in a variable and then pass that into options
:
const paymentElementOptions = {
layout: "accordion"
}
return (
<form id="payment-form" onSubmit={handleSubmit}>
<PaymentElement id="payment-element" options={paymentElementOptions} />
...
)
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论