Skip to content

Commit

Permalink
Merge pull request #112 from Bindambc/52-new-commerce-settings-endpoi…
Browse files Browse the repository at this point in the history
…nt-api-v160

Commerce Settings Endpoint
  • Loading branch information
Bindambc authored Aug 18, 2023
2 parents c6b488b + d71ed94 commit 550e312
Show file tree
Hide file tree
Showing 8 changed files with 250 additions and 0 deletions.
54 changes: 54 additions & 0 deletions src/main/java/com/whatsapp/api/domain/config/CommerceDataItem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.whatsapp.api.domain.config;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;

/**
* The type Commerce data item.
*
* <p>
* <li> <b>is_catalog_visible</b> – <i>Optional</i>. Set to true to enable cart and display cart-related buttons in the business phone number's messages, catalogs, and product details.
* Set to false to disable cart. When disabled, customers can see products and their details in messages, but all cart related buttons will not appear in any message view.
* </br>
* <p>
* <li> <b>is_cart_enabled</b> – <i>Optional</i>. Set to true to enable catalog. When enabled, catalog storefront icon and catalog-related buttons appear in the business phone number's messages and business profile.
* Set to false to disable catalog. If disabled, the storefront icon and catalog-related buttons will not appear in any views and the catalog preview with thumbnails will not appear in the business profile view.
* In addition, wa.me links to the business's catalog, as well as the View catalog button that appears when sending catalog links in a message will display an Invalid catalog link warning when tapped.
* <p>
*/
@JsonInclude(JsonInclude.Include.NON_NULL)
public class CommerceDataItem {
@JsonProperty("id")
private String id;

@JsonProperty("is_catalog_visible")
private Boolean isCatalogVisible;

@JsonProperty("is_cart_enabled")
private Boolean isCartEnabled;

public CommerceDataItem() {
}

public String getId() {
return id;
}

public Boolean isCatalogVisible() {
return isCatalogVisible;
}

public CommerceDataItem setCatalogVisible(Boolean catalogVisible) {
isCatalogVisible = catalogVisible;
return this;
}

public Boolean isCartEnabled() {
return isCartEnabled;
}

public CommerceDataItem setCartEnabled(Boolean cartEnabled) {
isCartEnabled = cartEnabled;
return this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.whatsapp.api.domain.config;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.List;

@JsonInclude(JsonInclude.Include.NON_NULL)
public record GraphCommerceSettings(

@JsonProperty("data")
List<CommerceDataItem> data
) {
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.whatsapp.api.impl;

import com.whatsapp.api.domain.config.CommerceDataItem;
import com.whatsapp.api.domain.config.GraphCommerceSettings;
import com.whatsapp.api.domain.phone.PhoneNumber;
import com.whatsapp.api.domain.phone.PhoneNumbers;
import com.whatsapp.api.domain.phone.RequestCode;
Expand Down Expand Up @@ -188,4 +190,34 @@ public Response verifyCode(String phoneNumberId, VerifyCode verifyCode) {
return executeSync(whatsappBusinessManagementApiService.verifyCode(phoneNumberId, verifyCode));
}


/**
* Get a business phone number's WhatsApp Commerce Settings. Returns empty if commerce settings have not been set.
*
* @param phoneNumberId the phone number id
* @param fields the fields. Available options:
* <ul>
* <li>id</li>
* <li>is_cart_enabled</li>
* <li>is_catalog_visible</li>
* </ul>
* @return the response
* @see <a href="https://developers.facebook.com/docs/graph-api/reference/whats-app-business-account-to-number-current-status/whatsapp_commerce_settings">api docs</a>
*/
public GraphCommerceSettings getWhatsappCommerceSettings(String phoneNumberId, String... fields) {
return executeSync(whatsappBusinessManagementApiService.getWhatsappCommerceSettings(phoneNumberId, Map.of("fields", String.join(",", fields))));
}

/**
* Update a business WhatsApp Commerce Settings.
*
* @param phoneNumberId the phone number id
* @param commerceDataItem the commerce configuration fields
* @return the response
* @see <a href="https://developers.facebook.com/docs/graph-api/reference/whats-app-business-account-to-number-current-status/whatsapp_commerce_settings">api docs</a>
*/
public Response updateWhatsappCommerceSettings(String phoneNumberId, CommerceDataItem commerceDataItem) {
return executeSync(whatsappBusinessManagementApiService.updateWhatsappCommerceSettings(phoneNumberId, commerceDataItem));
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.whatsapp.api.service;

import com.whatsapp.api.domain.config.CommerceDataItem;
import com.whatsapp.api.domain.config.GraphCommerceSettings;
import com.whatsapp.api.domain.phone.PhoneNumber;
import com.whatsapp.api.domain.phone.PhoneNumbers;
import com.whatsapp.api.domain.phone.RequestCode;
Expand Down Expand Up @@ -118,5 +120,23 @@ public interface WhatsappBusinessManagementApiService {
@POST("/" + API_VERSION + "/{phone-number-ID}/verify_code")
Call<Response> verifyCode(@Path("phone-number-ID") String phoneNumberId, @Body VerifyCode verifyCode);

/**
* Business phone number's whatsApp commerce settings call.
*
* @param phoneNumberId the phone number id
* @param queryParams the query params
* @return the call
*/
@GET("/" + API_VERSION + "/{phone-number-ID}/whatsapp_commerce_settings")
Call<GraphCommerceSettings> getWhatsappCommerceSettings(@Path("phone-number-ID") String phoneNumberId, @QueryMap Map<String, String> queryParams);

/**
* Business phone number's whatsApp commerce settings call.
*
* @param phoneNumberId the phone number id
* @param commerceDataItem the query params
* @return the call
*/
@POST("/" + API_VERSION + "/{phone-number-ID}/whatsapp_commerce_settings")
Call<Response> updateWhatsappCommerceSettings(@Path("phone-number-ID") String phoneNumberId, @Body CommerceDataItem commerceDataItem);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.whatsapp.api.examples;

import com.whatsapp.api.WhatsappApiFactory;
import com.whatsapp.api.impl.WhatsappBusinessManagementApi;

import static com.whatsapp.api.TestConstants.PHONE_NUMBER_ID;
import static com.whatsapp.api.TestConstants.TOKEN;

public class GetWhatsappCommerceSettingsExample {

public static void main(String[] args) {

WhatsappApiFactory factory = WhatsappApiFactory.newInstance(TOKEN);

WhatsappBusinessManagementApi whatsappBusinessManagementApi = factory.newBusinessManagementApi();
//request without parameters
var response = whatsappBusinessManagementApi.getWhatsappCommerceSettings(PHONE_NUMBER_ID);

System.out.println(response);

//request with parameters
var response2 = whatsappBusinessManagementApi.getWhatsappCommerceSettings(PHONE_NUMBER_ID, "id", "is_cart_enabled");

System.out.println(response2);

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.whatsapp.api.examples;

import com.whatsapp.api.WhatsappApiFactory;
import com.whatsapp.api.domain.config.CommerceDataItem;
import com.whatsapp.api.impl.WhatsappBusinessManagementApi;

import static com.whatsapp.api.TestConstants.PHONE_NUMBER_ID;
import static com.whatsapp.api.TestConstants.TOKEN;

public class UpdateWhatsappCommerceSettingsExample {

public static void main(String[] args) {

WhatsappApiFactory factory = WhatsappApiFactory.newInstance(TOKEN);

WhatsappBusinessManagementApi whatsappBusinessManagementApi = factory.newBusinessManagementApi();

CommerceDataItem dataItem = new CommerceDataItem()
.setCatalogVisible(true);

var response = whatsappBusinessManagementApi.updateWhatsappCommerceSettings(PHONE_NUMBER_ID, dataItem);

System.out.println(response);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.whatsapp.api.MockServerUtilsTest;
import com.whatsapp.api.TestConstants;
import com.whatsapp.api.WhatsappApiFactory;
import com.whatsapp.api.domain.config.CommerceDataItem;
import com.whatsapp.api.domain.phone.RequestCode;
import com.whatsapp.api.domain.phone.VerifyCode;
import com.whatsapp.api.domain.phone.type.CodeMethodType;
Expand Down Expand Up @@ -502,6 +503,22 @@ void testRetrieveMessageTemplate3() throws IOException, URISyntaxException {

}

@Test
void testRetrieveMessageTemplate3WithLimit() throws IOException, URISyntaxException {
WhatsappApiFactory factory = WhatsappApiFactory.newInstance(TOKEN);

WhatsappBusinessManagementApi whatsappBusinessCloudApi = factory.newBusinessManagementApi();
mockWebServer.enqueue(new MockResponse().setResponseCode(200).setBody(fromResource("/retTemplate3.json")));

var templates = whatsappBusinessCloudApi.retrieveTemplates(WABA_ID, 1, "10");


Assertions.assertEquals(1, templates.data().size());
Assertions.assertEquals("welcome_template3", templates.data().get(0).name());
Assertions.assertEquals("Hello {{1}}, welcome to our {{2}} test.", templates.data().get(0).components().get(1).getText());

}

@Test
void testRetrievePhoneNumber() throws IOException, URISyntaxException, InterruptedException {
mockWebServer.enqueue(new MockResponse().setResponseCode(200).setBody(fromResource("/phone/phoneNumber.json")));
Expand Down Expand Up @@ -681,5 +698,56 @@ void verifyCodeError() throws IOException, URISyntaxException, InterruptedExcept

}

/**
* Method under test: {@link WhatsappBusinessManagementApi#getWhatsappCommerceSettings(String, String...)}
*/
@Test
void getWhatsappCommerceSettings() throws IOException, URISyntaxException, InterruptedException {
mockWebServer.enqueue(new MockResponse()
.setResponseCode(200)
.setBody(fromResource("/config/commerceSettings.json")));

WhatsappApiFactory factory = WhatsappApiFactory.newInstance(TestConstants.TOKEN);

WhatsappBusinessManagementApi businessManagementApi = factory.newBusinessManagementApi();

var response = businessManagementApi.getWhatsappCommerceSettings(PHONE_NUMBER_ID, "is_catalog_visible");

RecordedRequest recordedRequest = mockWebServer.takeRequest();
Assertions.assertEquals("GET", recordedRequest.getMethod());
Assertions.assertEquals("/" + API_VERSION + "/" + PHONE_NUMBER_ID + "/whatsapp_commerce_settings?fields=is_catalog_visible", recordedRequest.getPath());

Assertions.assertFalse(response.data().isEmpty());
Assertions.assertEquals("1001185490903808", response.data().get(0).getId());
Assertions.assertTrue(response.data().get(0).isCatalogVisible());
}

/**
* Method under test: {@link WhatsappBusinessManagementApi#updateWhatsappCommerceSettings(String, CommerceDataItem)}
*/
@Test
void updateWhatsappCommerceSettings() throws IOException, URISyntaxException, InterruptedException {
mockWebServer.enqueue(new MockResponse()
.setResponseCode(200)
.setBody(fromResource("/reponse.json"))
);

WhatsappApiFactory factory = WhatsappApiFactory.newInstance(TestConstants.TOKEN);

WhatsappBusinessManagementApi businessManagementApi = factory.newBusinessManagementApi();

CommerceDataItem commerceDataItem = new CommerceDataItem()
.setCartEnabled(true)
.setCatalogVisible(true);

var response = businessManagementApi.updateWhatsappCommerceSettings(PHONE_NUMBER_ID, commerceDataItem);

RecordedRequest recordedRequest = mockWebServer.takeRequest();
Assertions.assertEquals("POST", recordedRequest.getMethod());
Assertions.assertEquals("/" + API_VERSION + "/" + PHONE_NUMBER_ID + "/whatsapp_commerce_settings", recordedRequest.getPath());

Assertions.assertTrue(response.success());
}

}

9 changes: 9 additions & 0 deletions src/test/resources/config/commerceSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"data": [
{
"is_cart_enabled": true,
"is_catalog_visible": true,
"id": "1001185490903808"
}
]
}

0 comments on commit 550e312

Please sign in to comment.