Sign Up and Create a Stripe Account | Stripe
Stripe提供测试模式,通过测试模式可以实现支付流程
NuGet 安装 Stripe.net 库
///
/// APP支付
/// 官方文档:https://stripe.com/docs/payments/alipay/accept-a-payment?platform=mobile&ui=android
///
/// 金额
/// 订单号
///
public string AppPay(decimal amount, string orderId)
{
//密钥
StripeConfiguration.ApiKey = "sk_test_********";
var options = new PaymentIntentCreateOptions
{
Amount = (long)amount * 100,
Currency = "cny",
PaymentMethodTypes = new List { "alipay", "wechat_pay" },
Metadata = new Dictionary { { "order_id", orderId } },
};
var service = new PaymentIntentService();
var intent = service.Create(options);
//将 client secret 发送给您的客户端以安全地完成支付过程
return intent.ClientSecret;
}
///
/// 电脑网站支付
/// 官方文档:https://stripe.com/docs/payments/alipay/accept-a-payment?platform=web
///
/// 金额
/// 订单号
/// 跳转地址
///
public static string WebPay(decimal amount, string orderId, string returnUrl)
{
//密钥
StripeConfiguration.ApiKey = "sk_test_********";
var options = new SessionCreateOptions
{
PaymentMethodTypes = new List { "alipay", "wechat_pay" },
LineItems = new List
{
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 { { "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
///
/// Stripe webhook 支付回调
/// 官方文档:https://stripe.com/docs/payments/handling-payment-events
///
///
[HttpPost]
public async Task 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();
}
}
https://stripe.com/zh-cn-ca/pricing/local-payment-methods