简体   繁体   English

PayPal react native 中的集成和支付状态

[英]PayPal Integration and payment status in react native

I'm trying to integrate PayPal as a payment method in my react-native app.我正在尝试将 PayPal 作为付款方式集成到我的 react-native 应用程序中。 I get an access token by hitting https://api.sandbox.paypal.com/v1/oauth2/token API, but when I'm trying to pass access token to payment API https://api.sandbox.paypal.com/v1/payments/payment got response with status code 400 or 500 both are varying. I get an access token by hitting https://api.sandbox.paypal.com/v1/oauth2/token API, but when I'm trying to pass access token to payment API https://api.sandbox.paypal.com/v1/payments/payment得到状态码为 400 或 500 的响应都不同。

My code is我的代码是

  let currency = '100 INR'
currency.replace("INR", "")

const dataDetail = {
    "intent": "sale",
    "payer": {
        "payment_method": "paypal"
    },
    "transactions": [{
        "amount": {
            "total": currency,
            "currency": "INR",
            "details": {
                "subtotal": currency,
                "tax": "0",
                "shipping": "0",
                "handling_fee": "0",
                "shipping_discount": "0",
                "insurance": "0"
            }
        }
    }],
    "redirect_urls": {
        "return_url": "https://example.com",
        "cancel_url": "https://example.com"
    }
}
fetch('https://api.sandbox.paypal.com/v1/oauth2/token', { 
    method: 'POST',
    headers: { 
         'Accept': 'application/json', 
         'Accept-Language': 'en_US',
         'Content-Type': 'application/x-www-form-urlencoded',
         'Authorization': 'Basic ' + btoa('AUrtJxcHfMUlDjHgV2FHMOUnzMkUeu86_km7h67uEHzH5b5RN7Vo-q8AYPtcdz7Iaioc46xW0H9JQZmT:EMbbJ-YqQLT6liuPtJURq2pAgh9WuUTDKmV355_VIeADst0BMlnUNKiHVLK7itCyZFXrEQOex9p93WO8')
    },
    body: 'grant_type=client_credentials'
}).then(response => response.json())
  .then(async (data) => {
    console.log(data.access_token)
    this.setState({accessToken:data.access_token})
    // console.log(JSON.stringify(dataDetail))
    fetch ('https://api.sandbox.paypal.com/v1/payments/payment',
    {
        method: 'POST', 
        headers: {
            'Content-Type': 'application/json',
            'Authorization': 'Bearer '+(this.state.accessToken)
        },
        body:dataDetail
    }
)
    .then(response => {
        console.log('response=====',response)
        // const { id, links } = response.data
        // const approvalUrl = links.find(data => data.rel == "approval_url")

        // this.setState({
        //     paymentId: id,
        //     approvalUrl: approvalUrl.href
        // })
    }).catch(err => {
        console.log({ ...err })
    })
}).catch(function (error) {
    let edata = error.message;
    console.log('Error:', edata)
}
)

Any one can help please?任何人都可以帮忙吗? response回复

 {"_bodyBlob": {"_data": {"__collector": [Object], "blobId": "f2d967a5-2700-4f03-b2ac-2e4ec22f10e4", "offset": 0, "size": 232}}, "_bodyInit": {"_data": {"__collector": [Object], "blobId": "f2d967a5-2700-4f03-b2ac-2e4ec22f10e4", "offset": 0, "size": 232}}, "bodyUsed": false, "headers": {"map": {"cache-control": "max-age=0, no-cache, no-store, must-revalidate", "content-language": "*", "content-length": "232", "content-type": "application/json", "date": "Tue, 23 Feb 2021 07:20:25 GMT", "paypal-debug-id": "27bb91ae40c3a"}}, "ok": false, "status": 400, "statusText": undefined, "type": "default", "url": "https://api.sandbox.paypal.com/v1/payments/payment"}

reference Link PAYPAL INTEGRATION IN REACT-NATIVE参考链接PAYPAL INTEGRATION IN REACT-NATIVE

https://api.sandbox.paypal.com/v1/payments/payment https://api.sandbox.paypal.com/v1/payments/payment

v1/payments is a deprecated API . v1/payments已弃用的 API You should integrate the current v2/checkout/orders instead.您应该整合当前的 v2/checkout/orders。

Here is some documentation -- you want "Create Order" and "Capture Order" API calls. 这是一些文档——您需要“创建订单”和“捕获订单”API 调用。


But, if you want to understand the problems with your v1 code, here they are fixed and with proper request+response data logging但是,如果您想了解您的 v1 代码的问题,在这里它们是固定的,并具有适当的请求+响应数据记录

Issues:问题:

  • currency variable wasn't being set to a valid number w/o spaces currency变量未设置为不带空格的有效数字
  • You need to JSON.stringify() your request body.您需要 JSON.stringify() 您的请求正文。

 <script> let currency = '100 INR'; currency = currency.replace("INR", "").trim(); //this was a problem line, replace() is not in-place. And also extra space to trim let accessToken = ''; const dataDetail = { "intent": "sale", "payer": { "payment_method": "paypal" }, "transactions": [{ "amount": { "total": currency, "currency": "INR", "details": { "subtotal": currency, "tax": "0", "shipping": "0", "handling_fee": "0", "shipping_discount": "0", "insurance": "0" } } }], "redirect_urls": { "return_url": "https://example.com", "cancel_url": "https://example.com" } } fetch('https://api.sandbox.paypal.com/v1/oauth2/token', { method: 'POST', headers: { 'Accept': 'application/json', 'Accept-Language': 'en_US', 'Content-Type': 'application/x-www-form-urlencoded', 'Authorization': 'Basic ' + btoa('AUrtJxcHfMUlDjHgV2FHMOUnzMkUeu86_km7h67uEHzH5b5RN7Vo-q8AYPtcdz7Iaioc46xW0H9JQZmT:EMbbJ-YqQLT6liuPtJURq2pAgh9WuUTDKmV355_VIeADst0BMlnUNKiHVLK7itCyZFXrEQOex9p93WO8') }, body: 'grant_type=client_credentials' }).then(response => response.json()).then(async (data) => { console.log(data.access_token) accessToken=data.access_token // console.log(JSON.stringify(dataDetail)) let createRequest = { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer '+(accessToken) }, body:JSON.stringify(dataDetail) } console.log('Request body string',createRequest.body); console.log('Request body (formatted)', JSON.stringify( JSON.parse(createRequest.body),null,4) ); fetch ('https://api.sandbox.paypal.com/v1/payments/payment',createRequest ).then(function(response) { console.log('Response object', response); return response.json() }).then(async(data) => { console.log('Response data',data); console.log('Response data (formatted)', JSON.stringify(data,null,4) ); }).catch(err => { console.log({...err }) }) }).catch(function (error) { let edata = error.message; console.log('Error:', edata) }) </script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM