時間:2019-1-29 15:39:39html
文章相應連接:前端
官方SDK:https://developer.paypal.com/docs/api/rest-sdks/git
官方API:https://developer.paypal.com/docs/api/payments/v1/github
官方.NET 支付案例:https://paypal.github.io/PayPal-NET-SDK/Samples/PaymentWithPayPal.aspx.htmlweb
官方SDK 支付使用邏輯:https://developer.paypal.com/docs/api/quickstart/create-process-order/#api
PayPal 支付流程說明:app
1。安裝官方SDK (上面的第一個連接)dom
.NET 案例: Install-package Paypalui
2.編寫支付代碼(上面的第4個連接)url
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,祕鑰。等等