Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
751 views
in Technique[技术] by (71.8m points)

php - Not able to place the order using paypal in magento 2 rest

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...