सामान के लेन-देन को v3 (Dialogflow) पर माइग्रेट करें

ऑर्डर v2 एपीआई को 7 अगस्त, 2019 से बंद कर दिया गया है. इसकी जगह ऑर्डर 3 वर्शन ने ले ली है. अगर आपने इस तारीख से पहले ऐसी कार्रवाई बनाई है जो फ़िज़िकल लेन-देन मैनेज करती है, तो इस गाइड की मदद से अपनी सेट की गई कार्रवाई को ऑर्डर v3 का इस्तेमाल करने के लिए अपडेट करें.

एपीआई में किए गए बदलाव

ऑर्डर एपीआई के वर्शन 3 में, वर्शन 2 से लेकर ये अहम बदलाव किए गए हैं:

  • अनुरोध का स्ट्रक्चर - वर्शन 3 के ऑर्डर स्ट्रक्चर में सिर्फ़ कार्ट का कॉन्टेंट शामिल करने के बजाय, ऑर्डर की पूरी जानकारी होती है.
  • ऑर्डर के अपडेट - ऑर्डर की जगह को मैनेज करने के लिए, ऑर्डर 3 ऑर्डर एपीआई, ऑर्डर से जुड़े अपडेट भी हैंडल करता है. इसलिए, आपके एंडपॉइंट को उपयोगकर्ताओं को उनके ऑर्डर के बारे में अपडेट भेजने के लिए, Actions API का इस्तेमाल करने की ज़रूरत नहीं होती. आप ऑर्डर एपीआई को PATCH अनुरोधों के तौर पर भी अपडेट भेजते हैं. ये अपडेट, मौजूदा order ऑब्जेक्ट को अपडेट करते हैं, न कि स्टेटस के अपडेट भेजने के लिए POST अनुरोध भेजने के बजाय.
  • पेमेंट की सुविधा - ऑर्डर एपीआई के तीसरे वर्शन पर Google Pay का इस्तेमाल करने वाली कार्रवाइयों में, पेमेंट गेटवे की जानकारी को एक नए JSON स्ट्रक्चर में नेस्ट किया जाता है. इससे, प्रोसेस को ज़्यादा बेहतर बनाया जा सकता है और ईयू के कानूनों का पालन किया जा सकता है.

Node.JS को v3 पर माइग्रेट करें

Node.JS क्लाइंट लाइब्रेरी का इस्तेमाल करने वाली कार्रवाई को माइग्रेट करने के लिए, यह तरीका अपनाएं.

1. ऑर्डर v3 फ़्लैग जोड़ें

ट्रांज़ैक्शन मैनेज करने के लिए इस्तेमाल किए गए क्लाइंट लाइब्रेरी फ़ंक्शन, ऑर्डर के वर्शन 3 के लिए अपडेट किए जाते हैं. आपको बस फ़ुलफ़िलमेंट कोड में एक फ़्लैग जोड़ना होगा. यह कोड, वर्शन 3 JSON को भेजने के लिए फ़ंक्शन को अपडेट करेगा.

ग्राहक को आइटम भेजने के लिए, यह कोड जोड़ें:

Node.js
const {dialogflow} = require('actions-on-google');
const app = dialogflow({ordersv3: true});

2. कार्ट असेंबली अपडेट करें

ProposedOrder टाइप को ज़्यादा जानकारी वाले Order ऑब्जेक्ट से बदल दिया गया है. अपने ProposedOrder कार्ट को Order कार्ट में बदलने के लिए JSON रेफ़रंस देखें.

3. पेमेंट पैरामीटर अपडेट करें

ऑर्डर का सुझाव देने वाले चरण में पैसे चुकाने की जानकारी का स्ट्रक्चर, एपीआई के अलग-अलग वर्शन के हिसाब से अलग-अलग होता है.

actions.intent.TRANSACTION_DECISION इंटेंट अनुरोध में, पुराने paymentOptions ऑब्जेक्ट को नए paymentParameters ऑब्जेक्ट से बदलें. ज़्यादातर शामिल फ़ील्ड एक जैसे होते हैं, JSON ऑब्जेक्ट के स्ट्रक्चर में कुछ बदलाव करने के लिए सेव करें.

नीचे दिए गए कोड स्निपेट में उदाहरण के तौर पर, Google Pay के लिए वर्शन 3 का इस्तेमाल करके actions.intent.TRANSACTION_DECISION इंटेंट अनुरोध और तुलना करने के लिए पुराने वर्शन 2 का अनुरोध दिखाया गया है.

Node.JS नया (v3)
conv.ask(new TransactionDecision({
  orderOptions: {
    requestDeliveryAddress: false,
    userInfoOptions: {
      userInfoProperties: [
        'EMAIL',
      ],
    },
  },
  paymentParameters: {
    googlePaymentOption: {
      // facilitationSpec is expected to be a serialized JSON string
      facilitationSpec: JSON.stringify({
        apiVersion: 2,
        apiVersionMinor: 0,
        merchantInfo: {
          merchantName: 'Example Merchant',
        },
        allowedPaymentMethods: [
          {
            type: 'CARD',
            parameters: {
              allowedAuthMethods: ['PAN_ONLY', 'CRYPTOGRAM_3DS'],
              allowedCardNetworks: [
                'AMEX', 'DISCOVER', 'JCB', 'MASTERCARD', 'VISA'],
            },
            tokenizationSpecification: {
              type: 'PAYMENT_GATEWAY',
              parameters: {
                gateway: 'example',
                gatewayMerchantId: 'exampleGatewayMerchantId',
              },
            },
          },
        ],
        transactionInfo: {
          totalPriceStatus: 'FINAL',
          totalPrice: '10.00',
          currencyCode: 'USD',
        },
      }),
    },
  },
  presentationOptions: {
    actionDisplayName: 'PLACE_ORDER',
  },
  order: order,
}));
Node.JS Old (v2)
conv.ask(new TransactionDecision({
  orderOptions: {
    requestDeliveryAddress: false,
  },
  paymentOptions: {
    googleProvidedOptions: {
      prepaidCardDisallowed: false,
      supportedCardNetworks: ['VISA', 'AMEX', 'DISCOVER', 'MASTERCARD'],
      tokenizationParameters: {
        tokenizationType: 'PAYMENT_GATEWAY',
        // These will be provided by payment processor,
        // like Stripe, Braintree, Vantiv, Ayden, etc.
        parameters: {
          'gateway': 'stripe',
          'stripe:publishableKey': (conv.sandbox ? 'pk_test_key' : 'pk_live_key'),
          'stripe:version': '2018-11-08'
        },
      },
    },
  },
  proposedOrder: order,
}));
JSON नया (v3)

ध्यान दें कि नीचे दिया गया Dialogflow JSON कोड, वेबहुक रिस्पॉन्स के बारे में बताता है.

{
  "payload": {
    "google": {
      "expectUserResponse": true,
      "systemIntent": {
        "intent": "actions.intent.TRANSACTION_DECISION",
        "data": {
          "@type": "type.googleapis.com/google.actions.transactions.v3.TransactionDecisionValueSpec",
          "orderOptions": {
            "requestDeliveryAddress": "false"
          },
          "paymentParameters": {
            "googlePaymentOption": {
              "facilitationSpec": "{\"apiVersion\":2,\"apiVersionMinor\":0,\"merchantInfo\":{\"merchantName\":\"Example Merchant\"},\"allowedPaymentMethods\":[{\"type\":\"CARD\",\"parameters\":{\"allowedAuthMethods\":[\"PAN_ONLY\",\"CRYPTOGRAM_3DS\"],\"allowedCardNetworks\":[\"AMEX\",\"DISCOVER\",\"JCB\",\"MASTERCARD\",\"VISA\"]},\"tokenizationSpecification\":{\"type\":\"PAYMENT_GATEWAY\",\"parameters\":{\"gateway\":\"example\",\"gatewayMerchantId\":\"exampleGatewayMerchantId\"}}}],\"transactionInfo\":{\"totalPriceStatus\":\"FINAL\",\"totalPrice\":\"10.00\",\"currencyCode\":\"USD\"}}"
            }
          },
          "presentationOptions": {
            "actionDisplayName": "PLACE_ORDER"
          },
          "order": {
            "createTime": "2019-08-01T17:12:13.765Z",
            "lastUpdateTime": "2019-08-01T17:12:13.765Z",
            "merchantOrderId": "UNIQUE_ORDER_ID",
            "userVisibleOrderId": "USER_VISIBLE_ORDER_ID",
            "transactionMerchant": {
              "id": "http://www.example.com",
              "name": "Example Merchant"
            },
            "contents": {
              "lineItems": [
                {
                  "id": "LINE_ITEM_ID",
                  "name": "Pizza",
                  "description": "A four cheese pizza.",
                  "priceAttributes": [
                    {
                      "type": "REGULAR",
                      "name": "Line Item Price",
                      "state": "ACTUAL",
                      "amount": {
                        "currencyCode": "USD",
                        "amountInMicros": 8990000
                      },
                      "taxIncluded": true
                    }
                  ],
                  "notes": [
                    "Extra cheese."
                  ],
                  "purchase": {
                    "quantity": 1,
                    "unitMeasure": {
                      "measure": 1,
                      "unit": "POUND"
                    },
                    "itemOptions": [
                      {
                        "id": "ITEM_OPTION_ID",
                        "name": "Pepperoni",
                        "prices": [
                          {
                            "type": "REGULAR",
                            "state": "ACTUAL",
                            "name": "Item Price",
                            "amount": {
                              "currencyCode": "USD",
                              "amountInMicros": 1000000
                            },
                            "taxIncluded": true
                          }
                        ],
                        "note": "Extra pepperoni",
                        "quantity": 1,
                        "subOptions": []
                      }
                    ]
                  }
                }
              ]
            },
            "buyerInfo": {
              "email": "janedoe@gmail.com",
              "firstName": "Jane",
              "lastName": "Doe",
              "displayName": "Jane Doe"
            },
            "priceAttributes": [
              {
                "type": "TOTAL",
                "name": "Total Price",
                "state": "ESTIMATE",
                "amount": {
                  "currencyCode": "USD",
                  "amountInMicros": 15770000
                },
                "taxIncluded": true
              },
              {
                "type": "TAX",
                "name": "Tax",
                "state": "ESTIMATE",
                "amount": {
                  "currencyCode": "USD",
                  "amountInMicros": 3780000
                },
                "taxIncluded": true
              },
              {
                "type": "SUBTOTAL",
                "name": "Subtotal",
                "state": "ESTIMATE",
                "amount": {
                  "currencyCode": "USD",
                  "amountInMicros": 9990000
                },
                "taxIncluded": true
              },
              {
                "type": "DELIVERY",
                "name": "Delivery",
                "state": "ACTUAL",
                "amount": {
                  "currencyCode": "USD",
                  "amountInMicros": 2000000
                },
                "taxIncluded": true
              }
            ],
            "followUpActions": [
              {
                "type": "VIEW_DETAILS",
                "title": "View details",
                "openUrlAction": {
                  "url": "http://example.com"
                }
              },
              {
                "type": "CALL",
                "title": "Call us",
                "openUrlAction": {
                  "url": "tel:+16501112222"
                }
              },
              {
                "type": "EMAIL",
                "title": "Email us",
                "openUrlAction": {
                  "url": "mailto:person@example.com"
                }
              }
            ],
            "termsOfServiceUrl": "www.example.com",
            "note": "Sale event",
            "promotions": [
              {
                "coupon": "COUPON_CODE"
              }
            ],
            "purchase": {
              "status": "CREATED",
              "userVisibleStatusLabel": "CREATED",
              "type": "FOOD",
              "returnsInfo": {
                "isReturnable": false,
                "daysToReturn": 1,
                "policyUrl": "http://www.example.com"
              },
              "fulfillmentInfo": {
                "id": "FULFILLMENT_SERVICE_ID",
                "fulfillmentType": "DELIVERY",
                "expectedFulfillmentTime": {
                  "timeIso8601": "2017-01-16T01:30:15.01Z"
                },
                "location": {
                  "zipCode": "94086",
                  "city": "Sunnyvale",
                  "postalAddress": {
                    "regionCode": "US",
                    "postalCode": "94086",
                    "administrativeArea": "CA",
                    "locality": "Sunnyvale",
                    "addressLines": [
                      "222, Some other Street"
                    ]
                  }
                },
                "price": {
                  "type": "REGULAR",
                  "name": "Delivery Price",
                  "state": "ACTUAL",
                  "amount": {
                    "currencyCode": "USD",
                    "amountInMicros": 2000000
                  },
                  "taxIncluded": true
                },
                "fulfillmentContact": {
                  "email": "johnjohnson@gmail.com",
                  "firstName": "John",
                  "lastName": "Johnson",
                  "displayName": "John Johnson"
                }
              },
              "purchaseLocationType": "ONLINE_PURCHASE"
            }
          }
        }
      }
    }
  }
}
JSON पुराना (v2)

ध्यान दें कि नीचे दिया गया Dialogflow JSON कोड, वेबहुक रिस्पॉन्स के बारे में बताता है.

{
  "payload": {
    "google": {
      "expectUserResponse": true,
      "systemIntent": {
        "intent": "actions.intent.TRANSACTION_DECISION",
        "data": {
          "@type": "type.googleapis.com/google.actions.v2.TransactionDecisionValueSpec",
          "orderOptions": {
            "requestDeliveryAddress": false
          },
          "paymentOptions": {
            "googleProvidedOptions": {
              "prepaidCardDisallowed": false,
              "supportedCardNetworks": [
                "VISA",
                "AMEX",
                "DISCOVER",
                "MASTERCARD"
              ],
              "tokenizationParameters": {
                "tokenizationType": "PAYMENT_GATEWAY",
                "parameters": {
                  "gateway": "stripe",
                  "stripe:publishableKey": "pk_test_key",
                  "stripe:version": "2018-11-08"
                }
              }
            }
          },
          "proposedOrder": {
            "id": "UNIQUE_ORDER_ID222",
            "cart": {
              "merchant": {
                "id": "book_store_id",
                "name": "A Book Store"
              },
              "lineItems": [
                {
                  "name": "My Memoirs",
                  "id": "mymemoirs_id",
                  "price": {
                    "amount": {
                      "currencyCode": "USD",
                      "nanos": 990000000,
                      "units": 8
                    },
                    "type": "ACTUAL"
                  },
                  "quantity": 1,
                  "subLines": [
                    {
                      "note": "By Bestselling Novelist"
                    }
                  ],
                  "type": "REGULAR"
                },
                {
                  "name": "Biography",
                  "id": "biography_id",
                  "price": {
                    "amount": {
                      "currencyCode": "USD",
                      "nanos": 990000000,
                      "units": 10
                    },
                    "type": "ACTUAL"
                  },
                  "quantity": 1,
                  "subLines": [
                    {
                      "note": "Signed copy"
                    }
                  ],
                  "type": "REGULAR"
                }
              ],
              "notes": "Sale event",
              "otherItems": []
            },
            "otherItems": [
              {
                "name": "Subtotal",
                "id": "subtotal",
                "price": {
                  "amount": {
                    "currencyCode": "USD",
                    "nanos": 980000000,
                    "units": 19
                  },
                  "type": "ESTIMATE"
                },
                "type": "SUBTOTAL"
              },
              {
                "name": "Tax",
                "id": "tax",
                "price": {
                  "amount": {
                    "currencyCode": "USD",
                    "nanos": 780000000,
                    "units": 2
                  },
                  "type": "ESTIMATE"
                },
                "type": "TAX"
              }
            ],
            "totalPrice": {
              "amount": {
                "currencyCode": "USD",
                "nanos": 760000000,
                "units": 22
              },
              "type": "ESTIMATE"
            }
          }
        }
      }
    }
  }
}

4. ऑर्डर एपीआई की मदद से अपडेट भेजें

ऑर्डर एपीआई का वर्शन 3, ऑर्डर से जुड़े अपडेट मैनेज करता है. इसलिए, अब आपको Actions API पर पोस्ट अनुरोध भेजने की ज़रूरत नहीं है. इसके बजाय, ऑर्डर एपीआई को एक पैच अनुरोध भेजा जाता है, जो आपके Order ऑब्जेक्ट का कॉन्टेंट अपडेट करता है.

नया बेयर टोकन वापस पाएं

आपके पास उसी JSON सेवा खाते की कुंजी का इस्तेमाल करने का विकल्प होता है जिसका इस्तेमाल आपने Actions API के लिए, बीयरर टोकन पाने के लिए किया था. आपको ऑर्डर एपीआई के लिए, एक नए बेयरर टोकन का अनुरोध करना होगा. Google API क्लाइंट लाइब्रेरी और "https://www.googleapis.com/auth/actions.order.developer" दायरे का इस्तेमाल करके, बेयरर टोकन के लिए अपनी सेवा कुंजी की अदला-बदली करें.

एपीआई क्लाइंट लाइब्रेरी के GitHub पेज पर, आपको इंस्टॉल करने के तरीके और उदाहरण मिल सकते हैं. ऑर्डर एपीआई पासकोड एक्सचेंज के उदाहरण के लिए, हमारे Node.js सैंपल में, अपडेट किए गए order-update.js का रेफ़रंस भी दिया जा सकता है.

अपडेट भेजें

ऑर्डर एपीआई की मदद से ऑर्डर अपडेट भेजने का तरीका, Actions API की मदद से अपडेट भेजने जैसा है. हालांकि, इसमें पोस्ट अनुरोध के बजाय PATCH अनुरोध भेजा जाता है. PATCH अनुरोध को JSON फ़ॉर्मैट में, इस फ़ॉर्मैट में भेजा जाना चाहिए:

{ "orderUpdate": OrderUpdate" }

OrderUpdate का फ़ॉर्मैट, वर्शन 3 में भी अलग है. पैच करने के अनुरोध का रेफ़रंस देखें और ज़रूरत के मुताबिक अपने OrderUpdate फ़ील्ड अपडेट करें. नीचे दिया गया कोड स्निपेट, PATCH अनुरोध का एक उदाहरण दिखाता है, जो किसी ऑर्डर के स्टेटस को "DELIVERED" में अपडेट करता है:

Node.js
// Import the 'googleapis' module for authorizing the request.
const {google} = require('googleapis');
// Import the 'request' module for sending an HTTP POST request.
const request = require('request');
// Import the OrderUpdate class from the Actions on Google client library.
const {OrderUpdate} = require('actions-on-google');
// Import the service account key used to authorize the request. Replace the string path with a path to your service account key.
const key = require('./service-account.json');
// Create a new JWT client for the Actions API using credentials from the service account key.
let jwtClient = new google.auth.JWT(
    key.client_email,
    null,
    key.private_key,
    ['https://www.googleapis.com/auth/actions.order.developer'],
    null
);
// Authorize the client asynchronously, passing in a callback to run upon authorization.
jwtClient.authorize((err, tokens) => {
    if (err) {
        console.log(err);
        return;
    }
    // Declare the ID of the order to update.
    const orderId = '<UNIQUE_MERCHANT_ORDER_ID>';

    const orderUpdate = new OrderUpdate({
        updateMask: [
          'lastUpdateTime',
          'purchase.status',
          'purchase.userVisibleStatusLabel',
        ].join(','),
        order: {
          merchantOrderId: orderId,
          lastUpdateTime: new Date().toISOString(),
          purchase: {
            status: 'DELIVERED',
            userVisibleStatusLabel: 'Order delivered',
          },
        },
        reason: 'Order status updated to delivered.',
    });

    // Set up the PATCH request header and body, including the authorized token
    // and order update.
    const bearer = 'Bearer ' + tokens.access_token;
    const options = {
        method: 'PATCH',
        url: `https://actions.googleapis.com/v3/orders/${orderId}`,
        headers: {
          'Authorization': bearer,
        },
        body: {
          header: {
            'isInSandbox': true,
          },
          orderUpdate,
        },
        json: true,
      };
    // Send the PATCH request to the Orders API.
    request.patch(options, (err, httpResponse, body) => {
        if (err) {
            console.log('There was an error...');
            console.log(err);
            return;
        }
    });
});

ऑर्डर की अन्य स्थितियां मैनेज करना

ऑर्डर एपीआई के वर्शन 3 में, वे अतिरिक्त ऑर्डर की स्थिति की वैल्यू काम करती हैं जो वर्शन 2 में उपलब्ध नहीं थीं. आपको हर लेन-देन के लिए प्रासंगिक हर स्थिति के लिए ऑर्डर के अपडेट भेजने चाहिए.

नीचे दी गई स्थिति की वैल्यू, वर्शन 3 के लिए नई हैं:

  • IN_PREPARATION - ऑर्डर को शिपमेंट/डिलीवरी के लिए तैयार किया जा रहा है. जैसे कि खाना पकाया जा रहा है या कोई आइटम पैक किया जा रहा है.
  • READY_FOR_PICKUP - पाने वाला व्यक्ति ऑर्डर ले सकता है.
  • DELIVERED - ऑर्डर पाने वाले व्यक्ति को डिलीवर कर दिया गया है
  • OUT_OF_STOCK - ऑर्डर में मौजूद एक या उससे ज़्यादा सामान, स्टॉक में नहीं हैं.
  • CHANGE_REQUESTED - उपयोगकर्ता ने ऑर्डर में बदलाव का अनुरोध किया है और बदलाव प्रोसेस किया जा रहा है.

FULFILLED स्टेटस के इस्तेमाल पर रोक लगा दी गई है और इसे DELIVERED से बदल दिया गया है.

Java को v3 पर माइग्रेट करें

Java क्लाइंट लाइब्रेरी का इस्तेमाल करने वाली कार्रवाई को माइग्रेट करने के लिए, यह तरीका अपनाएं.

1. कार्ट असेंबली अपडेट करें

ProposedOrder टाइप को ज़्यादा जानकारी वाले Order ऑब्जेक्ट से बदल दिया गया है. अपने ProposedOrder कार्ट को Order कार्ट में बदलने के लिए JSON रेफ़रंस देखें.

2. पेमेंट पैरामीटर अपडेट करें

ऑर्डर का सुझाव देने वाले चरण में पैसे चुकाने की जानकारी का स्ट्रक्चर, एपीआई के अलग-अलग वर्शन के हिसाब से अलग-अलग होता है.

actions.intent.TRANSACTION_DECISION इंटेंट अनुरोध में, पुराने paymentOptions ऑब्जेक्ट को नए paymentParameters ऑब्जेक्ट से बदलें. ज़्यादातर शामिल फ़ील्ड एक जैसे होते हैं, JSON ऑब्जेक्ट के स्ट्रक्चर में कुछ बदलाव करने के लिए सेव करें.

नीचे दिए गए कोड स्निपेट में उदाहरण के तौर पर, Google Pay के लिए वर्शन 3 का इस्तेमाल करके actions.intent.TRANSACTION_DECISION इंटेंट अनुरोध और तुलना करने के लिए पुराने वर्शन 2 का अनुरोध दिखाया गया है.

Java (v3)
// Create order options
OrderOptionsV3 orderOptions = new OrderOptionsV3()
    .setRequestDeliveryAddress(false)
    .setUserInfoOptions(new UserInfoOptions()
        .setUserInfoProperties(Collections.singletonList("EMAIL")));

// Create presentation options
PresentationOptionsV3 presentationOptions = new PresentationOptionsV3()
    .setActionDisplayName("PLACE_ORDER");

// Create payment parameters
JSONObject merchantInfo = new JSONObject();
merchantInfo.put("merchantName", "Example Merchant");

JSONObject facilitationSpec = new JSONObject();
facilitationSpec.put("apiVersion", 2);
facilitationSpec.put("apiVersionMinor", 0);
facilitationSpec.put("merchantInfo", merchantInfo);

JSONObject allowedPaymentMethod = new JSONObject();
allowedPaymentMethod.put("type", "CARD");

JSONArray allowedAuthMethods = new JSONArray();
allowedAuthMethods.addAll(Arrays.asList("PAN_ONLY", "CRYPTOGRAM_3DS"));
JSONArray allowedCardNetworks = new JSONArray();
allowedCardNetworks.addAll(Arrays.asList("AMEX", "DISCOVER", "JCB", "MASTERCARD", "VISA"));

JSONObject allowedPaymentMethodParameters = new JSONObject();
allowedPaymentMethodParameters.put("allowedAuthMethods", allowedAuthMethods);
allowedPaymentMethodParameters.put("allowedCardNetworks", allowedCardNetworks);

allowedPaymentMethod.put("parameters", allowedPaymentMethodParameters);

JSONObject tokenizationSpecificationParameters = new JSONObject();
tokenizationSpecificationParameters.put("gateway", "example");
tokenizationSpecificationParameters.put("gatewayMerchantId", "exampleGatewayMerchantId");

JSONObject tokenizationSpecification = new JSONObject();
tokenizationSpecification.put("type", "PAYMENT_GATEWAY");
tokenizationSpecification.put("parameters", tokenizationSpecificationParameters);
allowedPaymentMethod.put("tokenizationSpecification", tokenizationSpecification);

JSONArray allowedPaymentMethods = new JSONArray();
allowedPaymentMethods.add(allowedPaymentMethod);

facilitationSpec.put("allowedPaymentMethods", allowedPaymentMethods);

JSONObject transactionInfo = new JSONObject();
transactionInfo.put("totalPriceStatus", "FINAL");
transactionInfo.put("totalPrice", "10.00");
transactionInfo.put("currencyCode", "USD");

facilitationSpec.put("transactionInfo", transactionInfo);

GooglePaymentOption googlePaymentOption = new GooglePaymentOption()
    .setFacilitationSpec(facilitationSpec.toJSONString());

PaymentParameters paymentParameters = new PaymentParameters()
    .setGooglePaymentOption(googlePaymentOption);

// Ask for transaction decision
return getResponseBuilder(request)
    .add("Placeholder for transaction decision text")
    .add(new TransactionDecision()
        .setOrder(order)
        .setOrderOptions(orderOptions)
        .setPresentationOptions(presentationOptions)
        .setPaymentParameters(paymentParameters)
    )
    .build();
Java Old (v2)
OrderOptions orderOptions;
PaymentOptions paymentOptions;

// Setup Google provided payment options
Map<String, String> parameters = new HashMap<>();
parameters.put("gateway", "stripe");
parameters.put("stripe:publishableKey", request.isInSandbox() ? "pk_test_key" : "pk_live_key");
parameters.put("stripe:version", "2017-04-06");
PaymentMethodTokenizationParameters tokenizationParameters =
    new PaymentMethodTokenizationParameters()
        .setTokenizationType("PAYMENT_GATEWAY")
        .setParameters(parameters);
orderOptions = new OrderOptions().setRequestDeliveryAddress(false);
GoogleProvidedPaymentOptions googleProvidedPaymentOptions =
    new GoogleProvidedPaymentOptions()
        .setPrepaidCardDisallowed(false)
        .setSupportedCardNetworks(Arrays.asList("VISA", "AMEX"))
        .setTokenizationParameters(tokenizationParameters);
paymentOptions = new PaymentOptions().setGoogleProvidedOptions(googleProvidedPaymentOptions);

return getResponseBuilder(request)
    .add("Placeholder for transaction decision text")
    .add(
        new TransactionDecision()
            .setOrderOptions(orderOptions)
            .setPaymentOptions(paymentOptions)
            .setProposedOrder(proposedOrder))
    .build();
JSON नया (v3)

ध्यान दें कि नीचे दिया गया Dialogflow JSON कोड, वेबहुक रिस्पॉन्स के बारे में बताता है.

{
  "payload": {
    "google": {
      "expectUserResponse": true,
      "systemIntent": {
        "intent": "actions.intent.TRANSACTION_DECISION",
        "data": {
          "@type": "type.googleapis.com/google.actions.transactions.v3.TransactionDecisionValueSpec",
          "orderOptions": {
            "requestDeliveryAddress": "false"
          },
          "paymentParameters": {
            "googlePaymentOption": {
              "facilitationSpec": "{\"apiVersion\":2,\"apiVersionMinor\":0,\"merchantInfo\":{\"merchantName\":\"Example Merchant\"},\"allowedPaymentMethods\":[{\"type\":\"CARD\",\"parameters\":{\"allowedAuthMethods\":[\"PAN_ONLY\",\"CRYPTOGRAM_3DS\"],\"allowedCardNetworks\":[\"AMEX\",\"DISCOVER\",\"JCB\",\"MASTERCARD\",\"VISA\"]},\"tokenizationSpecification\":{\"type\":\"PAYMENT_GATEWAY\",\"parameters\":{\"gateway\":\"example\",\"gatewayMerchantId\":\"exampleGatewayMerchantId\"}}}],\"transactionInfo\":{\"totalPriceStatus\":\"FINAL\",\"totalPrice\":\"10.00\",\"currencyCode\":\"USD\"}}"
            }
          },
          "presentationOptions": {
            "actionDisplayName": "PLACE_ORDER"
          },
          "order": {
            "createTime": "2019-08-01T17:12:13.765Z",
            "lastUpdateTime": "2019-08-01T17:12:13.765Z",
            "merchantOrderId": "UNIQUE_ORDER_ID",
            "userVisibleOrderId": "USER_VISIBLE_ORDER_ID",
            "transactionMerchant": {
              "id": "http://www.example.com",
              "name": "Example Merchant"
            },
            "contents": {
              "lineItems": [
                {
                  "id": "LINE_ITEM_ID",
                  "name": "Pizza",
                  "description": "A four cheese pizza.",
                  "priceAttributes": [
                    {
                      "type": "REGULAR",
                      "name": "Line Item Price",
                      "state": "ACTUAL",
                      "amount": {
                        "currencyCode": "USD",
                        "amountInMicros": 8990000
                      },
                      "taxIncluded": true
                    }
                  ],
                  "notes": [
                    "Extra cheese."
                  ],
                  "purchase": {
                    "quantity": 1,
                    "unitMeasure": {
                      "measure": 1,
                      "unit": "POUND"
                    },
                    "itemOptions": [
                      {
                        "id": "ITEM_OPTION_ID",
                        "name": "Pepperoni",
                        "prices": [
                          {
                            "type": "REGULAR",
                            "state": "ACTUAL",
                            "name": "Item Price",
                            "amount": {
                              "currencyCode": "USD",
                              "amountInMicros": 1000000
                            },
                            "taxIncluded": true
                          }
                        ],
                        "note": "Extra pepperoni",
                        "quantity": 1,
                        "subOptions": []
                      }
                    ]
                  }
                }
              ]
            },
            "buyerInfo": {
              "email": "janedoe@gmail.com",
              "firstName": "Jane",
              "lastName": "Doe",
              "displayName": "Jane Doe"
            },
            "priceAttributes": [
              {
                "type": "TOTAL",
                "name": "Total Price",
                "state": "ESTIMATE",
                "amount": {
                  "currencyCode": "USD",
                  "amountInMicros": 15770000
                },
                "taxIncluded": true
              },
              {
                "type": "TAX",
                "name": "Tax",
                "state": "ESTIMATE",
                "amount": {
                  "currencyCode": "USD",
                  "amountInMicros": 3780000
                },
                "taxIncluded": true
              },
              {
                "type": "SUBTOTAL",
                "name": "Subtotal",
                "state": "ESTIMATE",
                "amount": {
                  "currencyCode": "USD",
                  "amountInMicros": 9990000
                },
                "taxIncluded": true
              },
              {
                "type": "DELIVERY",
                "name": "Delivery",
                "state": "ACTUAL",
                "amount": {
                  "currencyCode": "USD",
                  "amountInMicros": 2000000
                },
                "taxIncluded": true
              }
            ],
            "followUpActions": [
              {
                "type": "VIEW_DETAILS",
                "title": "View details",
                "openUrlAction": {
                  "url": "http://example.com"
                }
              },
              {
                "type": "CALL",
                "title": "Call us",
                "openUrlAction": {
                  "url": "tel:+16501112222"
                }
              },
              {
                "type": "EMAIL",
                "title": "Email us",
                "openUrlAction": {
                  "url": "mailto:person@example.com"
                }
              }
            ],
            "termsOfServiceUrl": "www.example.com",
            "note": "Sale event",
            "promotions": [
              {
                "coupon": "COUPON_CODE"
              }
            ],
            "purchase": {
              "status": "CREATED",
              "userVisibleStatusLabel": "CREATED",
              "type": "FOOD",
              "returnsInfo": {
                "isReturnable": false,
                "daysToReturn": 1,
                "policyUrl": "http://www.example.com"
              },
              "fulfillmentInfo": {
                "id": "FULFILLMENT_SERVICE_ID",
                "fulfillmentType": "DELIVERY",
                "expectedFulfillmentTime": {
                  "timeIso8601": "2017-01-16T01:30:15.01Z"
                },
                "location": {
                  "zipCode": "94086",
                  "city": "Sunnyvale",
                  "postalAddress": {
                    "regionCode": "US",
                    "postalCode": "94086",
                    "administrativeArea": "CA",
                    "locality": "Sunnyvale",
                    "addressLines": [
                      "222, Some other Street"
                    ]
                  }
                },
                "price": {
                  "type": "REGULAR",
                  "name": "Delivery Price",
                  "state": "ACTUAL",
                  "amount": {
                    "currencyCode": "USD",
                    "amountInMicros": 2000000
                  },
                  "taxIncluded": true
                },
                "fulfillmentContact": {
                  "email": "johnjohnson@gmail.com",
                  "firstName": "John",
                  "lastName": "Johnson",
                  "displayName": "John Johnson"
                }
              },
              "purchaseLocationType": "ONLINE_PURCHASE"
            }
          }
        }
      }
    }
  }
}
JSON पुराना (v2)

ध्यान दें कि नीचे दिया गया Dialogflow JSON कोड, वेबहुक रिस्पॉन्स के बारे में बताता है.

{
  "payload": {
    "google": {
      "expectUserResponse": true,
      "systemIntent": {
        "intent": "actions.intent.TRANSACTION_DECISION",
        "data": {
          "@type": "type.googleapis.com/google.actions.v2.TransactionDecisionValueSpec",
          "orderOptions": {
            "requestDeliveryAddress": false
          },
          "paymentOptions": {
            "googleProvidedOptions": {
              "prepaidCardDisallowed": false,
              "supportedCardNetworks": [
                "VISA",
                "AMEX",
                "DISCOVER",
                "MASTERCARD"
              ],
              "tokenizationParameters": {
                "tokenizationType": "PAYMENT_GATEWAY",
                "parameters": {
                  "gateway": "stripe",
                  "stripe:publishableKey": "pk_test_key",
                  "stripe:version": "2018-11-08"
                }
              }
            }
          },
          "proposedOrder": {
            "id": "UNIQUE_ORDER_ID222",
            "cart": {
              "merchant": {
                "id": "book_store_id",
                "name": "A Book Store"
              },
              "lineItems": [
                {
                  "name": "My Memoirs",
                  "id": "mymemoirs_id",
                  "price": {
                    "amount": {
                      "currencyCode": "USD",
                      "nanos": 990000000,
                      "units": 8
                    },
                    "type": "ACTUAL"
                  },
                  "quantity": 1,
                  "subLines": [
                    {
                      "note": "By Bestselling Novelist"
                    }
                  ],
                  "type": "REGULAR"
                },
                {
                  "name": "Biography",
                  "id": "biography_id",
                  "price": {
                    "amount": {
                      "currencyCode": "USD",
                      "nanos": 990000000,
                      "units": 10
                    },
                    "type": "ACTUAL"
                  },
                  "quantity": 1,
                  "subLines": [
                    {
                      "note": "Signed copy"
                    }
                  ],
                  "type": "REGULAR"
                }
              ],
              "notes": "Sale event",
              "otherItems": []
            },
            "otherItems": [
              {
                "name": "Subtotal",
                "id": "subtotal",
                "price": {
                  "amount": {
                    "currencyCode": "USD",
                    "nanos": 980000000,
                    "units": 19
                  },
                  "type": "ESTIMATE"
                },
                "type": "SUBTOTAL"
              },
              {
                "name": "Tax",
                "id": "tax",
                "price": {
                  "amount": {
                    "currencyCode": "USD",
                    "nanos": 780000000,
                    "units": 2
                  },
                  "type": "ESTIMATE"
                },
                "type": "TAX"
              }
            ],
            "totalPrice": {
              "amount": {
                "currencyCode": "USD",
                "nanos": 760000000,
                "units": 22
              },
              "type": "ESTIMATE"
            }
          }
        }
      }
    }
  }
}

3. ऑर्डर एपीआई की मदद से अपडेट भेजें

ऑर्डर एपीआई का वर्शन 3, ऑर्डर से जुड़े अपडेट मैनेज करता है. इसलिए, अब आपको Actions API पर पोस्ट अनुरोध भेजने की ज़रूरत नहीं है. इसके बजाय, ऑर्डर एपीआई को एक पैच अनुरोध भेजा जाता है, जो आपके Order ऑब्जेक्ट का कॉन्टेंट अपडेट करता है.

नया बेयर टोकन वापस पाएं

आपके पास उसी JSON सेवा खाते की कुंजी का इस्तेमाल करने का विकल्प होता है जिसका इस्तेमाल आपने Actions API के लिए, बीयरर टोकन पाने के लिए किया था. आपको ऑर्डर एपीआई के लिए, एक नए बेयरर टोकन का अनुरोध करना होगा. Google API क्लाइंट लाइब्रेरी और "https://www.googleapis.com/auth/actions.order.developer" दायरे का इस्तेमाल करके, बेयरर टोकन के लिए अपनी सेवा कुंजी की अदला-बदली करें.

एपीआई क्लाइंट लाइब्रेरी के GitHub पेज पर, आपको इंस्टॉल करने के तरीके और उदाहरण मिल सकते हैं. ऑर्डर एपीआई की कुंजी एक्सचेंज के उदाहरण के लिए, हमारे Java सैंपल में अपडेट किए गए order-update.js का रेफ़रंस भी दिया जा सकता है.

अपडेट भेजें

ऑर्डर एपीआई की मदद से ऑर्डर अपडेट भेजने का तरीका, Actions API की मदद से अपडेट भेजने जैसा है. हालांकि, इसमें पोस्ट अनुरोध के बजाय PATCH अनुरोध भेजा जाता है. PATCH अनुरोध को JSON फ़ॉर्मैट में, इस फ़ॉर्मैट में भेजा जाना चाहिए:

{ "orderUpdate": OrderUpdate" }

OrderUpdate का फ़ॉर्मैट, वर्शन 3 में भी अलग है. पैच करने के अनुरोध का रेफ़रंस देखें और ज़रूरत के मुताबिक अपने OrderUpdate फ़ील्ड अपडेट करें. नीचे दिया गया कोड स्निपेट, PATCH अनुरोध का एक उदाहरण दिखाता है, जो किसी ऑर्डर के स्टेटस को "DELIVERED" में अपडेट करता है:

JavaScript
// Create order update
FieldMask fieldMask = FieldMask.newBuilder().addAllPaths(Arrays.asList(
    "last_update_time",
    "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);

ऑर्डर की अन्य स्थितियां मैनेज करना

ऑर्डर एपीआई के वर्शन 3 में, वे अतिरिक्त ऑर्डर की स्थिति की वैल्यू काम करती हैं जो वर्शन 2 में उपलब्ध नहीं थीं. आपको हर लेन-देन के लिए प्रासंगिक हर स्थिति के लिए ऑर्डर के अपडेट भेजने चाहिए.

नीचे दी गई स्थिति की वैल्यू, वर्शन 3 के लिए नई हैं:

  • IN_PREPARATION - ऑर्डर को शिपमेंट/डिलीवरी के लिए तैयार किया जा रहा है. जैसे कि खाना पकाया जा रहा है या कोई आइटम पैक किया जा रहा है.
  • READY_FOR_PICKUP - पाने वाला व्यक्ति ऑर्डर ले सकता है.
  • DELIVERED - ऑर्डर पाने वाले व्यक्ति को डिलीवर कर दिया गया है
  • OUT_OF_STOCK - ऑर्डर में मौजूद एक या उससे ज़्यादा सामान, स्टॉक में नहीं हैं.
  • CHANGE_REQUESTED - उपयोगकर्ता ने ऑर्डर में बदलाव का अनुरोध किया है और बदलाव प्रोसेस किया जा रहा है.

FULFILLED स्टेटस के इस्तेमाल पर रोक लगा दी गई है और इसे DELIVERED से बदल दिया गया है.