簡體   English   中英

PayPal Webhook 驗證 Java SDK

[英]PayPal Webhook Verification Java SDK

我目前正在將“PayPal Smart Payment Buttons”集成到 WebApp 中。 傳遞自定義字段並使用此數據接收 Webhook / 購買確認非常有效。

我無法驗證收到的 Webhook。 文檔很差,導致進入 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 驗證?

webhook 集成不支持 SDK

(此頁面上對舊 SDK 的引用: https://developer.paypal.com/docs/integration/direct/webhooks/rest-webhooks/#verify-event-notifications已過期)

所以,你有一些選擇。

最后一個選項實際上是我推薦的。

這是您需要的服務器端 SDK: https://github.com/paypal/Checkout-Java-SDK

這樣,您將實現兩條路線,一條用於“設置交易”(創建訂單),另一條用於“捕獲交易”(捕獲訂單)。 這里有這些步驟的指南: https://developer.paypal.com/docs/checkout/reference/server-integration/

然后將連接到這兩個服務器端路由的 web 前端是: https://developer.ZA0A058BAAEEF16E88F6BDserver2EE36C03F6FZ.com/demo/checkout/#/pattern/

使用此服務器端集成時不需要 webhook; 在服務器上進行捕獲時,您會立即響應成功或失敗。

和你有完全相同的問題,這就是為什么我創建了自己的 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();
    }
}

希望我能幫上忙,祝你有美好的一天!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM