Ir al contenido
Menú
Se marcó esta pregunta
1 Responder
8320 Vistas

I wrote a simple Flask server that catches JSON data from a Post webhook and creates a sales record within Odoo v13. At the time this solution worked since my Json data only has 2 items that I needed"ItemID" and "ItemQuantity". However, now the Flask server needs to support multiple "ItemID" and "ItemQuantity". I am not sure how to best go about this.

This is the Flask server

from flask import Flask, request, abort
import xmlrpc.client


app = Flask(__name__)


# Tells the server what route and method to use
@app.route('/webhook', methods=['POST'])
def webhook():
    if request.method == 'POST':

 # Cathces the post webhook from 3D Cart and goes in, gets the product sku and the quantity. Assigns them to variables x & quantity

        content = request.json
        changetos = {'700598114683':2,
                     '763736666666':3,
                     '34232323233333':4}
        x = content[0]['OrderItemList'][0]['ItemID']
        x = changetos.get(x, x)
        quantity = content[0]['OrderItemList'][0]['ItemQuantity']
        print(content[0]['OrderItemList'][0]['ItemID'])


# Logs in to Odoo server
        url = ""
        db = "randomDB"
        username = "random"
        password = "random"

        common = xmlrpc.client.ServerProxy('{}/xmlrpc/2/common'.format(url))
        print(common.version())

        uid = common.authenticate(db, username, password, {})
        print(uid)




 # Takes variables that were saved from the JSON post webhook and plugs into the sales record

        so_id = models.execute_kw(db, uid, password, 'sale.order', 'create', [{
            'partner_id': 10,
            'order_line': [(0, 0, {'product_id':x,'product_uom_qty': quantity})]
        }])

        return '', 200
    else:
        abort(400)


if __name__ == '__main__':
    app.run()

This is the json data that will create a sales record for the first "ItemID" and "ItemQuantity", but will not create one for the second instance of "ItemID" and "ItemQuantity".

[
{
    "InvoiceNumberPrefix": "AB-",
    "InvoiceNumber": 1000,
    "OrderID": 1,
    "CustomerID": 1,
    "OrderDate": "2016-03-17T09:52:38",
    "OrderStatusID": 1,
    "LastUpdate": "2016-03-17T09:52:38",
    "UserID": "John Doe",
    "SalesPerson": "Jane Doe",
    "ContinueURL": "http://www.some-url.com",
    "BillingFirstName": "John",
    "BillingLastName": "Doe",
    "BillingCompany": "3dcart",
    "BillingAddress": "6691 Nob Hill Rd.",
    "BillingAddress2": "Suite 100",
    "BillingCity": "Tamarac",
    "BillingState": "FL",
    "BillingZipCode": "33321",
    "BillingCountry": "US",
    "BillingPhoneNumber": "800-828-6650",
    "BillingEmail": "john@doe.com",
    "BillingPaymentMethod": "Online Credit Card",
    "BillingOnLinePayment": "true",
    "BillingPaymentMethodID": "1",
    "ShipmentList": [
      {
        "ShipmentID": 1,
        "ShipmentLastUpdate": "2016-03-17T09:52:38",
        "ShipmentBoxes": 1,
        "ShipmentInternalComment": "Some comment",
        "ShipmentOrderStatus": 4,
        "ShipmentAddress": "123 Main St.",
        "ShipmentAddress2": "Apt 123",
        "ShipmentAlias": "Someone else",
        "ShipmentCity": "Tamarac",
        "ShipmentCompany": "3dcart",
        "ShipmentCost": 12.15,
        "ShipmentCountry": "US",
        "ShipmentEmail": "someone@3dcart.com",
        "ShipmentFirstName": "Someone",
        "ShipmentLastName": "Else",
        "ShipmentMethodID": 1,
        "ShipmentMethodName": "UPS Ground",
        "ShipmentShippedDate": "2016-03-17",
        "ShipmentPhone": "800-555-1212",
        "ShipmentState": "FL",
        "ShipmentZipCode": "33319",
        "ShipmentTax": 0.00,
        "ShipmentWeight": 1.25,
        "ShipmentTrackingCode": "ABC123",
        "ShipmentUserID": "John Doe",
        "ShipmentNumber": 1,
        "ShipmentAddressTypeID": 1
      },
      {
        "ShipmentID": 2,
        "ShipmentLastUpdate": "2016-03-17T09:52:38",
        "ShipmentBoxes": 1,
        "ShipmentInternalComment": "Another comment",
        "ShipmentOrderStatus": "2",
        "ShipmentAddress": "6691 Nob Hill Rd.",
        "ShipmentAddress2": "Suite 100",
        "ShipmentAlias": "John",
        "ShipmentCity": "Tamarac",
        "ShipmentCompany": "3dcart",
        "ShipmentCost": 7.83,
        "ShipmentCountry": "US",
        "ShipmentEmail": "sjohn@doe.com",
        "ShipmentFirstName": "John",
        "ShipmentLastName": "Doe",
        "ShipmentMethodID": 2,
        "ShipmentMethodName": "USPS Priority",
        "ShipmentShippedDate": "",
        "ShipmentPhone": "800-828-6650",
        "ShipmentState": "FL",
        "ShipmentZipCode": "33321",
        "ShipmentTax": 0.00,
        "ShipmentWeight": 1.25,
        "ShipmentTrackingCode": "ABC123",
        "ShipmentUserID": "John Doe",
        "ShipmentNumber": 2,
        "ShipmentAddressTypeID": 2
      }
    ],
    "OrderItemList": [
      {
        "CatalogID": 1,
        "ItemIndexID": 1,
        "ItemID": "700598114683",
        "ItemShipmentID": 1,
        "ItemQuantity": 7,
        "ItemWarehouseID": 1,
        "ItemDescription": "Sample Product",
        "ItemUnitPrice": 9.99,
        "ItemWeight": 1.25,
        "ItemOptionPrice": 0.00,
        "ItemAdditionalField1": "3dcart internal use only",
        "ItemAdditionalField2": "3dcart internal use only",
        "ItemAdditionalField3": "3dcart internal use only",
        "ItemPageAdded": "Sample-Product.html",
        "ItemDateAdded": "2016-03-17T09:52:38",
        "ItemUnitCost": 4.57,
        "ItemUnitStock": 1,
        "ItemOptions": "Comma separated list of option IDs selected when item was added to cart (example: 123,456,789)",
        "ItemCatalogIDOptions": "Comma separated list of bundled item catalog IDs (example: 123,456,789)"
      },
      {
        "CatalogID": 1,
        "ItemIndexID": 1,
        "ItemID": "763736666666",
        "ItemShipmentID": 2,
        "ItemQuantity": 5,
        "ItemWarehouseID": 1,
        "ItemDescription": "Sample Product",
        "ItemUnitPrice": 9.99,
        "ItemWeight": 1.25,
        "ItemOptionPrice": 0.00,
        "ItemAdditionalField1": "3dcart internal use only",
        "ItemAdditionalField2": "3dcart internal use only",
        "ItemAdditionalField3": "3dcart internal use only",
        "ItemPageAdded": "Sample-Product.html",
        "ItemDateAdded": "2016-03-17T09:52:38",
        "ItemUnitCost": 1.1,
        "ItemUnitStock": 1,
        "ItemOptions": "Comma separated list of option IDs selected when item was added to cart (example: 123,456,789)",
        "ItemCatalogIDOptions": "Comma separated list of bundled item catalog IDs (example: 123,456,789)"
      }
    ],
    "OrderDiscount": 0.00,
    "SalesTax": 0.00,
    "SalesTax2": 0.00,
    "SalesTax3": 0.00,
    "OrderAmount": 39.96,
    "AffiliateCommission": 0.00,
    "TransactionList": [
      {
        "TransactionIndexID": 1,
        "OrderID": 22517,
        "TransactionID": "123XXX123",
        "TransactionDateTime": "2016-03-17T09:52:38",
        "TransactionType": "Authorize",
        "TransactionMethod": "AUTHORIZENET",
        "TransactionAmount": 10.99,
        "TransactionApproval": "SXLXXX",
        "TransactionReference": "rlsjSskdjDD/RpGDKHndihdnD",
        "TransactionGatewayID": 1,
        "TransactionCVV2": "CVVRESPONSE",
        "TransactionAVS": "AVSRESPONSE",
        "TransactionResponseText": "Transaction Response Text",
        "TransactionResponseCode": "Transaction Response Code",
        "TransactionCaptured": 1
      },
      {
        "TransactionIndexID": 1,
        "OrderID": 22517,
        "TransactionID": "123XXX123",
        "TransactionDateTime": "2016-03-17T09:52:38",
        "TransactionType": "Authorize",
        "TransactionMethod": "AUTHORIZENET",
        "TransactionAmount": 10.99,
        "TransactionApproval": "SXLXXX",
        "TransactionReference": "rlsjSskdjDD/RpGDKHndihdnD",
        "TransactionGatewayID": 1,
        "TransactionCVV2": "CVVRESPONSE",
        "TransactionAVS": "AVSRESPONSE",
        "TransactionResponseText": "Transaction Response Text",
        "TransactionResponseCode": "Transaction Response Code",
        "TransactionCaptured": 1
      }
    ],
    "CardType": "Visa",
    "CardNumber": "XXXXXXXXXXXX1111",
    "CardName": "John Doe",
    "CardExpirationMonth": "09",
    "CardExpirationYear": "2019",
    "CardIssueNumber": "XXXXXXXXXXXX1111",
    "CardStartMonth": "01",
    "CardStartYear": "1999",
    "CardAddress": "123 Anywhere St., Small Town, 33333",
    "CardVerification": "123",
    "RewardPoints": "",
    "QuestionList": 
    [
      {
        "OrderID": 1,
        "QuestionID": 1,
        "QuestionTitle": "Security Question",
        "QuestionAnswer": "Cat/Dog",
        "QuestionType": "checkbox",
        "QuestionCheckoutStep": 1,
        "QuestionSorting": 1,
        "QuestionDiscountGroup": 1
      },
      {
        "OrderID": 1,
        "QuestionID": 1,
        "QuestionTitle": "Verification Question",
        "QuestionAnswer": "Wet/Dry",
        "QuestionType": "checkbox",
        "QuestionCheckoutStep": 1,
        "QuestionSorting": 1,
        "QuestionDiscountGroup": 1
      }
    ],
    "Referer": "Google",
    "IP": "123.456.789",
    "CustomerComments": "Customer comments at checkout",
    "InternalComments": "Comments added to the order by the merchant",
    "ExternalComments": "Comments by the merchant that the customer can see"
  }
]

Not sure how to best go about this. I have tried using loops but have not had any success

Avatar
Descartar

Hi,

I am struggling with webhook and odoo too and surfing for any ideas. Can you tell me the reason why you have to make another server to catch the webhook and then connect to odoo to do the other stuff? Why can't we just handle the webhook directly via odoo (this is actually the thing I am trying with using odoo controllers but not successful). I don't know whether I am missing something.

Mejor respuesta
so_id = models.execute_kw(db, uid, password, 'sale.order', 'create', [{
            'partner_id': 10, }])
lopp over items x
so_line_ids = models.execute_kw(db, uid, password, 'sale.order', 'create', [{ 'product_id':x.product_id,'product_uom_qty':x.product_uom_qty,order_id:so_id.id or so_id
}])
Not sure about the syntax but this is how it works in REST API first create SO then use it's id in SOL then it will be attached to that SO
Avatar
Descartar
Publicaciones relacionadas Respuestas Vistas Actividad
2
jun 24
6330
2
mar 24
657
1
dic 22
3255
1
jun 21
2195
0
mar 21
2425