在测试环境中创建webhook通知

时间:2016-08-13 12:18:38

标签: braintree braintree-sandbox

我目前正在尝试创建测试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对象,告诉我哪个客户订阅了它,例如所有加载项都包含在结算方案中。

有没有办法在沙箱模式下获得此类测试通知?

1 个答案:

答案 0 :(得分:0)

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

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

webhookTesting().sampleNotification()接收虚拟对象是expected behavior,它可以帮助您确保可以正确捕获各种类型的webhook。一旦该逻辑到位,在设置&gt;下的沙盒网关中; Webhooks您可以指定端点以接收真实的webhook通知。

对于SUBSCRIPTION_CHARGED_SUCCESSFULLY,您确实会收到包含附加信息的Subscription object以及包含客户信息的Transaction objects数组。