Not able to place the order using paypal in magento 2 rest
Asked Answered
S

4

4

I am developing an e-commerce mobiloe application using magento 2 rest apis only.This is the flow for making the REST API calls for order placement.

1.Create a cart

api -->{{url}}/index.php/rest/V1/carts/mine

This api will return a unique cart id

2.Add products to cart

api --> {{url}}/index.php/rest/V1/carts/mine/items

body ->

{
 "cart_item": {
     "quote_id": cartId,
     "sku": skuName,
     "qty": 1
     }
}

3. Estimate Shipping Methods

url --> {{url}}/index.php/rest/V1/carts/mine/estimate-shipping-methods

body ->

{
    "address": {
        "region": "Trivandrum",
        "region_id": 12,
        "region_code": "CA",
        "country_id": "IN",
        "street": [
            "Amstor house",
            "Eramam"
        ],
        "telephone": "5656565454",
        "postcode": "670390",
        "city": "Kazhakuttam",
        "firstname": "Peter",
        "lastname": "K",
        "same_as_billing": 0,
        "save_in_address_book": 0
    }

}

This will return all possible shipping methods based on shipping address.In my case the result is

[
  {
    "carrier_code": "freeshipping",
    "method_code": "freeshipping",
    "carrier_title": "Free Shipping",
    "method_title": "Free",
    "amount": 0,
    "base_amount": 0,
    "available": true,
    "error_message": "",
    "price_excl_tax": 0,
    "price_incl_tax": 0
  }
]

4)Save shipping information

url --> {{url}}/index.php/rest/V1/carts/mine/shipping-information

body data ->

{
    "addressInformation": {
        "shipping_address": {

            "region": "Trivandrum",
            "region_id": 12,
            "region_code": "CA",
            "country_id": "IN",
            "street": [
                "Amstor house",
                "Eramam"
            ],
            "telephone": "5656565454",
            "postcode": "670390",
            "city": "Kazhakuttam",
            "firstname": "Peter",
            "lastname": "K",

        },
        "billing_address": {
            "region": "Trivandrum",
            "region_id": 12,
            "region_code": "CA",
            "country_id": "IN",
            "street": [
                "Amstor house",
                "Eramam"
            ],
            "telephone": "5656565454",
            "postcode": "670390",
            "city": "Kazhakuttam",
            "firstname": "Peter",
            "lastname": "K",
        },
        "shipping_method_code": "freeshipping",
        "shipping_carrier_code": "freeshipping"


    }
}

This will return all possible payment methods. Here i am using paypal_express for payment.

5. Payment using paypal plugin

Here i will pay the amount using paypal cordova plugin.Also configured the IPN [{{url}}/paypal/ipn/]in paypal account

This api will return the following data,

{
   "client": {
       "environment": "sandbox",
       "paypal_sdk_version": "2.14.4",
       "platform": "Android",
       "product_name": "PayPal-Android-SDK"
   },
   "response": {
       "create_time": "2016-11-19T05:25:46Z",
       "id": "PAY-5VS11410F5341972MLAX6ETA",
       "intent": "sale",
       "state": "approved"
   },
   "response_type": "payment"
}

5.Save payment and place order

url --> {{url}}/index.php/rest/V1/carts/mine/payment-information

data ->

{
    "cartId": 3,
    "billingAddress‌​": {
        "region": "Trivandrum",
        "region_id": 12,
        "region_code": "CA",
        "country_id": "IN",
        "street": [
            "Amstor house",
            "Eramam"
        ],
        "telephone": "5656565454",
        "postcode": "670390",
        "city": "Kazhakuttam",
        "firstname": "Peter",
        "lastname": "K"
    },
    "paymentMethod": {
        "method": "paypal_express"

    }
}

But this api will returning

{
  "message": "PayPal gateway has rejected request. Invalid token (#10410: Invalid token)."
}

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

Stealth answered 15/12, 2016 at 6:46 Comment(2)
sure, I have the same issue.Ferreira
PayPal will send an IPN after receiving a payment. You should use this to place the order...Otherwise your clients can attempt to spoof payments by reverse engineering your app.Breaux
F
1

Paypal Express payment method doesn't support online capturing. There is no way to get a full order creation flow like on Checkout via Magento API interface. It is impossible to change the order state and process payments. As a workaround try the following:

  1. Create a custom payment method
  2. Enable for REST API only(Not on website checkout page)
  3. While making payment using rest api use this method (after successful payment using you android/ios SDK)
  4. After placing the order make send transaction id(PAY-xxxxx) return by paypal sdk payment to save trasaction.(tell your server side tio implement this call).

I am writting a complete atrticle regarding this step by step. I will let you know when it is done.

Ferreira answered 16/12, 2016 at 12:42 Comment(4)
Hi Manish any news regarding your article?Replenish
You can find here : ipragmatech.com/validate-payment-paypal-magento-rest-apiFerreira
Thanks!. Do you have a github repo with code ready to be used?Replenish
Sure, We will add this by the weekend. or message me [email protected] so that I can send you the code.Ferreira
P
1

Place Order by PayPal Rest API

For the Place order by the Paypal rest API, you need an active cart with shipping and billing address

By default, we need to follow the few setups for placing the order

• Step 1. Create an empty cart

• Step 2. Add products to the cart

• Step 3. Set the shipping address

• Step 4. Set billing address

• Step 5. Set the delivery method

• Step 6. Apply a coupon (if you have)

• Step 7. Set the payment method

• Step 8. Place order

After steps 6 follow below APIs

We need to call the below APIs one by one for placing the order with Paypal

Create Paypal Express Token

  • URL : {you website url}/rest/default/V1/paypalapi/createpaypalexpresstoken

  • Method: POST

  • Set Bearer Token in the Hearer (if customer ) for the guest user no need to set it

  • Content-type : JSON

  • Body For guest users:

    { 
       "cart_id":"5QWFYZdyccucvgD2QMLDCp5fhjmaH2xg",
       "cancel_url":"cancel_url",
       "return_url":"return_url" 
    }
    
  • Body For Customer user:

      { 
         "cart_id": 22, 
         "cancel_url": "cancel_url", 
         "return_url": "return_url" 
     }
    
  • You will get the response like this :

      [ 
           { 
              "code": 200, 
              "token": "EC-4MD50688YD296870K",
              "paypal_urls":{ 
                   "start": "https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=EC-4MD50688YD296870K&useraction=commit",
                    "edit": "https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&useraction=continue&token=EC-4MD50688YD296870K" 
               } 
           }]
    

Explanation

  • you need to redirect the customer to the {start URL } and after making the payment, PayPal will redirect the user with two params in the redirect URL "payer_id" and "token" Both values you need to call in the below API

Set Payment Method On Cart

URL : {you website url}/rest/default/V1/paypalapi/setpaymentmethodoncart

  • Method: POST

  • Set Bearer Token in the Hearer (if customer ) for the guest user no
    need to set it

  • Content-type : JSON

  • Body For guest users:

     { 
       "cart_id": "5QWFYZdyccucvgD2QMLDCp5fhjmaH2xg", 
       "payer_id": "9T3GV67ZSL378", 
       "token": "EC-4MD50688YD296870K", 
       "payment_method": "paypal_express" 
    }
    
  • Body For Customer user:

     {
         "cart_id": 22, 
         "payer_id": "9T3GV67ZSL378",
         "token": "EC-4MD50688YD296870K", 
         "payment_method": "paypal_express", 
         "customer_id": 141 
    }
    
  • You will get the response like this :

    [
    { "code": 200, "selected_payment_method": { "code": "paypal_express", "title": "PayPal Express Checkout" } } ]

Place Order

URL : {you website url}/rest/default/V1/paypalapi/placeorder

  • Method: POST

  • Set Bearer Token in the Hearer (if customer ) for the guest user no need to set it

  • Content-type : JSON

  • Body For guest users:

    { "cart_id": "5QWFYZdyccucvgD2QMLDCp5fhjmaH2xg" }

  • Body For Customer user:

    { "cart_id": 22, "customer_id": 141 }

  • You will get the response like this :

    [ { "code": 200, "order_number": 000000142 } ]

Here is the link to the Paypal Rest API module

https://github.com/santosh-gaggle/paypal-rest-api

Panegyrize answered 8/3, 2022 at 18:23 Comment(0)
M
0

In case someone still looking the solution.

In the time I'm answering this, You will need to create a Magento 2 module to process the payment ID.

After you receive the response from in example Paypal android SDK.

Below is the JSON format that you can send to Magento endpoint :

  • for logged user : PUT /V1/carts/mine/order
  • for guest : PUT /V1/guest-carts/:cartId/order

Referrence : http://devdocs.magento.com/swagger

The "paypal_express_payment_payload" is just a custom attribute to hold the paypal payment response previously from android SDK.

{
    "paymentMethod": {
        "method": "paypal_express",
        "additional_data": {
            "paypal_express_payment_payload": "{\"create_time\":\"2017-06-15T23:13:52Z\",\"id\":\"PAY-2LB41725NU654612TLFBRIUQ\",\"intent\":\"sale\",\"state\":\"approved\"}"
        }
    }
}

To process the "paypal_express_payment_payload" data, you can implement a Interceptor in your Magento 2 module :

di.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Paypal\Model\Express">
        <plugin name="mymodule_magento_paypal_model_express_plugin" 
                type="Mycompanyorpersonal\Mymodule\Plugin\Paypal\Model\Express" 
                sortOrder="99999" 
                disabled="false" />
    </type>
</config>

Mycompanyorpersonal\Mymodule\Plugin\Paypal\Model\Express.php

You can find the full PHP codes in my following gist : https://gist.github.com/feelinc/de817030e00adc7ff7001de1807c1835

Milford answered 16/6, 2017 at 5:24 Comment(0)
P
0

If you use the below to run a post query replace runPostQuery with your curl request. this will pass a token that already has been successful to magento 2.

 $payment['paymentMethod'] = ['method' =>'paypal_express',
               'additional_data' => array (
                   'paypal_express_checkout_token' => $request->query->get('token'),
                   'paypal_express_checkout_redirect_required' => false,
                   'paypal_express_checkout_payer_id' => $request->query->get('PayerID')
               )];


           $completedPayment = $this->runPostQuery('carts/mine/payment-information', $headers, json_encode($payment));

You will need to create a plugin to add the last transaction id to the payment see the above comment, but the above payload to payment-information will allow you to get past _placeOrder function in Paypal\Model\Express.php

The paypal_express_checkout_token is the token passed back to the browser from paypal same as PayerId this allows to check the payment, which will return successful and not require a redirect, but is not the payment reference just the action token.

Papaya answered 18/8, 2018 at 17:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.