Magento 2-使用REST API创建订单


24

我需要使用Magento REST API从移动客户端创建订单。就我而言,移动端将直接使用PayPal SDK实施付款。我需要做的是通过将付款方式设置为汇票来创建订单并执行客人结帐。我应该使用哪个API来实现这一目标?

Answers:


27

我终于弄明白了。这是我在做什么。

取得一个产品

curl -g -X GET "$base_url/index.php/rest/V1/products/24-MB05/" \
-H "Authorization: Bearer $token" 

创建购物车

curl -g -X POST "$base_url/index.php/rest/V1/guest-carts/" \
-H "Authorization: Bearer $token" 

取得购物车

curl -g -X GET "$base_url/index.php/rest/V1/guest-carts/56241bf6bc084cd7589426c8754fc9c5" \
-H "Authorization: Bearer $token" 

将产品添加到购物车

curl -g -X POST "$base_url/index.php/rest/V1/guest-carts/56241bf6bc084cd7589426c8754fc9c5/items" \
-H "Authorization: Bearer $token" \
-H "Content-Type:application/json" \
 -d '{ "cartItem": { "quote_id": "56241bf6bc084cd7589426c8754fc9c5", "sku": "24-MB05", "qty": 1 } }'

添加运送信息

curl -g -X POST "$base_url/index.php/rest/V1/guest-carts/56241bf6bc084cd7589426c8754fc9c5/shipping-information" \
    -H "Authorization: Bearer $token" \
    -H "Content-Type:application/json" \
     -d '
{
    "addressInformation": {
        "shippingAddress": {
            "region": "MH",
            "region_id": 0,
            "country_id": "IN",
            "street": [
                "Chakala,Kalyan (e)"
            ],
            "company": "abc",
            "telephone": "1111111",
            "postcode": "12223",
            "city": "Mumbai",
            "firstname": "Sameer",
            "lastname": "Sawant",
            "email": "abc@abc.com",
            "prefix": "address_",
            "region_code": "MH",
            "sameAsBilling": 1
        },
        "billingAddress": {
            "region": "MH",
            "region_id": 0,
            "country_id": "IN",
            "street": [
                "Chakala,Kalyan (e)"
            ],
            "company": "abc",
            "telephone": "1111111",
            "postcode": "12223",
            "city": "Mumbai",
            "firstname": "Sameer",
            "lastname": "Sawant",
            "email": "abc@abc.com",
            "prefix": "address_",
            "region_code": "MH"
        },
        "shipping_method_code": "flatrate",
        "shipping_carrier_code": "flatrate"
    }
}
 '

获取付款方式

curl -g -X GET "$base_url/index.php/rest/V1/guest-carts/56241bf6bc084cd7589426c8754fc9c5/payment-information" \
    -H "Authorization: Bearer $token" 

下订单

curl -g -X PUT "$base_url/index.php/rest/V1/guest-carts/56241bf6bc084cd7589426c8754fc9c5/order" \
    -H "Authorization: Bearer $token" \
    -H "Content-Type:application/json" \
     -d '
{
    "paymentMethod": {
        "method": "checkmo"
    }
}'                      

1
如果有人想通过php尝试此操作,我根据您的回答创建了一个示例php脚本:github.com/acolono/php-magento-api-sandbox
Nebel54 '17

1
@zzpaul,如何使用付款方式通过上述方式传递信用卡数据?
Rakesh Jesadiya

6

我认为有一个小错误:要在身体上下订单,必须将payMethod作为第一个键,例如:

{
    "paymentMethod": { 
        "method": "checkmo" 
    }
}

4
  1. 创建空的购物车网址:http:// www。[您的网站] .com / rest / V1 / carts / mine 通话:发布回复:cartID,例如:4290

  2. 将商品添加到购物车网址:http:// www。[您的网站] .com / rest / V1 / carts / mine /商品 正文:

    {"cartItem":{
        "sku":"JFCO00017",
        "qty":1,
        "name":"Devil May Cry III 3 Dante",
        "price":81.55,
        "product_type":"simple",
        "quote_id":"4290",
        "product_option":
            {"extension_attributes":
               {
                 "custom_options":[
                  {"option_id":"thumbnail",
             "option_value":"\/d\/e\/devilmaycryiii3dantecosplay_1_.jpg"
               },
               {
                 "option_id":"color_2",
                 "option_value":"Red"
               },
               {
                "option_id":"google_size",
                "option_value":"xxs"}]
           }
        }
      }
    }
  3. 添加计费信息网址:http:// www。[您的网站] .com / rest / V1 / carts / mine / billing-address 正文:

    {
    "address": {
    "city": "Springfield",
    "company": "iprag",
    "countryId": "IN",
    "email": "customer_email@domain.com",
    "firstname": "Jane",
    "lastname": "Doe",
    "postcode": "90210",
    "region": "UP",
    "saveInAddressBook": 1,
    "street": ["Street"],
    "telephone": "5551234"
    },
    "useForShipping": true
    }
  4. 获取送货方法网址:http:// www。[yoursite] .com / rest / V1 / carts / mine / shipping-methods

    {
    "carrier_code": "flatrate",
    "method_code": "flatrate",
    "carrier_title": "Flat Rate",
    "method_title": "Fixed",
    "amount": 10,
    "base_amount": 10,
    "available": true,
    "error_message": "",
    "price_excl_tax": 10,
    "price_incl_tax": 10

    }

  5. 添加运输信息网址:http:// www。[您的网站] .com / rest / V1 / carts / mine / shipping-信息 主体:

    {
     "addressInformation": {
     "billingAddress": {
        "city": "Springfield",
        "company": "iprag",
        "email": "customer_email@domain.com",
        "firstname": "Jane",
        "lastname": "Doe",
        "postcode": "335001",
        "region": "UP",
        "street": ["Street"],
        "telephone": "5551234"
    },
    "shippingAddress": {
        "city": "Springfield",
        "company": "iprag",
        "email": "customer_email@domain.com",
        "firstname": "Jane",
        "lastname": "Doe",
        "postcode": "335001",
        "region": "UP",
        "street": ["Street"],
        "telephone": "5551234"
      },
      "shippingCarrierCode": "flatrate",
      "shippingMethodCode": "flatrate"
    }
    }

回复:付款方式和购物车详细信息

  1. 订单网址:http:// www。[您的网站] .com / rest / V1 / carts / mine / order 正文:

    {
     "paymentMethod":{"method":"checkmo"},
     "shippingMethod":
        {
          "method_code":"flatrate",
    
          "carrier_code":"flatrate",
          "additionalProperties":{}
    
        }
    
    }

回应:orderid


如何使用付款方式通过上述方式传递信用卡数据?
Rakesh Jesadiya

@RakeshJesadiya,有一个Magento rest api调用,以获取所有可用的付款方式以获取列表并下订单以替换上述方式。
Manish

你能抬起头,让我知道
questions/

@paul您是否已将REST API与PayPal Pro和Express一起使用创建了订单?
Ketan Panchal,

@KetanPanchal No
Manish

0

有一个官方教程展示如何使通过REST API的顺序:
订单处理教程Magento的2.2

其中包括非常详细的步骤:

  1. 配置商店
  2. 获取管理员令牌
  3. 建立客户
  4. 创建报价
  5. 将商品添加到购物车
  6. 准备结帐
  7. 创建订单
  8. 创建发票
  9. 创建货件
  10. 发放部分退款

该教程包括如何添加不同种类的产品,不同的运输方式以及带有示例代码的许多其他有用信息。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.