1、注册Stripe
Sign Up and Create a Stripe Account | Stripe
2、测试模式
Stripe提供测试模式,通过测试模式可以实现支付流程
3、支付开发
NuGet 安装 Stripe.net 库
/// <summary>
/// APP支付
/// 官方文档:https://stripe.com/docs/payments/alipay/accept-a-payment?platform=mobile&ui=android
/// </summary>
/// <param name="amount">金额</param>
/// <param name="orderId">订单号</param>
/// <returns></returns>
public string AppPay(decimal amount, string orderId)
{
//密钥
StripeConfiguration.ApiKey = "sk_test_********";
var options = new PaymentIntentCreateOptions
{
Amount = (long)amount * 100,
Currency = "cny",
PaymentMethodTypes = new List<string> { "alipay", "wechat_pay" },
Metadata = new Dictionary<string, string> { { "order_id", orderId } },
};
var service = new PaymentIntentService();
var intent = service.Create(options);
//将 client secret 发送给您的客户端以安全地完成支付过程
return intent.ClientSecret;
}
/// <summary>
/// 电脑网站支付
/// 官方文档:https://stripe.com/docs/payments/alipay/accept-a-payment?platform=web
/// </summary>
/// <param name="amount">金额</param>
/// <param name="orderId">订单号</param>
/// <param name="returnUrl">跳转地址</param>
/// <returns></returns>
public static string WebPay(decimal amount, string orderId, string returnUrl)
{
//密钥
StripeConfiguration.ApiKey = "sk_test_********";
var options = new SessionCreateOptions
{
PaymentMethodTypes = new List<string> { "alipay", "wechat_pay" },
LineItems = new List<SessionLineItemOptions>
{
new SessionLineItemOptions
{
PriceData = new SessionLineItemPriceDataOptions
{
UnitAmount = (long)amount * 100,
Currency = "cny",
ProductData = new SessionLineItemPriceDataProductDataOptions()
{
Name = "产品名称",
},
},
Quantity = 1,
},
},
Currency = "cny",
PaymentIntentData = new SessionPaymentIntentDataOptions()
{
Metadata = new Dictionary<string, string> { { "order_id", orderId } },
},
Mode = "payment",
SuccessUrl = returnUrl,
CancelUrl = returnUrl,
};
options.AddExtraParam("payment_method_options[wechat_pay][client]", "web");
var service = new SessionService();
var session = service.Create(options);
//返回支付链接以完成支付过程
return session.Url;
}
相关文档
APP支付:Accept an Alipay payment | Stripe Documentation
电脑网站支付:https://stripe.com/docs/payments/alipay/accept-a-payment?platform=web
4、Webhooks回调
/// <summary>
/// Stripe webhook 支付回调
/// 官方文档:https://stripe.com/docs/payments/handling-payment-events
/// </summary>
/// <returns></returns>
[HttpPost]
public async Task<IActionResult> StripeCallBack()
{
var json = await new StreamReader(HttpContext.Request.Body).ReadToEndAsync();
try
{
//webhook => 密钥签名
var endpointSecret = "whsec_*******";
var stripeEvent = EventUtility.ConstructEvent(json, Request.Headers["Stripe-Signature"], endpointSecret);
// Handle the event
if (stripeEvent.Type == Events.PaymentIntentSucceeded)
{
var paymentIntent = stripeEvent.Data.Object as PaymentIntent;
Console.WriteLine(paymentIntent.Metadata["order_id"]);
Console.WriteLine("PaymentIntent was successful!");
}
// ... handle other event types
else
{
Console.WriteLine("Unhandled event type: {0}", stripeEvent.Type);
}
return Ok();
}
catch (StripeException)
{
return BadRequest();
}
}