【问题标题】:Update User to show they have paid - Stripe .Net Core更新用户以显示他们已付款 - Stripe .Net Core
【发布时间】:2020-04-21 11:13:14
【问题描述】:

我正在开发一款应用,允许会员使用 Stripe 预订/支付课程费用。但是,我发现很难更新应用程序以说明用户是否已成功付款。

目的是当用户付款时,付款将分配给他们,并且他们能够在其帐户中获得付款清单。 以下是我的控制器中的代码:

[HttpPost]
public IActionResult PaymentSuccess(Event obj, int memberId, int eventId)
{
    // add member to event attendees? if so just call BookEvent above
    var eventBook = BookEvent(memberId, eventId);
    var result = new { eventBook }; //service.UpdateEventAfterPayment(obj);
    return Json(result);
}

public IActionResult Charge(string stripeEmail, string stripeToken, Event obj)
{
    //var json = await new StreamReader(HttpContext.Request.Body).ReadToEndAsync();
     var customers = new Stripe.CustomerService();
     var charges = new Stripe.ChargeService();

     var customer = customers.Create(new Stripe.CustomerCreateOptions
     {
       Email = stripeEmail,
       Source = stripeToken
      }) ;

      var charge = charges.Create(new Stripe.ChargeCreateOptions
      {
        Amount = 300,
        Description = "Class Payment",
        Currency="gbp",
        Customer= customer.Id,
        ReceiptEmail= stripeEmail,
        Metadata = new Dictionary<string, string>()
        {
            {"OrderId", "123" },
            {"PostCode", "BT480GY" }
        }
        });

        if(charge.Status == "succeeded")
        {
          string BalanceTransactionId = charge.BalanceTransactionId;
          var email = User.FindFirst("sub")?.Value;
          var customerPaid = charge.Paid;
          customerPaid = true;
          return RedirectToAction(nameof(Index));
        }
          return RedirectToAction(nameof(Index));
        }
    }

以下是我的 chtml 文件中的内容:

<div id="eventBook" class="text-center">
                        <form asp-action="Charge" asp-controller="Timetable" method="post">
                            <article>
                                <label>Amount: £3.00</label>
                            </article>
                            <script src="//checkout.stripe.com/v2/checkout.js"
                                    class="stripe-button"
                                    data-key="@Stripe.Value.PublishableKey"
                                    data.locale="auto"
                                    data-description="Class Charge">
                            </script>
                        </form>
                    </div>

我不确定我是否从条带中传递了正确的数据以将付款分配给特定用户,然后他们可以继续查看交易列表。

【问题讨论】:

    标签: javascript html asp.net-core stripe-payments


    【解决方案1】:

    只要charges.Create() 没有抛出异常,你就可以假设charge 成功了。更多关于错误处理的信息:

    https://stripe.com/docs/api/errors/handling

    创建客户后,我建议将 Stripe 客户 ID 保存到您的数据库或会话中。稍后,当您想要列出该客户的费用时,可以进行“列出费用”API 调用并传递该客户 ID 以仅获取该客户的费用: https://stripe.com/docs/api/charges/list#list_charges-customer

    var customerID = "cus_1234xxx..."; // Retrieved from session or DB
    
    var options = new ChargeListOptions { Limit = 3, Customer = customerID };
    var service = new ChargeService();
    StripeList<Charge> charges = service.List(
      options
    );
    

    希望一切顺利!

    【讨论】:

      【解决方案2】:

      理想情况下,您不应依赖 Stripe 客户与系统中的用户之间的 1-1 关联。除非您坚持支付来源,否则不需要 Stripe 客户。即使您这样做,也不一定要求每个用户都有一个 Stripe 客户。

      这里更好的方法是记录您的费用ID,并将那个与用户相关联。然后,您应该有一个 Webhook 端点,以便 Stripe 在收费通过时发出通知。当 webhook 被命中时,您可以使用与之关联的费用 ID 查找您身边的付款/用户,并相应地更新任何内容。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-02-04
        • 2021-12-01
        • 2017-07-28
        • 2022-01-16
        • 2015-03-01
        • 2017-01-22
        • 2021-07-28
        • 1970-01-01
        相关资源
        最近更新 更多