文章相应链接:
官方SDK:https://developer.paypal.com/docs/api/rest-sdks/
官方API:https://developer.paypal.com/docs/api/payments/v1/
官方.NET 支付案例:https://paypal.github.io/PayPal-NET-SDK/Samples/PaymentWithPayPal.aspx.html
官方SDK 支付使用逻辑:https://developer.paypal.com/docs/api/quickstart/create-process-order/#
PayPal 支付流程说明:
1。安装官方SDK (上面的第一个链接)
.NET 案例: Install-package Paypal
2.编写支付代码(上面的第4个链接)
3.创建支付订单详情,返回一个approval_url(获得这个URL)返回前端请求跳转到这个页面确认订单信息,用户点击支付按钮。
4.paypa 回调 后台配置的 URL
5.处理业务逻辑,例如修改订单状态等等
6.UI 渲染支付成功页面。
7. End 结束。
代码案例:
public static bool PayPalCreate(string payerId) // 这个ID 是回调的时候自己传递过来的。所以自行获取调用
{
try
{
var payer = new Payer() { payment_method = "paypal" };
var config = ConfigManager.Instance.GetProperties();
var accessToken = new OAuthTokenCredential(config).GetAccessToken();
var apiContext = new APIContext(accessToken);
string createParamId = "";
if (string.IsNullOrEmpty(payerId))
{
var guid = Convert.ToString((new Random()).Next(100000));
//var baseURI = Request.Url.Scheme + "://" + Request.Url.Authority + "/PaymentWithPayPal.aspx?";
var baseUrl = "https://164l647f09.51mypc.cn/PayStripe.ashx?method=PayImageListTest?";
var redirectUrl = baseUrl + "guid=" + guid;
var redirUrls = new RedirectUrls()
{
cancel_url = redirectUrl + "&cancel=true",
return_url = redirectUrl
};
var itemList = new ItemList()
{
items = new List<Item>()
{
new Item()
{
name = "Item Name",
currency = "TWD",
price = "15",
quantity = "5",
sku = "sku"
},
new Item()
{
name = "Item Name2",
currency = "TWD",
price = "15",
quantity = "5",
sku = "sku"
}
}
};
var details = new Details()
{
tax = "30", // 这几个价格都是要 求和, 并且相等, 官网有说明
shipping = "10",
subtotal = "150"
};
var amount = new Amount()
{
currency = "TWD",
total = "190.00", // Total must be equal to sum of shipping, tax and subtotal.
details = details
};
var transactionList = new List<Transaction>();
transactionList.Add(new Transaction()
{
description = "Transaction description.",
invoice_number = "123456789101113",
amount = amount,
item_list = itemList
});
var payment = new Payment()
{
intent = "sale",
payer = payer,
redirect_urls = redirUrls,
transactions = transactionList,
};
var createdPayment = payment.Create(new APIContext(accessToken));
createParamId = createdPayment.id; // 需要存储起来,方便回调的时候用这个ID去做支付扣款,我这边没做存储,只是复制出来了,然后调试的时候粘贴上去做的扣款
// payerId = createdPayment.payer.payer_info.payer_id;
var links = createdPayment.links.GetEnumerator();
while (links.MoveNext())
{
var link = links.Current;
if (link.rel.ToLower().Trim().Equals("approval_url"))
{
var linkUrl = link.href;
// Redirect the customer to link.href
}
}
// Using the information from the redirect, setup the payment to execute.
}
else
{
var createdPaymentId = createParamId; // 需要存储起来,方便回调的时候用这个ID去做支付扣款,我这边没做存储,只是复制出来了,然后调试的时候粘贴上去做的扣款
var paymentExecution = new PaymentExecution() { payer_id = payerId };
var payment2 = new Payment() { id = createdPaymentId };
// Execute the payment.
var executedPayment = payment2.Execute(apiContext, paymentExecution);
}
return true;
}
catch (Exception ex)
{
return false;
}
}
一个简单的支付案例分享,官网有很多支付模式,大伙可以自己去瞅瞅。
文章中有很多依赖没有完全写清楚,需要自行去看看官方文档 琢磨,比如安装了Nuget包,web.config 的配置没写 ClientID,秘钥。等等