前言
跨境电商行业中,支付环节相信大家都不陌生,对接的国外支付平台可能很多,但是PayPal作为全球最重要的支付方式之一,支付转化率也有保证,一直深受各跨境电商平台青睐,本章将继续介绍PayPal对接示例实战,
本次讲解是针对PayPal官方文档Payments支付流程介绍,基于.Net代码示例
PayPal参考文档
名词解释
- 同步回调:及时处理支付状态的处理流程
- 异步回调:异步处理支付状态的处理流程(支付状态最终以异步回传为准)
配置准备工作
登录PayPal沙盒开发者中心,
endpoint:paypal api地址
mode:沙盒/生产
connectionTimeout: 超时时间
requestRetries:重试次数
clientId:客户端ID
clientSecret:私钥(不要泄漏)
webhook.id:回调ID
配置文件配置如下
<configSections>
<section name="paypal" type="PayPal.Manager.SDKConfigHandler, PayPalCoreSDK" />
</configSections>
<paypal>
<settings>
<add name="endpoint" value="https://api.sandbox.paypal.com/" />
<add name="oauth.EndPoint" value="https://api.sandbox.paypal.com/" />
<add name="mode" value="sandbox" />
<add name="connectionTimeout" value="360000" />
<add name="requestRetries" value="1" />
<add name="clientId" value="「clientId」" />
<add name="clientSecret" value="「clientSecret」" />
<add name="webhook.id" value="「webhook.id」" />
</settings>
</paypal>
请求授权获取Token
定义PaypalUtils工具类
static PaypalUtils()
{
var config = GetConfig();
ClientId = config["clientId"];
ClientSecret = config["clientSecret"];
Endpoint = config["endpoint"];
WebhookId = config["webhook.id"];
}
// Create the configuration map that contains mode and other optional configuration details.
public static Dictionary<string, string> GetConfig()
{
var result = ConfigManager.Instance.GetProperties();
result["clientId"] = result["clientId"];
result["clientSecret"] = result["clientSecret"];
result["webhook.id"] = result["webhook.id"];
return result;
}
// Create accessToken
public static string GetAccessToken()
{
// ###AccessToken
// Retrieve the access token from
// OAuthTokenCredential by passing in
// ClientID and ClientSecret
// It is not mandatory to generate Access Token on a per call basis.
// Typically the access token can be generated once and
// reused within the expiry window
var accessToken = string.Empty;
try
{
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls | SecurityProtocolType.Ssl3;
ServicePointManager.DefaultConnectionLimit =