如何使用React.js实现带有条纹效果的手风琴布局?

huangapple go评论94阅读模式
英文:

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(&quot;express&quot;);
const app = express();
const { resolve } = require(&quot;path&quot;);
// Replace if using a different env file or config
const env = require(&quot;dotenv&quot;).config({ path: &quot;./.env&quot; });

const stripe = require(&quot;stripe&quot;)(process.env.STRIPE_SECRET_KEY, {
  apiVersion: &quot;2022-08-01&quot;,
});

app.use(express.static(process.env.STATIC_DIR));

app.get(&quot;/&quot;, (req, res) =&gt; {
  const path = resolve(process.env.STATIC_DIR + &quot;/index.html&quot;);
  res.sendFile(path);
});

app.get(&quot;/config&quot;, (req, res) =&gt; {
  res.send({
    publishableKey: process.env.STRIPE_PUBLISHABLE_KEY,
  });
});

app.post(&quot;/create-payment-intent&quot;, async (req, res) =&gt; {
  try {
    const paymentIntent = await stripe.paymentIntents.create({
      currency: &quot;EUR&quot;,
      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, () =&gt;
  console.log(`Node server listening at http://localhost:5252`)
);

Client:

import { useEffect, useState } from &quot;react&quot;;

import { Elements } from &quot;@stripe/react-stripe-js&quot;;
import CheckoutForm from &quot;./CheckoutForm&quot;;
import { loadStripe } from &quot;@stripe/stripe-js&quot;;

function Payment() {
  const [stripePromise, setStripePromise] = useState(null);
  const [clientSecret, setClientSecret] = useState(&quot;&quot;);
   
   useEffect(() =&gt; {
    fetch(&quot;/config&quot;).then(async (r) =&gt; {
      const { publishableKey } = await r.json();
      setStripePromise(loadStripe(publishableKey));
    });
  }, []);

  useEffect(() =&gt; {
    fetch(&quot;/create-payment-intent&quot;, {
      method: &quot;POST&quot;,
      body: JSON.stringify({}),
    }).then(async (result) =&gt; {
      var { clientSecret } = await result.json();
      setClientSecret(clientSecret);
    });
  }, []);

  return (
    &lt;&gt;
      &lt;h1&gt;React Stripe and the Payment Element&lt;/h1&gt;
      {clientSecret &amp;&amp; stripePromise &amp;&amp; (
        &lt;Elements stripe={stripePromise} options={{ clientSecret }}&gt;
          &lt;CheckoutForm /&gt;
        &lt;/Elements&gt;
      )}
    &lt;/&gt;
  );
}

export default Payment;

checout:

import { PaymentElement } from &quot;@stripe/react-stripe-js&quot;;
import { useState } from &quot;react&quot;;
import { useStripe, useElements } from &quot;@stripe/react-stripe-js&quot;;

export default function CheckoutForm() {
  const stripe = useStripe();
  const elements = useElements();

  const [message, setMessage] = useState(null);
  const [isProcessing, setIsProcessing] = useState(false);
  

  const handleSubmit = async (e) =&gt; {
    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 === &quot;card_error&quot; || error.type === &quot;validation_error&quot;) {
      setMessage(error.message);
    } else {
      setMessage(&quot;An unexpected error occured.&quot;);
    }

    setIsProcessing(false);
  };

    const paymentElement = elements.create(&#39;payment&#39;, {
      layout: {
       type: &#39;accordion&#39;,
        defaultCollapsed: false,
        radios: true,
      spacedAccordionItems: false
     }
   });

  return (
    &lt;form id=&quot;payment-form&quot; onSubmit={handleSubmit}&gt;
      &lt;PaymentElement id=&quot;payment-element&quot;/&gt;
      &lt;button disabled={isProcessing || !stripe || !elements} id=&quot;submit&quot;&gt;
        &lt;span id=&quot;button-text&quot;&gt;
          {isProcessing ? &quot;Processing ... &quot; : &quot;Pay now&quot;}
        &lt;/span&gt;
      &lt;/button&gt;
      {/* Show any error or success messages */}
      {message &amp;&amp; &lt;div id=&quot;payment-message&quot;&gt;{message}&lt;/div&gt;}
    &lt;/form&gt;
  );
}

I have tried this. But it's not working.

const paymentElement = elements.create(&#39;payment&#39;, {
      layout: {
       type: &#39;accordion&#39;,
        defaultCollapsed: false,
        radios: true,
      spacedAccordionItems: false
     }
   });

  

return (
    &lt;form id=&quot;payment-form&quot; onSubmit={handleSubmit}&gt;
      &lt;PaymentElement id=&quot;payment-element&quot; options={{paymentElement }}/&gt;
      &lt;button disabled={isProcessing || !stripe || !elements} id=&quot;submit&quot;&gt;
        &lt;span id=&quot;button-text&quot;&gt;
          {isProcessing ? &quot;Processing ... &quot; : &quot;Pay now&quot;}
        &lt;/span&gt;
      &lt;/button&gt;
      {/* Show any error or success messages */}
      {message &amp;&amp; &lt;div id=&quot;payment-message&quot;&gt;{message}&lt;/div&gt;}
    &lt;/form&gt;
  );

答案1

得分: 2

在当前这一行的地方:

&lt;PaymentElement id=&quot;payment-element&quot; options={{paymentElement }}/&gt;

你需要更新它并在options中包括你想要的付款元素选项,大致看起来会像这样:

&lt;PaymentElement id=&quot;payment-element&quot; options={{layout: {type: &quot;accordion&quot;, ... }}/&gt;

或者,你可以将你想要的选项存储在一个变量中,然后将其传递给options

const paymentElementOptions = {
  layout: &quot;accordion&quot;
}

return (
  &lt;form id=&quot;payment-form&quot; onSubmit={handleSubmit}&gt;
    &lt;PaymentElement id=&quot;payment-element&quot; options={paymentElementOptions} /&gt;
  ...
)
英文:

Where you currently have this line:

&lt;PaymentElement id=&quot;payment-element&quot; options={{paymentElement }}/&gt;`

You'll want to update that and include your desired Payment Element options in options there, so that would look roughly like this:

&lt;PaymentElement id=&quot;payment-element&quot; options={{layout: {type: &quot;accordion&quot;, ... }}/&gt;`

Alternatively you can store your desired options in a variable and then pass that into options:

  const paymentElementOptions = {
    layout: &quot;accordion&quot;
  }

  return (
    &lt;form id=&quot;payment-form&quot; onSubmit={handleSubmit}&gt;
      &lt;PaymentElement id=&quot;payment-element&quot; options={paymentElementOptions} /&gt;
    ...
  )

</details>



huangapple
  • 本文由 发表于 2023年3月10日 01:50:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/75688301.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定