Additional charge at a later time

How can I use the API to post an additional charge on a customers account?

In one case, we don't know the shipping cost ahead of time. I'm thinking we charge $0 for shipping and use the authorize only setting for that account. We can then authorize the charge when the customer agrees to the additional shipping cost. After capturing the original charge, we still need to be able to charge the additional fee.

Would this have to be a completely separate transaction?

Also I read somewhere in the docs that the only way to create a transaction is with a cart. Does that mean I have to create a $0 cart with a shipping fee, then post it to the transactions endpoint to get my new shipping only transaction?

It'd be nice to have some examples of creating transactions, but I couldn't find any.

Thank you.
Comments
  • lukeluke FoxyCart Team
    Hello Raul.

    To create a transaction via the hAPI, you simply POST to the cart uri with a customer_id or set the customer_id on the cart via a PATCH or PUT before POSTing no data to it. Information on that can be found here: https://api.foxycart.com/rels/cart

    We're working towards auth captures, voids, refunds, and more in the future, but that support will be gateway specific and will be rolled out as we finish up the first example gateway which is being tested by our team now. Until that functionality exists, a new transaction would need to be created by creating the cart with the contents you want, setting the customer_id for the cart of a customer who has a valid, saved payment card already in our system, and POSTing to the cart uri.

    Does that help?

    Here's an example of what that might look like once you've created the cart with the cart items you want:

    curl -k -i -H "FOXY-API-VERSION: 1" -H "Authorization: Bearer a922dc94we97e7789078a907ec433fa12345678" -d "customer_id=47556" https://api.foxycart.com/carts/4857116

    HTTP/1.1 201 Created
    Server: nginx/1.4.6 (Ubuntu)
    Content-Type: application/hal+json
    Transfer-Encoding: chunked
    Connection: keep-alive
    X-Powered-By: PHP/5.5.9-1ubuntu4.14
    Cache-Control: private, must-revalidate
    Date: Wed, 11 Jan 2017 03:20:47 GMT
    Location: https://api.foxycart.com/transactions/4857116
    Vary: FOXY-API-VERSION, If-None-Match, Accept
    ETag: W/"1010af5e6d4151500e6b479a4a6f201201fba9bb"

    {
    "_links": {
    "curies": [
    {
    "name": "fx",
    "href": "https://api.foxycart.com/rels/{rel}",
    "templated": true
    }
    ],
    "self": {
    "href": "https://api.foxycart.com/transactions/4857116",
    "title": "This Transaction"
    },
    "fx:attributes": {
    "href": "https://api.foxycart.com/transactions/4857116/attributes",
    "title": "Attributes for this Transaction"
    },
    "fx:store": {
    "href": "https://api.foxycart.com/stores/66",
    "title": "This Store"
    },
    "fx:customer": {
    "href": "https://api.foxycart.com/customers/47556",
    "title": "This Customer"
    },
    "fx:items": {
    "href": "https://api.foxycart.com/transactions/4857116/items",
    "title": "The Items for this Transaction"
    },
    "fx:payments": {
    "href": "https://api.foxycart.com/transactions/4857116/payments",
    "title": "The Payments for this Transaction"
    },
    "fx:applied_taxes": {
    "href": "https://api.foxycart.com/transactions/4857116/applied_taxes",
    "title": "The Applied Taxes for this Transaction"
    },
    "fx:custom_fields": {
    "href": "https://api.foxycart.com/transactions/4857116/transaction_custom_fields",
    "title": "The Custom Fields for this Transaction"
    },
    "fx:discounts": {
    "href": "https://api.foxycart.com/transactions/4857116/discounts",
    "title": "The Discounts for this Transaction"
    },
    "fx:shipments": {
    "href": "https://api.foxycart.com/transactions/4857116/shipments",
    "title": "The Shipments for this Transaction"
    },
    "fx:billing_addresses": {
    "href": "https://api.foxycart.com/transactions/4857116/billing_addresses",
    "title": "The Billing Addresses for this Transaction"
    },
    "fx:process_webhook": {
    "href": "https://api.foxycart.com/transactions/4857116/process_webhook",
    "title": "POST here to resend the webhook notification for this transaction"
    },
    "fx:send_emails": {
    "href": "https://api.foxycart.com/transactions/4857116/send_emails",
    "title": "POST here to resend emails for this transaction."
    }
    },
    "message": "transaction 4857116 created successfully."
    }
  • RaulRaul Member
    That'll work. Since this is a new (shipping only) charge, I'm creating a cart, then adding a 0$ item with the shipping cost. I then tie it to the customer by using the link from the previous transaction. I got all of this working in the hal-browser, and am now implementing it in my code base.

    Thanks for the help. I'll update this thread if I run into trouble.
  • RaulRaul Member
    After coding it all up in PHP I get a "400 Bad Request" when trying to post to the final cart and get the transaction. The message says "Please select a shipping method". The cart nor the transaction has a shipping method so I don't see where it would go.

    Also, I didn't run into this problem using the sandbox in the hal browser so it could be related to settings on my store.

    Do you know what I have to do to get past this error?

    Thanks.
  • lukeluke FoxyCart Team
    If you want to whisper me specific details for your store, I can look into this in more detail, but most likely you'll need to use a product category for the line item you're adding (the shipping cost) which doesn't require shipping. That may sound a bit backwards, but most likely your default product category requires a shipping rate to be selected and since there's no interface for doing that via the hAPI, you'll have to use a "no shipping" product category. Does that help?
  • RaulRaul Member
    Ah i see. I tried it with a different store that has no required shipping under the default category and it worked. Since we are only planning on using this feature for stores with a single category. It won't be much of a problem. In the future I can write it to search for a category with a specific name and no shipping to use that as the category.

    Thank you.
Sign In or Register to comment.