首页 > 解决方案 > 更新订阅 Braintree 和 prorateCharges 错误 ID 已被占用

问题描述

大家好,所以我正在尝试将每月订阅更新为 Braintree 中的另一个每月订阅并按比例分配费用。在阅读了他们的文档后,我对如何有效地做到这一点感到困惑。当我去更新订阅时,我收到以下错误消息:“ID 已被占用。”

router.put("/update-subscription", async (req, res, next) => {
  try {
    console.log("hit route");
    if (!res.locals.user) {
      throw { status: 403, message: "Not logged in." };
    } else {
      const { subscriptionId, selectedPlanName } = req.body;
      const oldSubscriptionId = subscriptionId;
      const selectedPlanId = selectedPlanName.replace(/\s/g, "_");
      const userId = res.locals.user.id;
      const [[userData]] = await database.query("call getUserByUserId(?)", [
        userId
      ]);
      const { braintreeId } = userData;
      const { paymentMethods } = await gateway.customer.find("" + braintreeId);
      const { token } = paymentMethods.find(p => p.default);
      console.log("oldSubscriptionId", oldSubscriptionId);
      console.log("selectedPlanId", selectedPlanId);
      const subUpdateResponse = await gateway.subscription.update(
        oldSubscriptionId,
        {
          id: selectedPlanId,
          paymentMethodToken: token,
          options: {
            prorateCharges: true
          }
        }
      );
      console.log("subUpdateResponse", subUpdateResponse);
      if (subUpdateResponse.success) {
        res.send("Successfully updated plan");
      } else {
        throw { message: "An error occurred." };
      }
    }
  } catch (error) {
    next(error);
  }
});

这是日志

subUpdateResponse ErrorResponse {
  errors:
   ValidationErrorsCollection {
     validationErrors: {},
     errorCollections: { subscription: [Object] } },
  params:
   { id: 'SILVER_MONTHLY',
     paymentMethodToken: 'krs2p5',
     options: { prorateCharges: 'true' } },
  message: 'ID has already been taken.',
  success: false }
{ message: 'An error occurred.' }

我了解“SILVER_MONTHLY”ID 已被使用我的意思是我正在尝试从一个订阅更新到另一个订阅,显然我尝试更新到的订阅已经被使用。同样,我要做的就是从用户已经使用的订阅更新到用户选择要更新到的订阅。这里的任何帮助都会很棒。谢谢

标签: updatessubscriptionbraintree

解决方案


完全披露,我在 Braintree 工作。如果您有任何其他问题,我建议您联系支持

您在不正确的参数中传递了计划 ID。该id参数可用于您希望拥有的新订阅 ID。要更新到不同的计划,您需要传递一个planId参数,该参数表示您要将它们更新到的计划。

我还注意到您正在传递一个paymentMethodToken参数。这仅在您更新付款方式令牌时使用。如果这是您的意图,请继续!否则,您不需要在更新请求中传递此参数。

例如,如果您想更新计划并保留相同的付款方式令牌,您的请求可能如下所示:

const subUpdateResponse = await gateway.subscription.update(
  oldSubscriptionId,
  {
    planId: selectedPlanId,
    options: {
      prorateCharges: true
    }
  }
);

推荐阅读