用于 PayPal 模块的 Magento 2 REST api

Posted

技术标签:

【中文标题】用于 PayPal 模块的 Magento 2 REST api【英文标题】:Magento 2 REST api for PayPal module 【发布时间】:2019-02-19 20:53:12 【问题描述】:

我正在使用 Magento 2 REST api 作为后端和 Angular 6 在前端技术中创建电子商务应用程序。

我已经成功集成了CART的add/update/delete/list api

我正在使用 Paypal 支付方式,从 PayPal 获得成功响应后,我需要将 PayPal 响应存储到 Magento 2 数据库,但它返回错误。

我需要post请求的参数名称。

错误

 "message": "No such entity with %fieldName = %fieldValue",
"parameters": 
    "fieldName": "cartId",
    "fieldValue": null

发布网址

http://127.0.0.1/magento2/index.php/rest/V1/carts/mine/payment-information

请求数据

    
    "email": "vikram.sharma@xymob.in",
    "cartId": 2,
    "billing_address": 
                    "email": "vikram.sharma@xymob.in",
                "region": "New York",
                "region_id": 43,
                "region_code": "NY",
                    "country_id": "US",
                    "street": ["123 Oak Ave"],
                    "postcode": "10577",
                    "city": "Purchase",
                    "telephone": "512-555-1111",
                    "firstname": "Jane",
                    "lastname": "Doe"
         ,
    "paymentMethod": 
        "method": "paypal_express",
        "additional_data": 
            "paypal_express_payment_payload": "\"id\":\"PAY-3F734824YB370341TLOQNO3Y\",\"intent\":\"sale\",\"state\":\"approved\",\"cart\":\"9W5899822J563802S\",\"create_time\":\"2018-09-18T10:50:31Z\",\"payer\":\"payment_method\":\"paypal\",\"status\":\"VERIFIED\",\"payer_info\":\"email\":\"vikramdummy-buyer@outlook.com\",\"first_name\":\"test\",\"middle_name\":\"test\",\"last_name\":\"buyer\",\"payer_id\":\"CAQR5B2LGGB9S\",\"country_code\":\"US\",\"shipping_address\":\"recipient_name\":\"test buyer\",\"line1\":\"1 Main St\",\"city\":\"San Jose\",\"state\":\"CA\",\"postal_code\":\"95131\",\"country_code\":\"US\",\"transactions\":[\"amount\":\"total\":\"102.00\",\"currency\":\"USD\",\"details\":,\"item_list\":\"items\":[\"name\":\"SATA HardDisk\",\"price\":\"1.00\",\"currency\":\"USD\",\"quantity\":2,\"name\":\"Logitech Mouse\",\"price\":\"100.00\",\"currency\":\"USD\",\"quantity\":1],\"related_resources\":[\"sale\":\"id\":\"7CM65242K9229980W\",\"state\":\"completed\",\"payment_mode\":\"INSTANT_TRANSFER\",\"protection_eligibility\":\"ELIGIBLE\",\"parent_payment\":\"PAY-3F734824YB370341TLOQNO3Y\",\"create_time\":\"2018-09-18T10:50:29Z\",\"update_time\":\"2018-09-18T10:50:29Z\",\"amount\":\"total\":\"102.00\",\"currency\":\"USD\",\"details\":\"subtotal\":\"102.00\"]]",
            "paypal_express_checkout_token": "EC-9W5899822J563802S",
            "paypal_express_checkout_redirect_required": false,
            "paypal_express_checkout_payer_id": "CAQR5B2LGGB9S"
        
    

这是我的参考网址

Rest api for order place , Magento swagger

这是我的邮递员 PHP curl 代码:-

 <?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "http://127.0.0.1/magento2/index.php/rest/V1/carts/mine/payment-information",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => "\n  \"email\": \"vikram.sharma@xymob.in\",\n  \"billing_address\": \n              \"email\": \"vikram.sharma@xymob.in\",\n              \"region\": \"New York\",\n              \"region_id\": 43,\n              \"region_code\": \"NY\",\n              \"country_id\": \"US\",\n              \"street\": [\"123 Oak Ave\"],\n              \"postcode\": \"10577\",\n              \"city\": \"Purchase\",\n              \"telephone\": \"512-555-1111\",\n              \"firstname\": \"Jane\",\n              \"lastname\": \"Doe\"\n     ,\n    \"paymentMethod\": \n    \"method\": \"paypal_express\",\n    \"additional_data\": \n      \"paypal_express_payment_payload\": \"\\\"id\\\":\\\"PAY-3F734824YB370341TLOQNO3Y\\\",\\\"intent\\\":\\\"sale\\\",\\\"state\\\":\\\"approved\\\",\\\"cart\\\":\\\"9W5899822J563802S\\\",\\\"create_time\\\":\\\"2018-09-18T10:50:31Z\\\",\\\"payer\\\":\\\"payment_method\\\":\\\"paypal\\\",\\\"status\\\":\\\"VERIFIED\\\",\\\"payer_info\\\":\\\"email\\\":\\\"vikramdummy-buyer@outlook.com\\\",\\\"first_name\\\":\\\"test\\\",\\\"middle_name\\\":\\\"test\\\",\\\"last_name\\\":\\\"buyer\\\",\\\"payer_id\\\":\\\"CAQR5B2LGGB9S\\\",\\\"country_code\\\":\\\"US\\\",\\\"shipping_address\\\":\\\"recipient_name\\\":\\\"test buyer\\\",\\\"line1\\\":\\\"1 Main St\\\",\\\"city\\\":\\\"San Jose\\\",\\\"state\\\":\\\"CA\\\",\\\"postal_code\\\":\\\"95131\\\",\\\"country_code\\\":\\\"US\\\",\\\"transactions\\\":[\\\"amount\\\":\\\"total\\\":\\\"102.00\\\",\\\"currency\\\":\\\"USD\\\",\\\"details\\\":,\\\"item_list\\\":\\\"items\\\":[\\\"name\\\":\\\"SATA HardDisk\\\",\\\"price\\\":\\\"1.00\\\",\\\"currency\\\":\\\"USD\\\",\\\"quantity\\\":2,\\\"name\\\":\\\"Logitech Mouse\\\",\\\"price\\\":\\\"100.00\\\",\\\"currency\\\":\\\"USD\\\",\\\"quantity\\\":1],\\\"related_resources\\\":[\\\"sale\\\":\\\"id\\\":\\\"7CM65242K9229980W\\\",\\\"state\\\":\\\"completed\\\",\\\"payment_mode\\\":\\\"INSTANT_TRANSFER\\\",\\\"protection_eligibility\\\":\\\"ELIGIBLE\\\",\\\"parent_payment\\\":\\\"PAY-3F734824YB370341TLOQNO3Y\\\",\\\"create_time\\\":\\\"2018-09-18T10:50:29Z\\\",\\\"update_time\\\":\\\"2018-09-18T10:50:29Z\\\",\\\"amount\\\":\\\"total\\\":\\\"102.00\\\",\\\"currency\\\":\\\"USD\\\",\\\"details\\\":\\\"subtotal\\\":\\\"102.00\\\"]]\",\n      \"paypal_express_checkout_token\": \"EC-9W5899822J563802S\",\n      \"paypal_express_checkout_redirect_required\": false,\n      \"paypal_express_checkout_payer_id\": \"CAQR5B2LGGB9S\"\n    \n  \n",
  CURLOPT_HTTPHEADER => array(
    "authorization: Bearer 1fpiepedxeqm6fse81icfv8yx4hmwbi3",
    "cache-control: no-cache",
    "content-type: application/json",
    "postman-token: 85951c6b-ff29-b785-65c9-d10c39a34424"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) 
  echo "cURL Error #:" . $err;
 else 
  echo $response;

【问题讨论】:

为什么要在请求数据中添加email 字段? 我已经更新了请求和响应请检查 你使用POST操作吗?你能说一下你是怎么打电话的吗? 是的,我正在使用 post 方法,我正在使用 postman 【参考方案1】:

尝试使用cart_id,而不是cartId

如果您看到webapi.xml,我们有:

<route url="/V1/carts/mine/payment-information" method="POST">
     <service class="Magento\Checkout\Api\PaymentInformationManagementInterface" method="savePaymentInformationAndPlaceOrder"/>
     <resources>
         <resource ref="self" />
     </resources>
     <data>
         <parameter name="cartId" force="true">%cart_id%</parameter>
     </data>
 </route>

参数部分表示路由从请求中获取cart_id参数,并将其更改为名为@9​​87654326@的php参数。

【讨论】:

我已经尝试了这两个参数,但它不起作用。另外我是登录用户,所以我们不需要发送 cardId 参数。 我真的不明白,因为如果我把参数cartId 放在我的请求数据中,我得到的错误和你一样。如果我输入cart_id,我通过了这个验证。您是否有任何特定模块可以覆盖 Magento Api 代码的某些部分? 一件事我需要知道如何在 magento 控制器中获取我发布的数据 我是 magento 的新手,我需要你的帮助 你的路线进入这个模型函数src/vendor/magento/module-checkout/Model/PaymentInformationManagement.php:76。如果你有你的$cartId 值,你可以在这里查看。

以上是关于用于 PayPal 模块的 Magento 2 REST api的主要内容,如果未能解决你的问题,请参考以下文章

Magento2 - 使用 Paypal 计费协议创建自定义订单(“强制参数缺少 referenceId 错误”)

获取 Paypal Express [Magento] 的完整账单地址

Magento - PayPal IPN 配置

Magento:使用 PayPal Express 时添加支付费用 [关闭]

Magento 1.7.0.2 退款不与 PayPal 同步

Magento 中的 Paypal Payments 高级问题