我目前正在将“ PayPal智能支付按钮”集成到WebApp中。 传递自定义字段并使用此数据接收Webhook /购买确认会很好。
我在验证收到的Webhook时遇到麻烦。该文档很差,导致mit转到v1(不建议使用)或v2 Java SDK,其中没有提及Webhook验证。
我用Java集成了以下SDK。
<dependency>
<groupId>com.paypal.sdk</groupId>
<artifactId>checkout-sdk</artifactId>
<version>1.0.2</version>
</dependency>
但是我无法找到验证Webhook的方法。 我是否阅读了某些内容,或者如何获得Webhook验证?
答案 0 :(得分:1)
不支持用于Webhook集成的SDK
(此页面上对旧SDK的引用:https://developer.paypal.com/docs/integration/direct/webhooks/rest-webhooks/#verify-event-notifications已过期)
所以,您有一些选择。
DIY验证,使用事件标题中的信息: https://developer.paypal.com/docs/integration/direct/webhooks/notification-messages/#event-headers
与HTTPS API的直接集成: https://developer.paypal.com/docs/api/webhooks/v1/#verify-webhook-signature
完全不使用webhooks,而将集成切换到不需要webhooks的服务器端实现。
最后一个选择实际上就是我的建议。
这是您需要的服务器端SDK:https://github.com/paypal/Checkout-Java-SDK
这样,您将实现两条路线,一条用于“设置交易”(创建订单),另一条用于“捕获交易”(捕获订单)。这里有这些步骤的指南:https://developer.paypal.com/docs/checkout/reference/server-integration/
然后将连接到这两个服务器端路由的Web前端为:https://developer.paypal.com/demo/checkout/#/pattern/server
使用此服务器端集成时,无需网络钩子;在服务器上进行捕获时,您会立即获得成功或失败的响应。
答案 1 :(得分:0)
和您遇到了完全相同的问题,这就是我创建自己的 API 来处理该问题的原因:https://github.com/Osiris-Team/PayHook
它使用第一个 SDK 中提供的官方验证方法。
以下是使用我的 API 和 spring 的示例:
@RestController
@RequestMapping(value = "paypal-hook", method = RequestMethod.POST)
public class PayHookExample {
// This listens at https://.../paypal-hook
// for paypal notification messages and returns a "OK" text as response.
@GetMapping(produces = "text/plain")
public @ResponseBody String receiveAndRespond(HttpServletRequest request) {
System.out.println("Received webhook event at .../paypal-hook/...");
try{
PayHook payHook = new PayHook();
payHook.setSandboxMode(true); // Default is false. Remove this in production.
// Get the header and body
WebhookEventHeader header = payHook.parseAndGetHeader(getHeadersAsMap(request));
JsonObject body = payHook.parseAndGetBody(getBodyAsString(request));
// Create this event
WebhookEvent event = new WebhookEvent(
"insert your valid webhook id here", // Get it from here: https://developer.paypal.com/developer/applications/
Arrays.asList("CHECKOUT.ORDER.APPROVED", "PAYMENTS.PAYMENT.CREATED"), // Insert your valid event types/names here. Full list of all event types/names here: https://developer.paypal.com/docs/api-basics/notifications/webhooks/event-names
header,
body);
// Do event validation
payHook.validateWebhookEvent(event);
System.out.println("Validation successful!");
} catch (Exception e) {
e.printStackTrace();
System.out.println("Validation failed: "+e.getMessage());
}
return "OK";
}
// Simple helper method to help you extract the headers from HttpServletRequest object.
private Map<String, String> getHeadersAsMap(HttpServletRequest request) {
Map<String, String> map = new HashMap<String, String>();
@SuppressWarnings("rawtypes")
Enumeration headerNames = request.getHeaderNames();
while (headerNames.hasMoreElements()) {
String key = (String) headerNames.nextElement();
String value = request.getHeader(key);
map.put(key, value);
}
return map;
}
// Simple helper method to fetch request data as a string from HttpServletRequest object.
private String getBodyAsString(HttpServletRequest request) throws IOException {
StringBuilder stringBuilder = new StringBuilder();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(request.getInputStream()))){
String line = "";
while ((line=reader.readLine())!=null)
stringBuilder.append(line);
}
return stringBuilder.toString();
}
}
希望能帮到你, 祝你有美好的一天!