简体   繁体   English

PayPal Webhook 验证 Java SDK

[英]PayPal Webhook Verification Java SDK

I am currently integrating the "PayPal Smart Payment Buttons" into a WebApp.我目前正在将“PayPal Smart Payment Buttons”集成到 WebApp 中。 Passing custom fields and receiving a Webhook / Purchase Confirmation with this data works quite fine.传递自定义字段并使用此数据接收 Webhook / 购买确认非常有效。

I am having trouble with validating a received Webhook.我无法验证收到的 Webhook。 The Documentation is poor and leads mit either to v1 (deprecated) or to v2 Java SDK where nothing is mentioned about Webhook verification.文档很差,导致进入 v1(已弃用)或 v2 Java SDK,其中没有提及 Webhook 验证。

I integrated the following SDK in Java.我在Java中集成了以下SDK。

<dependency>
            <groupId>com.paypal.sdk</groupId>
            <artifactId>checkout-sdk</artifactId>
            <version>1.0.2</version>
        </dependency>

But I am not able to find a way to verify a Webhook.但我无法找到验证 Webhook 的方法。 Did I read over something or how can I achieve the Webhook verification?我是否阅读了某些内容或如何实现 Webhook 验证?

There is no supported SDK for webhook integration webhook 集成不支持 SDK

(The references to old SDKs on this page: https://developer.paypal.com/docs/integration/direct/webhooks/rest-webhooks/#verify-event-notifications are out of date) (此页面上对旧 SDK 的引用: https://developer.paypal.com/docs/integration/direct/webhooks/rest-webhooks/#verify-event-notifications已过期)

So, you have some choices.所以,你有一些选择。

The last option is actually what I would recommend.最后一个选项实际上是我推荐的。

Here is the server-side SDK you need: https://github.com/paypal/Checkout-Java-SDK这是您需要的服务器端 SDK: https://github.com/paypal/Checkout-Java-SDK

With that you would implement two routes, one for "Set Up Transaction" (create order), and one for "Capture Transaction" (capture the order).这样,您将实现两条路线,一条用于“设置交易”(创建订单),另一条用于“捕获交易”(捕获订单)。 There is a guide for these steps here: https://developer.paypal.com/docs/checkout/reference/server-integration/这里有这些步骤的指南: https://developer.paypal.com/docs/checkout/reference/server-integration/

The web front-end that will then connect to those two server-side routes is: https://developer.paypal.com/demo/checkout/#/pattern/server然后将连接到这两个服务器端路由的 web 前端是: https://developer.ZA0A058BAAEEF16E88F6BDserver2EE36C03F6FZ.com/demo/checkout/#/pattern/

There is no need for webhooks when using this server-side integration;使用此服务器端集成时不需要 webhook; you have an immediate response of success or failure when doing the capture on the server.在服务器上进行捕获时,您会立即响应成功或失败。

Had exactly the same issue as you, thats why I created my own API for handling that: https://github.com/Osiris-Team/PayHook和你有完全相同的问题,这就是为什么我创建了自己的 API 来处理这个问题: https://github.com/Osiris-Team/PayHook

It's using the official validation methods provided in the first SDK.它使用第一个 SDK 中提供的官方验证方法。

Here is an example using my API with spring:这是一个使用我的 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();
    }
}

Hope I could help, have a nice day!希望我能帮上忙,祝你有美好的一天!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM