13

I am making REST API calls to magento website for order placement. Below is the sequence of my calls.

  1. Create a cart rest/V1/guest-carts/
  2. Get the cart rest/V1/guest-carts/bae0af147b83f1561f66cc4e3c97916e
  3. Add products to cart rest/V1/guest-carts/24/items
  4. Add billing information, set payment method and place order rest/V1/guest-carts/24/payment-information

In this sequence the order is getting the placed with order status "pending". I am wondering how the payment is captured through api if any payment gateway(Ex. PayPal) is involved to process payment and generate invoice.

Is there any api missing in the above flow for capturing payments.

1 Answer 1

13
+50

I have used a fresh installation for the following using Magento2 CE 2.2.0-dev, and left the default "Luma" theme as well.

Concerning external payment methods (gateways) such as MiGS, Braintree, PayPal, and others, Magento supports these gateways out of the box but provides NO APIs for them that you can use along with other "Checkout" APIs.

So, if you are planning to build a checkout app/page using Magento's APIs, you will need to manually integrate with these services using their SDK/API, fortunately, you can learn a lot by checking how Magento integrates with these services in the built-in integrations.

To understand this, i had to configure Braintree since it is the easiest:

  1. Create sandbox account at https://www.braintreepayments.com/sandbox
  2. Access Magento Admin Area.
  3. Stores > Configuration > Sales > Payment Methods > Braintree > Configure.
  4. Make sure "Environment" is "Sandbox", and enter "Merchant ID", "Public Key", and "Private Key".
  5. Save Config.
  6. You may need to reindex and/or refresh cache.

Upon adding a product to the cart and proceeding to checkout, in the last step, choose the Braintree payment method, then after clicking the "Place Order" button, you will notice the following AJAX requests:

  1. 2 requests to Braintree API to validate the card, perform the transaction, and responds with transaction status.
  2. A request to Magento's API guest-carts/cartId/payment-information with the usual body as explained in Magento's API documentation.

After that the process continues as expected with redirection to success page when the last request responds in JSON containing the order entity_id

enter image description here

This confirms that calls to external services -Braintree in our example- is performed by the checkout page NOT by Magento internally, so we will need to do the same if we are to develop our own checkout page/app.

As for controlling the order status, i dug deeper and found out that using payment methods like MiGS, Braintree, or PayPal, resulted in an order with status processing instead of the usual pending, i suspected that this is either a payment-method configuration or an observer, but it appeared to be a configuration, here are more details.

Class: Magento\Sales\Model\Order\Payment

Method: Place()

There is a conditional that checks if Initialization is required for the method if ($methodInstance->isInitializeNeeded()), if that's the case, a method initialize() is executed which you define, and allows you to specify both state and status.

5
  • can you share the payload for the payment-information api
    – blakcaps
    Sep 19, 2016 at 6:18
  • 1
    {"cartId":"a98697cba964d6ba0d3b64d78a31df33","billingAddress":{"countryId":"EG","regionId":null,"region":"","street":["street",""],"company":"my company","telephone":"12346987","postcode":"12345","city":"city","firstname":"f name","lastname":"l name","saveInAddressBook":null},"paymentMethod":{"method":"braintree","additional_data":{"payment_method_nonce":"ef27df81-7c29-4a74-9049-15de33f1f49f"}},"email":"[email protected]"} Sep 19, 2016 at 8:37
  • Thanks for the payload. I am wondering, how to generate "payment_method_nonce".
    – blakcaps
    Sep 20, 2016 at 6:47
  • Don't really know but i think by inspecting requests back and forth that nonce can be easily discovered, if it really functions as a nonce. Sep 20, 2016 at 7:54
  • 1
    Braintree seems easy to integrate throught REST API. But how to integrate the external redirection payment gateway such as Papal or Ingenico ? May 16, 2017 at 6:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.