How can I post an order into woocommerce using Alamofire? - ios

I am trying to create an order from iOS to woo commerce using Alamofire. I am searching for a proper solution.
After trying to create an order I get this error:
{
code = "woocommerce_rest_cannot_create";
data = {
status = 401;
};
message = "Sorry, you are not allowed to create resources.";
}
Code:
let parameters: [String: AnyObject] = [
"consumer_key":"*******" as AnyObject, // here is my user name
"consumer_secret":"*******" as AnyObject, // here is my secret key
"shipping_total": "120.00" as AnyObject,
"total": "6015.00" as AnyObject,
"customer_id": 0 as AnyObject,
"billing": [
"first_name": "Faizul",
"last_name": "karim",
"company": "somecompany",
"address_1": "someAddress",
"address_2": "someAddress",
"city": "Dhaka",
"state": "Dhaka",
"postcode": "1203",
"country": "bd",
"email": "faizulkarim28#gmail.com",
"phone": "001929838939"
] as AnyObject,
"shipping": [
"first_name": "Faizul",
"last_name": "karim",
"company": "somecompany",
"address_1": "someAddress",
"address_2": "someAddress",
"city": "Dhaka",
"state": "Dhaka",
"postcode": "1203",
"country": "bd",
] as AnyObject,
"line_items":[
"id": 388,
"name": "Mens Casual Blazer - 40",
"product_id": 55677,
"variation_id": 57619,
"quantity": 1,
"tax_class": "",
"subtotal": "5895.00",
"subtotal_tax": "0.00",
"total": "5895.00",
"total_tax": "0.00",
] as AnyObject
]
Alamofire.request("https://infinitymegamall.com/wp-json/wc/v2/orders",method: .post, parameters: parameters)
.responseJSON{ response in
if let json = response.result.value {
print(json)
}
}

Are you sure your API Keys have write permissions? If GET works, it's possible that the key allows read only. Double check the permissions in your Woocommerce API Settings.

Related

How can I post request json body in swift using Alamofire?

I need to make a POST request in the json body for my app backend but the response returns a failure. I assume my json formatting or encoding is wrong but I can't figure out what the problem is. I have attempted a lot of different solutions but I haven't been able to find one that works. Can anyone see which part of my code is responsible for the failure?
let headers: HTTPHeaders = [
"accept": "application/json",
"content-type": "application/json",
"authorization": self.authValue,
"x-iyzi-rnd": self.randomString,
"cache-control": "no-cache"
]
let parameters: [String: Any] = [
"price": "1.0",
"paidPrice": "1.1",
"paymentChannel": "mobile_ios",
"paymentCard": [
"cardHolderName": "card_name",
"cardNumber": "card_no",
"expireYear": "2030",
"expireMonth": "09",
"cvc": "123"
],
"buyer": [
"id": "123123123",
"name": "john",
"surname": "doe",
"identityNumber": "12345678902",
"email": "johndoe#gmail.com",
"registrationAddress": "nidakulegöztepemerdivenköymahborasokno1",
"city": "istanbul",
"country": "turkey",
"ip": "192.168.1.82"
],
"shippingAddress": [
"address": "nidakulegöztepemerdivenköymahborasokno1",
"contactName": "janedoe",
"city": "istanbul",
"country": "turkey"
],
"billingAddress": [
"address": "nidakulegöztepemerdivenköymahborasokno1",
"contactName": "janedoe",
"city": "istanbul",
"country": "turkey"
],
"basketItems": [
[
"id": "321",
"price": "0.3",
"name": "binocular",
"category1": "collectibles",
"itemType": "physical"
],
[
"id": "432",
"price": "0.5",
"name": "gamecode",
"category1": "game",
"itemType": "virtual"
],
[
"id": "543",
"price": "0.2",
"name": "usb",
"category1": "electronics",
"itemType": "physical"
]
],
"currency": "try"
]
Alamofire.request("https://api.iyzipay.com/payment/auth", method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: headers).responseJSON(completionHandler: {
response in response
let jsonResponse = response.result.value as! NSDictionary
print(jsonResponse)
})
Is there any different way to post request json body?

Importing JSON or CSV data to Firestore using Swift

It appears there is still no way to import JSON or CSV files directly to a Firestore database. Many of the suggestions are for JavaScript-based apps that do not translate well to Swift. Does anyone have a good Swift solution for adding data to a Firestore database using JSON/CSV?
//example json
[
{
"name": "Stone Cove Marina Inc",
"email": "NOT IN SAMPLE",
"category": "Docks",
"category2": "Marinas",
"category3": "Dock Builders",
"address": "134 Salt Pond Rd",
"city": "Wakefield",
"state": "RI",
"zip": 2879,
"phone": "(401) 783-8990",
"website": "http://stonecovemarinari.com"
},
{
"name": "Bluehaven Homes",
"email": "NOT IN SAMPLE",
"category": "General Contractors",
"category2": "Home Builders",
"category3": "",
"address": "5701 Time Sq",
"city": "Amarillo",
"state": "TX",
"zip": 79119,
"phone": "(806) 452-2545",
"website": "http://www.bluehavenhomes.com/"
}
]
//here is the database structure
//collection is "businesses"; each "business" gets a document id; within each document id set the data
database.collection("businesses").document().setData(/*data here*/)
You can try
let str = """
[
{
"name": "Stone Cove Marina Inc",
"email": "NOT IN SAMPLE",
"category": "Docks",
"category2": "Marinas",
"category3": "Dock Builders",
"address": "134 Salt Pond Rd",
"city": "Wakefield",
"state": "RI",
"zip": 2879,
"phone": "(401) 783-8990",
"website": "http://stonecovemarinari.com"
},
{
"name": "Bluehaven Homes",
"email": "NOT IN SAMPLE",
"category": "General Contractors",
"category2": "Home Builders",
"category3": "",
"address": "5701 Time Sq",
"city": "Amarillo",
"state": "TX",
"zip": 79119,
"phone": "(806) 452-2545",
"website": "http://www.bluehavenhomes.com/"
}
]
"""
do {
let json = try JSONSerialization.jsonObject(with:str.data(using:.utf8)!, options: []) as! [[String: Any]]
for var i in 0...json.count - 1
{
database.collection("businesses").document().setData(json[i])
}
} catch {
print(error)
}

Post Data Into this type of format?

I am not getting how to POST this type of data into webservice
{
"customer_id":"",
"customer_message": " entered by user",
"discount_amount": "",
"ip_address":"1.0.10.22",
"billing_address": {
"first_name": "hello",
"last_name": "world",
"company": "",
"street_1": "45 W test",
"street_2": "",
"city": "London",
"state": "Texas",
"zip": "123456",
"country": "United States",
"country_iso2": "US",
"phone": "",
"email": "xyz#example.com"
},
"shipping_addresses": [
{
"first_name": "rest",
"last_name": "Mctest",
"company": "Test Address",
"street_1": "rest test",
"street_2": "",
"city": "test",
"state": "test",
"zip": "12345",
"country": "United States",
"country_iso2": "US",
"phone": "",
"email": "xyzer#example.com"
}
],
"products": [
{
"product_id": 5448,
"quantity": 2
}
]
I am not getting how to POST this type of data into webservice.
Please help me
i am doing this type of data now i have to post this type. Can anyone post this type.
let aParam = ["email": Email, "password":Password] as [String:Any]
You are taking parameters as [String: Any], where Any denotes each type of data types. Say String, Array and Dictionary.
For example,
let aParam: [String: Any] = ["customer_id": "",
"customer_message": "",
"billing_address": ["first_name" : "hello",
"last_name" : "world",
"company" : "",
"street_1" : "45 W test"],
"shipping_addresses": [
["first_name" : "hello",
"last_name" : "world",
"company" : "",
"street_1" : "45 W test"]
],
"products": [
["product_id" : 5448,
"quantity" : 2],
["product_id" : 5450,
"quantity" : 1]
]
]
And complex json structure would be handle with content-type as JSON, so:
You just need to update your header as:
let aHeader = ["Content-Type" : "application/json"]
Hope this will help

How to parse JSON without root key

I tried searching but couldn't find any proper solution for this. I am trying to parse JSON using the RestClient gem in Ruby without a root key. When I parse it, it returns blank values.
This is the sample JSON I am trying to parse.
[
{
"id": 1,
"name": "Leanne Graham",
"username": "Bret",
"email": "Sincere#april.biz",
"address": {
"street": "Kulas Light",
"suite": "Apt. 556",
"city": "Gwenborough",
"zipcode": "92998-3874",
"geo": {
"lat": "-37.3159",
"lng": "81.1496"
}
},
"phone": "1-770-736-8031 x56442",
"website": "hildegard.org",
"company": {
"name": "Romaguera-Crona",
"catchPhrase": "Multi-layered client-server neural-net",
"bs": "harness real-time e-markets"
}
},
{
"id": 2,
"name": "Ervin Howell",
"username": "Antonette",
"email": "Shanna#melissa.tv",
"address": {
"street": "Victor Plains",
"suite": "Suite 879",
"city": "Wisokyburgh",
"zipcode": "90566-7771",
"geo": {
"lat": "-43.9509",
"lng": "-34.4618"
}
},
"phone": "010-692-6593 x09125",
"website": "anastasia.net",
"company": {
"name": "Deckow-Crist",
"catchPhrase": "Proactive didactic contingency",
"bs": "synergize scalable supply-chains"
}
}
]
I get the proper output, but when I try to access specific fields, I get blank output:
require 'rest-client'
output = RestClient.get 'https://jsonplaceholder.typicode.com/users'
puts output
puts output[0]["username"]
I get no output for username.
rest-client does not parse the JSON itself. You need to do this as an explicit step:
require 'rest-client'
response = RestClient.get('https://jsonplaceholder.typicode.com/users')
output = JSON.parse(response.body) # or just JSON.parse(response) would also work
puts output[0]["username"]

Create a dictionary in swift with alamofire

Im trying to create a web request using alamofire in swift , by request object should be like this
{
"warranty": 0,
"descriptions": "string",
"product_name": "string",
"purchase_date": "23/10/2016",
"product_image": "string",
"product_receipt": "string",
"serial_number": "string",
"barcode_image": "string",
"serial_number_image": "string",
"product": {
"id": 1
},
"user": {
"id": 12
}
}
So in order to get this i have put my code as this
let parameters :[String:AnyObject] = [
"warranty":product.warrenty,
"descriptions":product.longDescription,
"product_name":product.initialName,
"purchase_date":product.purchaseDate,
"serial_number":product.serialCode,
"product": [
"id":product.id
],
"user": [
"id":userDefaults.getCustomerId()
]
]
But i when i do the request it seems that server doesn't accept this format , probably the way i assign
"product": {
"id": 1
},
"user": {
"id": 12
}
is not correct , what is the issue in here ? can some one point me the issue that i do here
I had the same issue and the problem was, i was missing the encoding option which you need to set .JSON. Match the below line with yours.
Alamofire.request(.POST, strURL, parameters: parameter as? [String : AnyObject], encoding: .JSON).responseJSON { (response: Response<AnyObject, NSError>) in

Resources