在测试环境中创建 webhook-notifications

Creating webhook-notifications in testing environment

我目前正在尝试创建一个测试 webhook 通知,如文档中所示:

HashMap<String, String> sampleNotification = gateway.webhookTesting().sampleNotification(
    WebhookNotification.Kind.SUBSCRIPTION_WENT_PAST_DUE, "my_id"
);

WebhookNotification webhookNotification = gateway.webhookNotification().parse(
    sampleNotification.get("bt_signature"),
    sampleNotification.get("bt_payload")
);

webhookNotification.getSubscription().getId();
// "my_id"

首先我不知道 my_id 实际上应该是什么。它应该是计划 ID 吗?或者应该是 Subscription ID?

我都测试过了。我已将其设置为我的保险库中现有的计费计划,我还尝试创建一个 Customer 到实际的 Subscription,如下所示:

public class WebhookChargedSuccessfullyLocal {

    private final static BraintreeGateway BT;

    static {
        String btConfig = "C:\workspaces\mz\mz-server\mz-web-server\src\main\assembly\dev\braintree.properties";
        Braintree.initialize(btConfig);
        BT = Braintree.instance();
    }

    public static void main(String[] args) {

        WebhookChargedSuccessfullyLocal webhookChargedSuccessfullyLocal = new WebhookChargedSuccessfullyLocal();
        webhookChargedSuccessfullyLocal.post();
    }

    /**
     * 
     */
    public void post() {

        CustomerRequest customerRequest = new CustomerRequest()
                .firstName("Testuser")
                .lastName("Tester");

        Result<Customer> createUserResult = BT.customer().create(customerRequest);

        if(createUserResult.isSuccess() == false) {
            System.err.println("Could not create customer");
            System.exit(1);
        }

        Customer customer = createUserResult.getTarget();

        PaymentMethodRequest paymentMethodRequest = new PaymentMethodRequest()
                .customerId(customer.getId())
                .paymentMethodNonce("fake-valid-visa-nonce");

        Result<? extends PaymentMethod> createPaymentMethodResult = BT.paymentMethod().create(paymentMethodRequest);

        if(createPaymentMethodResult.isSuccess() == false) {
            System.err.println("Could not create payment method");
            System.exit(1);
        }

        if(!(createPaymentMethodResult.getTarget() instanceof CreditCard)) {
            System.err.println("Unexpected error. Result is not a credit card.");
            System.exit(1);
        }

        CreditCard creditCard = (CreditCard) createPaymentMethodResult.getTarget();

        SubscriptionRequest subscriptionRequest = new SubscriptionRequest()
                .paymentMethodToken(creditCard.getToken())
                .planId("mmb2");

        Result<Subscription> createSubscriptionResult = BT.subscription().create(subscriptionRequest);

        if(createSubscriptionResult.isSuccess() == false) {
            System.err.println("Could not create subscription");
            System.exit(1);
        }

        Subscription subscription = createSubscriptionResult.getTarget();

        HashMap<String, String> sampleNotification = BT.webhookTesting()
                .sampleNotification(WebhookNotification.Kind.SUBSCRIPTION_CHARGED_SUCCESSFULLY, subscription.getId());

        WebhookNotification webhookNotification = BT.webhookNotification()
                .parse(
                        sampleNotification.get("bt_signature"),
                        sampleNotification.get("bt_payload")
                        );

        System.out.println(webhookNotification.getSubscription().getId());

    }

}

但我得到的只是一个没有任何设置的 WebhookNotification 实例。似乎只设置了它的 ID 和时间戳,仅此而已。

我的预期:

我希望收到一个 Subscription 对象,告诉我哪个客户订阅了它,例如计费计划中包含的所有附加组件。

有没有办法在沙盒模式下获得这样的测试通知?

完全披露:我在 Braintree 工作。如果您有任何其他问题,请随时联系 support.

webhookNotification.getSubscription().getId(); 将 return 与 sampleNotification 关联的订阅 ID,可以是任何用于测试目的的 ID,但在生产环境中将是订阅 ID。

webhookTesting().sampleNotification() 接收虚拟对象是 expected behavior,它可以帮助您确保可以正确捕获各种 webhook。一旦该逻辑就位,在设置 > Webhooks 下的 Sandbox Gateway 中,您可以指定您的端点以接收真正的 webhook 通知。

SUBSCRIPTION_CHARGED_SUCCESSFULLY 的情况下,您确实会收到包含客户信息的 Subscription object containing add-on information as well as an array of Transaction objects