Android-Cant使用woocommerce API创建订单 [英] Android-Cant create order using woocommerce api

查看:127
本文介绍了Android-Cant使用woocommerce API创建订单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用WooCommerce API无法创建订单

android代码(okhttp)是

 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();
 

注意

的一部分

 addFormDataPart("orders", postDataString).
 

如果删除了此部分,则将创建不包含地址,用户信息,价格,产品ID等详细信息的订单.所以所有参数都可以正常工作.

现在,如果不省略上述部分,则不会创建订单,并且错误显示为:

Invalid signature - provided signature does not match.

变量postDataString包含以下JSON数据

{
    "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
    }
}

此代码有什么问题. 谢谢你的时间. 如有疑问,请发表评论.

解决方案

正在编写此答案.

注意:-到目前为止,已经在stackoverflow上看到了很多类似的问题,但没有提供答案.许多人赢得了风滚草.:)

让我们创建优惠券资源.

下面的方法无论是用于创建优惠券还是订单都是相同的,以优惠券为例,因为它具有一个简短的json参数.

要发布的优惠券json是

{"code":"asdfas"}

创建优惠券资源的最小参数是代码"参数.其他参数是可选的.因此,请使用可能的最小json来创建优惠券.

网址应为

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 = FC1lJ8B ...

你们中许多可以列出资源的人也可以大概创建正确的签名.因此,这里没有记录签名的创建.可以查看那里的许多其他资源.

现在获取json媒体类型

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

现在是最终代码(使用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();

然后瞧!资源已创建:)

注意:已经过了2天,然后得出了这个答案. 如有疑问,请发表评论.

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.

解决方案

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.

这篇关于Android-Cant使用woocommerce API创建订单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆