|
5 | 5 | import java.util.ArrayList; |
6 | 6 | import java.util.List; |
7 | 7 |
|
| 8 | +import com.siftscience.model.CardBinMetadata; |
8 | 9 | import com.siftscience.model.DigitalOrder; |
| 10 | +import com.siftscience.model.PaymentMethod; |
9 | 11 | import com.siftscience.model.TransactionFieldSet; |
10 | 12 | import okhttp3.OkHttpClient; |
11 | 13 | import okhttp3.mockwebserver.MockResponse; |
@@ -794,4 +796,84 @@ public void testTransactionEventWithExtraWithdrawalFields() throws Exception { |
794 | 796 |
|
795 | 797 | server.shutdown(); |
796 | 798 | } |
| 799 | + |
| 800 | + @Test |
| 801 | + public void testTransactionEventWithBinMetadata() throws Exception { |
| 802 | + String expectedRequestBody = "{\n" + |
| 803 | + " \"$type\" : \"$transaction\",\n" + |
| 804 | + " \"$api_key\" : \"YOUR_API_KEY\",\n" + |
| 805 | + " \"$user_id\" : \"billy_jones_301\",\n" + |
| 806 | + " \"$amount\" : 506790000,\n" + |
| 807 | + " \"$currency_code\" : \"EUR\",\n" + |
| 808 | + "\n" + |
| 809 | + " \"$user_email\" : \"bill@gmail.com\",\n" + |
| 810 | + " \"$transaction_type\" : \"$buy\",\n" + |
| 811 | + " \"$transaction_id\" : \"719637215\",\n" + |
| 812 | + "\n" + |
| 813 | + " \"$payment_method\" : {\n" + |
| 814 | + " \"$payment_type\" : \"$credit_card\",\n" + |
| 815 | + " \"$payment_gateway\" : \"$braintree\",\n" + |
| 816 | + " \"$card_bin\" : \"542486\",\n" + |
| 817 | + " \"$card_last4\" : \"4444\",\n" + |
| 818 | + " \"$card_bin_metadata\": {\n" + |
| 819 | + " \"$country\": \"US\",\n" + |
| 820 | + " \"$level\" : \"Gold\",\n" + |
| 821 | + " \"$type\" : \"CREDIT\",\n" + |
| 822 | + " \"$brand\" : \"VISA\",\n" + |
| 823 | + " \"$bank\" : \"Chase\"\n" + |
| 824 | + " },\n" + |
| 825 | + " },\n" + |
| 826 | + "}"; |
| 827 | + |
| 828 | + // Start a new mock server and enqueue a mock response. |
| 829 | + MockWebServer server = new MockWebServer(); |
| 830 | + MockResponse response = new MockResponse(); |
| 831 | + response.setResponseCode(HTTP_OK); |
| 832 | + response.setBody("{\n" + |
| 833 | + " \"status\" : 0,\n" + |
| 834 | + " \"error_message\" : \"OK\",\n" + |
| 835 | + " \"time\" : 1327604222,\n" + |
| 836 | + " \"request\" : \"" + TestUtils.unescapeJson(expectedRequestBody) + "\"\n" + |
| 837 | + "}"); |
| 838 | + server.enqueue(response); |
| 839 | + server.start(); |
| 840 | + |
| 841 | + // Create a new client and link it to the mock server. |
| 842 | + SiftClient client = new SiftClient("YOUR_API_KEY", "YOUR_ACCOUNT_ID", |
| 843 | + new OkHttpClient.Builder() |
| 844 | + .addInterceptor(OkHttpUtils.urlRewritingInterceptor(server)) |
| 845 | + .build()); |
| 846 | + |
| 847 | + // Build and execute the request against the mock server. |
| 848 | + PaymentMethod paymentMethod = TestUtils.samplePaymentMethod1() |
| 849 | + .setCardBinMetadata(new CardBinMetadata() |
| 850 | + .setCountry("US") |
| 851 | + .setLevel("Gold") |
| 852 | + .setType("CREDIT") |
| 853 | + .setBrand("VISA") |
| 854 | + .setBank("Chase")); |
| 855 | + EventRequest request = client.buildRequest(new TransactionFieldSet() |
| 856 | + .setUserId("billy_jones_301") |
| 857 | + .setAmount(506790000L) |
| 858 | + .setCurrencyCode("EUR") |
| 859 | + .setUserEmail("bill@gmail.com") |
| 860 | + .setTransactionType("$buy") |
| 861 | + .setTransactionId("719637215") |
| 862 | + .setPaymentMethod(paymentMethod)); |
| 863 | + EventResponse siftResponse = request.send(); |
| 864 | + |
| 865 | + // Verify the request. |
| 866 | + RecordedRequest request1 = server.takeRequest(); |
| 867 | + Assert.assertEquals("POST", request1.getMethod()); |
| 868 | + Assert.assertEquals("/v205/events", request1.getPath()); |
| 869 | + JSONAssert.assertEquals(expectedRequestBody, request.getFieldSet().toJson(), true); |
| 870 | + |
| 871 | + // Verify the response. |
| 872 | + Assert.assertEquals(HTTP_OK, siftResponse.getHttpStatusCode()); |
| 873 | + Assert.assertEquals(0, (int) siftResponse.getBody().getStatus()); |
| 874 | + JSONAssert.assertEquals(response.getBody().readUtf8(), |
| 875 | + siftResponse.getBody().toJson(), true); |
| 876 | + |
| 877 | + server.shutdown(); |
| 878 | + } |
797 | 879 | } |
0 commit comments