Android-Cant create order using woocommerce api

两盒软妹~` 提交于 2019-12-25 10:21:28

问题


Cant create order using WooCommerce API

The android code (okhttp) is

RequestBody formBody = new MultipartBody.Builder()
        .setType(MultipartBody.FORM)
        .addFormDataPart(oauthConsumerKeyString, oauthConsumerKeyStringValue)
        .addFormDataPart(oauthNonceKeyString, oauthNonceKeyValue)
        .addFormDataPart(oauthSignatureMethodKey, oauthSignatureMethodKeyValue)
        .addFormDataPart(oauthTimestampKeyString, oauthTimeStampKeyStringValue)
        .addFormDataPart("oauth_signature", signature)
        .addFormDataPart("orders", postDataString)
        .build();
Request request = new Request.Builder()
        .url(urlOrders)
        .post(formBody)
        .build();

Notice the part of

addFormDataPart("orders", postDataString).

If this part is removed then the order is created without details like address, userinfo, price, product id and the like. So all the parameters are working all right.

Now when the above part is not omitted, the order does not get created and error is shown as:

Invalid signature - provided signature does not match.

The variable postDataString contains the following JSON data

{
    "payment_method": "bacs",
    "payment_method_title": "Direct Bank Transfer",
    "set_paid": true,
    "billing": {
        "first_name": "John",
        "last_name": "Doe",
        "address_1": "969 Market",
        "address_2": "",
        "city": "San Francisco",
        "state": "CA",
        "postcode": "94103",
        "country": "US",
        "email": "john.doe@example.com",
        "phone": "(555) 555-5555"
    },
    "shipping": {
        "first_name": "John",
        "last_name": "Doe",
        "address_1": "969 Market",
        "address_2": "",
        "city": "San Francisco",
        "state": "CA",
        "postcode": "94103",
        "country": "US"
    },
    "line_items": [
        {
            "product_id": "341",
            "quantity": "1"
        },
        {
            "product_id": "339",
            "quantity": "1"
        }
    ],
    "shipping_lines": {
        "method_id": "flat_rate",
        "method_title": "Flat Rate",
        "total": 10
    }
}

What is wrong with this code. Thanks for your time. If any doubt please comment.


回答1:


Am writing this answer.

Note:- As of now,have seen many questions like this are on stackoverflow and no answer is provided.Many have earned tumbleweed.:)

Lets create a coupon resource.

The below method is the same whether it is for creating coupon or orders.Taking coupon as an example as it has a short json parameter.

The coupon json to be posted is

{"code":"asdfas"}

The minimum parameter for creating a coupon resource is the "code" parameter.Other parameters are optional.So am using the minimum json possible to create a coupon.

The url should be as

http://pro.....epo.net/scoop/wp-json/wc/v2/coupons?oauth_consumer_key=ck_2f53925cb6d2c8.....f118d01ed80e&oauth_timestamp=1492154063&oauth_nonce=JqYIfq&oauth_signature_method=HMAC-SHA1&oauth_signature=FC1lJ8Vzw.....B86UGlAoWA=

Many of you who can list resources can also presumbly create correct signatures.So not documenting the signature creation here.Can look to the many other resources out there.

Now getting json media type

MediaType JSON = MediaType.parse("application/json; charset=utf-8");

Now the final code (Am using okhttp3)

okhttp3.RequestBody body = RequestBody.create(JSON, dataCouponJsonObject.toString());
okhttp3.Request request = new okhttp3.Request.Builder().url(the url given above)
                .post(body)
                .build();
response = client.newCall(request).execute();

And then voila! resources are created :)

Note: 2 days gone and then arrived at this answer. If any doubt please comment.



来源:https://stackoverflow.com/questions/43348471/android-cant-create-order-using-woocommerce-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!