在未触发条带Firebase扩展WebHook的情况下运行订阅支付 [英] Run Subscription Payments with Stripe Firebase Extension Webhook Not Firing

查看:19
本文介绍了在未触发条带Firebase扩展WebHook的情况下运行订阅支付的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Firebase/Vue应用程序中添加了the Stripe Subscriptions extension,以便可以管理客户的订阅计划,但我认为是条纹网络挂钩出现了问题。

我按照教程中所说的那样设置了Firebase.Rule,开始了扩展安装:

Firebase.Rules:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write;
    }
    match /customers/{uid} {
      allow read, write: if request.auth.uid == uid;

      match /checkout_sessions/{id} {
        allow read, write: if request.auth.uid == uid;
      }
      match /subscriptions/{id} {
        allow read, write: if request.auth.uid == uid;
      }
    }

    match /products/{id} {
      allow read: if true;
      allow write: if false;

      match /prices/{id} {
        allow read: if true;
        allow write: if false;
      }
    }
  }
}
我按如下方式配置了条纹WebHook,并在FireBase扩展中添加了WebHook密码:

我还在条纹仪表板中创建了一个限制键,并将其添加到扩展中。

当我测试从条纹仪表板创建新产品时,WebHook成功,并在FireBase中创建了一个集合:

条带产品仪表板:

FiRestore:

现在,问题是:

当我尝试创建和完成条纹签出时出现问题。从教程中看,我似乎需要调用stripe.redirectToCheckout(),它将为我处理大部分内容,但是我似乎找不到结账会话的sessionId。以下是他们展示的示例:

const docRef = await db
  .collection('customers')
  .doc(currentUser.uid)
  .collection('checkout_sessions')
  .add({
    price: 'price_1GqIC8HYgolSBA35zoTTN2Zl',
    success_url: window.location.origin,
    cancel_url: window.location.origin,
  });
// Wait for the CheckoutSession to get attached by the extension
docRef.onSnapshot((snap) => {
  const { sessionId } = snap.data();
  if (sessionId) {
    // We have a session, let's redirect to Checkout
    // Init Stripe
    const stripe = Stripe('pk_test_1234');
    stripe.redirectToCheckout({ sessionId });
  }
});

这是我的实现:

 async checkoutv2(item) {
      let currentUser = this.getCurrentUser();
      const stripe = window.Stripe(this.publishableKey);

      const docRef = await db
        .firestore()
        .collection("customers")
        .doc(currentUser.uid)
        .collection("checkout_sessions")
        .add({
          unit_amount: item["unit_amount"],
          plan: item["id"],
          description: item["description"],
          interval: item["interval"],
          currency: item["currency"],
          type: item["type"],
          interval_count: item["interval_count"],
          active: item["active"],
          // allow_promotion_codes: true,
          // tax_rates: ["txr_1HCshzHYgolSBA35WkPjzOOi"],
          success_url: window.location.origin,
          cancel_url: window.location.origin,
          mode: "subscription",
          metadata: {},
        });

      // Wait for the CheckoutSession to get attached by the extension
      docRef.onSnapshot((snap) => {
        const { sessionId } = snap.data();
        if (sessionId) {
          // We have a session, let's redirect to Checkout
          // Init Stripe
          stripe.redirectToCheckout({ sessionId });
        } else {
          console.log("NOPE");
        }
      });
    },
在将CheckOutSession";添加到FiRestore之后,它似乎会将";附加到扩展,并创建会话ID并将其添加到文档中。但我认为我的版本不会出现这种情况。正在创建checkout_sessions,但没有sessionId

以下是snap数据的外观:

sessionId上没有任何内容,但它确实有自己的ID,我相信这是FiRestore自动为集合创建的ID。

查看我的日志,没有触发任何网络挂钩。当我测试特定的WebHook事件时,有些成功,有些失败。

成功:

  • 与价格、产品创建/更新/删除有关的任何内容。
  • checkout.session.Complete
  • customer.scription.Created

失败(错误状态代码400):

  • customer.scription.update
  • customer.SUBSCRIPTION.DELETED(日志:[CUSTER.SUBSCRIPTION.DELETED]类型的条带事件[EVT_00000000000000]的WebHook处理程序失败:没有这样的订阅:‘SUB_00000000000000’)

有人能说出是怎么回事吗?我使用的是条纹API版本:2017-06-05。我已将以下内容添加到我的index.html

  <!-- Firebase App (the core Firebase SDK) is always required and must be listed first -->
    <script src="https://www.gstatic.com/firebasejs/7.14.6/firebase.js"></script>
    <script src="https://www.gstatic.com/firebasejs/7.14.6/firebase-functions.js"></script>

    <!-- If you enabled Analytics in your project, add the Firebase SDK for Analytics -->
    <script src="https://www.gstatic.com/firebasejs/7.14.5/firebase-analytics.js"></script>

    <!-- Add Firebase products that you want to use -->
    <script src="https://www.gstatic.com/firebasejs/7.14.5/firebase-auth.js"></script>
    <script src="https://www.gstatic.com/firebasejs/7.14.5/firebase-firestore.js"></script>

    <script src="https://cdn.firebase.com/libs/firebaseui/3.5.2/firebaseui.js"></script>
    <link
      type="text/css"
      rel="stylesheet"
      href="https://cdn.firebase.com/libs/firebaseui/3.5.2/firebaseui.css"
    />

我的Package.json:

{
  "name": "mathpath",
  "version": "0.1.0",
  "private": true,
  "description": "## Project setup ``` npm install ```",
  "author": "",
  "scripts": {
    "serve": "vue-cli-service serve",
    "build": "vue-cli-service build",
    "lint": "vue-cli-service lint",
    "start": "node index.js",
    "bd": "vue-cli-service build && firebase deploy"
  },
  "main": "babel.config.js",
  "dependencies": {
    "aws-sdk": "^2.719.0",
    "axios": "^0.19.2",
    "bootstrap": "^4.5.0",
    "bootstrap-vue": "^2.1.0",
    "core-js": "^3.6.5",
    "dns": "^0.2.2",
    "express": "^4.17.1",
    "firebase": "^7.17.1",
    "firebase-admin": "^9.0.0",
    "firebaseui": "^4.6.0",
    "fs": "0.0.1-security",
    "jquery": "^3.5.1",
    "katex": "^0.12.0",
    "markdown-it": "^11.0.0",
    "markdown-it-katex": "^2.0.3",
    "mathjax-node": "^2.1.1",
    "pg": "^8.2.2",
    "pg-hstore": "^2.3.3",
    "sequelize": "^6.3.0",
    "showdown": "^1.9.1",
    "showdown-katex": "^0.8.0",
    "slugify": "^1.4.5",
    "stripe": "^8.83.0",
    "uniqid": "^5.2.0",
    "vue": "^2.6.11",
    "vue-katex": "^0.4.0",
    "vue-router": "^3.3.4",
    "vue-stripe-checkout": "^3.5.7",
    "vue-youtube-embed": "^2.2.2",
    "vuefire": "^2.2.3",
    "vuex": "^3.5.1"
  },
  "devDependencies": {
    "@babel/polyfill": "^7.7.0",
    "@vue/cli-plugin-babel": "~4.4.0",
    "@vue/cli-plugin-eslint": "~4.4.0",
    "@vue/cli-service": "~4.4.0",
    "babel-eslint": "^10.1.0",
    "bootstrap": "^4.3.1",
    "eslint": "^6.7.2",
    "eslint-plugin-vue": "^6.2.2",
    "mutationobserver-shim": "^0.3.3",
    "popper.js": "^1.16.0",
    "portal-vue": "^2.1.6",
    "sass": "^1.19.0",
    "sass-loader": "^8.0.0",
    "vue-cli-plugin-bootstrap-vue": "~0.6.0",
    "vue-template-compiler": "^2.6.11"
  },
  "eslintConfig": {
    "root": true,
    "env": {
      "node": true
    },
    "extends": [
      "plugin:vue/essential",
      "eslint:recommended"
    ],
    "parserOptions": {
      "parser": "babel-eslint"
    },
    "rules": {
      "no-console": "off",
      "no-restricted-syntax": [
        "error",
        {
          "selector": "CallExpression[callee.object.name='console'][callee.property.name!=/^(log|warn|error|info|trace)$/]",
          "message": "Unexpected property on console object was called"
        }
      ]
    }
  },
  "browserslist": [
    "> 1%",
    "last 2 versions",
    "not dead"
  ],
  "license": "ISC"
}

推荐答案

我遇到了同样的问题。

以下是我在它开始工作之前所做的一些更改,我不确定到底是什么修复了它:

  • 关闭我的FireBase仿真器,并在生产数据上进行测试。在修复之前,它似乎可以在仿真器中工作(除了原始帖子中提到的问题),但是我还没有测试它在仿真器修复后是否可以工作。
  • 已删除条带中的所有测试数据和WebHook,并重新开始。
  • 已更新我的所有Firebase和VUE-Stripe-Check依赖项:
Firebase&q;:7.19.1&q;, ";Firebase-admin";:^9.1.1&q;, Firebase函数&q;:&q;^3.11.0&q;, ";Firebase-Tools";:^8.10.0";, Firebaseui&q;:^4.6.1&q;, ";vue-stripe-checkout";:";^3.5.7";

完成所有这些之后,我在STRIPE中创建了一个测试订阅产品,并看到它出现在我的Products集合中。这在修复之前就已经起作用了。最新的是,我现在将sessionId视为第二个snap.data()响应的一部分。第一个响应看起来与修复之前的响应一样,我以为它又失败了,但等待了一两秒钟之后,出现了一个带有附加会话ID参数👀的新响应

我也在使用Vue。然后,我获取该会话并将其提交给UPDATE STATE。我等待该会话更改,然后调用checkoutRef函数。这一次,用户的集合将使用新订阅进行更新。

对我来说,此代码位于store-auth.js

async checkoutUser({ commit }, payload) {
    let userId = firebaseAuth.currentUser.uid;

    const docRef = await firebaseDb
      .collection("users")
      .doc(userId)
      .collection("checkout_sessions")
      .add({
        price: "YOUR_PRICE_ID",
        success_url: window.location.origin,
        cancel_url: window.location.origin,
        mode: "subscription"
      });

    docRef.onSnapshot(snap => {
      const test = snap.data();

      const { sessionId } = snap.data();
      if (sessionId) {
        commit("setSession", sessionId);
      }
    });
  },

这是您使用vue-stripe-checkout的位置。对我来说,它在PagePlans.vue

    <stripe-checkout
    ref="checkoutRef"
    :pk="publishableKey"
    :mode="mode"
    :items="items"
    :successUrl="successUrl"
    :cancelUrl="cancelUrl"
    :session-id="sessionId"

  watch: {
    currentSession: {
      handler(newValue, oldValue) {
        this.sessionId = newValue

        this.$refs.checkoutRef.redirectToCheckout({scenarioId: newValue.sessionId})
      }
    }
  },
  computed: {
    ...mapGetters("auth", ["currentSession"]),
  },
  methods: {
    ...mapActions("auth", [
      "checkoutUser",
    ]),
    async checkout() {
      await this.checkoutUser()
    }
  }

这篇关于在未触发条带Firebase扩展WebHook的情况下运行订阅支付的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆