一、登录paypal官网,创建账户,创建项目,获取到Client ID,Secret
https://developer.paypal.com/developer/applications/
创建APP
获取到Client ID,Secret
二、配置类
1、在yml中增加Client ID,Secret,mode。mode表示沙箱(sandbox)还是正式环境(live)
PayPal:
ClientID: #程序ID
Secret: #密钥
mode: sandbox
successUrl: http://域名/gateway/paypal/success #支付成功后请求的回调
cancelUrl: http://域名/gateway/paypal/cancel #支付失败后请求的回调
2、引入paypal的maven环境的sdk
<dependency>
<groupId>com.paypal.sdk</groupId>
<artifactId>rest-api-sdk</artifactId>
<version>1.4.2</version>
</dependency>
3、增加配置类
package com.app.configuration;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.paypal.base.rest.APIContext;
import com.paypal.base.rest.OAuthTokenCredential;
import com.paypal.base.rest.PayPalRESTException;
/**
* paypal
*/
@Configuration
public class PaypalConfig {
@Value("${PayPal.ClientID}")
private String clientId;
@Value("${PayPal.Secret}")
private String clientSecret;
@Value("${PayPal.mode}")
private String mode;
@Bean
public Map<String, String> paypalSdkConfig(){
Map<String, String> sdkConfig = new HashMap<>();
sdkConfig.put("mode", mode);
return sdkConfig;
}
@Bean
public OAuthTokenCredential authTokenCredential(){
return new OAuthTokenCredential(clientId, clientSecret, paypalSdkConfig());
}
@Bean
public APIContext apiContext() throws PayPalRESTException{
APIContext apiContext = new APIContext(authTokenCredential().getAccessToken());
apiContext.setConfigurationMap(paypalSdkConfig());
return apiContext;
}
}
三、进行业务层处理
1、controller
package com.app.controller.gateway;
import javax.servlet.http.HttpServletRequest;
import com.yaan.youqi.app.component.OrderNotify;
import com.yaan.youqi.app.service.NotifyService;
import com.yaan.youqi.pay.service.PayPalService;
impor