Search in sources :

Example 1 with OrderUpdateV3

use of com.google.api.services.actions_fulfillment.v2.model.OrderUpdateV3 in project dialogflow-transactions-java by actions-on-google.

the class TransactionsApp method sendOrderUpdate.

private static void sendOrderUpdate(String orderId) throws IOException {
    // Setup service account credentials
    String serviceAccountKeyFileName = "service-account.json";
    // Setup service account credentials
    String serviceAccountFile = TransactionsApp.class.getClassLoader().getResource(serviceAccountKeyFileName).getFile();
    InputStream actionsApiServiceAccount = new FileInputStream(serviceAccountFile);
    ServiceAccountCredentials serviceAccountCredentials = (ServiceAccountCredentials) ServiceAccountCredentials.fromStream(actionsApiServiceAccount).createScoped(Collections.singleton("https://www.googleapis.com/auth/actions.order.developer"));
    AccessToken token = serviceAccountCredentials.refreshAccessToken();
    // Setup request with headers
    HttpPatch request = new HttpPatch("https://actions.googleapis.com/v3/orders/" + orderId);
    request.setHeader("Content-type", "application/json");
    request.setHeader("Authorization", "Bearer " + token.getTokenValue());
    // Create order update
    FieldMask fieldMask = FieldMask.newBuilder().addAllPaths(Arrays.asList("lastUpdateTime", "purchase.status", "purchase.userVisibleStatusLabel")).build();
    OrderUpdateV3 orderUpdate = new OrderUpdateV3().setOrder(new OrderV3().setMerchantOrderId(orderId).setLastUpdateTime(Instant.now().toString()).setPurchase(new PurchaseOrderExtension().setStatus("DELIVERED").setUserVisibleStatusLabel("Order delivered."))).setUpdateMask(FieldMaskUtil.toString(fieldMask)).setReason("Order status was updated to delivered.");
    // Setup JSON body containing order update
    JsonParser parser = new JsonParser();
    JsonObject orderUpdateJson = parser.parse(new Gson().toJson(orderUpdate)).getAsJsonObject();
    JsonObject body = new JsonObject();
    body.add("orderUpdate", orderUpdateJson);
    JsonObject header = new JsonObject();
    header.addProperty("isInSandbox", true);
    body.add("header", header);
    StringEntity entity = new StringEntity(body.toString());
    entity.setContentType(ContentType.APPLICATION_JSON.getMimeType());
    request.setEntity(entity);
    // Make request
    HttpClient httpClient = HttpClientBuilder.create().build();
    HttpResponse response = httpClient.execute(request);
    LOGGER.info(response.getStatusLine().getStatusCode() + " " + response.getStatusLine().getReasonPhrase());
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) JsonObject(com.google.gson.JsonObject) Gson(com.google.gson.Gson) HttpResponse(org.apache.http.HttpResponse) FileInputStream(java.io.FileInputStream) HttpPatch(org.apache.http.client.methods.HttpPatch) OrderV3(com.google.api.services.actions_fulfillment.v2.model.OrderV3) StringEntity(org.apache.http.entity.StringEntity) AccessToken(com.google.auth.oauth2.AccessToken) OrderUpdateV3(com.google.api.services.actions_fulfillment.v2.model.OrderUpdateV3) HttpClient(org.apache.http.client.HttpClient) ServiceAccountCredentials(com.google.auth.oauth2.ServiceAccountCredentials) FieldMask(com.google.protobuf.FieldMask) PurchaseOrderExtension(com.google.api.services.actions_fulfillment.v2.model.PurchaseOrderExtension) JsonParser(com.google.gson.JsonParser)

Example 2 with OrderUpdateV3

use of com.google.api.services.actions_fulfillment.v2.model.OrderUpdateV3 in project dialogflow-transactions-java by actions-on-google.

the class TransactionsApp method transactionDecisionComplete.

// Check result of asking to perform transaction / place order
@ForIntent("Transaction Decision Complete")
public ActionResponse transactionDecisionComplete(ActionRequest request) {
    ResourceBundle rb = ResourceBundle.getBundle("resources", request.getLocale());
    // Check transaction decision value
    Argument transactionDecisionValue = request.getArgument("TRANSACTION_DECISION_VALUE");
    Map<String, Object> extension = null;
    if (transactionDecisionValue != null) {
        extension = transactionDecisionValue.getExtension();
    }
    String transactionDecision = null;
    if (extension != null) {
        transactionDecision = (String) extension.get("transactionDecision");
    }
    ResponseBuilder responseBuilder = getResponseBuilder(request);
    if ((transactionDecision != null && transactionDecision.equals("ORDER_ACCEPTED"))) {
        OrderV3 order = ((OrderV3) extension.get("order"));
        order.setLastUpdateTime(Instant.now().toString());
        // Update order status
        PurchaseOrderExtension purchaseOrderExtension = order.getPurchase();
        purchaseOrderExtension.setStatus("CONFIRMED");
        purchaseOrderExtension.setUserVisibleStatusLabel("Order confirmed");
        order.setPurchase(purchaseOrderExtension);
        // Order update
        OrderUpdateV3 orderUpdate = new OrderUpdateV3().setType("SNAPSHOT").setReason("Reason string").setOrder(order);
        Map<String, Object> conversationData = request.getConversationData();
        String orderId = (String) conversationData.get("UNIQUE_ORDER_ID");
        String response = MessageFormat.format(rb.getString("transaction_decision_result_success"), orderId);
        responseBuilder.add(response).add(new StructuredResponse().setOrderUpdateV3(orderUpdate));
    } else {
        responseBuilder.add(rb.getString("transaction_failed"));
    }
    return responseBuilder.endConversation().build();
}
Also used : StructuredResponse(com.google.api.services.actions_fulfillment.v2.model.StructuredResponse) Argument(com.google.api.services.actions_fulfillment.v2.model.Argument) OrderUpdateV3(com.google.api.services.actions_fulfillment.v2.model.OrderUpdateV3) ResourceBundle(java.util.ResourceBundle) JsonObject(com.google.gson.JsonObject) JSONObject(org.json.simple.JSONObject) ResponseBuilder(com.google.actions.api.response.ResponseBuilder) OrderV3(com.google.api.services.actions_fulfillment.v2.model.OrderV3) PurchaseOrderExtension(com.google.api.services.actions_fulfillment.v2.model.PurchaseOrderExtension) ForIntent(com.google.actions.api.ForIntent)

Aggregations

OrderUpdateV3 (com.google.api.services.actions_fulfillment.v2.model.OrderUpdateV3)2 OrderV3 (com.google.api.services.actions_fulfillment.v2.model.OrderV3)2 PurchaseOrderExtension (com.google.api.services.actions_fulfillment.v2.model.PurchaseOrderExtension)2 JsonObject (com.google.gson.JsonObject)2 ForIntent (com.google.actions.api.ForIntent)1 ResponseBuilder (com.google.actions.api.response.ResponseBuilder)1 Argument (com.google.api.services.actions_fulfillment.v2.model.Argument)1 StructuredResponse (com.google.api.services.actions_fulfillment.v2.model.StructuredResponse)1 AccessToken (com.google.auth.oauth2.AccessToken)1 ServiceAccountCredentials (com.google.auth.oauth2.ServiceAccountCredentials)1 Gson (com.google.gson.Gson)1 JsonParser (com.google.gson.JsonParser)1 FieldMask (com.google.protobuf.FieldMask)1 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 ResourceBundle (java.util.ResourceBundle)1 HttpResponse (org.apache.http.HttpResponse)1 HttpClient (org.apache.http.client.HttpClient)1 HttpPatch (org.apache.http.client.methods.HttpPatch)1 StringEntity (org.apache.http.entity.StringEntity)1