Order status

swift
orders

#1

how can i change the status of my order? And is it possible to do it without paying from the app i want to do it pay on delivery


#2

Hey Karl :wave:

Great question. Could you please explain the scenario and use case a little better so I can recommend an approach for you to try? :smile:


#3

I have successfully created a cart and then converted it to an order, but in my moltin dashboard the status of my order is incomplete how is it possible to change its status to complete? and most importantly how can i modify my inventory and decrement my stock number from swift?


#4

An order will remain incomplete until it is paid. You can learn more about order object statuses here. Paying for an order can be achieved with our gateways, Stripe and Braintree or manually if using another payment provider. You can learn more about order payments here.

An order does not currently decrement stock levels of a product. You’ll need to make a separate call to our inventory service.


#5

Ah okay but what is the func that i use in swift to decrement the the stock?
i didn’t see any function related to the inventory


#6

Hi Karl,

Unfortunately due to security restrictions the iOS SDK does not allow you to decrement the stock, or perform actions upon that inventory.

You would have to implement a separate service which would be able to manage your stock levels through Moltin, using the Client Credentials authentication, such as a server side Node.JS app, or using cURL commands.

Hope this helps,
Craig


#7

Ah okay. I’ve been trying to change the inventory many times and i failed. Pls can you provide with the code that must be implemented to decrement the inventory! And by Client Credentials authentication you mean the client secret that it is available in my moltin dashboard?


#8

Also this could be done by an http request right?


#9

Hi Karl,

The client credentials does use the client secret available in your dashboard yes.

It can be done using a straight HTTP request you’re right - you can find out how to generate an API token for client credentials here http://docs.moltin.com/?bash#create-a-client-credential-token and then using that token you can call out to decrement inventory using http://docs.moltin.com/?bash#create-a-stock-transaction-for-a-product

Generally we don’t recommend using client credentials anywhere that could potentially be leaked, such as client facing applications. This is because client credentials allows you to do a whole range of actions which could potentially be dangerous. If possible, you should use a server side application to hide this functionality away.

Hope this helps,
Craig


#10

thank you so much this really helped.
but i still have question how can i know what is the bearer my access token so i can use it later in my inventory transactions?


#11

When you make a call to authenticate for client credentials, you’ll get back to access token in that response:

{
  "expires": 1500638876,
  "identifier": "client_credentials",
  "expires_in": 3600,
  "access_token": "232405fa7d09c11f736aacf3f5d0e34ddd340b14",
  "token_type": "Bearer"
}

You can then use this when you make calls to inventory later on by adding the header "Authorization: Bearer XXXX, replacing XXXX with the access token that was returned.


#12

Okay
i wrote this script and run it from the terminal and i got the access token, but how am i supposed to run it from my swift code? And to access this response to get the bearer?


Sorry for all these questions but i am getting confused xD


#13

Hi Karl,

I’d recommend calling this using URLSession in Swift. You can pass along data in the body using a URLRequest, so you’d pass in the data you’ve got in that screenshot, and then you’ll get back Data, which you can convert to JSON to get the bearer. You’ll then be able to make additional calls to the Moltin API directly using URLSession and URLRequest, rather than through the SDK.

This gist has a decent example, but you can also find more info on the Apple developer site.


#14

Please can you show what is the url exactly to get the access token and to delete from the inventory that i have to call? and if possible to have a an example on how to do it?
Thank you


#15

The URL for authentication is the one you’ve got in your screenshot above, but all that information can be found in API docs also:

The docs will tell you exactly what parameters you need to send for each API call, and which URL to send them too.

Your requests would look something like this (this one is for authentication specifically):

var request = URLRequest(url: authURL)
request.httpMethod = "POST"
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")

let data = "client_id=\(clientID)&client_secret=\(clientSecret)&grant_type=client_credentials".data(using: .utf8, allowLossyConversion: false)
request.httpBody = data

URLSession.shared.dataTask(with: request) { (data, response, error) in {
   ...
}

The data variable can then be parsed to get the access_token, which can be used in other requests such as decrementing inventory. For those requests, you’ll want to include the bearer header, as well as send data via JSON:

request.setValue("Bearer \(token)", forHTTPHeaderField: "Authorization")
request.setValue("application/json", forHTTPHeaderField: "Accept")
request.setValue("application/json", forHTTPHeaderField: "Content-Type")

request.httpBody = JSONSerialization.data(withJSONObject: payload, options: [])

#16

Thank you so much for your help it solved my issue and got the access token and this is my code to get it


But after getting my access token i am calling a url request to decrement my inventory but nothing is happening and i dont know what is the problem

please cab you tell me where is my mistake? Or if even i am calling the url correctly? and if not how to do it
Thank you again for baring with me :smiley:


#17

Hi Karl,

There’s two parts that could be an issue from what I can see here, but if you could let me also know what the error is printing out, that would also help.

  • Remove the line request.setValue("application/https://... - this may be intefering with the addValue later down the line.
  • Instead of setting your httpBody as a Data representation of the payload, you should use JSONSerialization in order to make sure your data is sent as JSON, e.g:
request.httpBody = try JSONSerialization.data(withJSONObject: payload, options: [])

Let me know if this helps,
Craig


#18

Hi Craig,
I just did as told me to do but this is still not working


as you can see in my debugger it is printing decrement and the access token that i am getting i am not getting any kind of error but nothing is happening to my product and the inventory is still the same. I don’t know what is the problem
You really helped me to get the access token if it is possible that you can show me the code of how to call this request to decrement from the inventory.
Thank you again for always replying and helping!


#19

Hey Karl,

It may be that your call is hitting the error, but due to your if / else statement, you’re not seeing the error be printed out. It’ll be good to see what each of those values (data, response, error) actually are.

print(response)

if let error = error {
   print(error.localizedDescription)
}

if let responseData = try? JSONSerialization.jsonObject(with: data, options: []) {
    print(responseData)
}

This will let you debug further and find out what is being returned from the API. I’ve also been told by my colleague that in order to get up to date stock information for a product, you may need to call this API. I’d suggest testing the above code first, and then once you’ve got that returning a response correctly, have a look at that API.


#20

Hello Craig,
Thank you so much for always helping! It finally worked. You are going to laugh at me but my mistake was that in my json project the type was stock_transaction instead of stock-transaction :sweat_smile:
that drove me crazy