首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在Vue 3脚本设置中创建Paypal Button?

如何在Vue 3脚本设置中创建Paypal Button?
EN

Stack Overflow用户
提问于 2022-07-12 11:31:49
回答 1查看 178关注 0票数 0

paypal开发人员文档展示了如何将Paypal实现到Vue.js中,但我不理解代码。此时,我甚至不确定这是vue 2代码、vue 3代码还是angular??代码。

1:在parent blade中导入脚本

代码语言:javascript
运行
复制
<script src="https://www.paypal.com/sdk/js?client-id=YOUR_CLIENT_ID"></script>

2:在组件的script tag中使用按钮?

代码语言:javascript
运行
复制
paypal.Buttons.driver("vue", window.Vue);

他说:这就是我迷路的地方,在app.js里用这个?:

代码语言:javascript
运行
复制
@ng.core.Component({
  selector: 'my-app',
  template:
    <div id="app">
        <paypal-buttons [props]="{
            createOrder: createOrder,
            onApprove: onApprove
        }"></paypal-buttons>
    </div>
  ,
})
class AppComponent {
    createOrder(data, actions) {
      return actions.order.create({
          purchase_units: [{
              amount: {
                  value: '0.01'
              }
          }]
      });
    }
    onApprove(data, actions) {
      return actions.order.capture();
    }
}
@ng.core.NgModule({
    imports: [
        ng.platformBrowser.BrowserModule,
        paypal.Buttons.driver('angular2', ng.core)
    ],
    declarations: [
        AppComponent
    ],
    bootstrap: [
        AppComponent
    ]
})
class AppModule {}
ng.platformBrowserDynamic
    .platformBrowserDynamic()
    .bootstrapModule(AppModule);

难道这不是vue code而是angular code

4:把这个放进vue component?:

代码语言:javascript
运行
复制
<div id="container">
  <app></app>
</div>
<script>
  const PayPalButton = paypal.Buttons.driver("vue", window.Vue);

  Vue.component("app", {
    // The style prop for the PayPal button should be passed in as `style-object` or `styleObject` to avoid conflict with Vue's `style` reserved prop.
    template: `
      <paypal-buttons :on-approve="onApprove" :create-order="createOrder" :on-shipping-change="onShippingChange" :on-error="onError" :style-object="style" />
    `,
    components: {
      "paypal-buttons": PayPalButton,
    },

    computed: {
      createOrder: function () {
        return (data, actions) => {
          return actions.order.create({
            purchase_units: [
              {
                amount: {
                  value: "10",
                },
              },
            ],
          });
        }
      },
      onApprove: function () {
        return (data, actions) => {
          return actions.order.capture();
        }
      },
      onShippingChange: function () {
        return (data, actions) => {
          if (data.shipping_address.country_code !== 'US') {
            return actions.reject();
          }
          return actions.resolve();
        }
      },
      onError: function () {
        return (err) => {
          console.error(err);
          window.location.href = "/your-error-page-here";
        }
      },
      style: function () {
        return {
          shape: 'pill',
          color: 'gold',
          layout: 'horizontal',
          label: 'paypal',
          tagline: false
        }
      },
    },
  });

  const vm = new Vue({
    el: "#container",
  });
</script>

我的问题是,如何在Vue 3的script setup中创建一个简单的paypal按钮?paypal cdn在父blade file中导入。

类似于:

代码语言:javascript
运行
复制
<script setup>
import {onMounted} from "vue";

onMounted(() => {
    //create component from -> paypal.Buttons.driver("vue", window.Vue);
})
</script>


<template>
  <div id="checkout" class="checkout">
    <paypal-buttons></paypal-buttons>
  </div>
</template>
EN

回答 1

Stack Overflow用户

发布于 2022-07-12 14:16:31

Paypal文档是一个巨大的混乱。服务器集成似乎是这样工作的:

如果您使用laravel作为后端,请将其导入app.blade.php/welcome.blade.php文件:

代码语言:javascript
运行
复制
<script src="https://www.paypal.com/sdk/js?client-id=YOUR_CLIENT_ID"></script>

然后你的vue component看起来就像这样:

代码语言:javascript
运行
复制
<script setup>
import {onMounted} from "vue";

onMounted(() => {
    paypal.Buttons({

        // Call your server to set up the transaction
        createOrder: function(data, actions) {
            return fetch('/demo/checkout/api/paypal/order/create/', {
                method: 'post'
            }).then(function(res) {
                return res.json();
            }).then(function(orderData) {
                return orderData.id;
            });
        },

        // Call your server to finalize the transaction
        onApprove: function(data, actions) {
            return fetch('/demo/checkout/api/paypal/order/' + data.orderID + '/capture/', {
                method: 'post'
            }).then(function(res) {
                return res.json();
            }).then(function(orderData) {
                // Three cases to handle:
                //   (1) Recoverable INSTRUMENT_DECLINED -> call actions.restart()
                //   (2) Other non-recoverable errors -> Show a failure message
                //   (3) Successful transaction -> Show confirmation or thank you

                // This example reads a v2/checkout/orders capture response, propagated from the server
                // You could use a different API or structure for your 'orderData'
                var errorDetail = Array.isArray(orderData.details) && orderData.details[0];

                if (errorDetail && errorDetail.issue === 'INSTRUMENT_DECLINED') {
                    return actions.restart(); // Recoverable state, per:
                    // https://developer.paypal.com/docs/checkout/integration-features/funding-failure/
                }

                if (errorDetail) {
                    var msg = 'Sorry, your transaction could not be processed.';
                    if (errorDetail.description) msg += '\n\n' + errorDetail.description;
                    if (orderData.debug_id) msg += ' (' + orderData.debug_id + ')';
                    return alert(msg); // Show a failure message (try to avoid alerts in production environments)
                }

                // Successful capture! For demo purposes:
                console.log('Capture result', orderData, JSON.stringify(orderData, null, 2));
                var transaction = orderData.purchase_units[0].payments.captures[0];
                alert('Transaction '+ transaction.status + ': ' + transaction.id + '\n\nSee console for all available details');

                // Replace the above to show a success message within this page, e.g.
                // const element = document.getElementById('paypal-button-container');
                // element.innerHTML = '';
                // element.innerHTML = '<h3>Thank you for your payment!</h3>';
                // Or go to another URL:  actions.redirect('thank_you.html');
            });
        }

    }).render('#paypal-button-container');
})

</script>


<template>
  <div id="checkout" class="checkout">
    <div id="paypal-button-container"></div>
  </div>
</template>

显示哪些支付方法将自动确定,并取决于您的IP (?)。您可以通过像这样操作script import来隐藏支付方法:

代码语言:javascript
运行
复制
<script src="https://www.paypal.com/sdk/js?client-id=YOUR_CLIENT_ID&disable-funding=card,giropay,sepa,sofort"></script>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72951616

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档