javascript - 从 Parse.com 的 webhook 中包含的 ID 检索客户电子邮件

标签 javascript parse-platform stripe-payments webhooks

我有一个应用程序使用 Parse.com 作为后端和一个外部站点作为我的支付网关。从 Stripe 收到客户/订阅网络钩子(Hook)数据后,我希望查找用户的电子邮件,这样我就可以运行云代码功能并将他们的用户状态更改为“付费”

我的 webhook 接收器是:

Parse.Cloud.define("update_user", function(request, response) {

  var data = request.params["data"]
  var customer = data.object.customer;

  response.success'Working' + request);
});

而且我可以使用以下方法从客户 ID 的 strip 中取回电子邮件:

Parse.Cloud.define("pay", function(request, response) {
Stripe.initialize(STRIPE_SECRET_KEY);
console.log(JSON.stringify(request.params));

    Stripe.Customers.retrieve(
        customerId, {
            success:function(results) {
                console.log(results["email"]);
                // alert(results["email"]);
                response.success(results);
            },
            error:function(error) {
                response.error("Error:" +error); 
            }
        }
    );
});

我需要帮助将它变成一个完整的函数,该函数在收到来自 Stripe 的每个 webhook 时运行。如果由于某种原因这不起作用,我也在为后备选项而苦苦挣扎。

编辑

根据第一个答案的一部分,我现在有:

Parse.Cloud.define("update_user", function(request, response) {
Stripe.initialize(STRIPE_SECRET_KEY);

    var data = request.params["data"]
    var customerId = data.object.customer;

    get_stripe_customer(customerId, 100).then(function(stripeResponse) {
        response.success(stripeResponse);
    }, function(error) {
        response.error(error);
    });
});

function get_stripe_customer (customerId) {
Stripe.initialize(STRIPE_SECRET_KEY);
    return Stripe.Customers.retrieve(
        customerId, {
            success:function(results) {
                console.log(results["email"]);
            },
            error:function(error) {
            }
        }
    );
};

我的知识真的落在了 Promise 方面,还有回调(success:, error, request response) 等进一步阅读将不胜感激。

现在可以正常工作了

最佳答案

出于兴趣,我这样做了:

Parse.Cloud.define("update_user", function(request, response) {

    var data = request.params["data"]
    var customerId = data.object.customer;

    get_stripe_customer(customerId, 100).then(function(stripeResponse) {
        return set_user_status(username, stripeResponse);
    }).then(function(username) {
        response.success(username);
    }, function(error) {
        response.error(error);
    });
});

function get_stripe_customer (customerId) {
Stripe.initialize(STRIPE_SECRET_KEY);
    return Stripe.Customers.retrieve(
        customerId, {
            success:function(results) {
              // console.log(results["email"]);
            },
            error:function(error) {
            }
        }
    );
};

function set_user_status(stripeResponse) {

  Parse.Cloud.useMasterKey();
  var emailquery = new Parse.Query(Parse.User);
    emailquery.equalTo("username", stripeResponse['email']);  // find all the women
    return emailquery.first({
      success: function(results) {
          alert('running set_user_status success');
          var user = results;
              user.set("tier", "paid");
              user.save();
    },
      error:function(error) {
        console.log('error finding user');
    }
  });
};

开放改进...

编辑 - 我 (@danh) 稍微清理了一下。一些注意事项:

自始至终都使用了 promises。更容易阅读和处理错误

get_stripe_customer 只需要一个参数(100 是我的想法,收取 100 美元)

set_user_status 似乎 只需要用户电子邮件作为参数,这显然在 stripeResponse 中

set_user_status 返回保存用户的 promise 。将由用户对象而不是用户名来实现

请确保您清楚如何识别用户。 stripe 显然提供了电子邮件地址,但在您的用户查询中(在 set_user_status 中)您将电子邮件与“用户名”进行了比较。一些系统设置username == email。确保您的执行或更改该查询。

Parse.Cloud.define("update_user", function(request, response) {
    var data = request.params["data"]
    var customerId = data.object.customer;

    get_stripe_customer(customerId).then(function(stripeResponse) {
        var email = stripeResponse.email;
        return set_user_status(email);
    }).then(function(user) {
        response.success(user);
    }, function(error) {
        response.error(error);
    });
});

function get_stripe_customer(customerId) {
    Stripe.initialize(STRIPE_SECRET_KEY);
    return Stripe.Customers.retrieve(customerId).then(function(results) {
        // console.log(results["email"]);
        return results;
    });
};

function set_user_status(email) {
    Parse.Cloud.useMasterKey();
    var emailquery = new Parse.Query(Parse.User);
    emailquery.equalTo("username", email);  // find all the women
    return emailquery.first().then(function(user) {
        user.set("tier", "paid");
        return user.save();
    }, function(error) {
        console.log('error finding user ' + error.message);
        return error;
    });
}

关于javascript - 从 Parse.com 的 webhook 中包含的 ID 检索客户电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29974854/

相关文章:

php - 如何退还 Stripe 订阅费用?

javascript - 抑制 iframe 上的保存对话框

javascript - Twilio Node.js API - 使用 'Dial' 转发调用时出现问题

javascript:从 GM 脚本调用嵌入式函数

javascript - 通过解析云码功能下载图片

ios - swift : Parse PFQueryTableViewController - numberOfRowsInSection when PFObject is Array

stripe-payments - 从 Stripe API 检索支持的国家/地区列表?

javascript - 如何在 React 中创建下拉菜单?

android - ParseQueryAdapter "load more..."文本

node.js - Stripe Angular 错误: Uncaught (in promise): TypeError: Cannot read property 'stripeService' of undefined