提交 1632dfd2 authored 作者: wudepeng's avatar wudepeng

paypal

上级 2942fc0c
...@@ -31,6 +31,12 @@ ...@@ -31,6 +31,12 @@
</dependencyManagement> </dependencyManagement>
<dependencies> <dependencies>
<dependency>
<groupId>com.squareup.okhttp</groupId>
<artifactId>okhttp</artifactId>
<version>2.2.0</version>
<scope>compile</scope>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.cloud</groupId> <groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-gcp-starter-vision</artifactId> <artifactId>spring-cloud-gcp-starter-vision</artifactId>
...@@ -269,6 +275,12 @@ ...@@ -269,6 +275,12 @@
<artifactId>junit</artifactId> <artifactId>junit</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
<!--发邮件 END--> <!--发邮件 END-->
</dependencies> </dependencies>
......
package com.example.afrishop_v3.config; package com.example.afrishop_v3.config;
import com.example.afrishop_v3.util.PayPalUtil;
import com.paypal.base.codec.binary.Base64;
import com.paypal.base.rest.APIContext; import com.paypal.base.rest.APIContext;
import com.paypal.base.rest.OAuthTokenCredential; import com.paypal.base.rest.OAuthTokenCredential;
import com.paypal.base.rest.PayPalRESTException; import com.paypal.base.rest.PayPalRESTException;
import com.squareup.okhttp.*;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.builder.ToStringExclude;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.*;
import org.springframework.context.annotation.Configuration; import org.testng.annotations.Test;
import org.springframework.context.annotation.Primary;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.*;
@Configuration @Configuration
public class PaypalConfig { public class PaypalConfig {
...@@ -23,6 +33,8 @@ public class PaypalConfig { ...@@ -23,6 +33,8 @@ public class PaypalConfig {
@Value("${paypal.mode}") @Value("${paypal.mode}")
private String mode; private String mode;
@Bean @Bean
public Map<String, String> paypalSdkConfig() { public Map<String, String> paypalSdkConfig() {
Map<String, String> sdkConfig = new HashMap<>(); Map<String, String> sdkConfig = new HashMap<>();
...@@ -30,16 +42,19 @@ public class PaypalConfig { ...@@ -30,16 +42,19 @@ public class PaypalConfig {
return sdkConfig; return sdkConfig;
} }
@Scope("prototype")
@Bean @Bean
public OAuthTokenCredential authTokenCredential() { public OAuthTokenCredential authTokenCredential() {
return new OAuthTokenCredential(clientId, clientSecret, paypalSdkConfig()); return new OAuthTokenCredential(clientId, clientSecret, paypalSdkConfig());
} }
@Primary @Scope("prototype")
@Bean @Bean
public APIContext apiContext() throws PayPalRESTException { public APIContext apiContext() throws IOException {
APIContext apiContext = new APIContext(authTokenCredential().getAccessToken()); APIContext apiContext = new APIContext(PayPalUtil.getToken());
apiContext.setConfigurationMap(paypalSdkConfig()); apiContext.setConfigurationMap(paypalSdkConfig());
return apiContext; return apiContext;
} }
}
}
\ No newline at end of file
package com.example.afrishop_v3.util;
import com.squareup.okhttp.*;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import java.io.IOException;
public class PayPalUtil {
//正式
// private static String url="https://api.paypal.com/v1";
//测试
private static String url="https://api.sandbox.paypal.com/v1";
public static String getToken() throws IOException {
OkHttpClient client = new OkHttpClient();
MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
RequestBody body = RequestBody.create(mediaType, "grant_type=client_credentials");
Request request = new Request.Builder()
.url(url+"/oauth2/token")
.post(body)
.addHeader("Content-Type", "application/x-www-form-urlencoded")
.addHeader("Authorization", "Bearer A21AAI3JoFShZn_xG0Pr6fjqDFQG2dWcDC2WePIM6qpFgplWmj-b9KfXm-crSa4cq6Z5PwmiE5DdMcNDzJoSsYrYCZd9myh2g")
.addHeader("cache-control", "no-cache")
.build();
Response response = client.newCall(request).execute();
String string = response.body().string();
net.sf.json.JSONObject jsonObject = JSONObject.fromObject(string);
String token_type = jsonObject.getString("token_type");
String access_token = jsonObject.getString("access_token");
String token = String.format("%s %s", token_type, access_token);
return token;
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论