如何将 PayPal 智能支付按钮与 PHP V2 的 REST API SDK 结合起来?

     2023-02-22     178

关键词:

【中文标题】如何将 PayPal 智能支付按钮与 PHP V2 的 REST API SDK 结合起来?【英文标题】:How to combine PayPal Smart Payment Button with REST API SDK for PHP V2? 【发布时间】:2020-11-29 06:18:00 【问题描述】:

在我包含默认代码 (https://developer.paypal.com/demo/checkout/#/pattern/server) 并将其更改如下后,我的问题就开始了:

<?php

session_start();
require_once '../inc/config.inc.php';

echo '
<!DOCTYPE html>

<head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
</head>

<body>

<div id="paypal-button-container"></div>

<script src="https://www.paypal.com/sdk/js?client-id=' . PAYPAL_CLIENT_ID . '&currency=EUR&disable-funding=credit,card"></script>
';
?>

<script>
    // Render the PayPal button into #paypal-button-container
    paypal.Buttons(
        // Call your server to set up the transaction
        createOrder: function(data, actions) 
            return fetch('01_payPalCheckout.php', 
                mode: "no-cors",
                method: 'post',
                headers: 
                    'content-type': 'application/json'
                
            ).then(function(res) 
                return res.json();
            ).then(function(orderData) 
                return orderData.id;
            );
        ,

        // Call your server to finalize the transaction
        onApprove: function(data, actions) 
            return fetch('01_checkout.php?orderId=' + data.orderID, 
                method: 'post'
            ).then(function(res) 
                return res.json();
            ).then(function(orderData) 
                var errorDetail = Array.isArray(orderData.details) && orderData.details[0];

                if (errorDetail && errorDetail.issue === 'INSTRUMENT_DECLINED') 
                    // Recoverable state, see: "Handle Funding Failures"
                    // https://developer.paypal.com/docs/checkout/integration-features/funding-failure/
                    return actions.restart();
                

                if (errorDetail) 
                    var msg = 'Sorry, your transaction could not be processed.';
                    if (errorDetail.description) msg += '\n\n' + errorDetail.description;
                    if (orderData.debug_id) msg += ' (' + orderData.debug_id + ')';
                    // Show a failure message
                    return alert(msg);
                

                // Show a success message to the buyer
                alert('Transaction completed by ' + orderData.payer.name.given_name);
            );
        


    ).render('#paypal-button-container');
</script>

<?php

echo '
</body>
</html>
';

?>

在 createOrder 中,我调用了 01_payPalCheckout.php,它的结构与 PHP SDK 中指定的一样:

<?php

use PayPalCheckoutSdk\Orders\OrdersCreateRequest;

session_start();
require_once '../inc/config.inc.php';

# 1: Environment, Client
$environment = new SandboxEnvironment(PAYPAL_CLIENT_ID, PAYPAL_SECRET);
$client = new PayPalHttpClient($environment);


# 2: Request Order
$request = new OrdersCreateRequest();
$request->prefer('return=representation');
$request->body = [
    "intent" => "CAPTURE",
    "purchase_units" => [[
        "reference_id" => "test_ref_id1",
        "amount" => [
            "value" => "100.00",
            "currency_code" => "USD"
        ]
    ]],
    "application_context" => [
        "cancel_url" => "https://example.com/cancel",
        "return_url" => "https://example.com/return"
    ]
];

try 

    // Call API with your client and get a response for your call
    $response = $client->execute($request);

    // JSON-Encodierung
    $response = json_encode($response);

    // If call returns body in response, you can get the deserialized version from the result attribute of the response
    return $response;

 catch (HttpException $ex) 

    echo $ex->statusCode;
    print_r($ex->getMessage());
    exit();


我在返回 $response 之前添加了 json_encode();生成的 JSON 代码是以下代码:


    "statusCode": 201,
    "result": 
        "id": "4H218056YS3363904",
        "intent": "CAPTURE",
        "status": "CREATED",
        "purchase_units": [
            
                "reference_id": "test_ref_id1",
                "amount": 
                    "currency_code": "USD",
                    "value": "100.00"
                ,
                "payee": 
                    "email_address": "info-facilitator@24960324320.de",
                    "merchant_id": "BYWLB3T6SPG54"
                
            
        ],
        "create_time": "2020-08-10T08:33:40Z",
        "links": [
            
                "href": "https:\/\/api.sandbox.paypal.com\/v2\/checkout\/orders\/4H218056YS3363904",
                "rel": "self",
                "method": "GET"
            ,
            
                "href": "https:\/\/www.sandbox.paypal.com\/checkoutnow?token=4H218056YS3363904",
                "rel": "approve",
                "method": "GET"
            ,
            
                "href": "https:\/\/api.sandbox.paypal.com\/v2\/checkout\/orders\/4H218056YS3363904",
                "rel": "update",
                "method": "PATCH"
            ,
            
                "href": "https:\/\/api.sandbox.paypal.com\/v2\/checkout\/orders\/4H218056YS3363904\/capture",
                "rel": "capture",
                "method": "POST"
            
        ]
    ,
    "headers": 
        "": "",
        "Cache-Control": "max-age=0, no-cache, no-store, must-revalidate",
        "Content-Length": "747",
        "Content-Type": "application\/json",
        "Date": "Mon, 10 Aug 2020 08",
        "Paypal-Debug-Id": "1b04a05438898"
    

在 onApprove 中我调用“'01_payPalCapture.php?orderId=' + data.orderID”(这里我也使用了https://github.com/paypal/Checkout-PHP-SDK提供的标准方法:

<?php

// Session starten
session_start();

use PayPalCheckoutSdk\Core\PayPalHttpClient;
use PayPalCheckoutSdk\Core\SandboxEnvironment;
use PayPalCheckoutSdk\Orders\OrdersCaptureRequest;

// Konfigurations-Datei einbinden
require_once '../inc/config.inc.php';


# 1: Environment, Client
$environment = new SandboxEnvironment(PAYPAL_CLIENT_ID, PAYPAL_SECRET);
$client = new PayPalHttpClient($environment);


# 2 Capture
$request = new OrdersCaptureRequest($_GET['orderId']);
$request->prefer('return=representation');

try 
    $response = $client->execute($request);
    $response = array('id' => $response->result->id);
    $response = json_encode($response);
    return $response;  // value of return: "id":"2KY036458M157715J"
catch (HttpException $ex) 
    echo $ex->statusCode;
    print_r($ex->getMessage());

在 01_cart.php 中,智能按钮会根据需要呈现,但单击“PayPal”只会导致错误消息,例如update_client_config_error 等

我认为从我的角度理解这两个脚本如何协同工作存在问题。

提前感谢您的提示和帮助(我已经连续解决这个问题 4 天了,我一直在努力通过所有 PayPal 帮助,但在互联网上没有找到关于这个特定问题的任何信息)。

【问题讨论】:

试试这个链接:developer.paypal.com/docs/checkout/reference/server-integration/… 【参考方案1】:

/httpdocs/01_payPalCheckout.php 路径不好

它必须是可以在您的网络浏览器中加载的

可以在 HTML href 中使用的东西,例如 &lt;a href="/01payPalCheckout.php"&gt;&lt;/a&gt;

在您的浏览器中测试加载 01payPalCheckout.php,确保它正在执行并返回正确的 JSON,然后修复您的客户端代码以指向将在获取时返回该 JSON 的正确路径

【讨论】:

我将 json_encode 添加到 01_payPalCheckout.php 并获得了正确的 JSON 代码(在我看来这是正确的代码) - 我编辑了我的问题以显示生成的 JSON 代码! 不完全是,它正在返回 result.id 并且您的 javascript 只读取 id ...你的意思是我应该只打印 01_payPalCheckout.php (print($result_id)) 中的 id 吗? 不,我的意思是它需要位于对象中的 id,而不是 result -> id 你能不能更明确一点,并给出一个代码示例?谢谢!

PayPal Checkout 与智能支付按钮的集成

】PayPalCheckout与智能支付按钮的集成【英文标题】:PayPalCheckoutIntegrationwithSmartPaymentButtons【发布时间】:2021-01-2800:07:36【问题描述】:我目前正在使用PHP框架Codeigniter4.0.4并尝试添加带有智能支付按钮的PayPalCheckout集成。我以PayPalA... 查看详情

PayPal 智能支付按钮:如何更改语言

】PayPal智能支付按钮:如何更改语言【英文标题】:PayPalSmartPaymentButtons:Howtochangethelanguage【发布时间】:2020-05-2517:13:32【问题描述】:如何更改PayPal智能支付按钮的语言?我当前的代码如下所示:paypal.Buttons(locale:\'en_US\',style:size... 查看详情

通过PayPal智能支付按钮完成支付后如何获取orderid

】通过PayPal智能支付按钮完成支付后如何获取orderid【英文标题】:HowtogetorderidaftercompletingpaymentthroughPayPalSmartPaymentButtons【发布时间】:2020-08-1304:19:10【问题描述】:我在我的网站中集成了PayPal智能支付按钮。我的网站是在PHPcodei... 查看详情

如何在 Rails 中使用 Paypal 智能按钮集成实现 Paypal 支付

】如何在Rails中使用Paypal智能按钮集成实现Paypal支付【英文标题】:HowtoimplementPaypalpayoutswithpaypalsmartbuttonintegrationinrails【发布时间】:2020-11-1105:42:54【问题描述】:我通过使用SmartButtons并在server-side中创建订单,在我的rails应用程... 查看详情

PayPal 智能支付按钮 - 请求数量

...在我的网站上销售单件商品,但我在任何文档中都找不到如何询问数量。这甚至可以通过智能支付按钮API实现吗?我只想让客户能够更改他们想要购买的商品的数量。我不介意它是在表单中还是在PayPal的结帐阶段。到目前为止 查看详情

如何将 Paypal 智能按钮集成到 vue 浏览器扩展 pwa [关闭]

】如何将Paypal智能按钮集成到vue浏览器扩展pwa[关闭]【英文标题】:HowtointegratePaypalsmartbuttonintovuebrowserextensionpwa[closed]【发布时间】:2021-02-2004:49:38【问题描述】:当我在寻找一种将支付和订阅集成到现有vue项目的方法时,我发... 查看详情

如何将paypal自适应支付与cake php 1.3版本集成?

】如何将paypal自适应支付与cakephp1.3版本集成?【英文标题】:HowdoIintegratepaypaladaptivepaymentwithcakephp1.3version?【发布时间】:2013-05-1608:20:03【问题描述】:我在Cakephp1.3中有一个应用程序,我需要集成最新的paypal自适应支付系统。我... 查看详情

Paypal 定期智能支付按钮

】Paypal定期智能支付按钮【英文标题】:PaypalrecurringSmartPaymentButton【发布时间】:2020-03-2304:09:00【问题描述】:是否可以使用Paypal定期付款的智能付款按钮来传递其他参数,例如发票ID。paypal.Buttons(createSubscription:function(data,actions... 查看详情

paypal智能支付按钮显示问题

】paypal智能支付按钮显示问题【英文标题】:paypalsmartpaymentbuttondisplayissue【发布时间】:2020-05-0914:47:50【问题描述】:我在我的应用程序中创建了PayPal智能支付按钮。默认情况下,它显示两个按钮,第一个按钮用于PayPal登录窗口... 查看详情

Paypal 与 reactjs -node.js 集成 - 实施指南

...:2020-09-1909:46:28【问题描述】:PayPal使用智能支付按钮:如何使用Paypal中的智能支付按钮?当我渲染按钮时出现错误,这是我想在这里使用的脚本:https://developer.paypal.com/de 查看详情

如何将 Braintree Paypal 与 iOS 集成?

】如何将BraintreePaypal与iOS集成?【英文标题】:HowtoIntegrateBraintreePaypalwithiOS?【发布时间】:2016-11-0602:14:02【问题描述】:braintreeiOS最新版本SDK是否也用于原生iOS应用支付系统集成,也使用paypal支付?我似乎找不到聪明的Sampleone... 查看详情

智能支付按钮服务器集成

...】:2020-03-0107:26:57【问题描述】:我正在按照这个示例将paypal与我的javawebapp集成:https://developer.paypal.com/docs/checkout/reference/server-integration/set-up-transaction/有一点不同,我想向服 查看详情

使用旧状态的 PayPal 智能支付按钮

】使用旧状态的PayPal智能支付按钮【英文标题】:PayPalSmartPaymentButtonsusingoldState【发布时间】:2020-10-1619:17:05【问题描述】:所以场景是我有一个React+Redux电子商务商店,并且有一个结帐页面,其中列出了购物车中的所有商品。... 查看详情

如何将订单详细信息传递给 PayPal 智能按钮结账

】如何将订单详细信息传递给PayPal智能按钮结账【英文标题】:HowcanIpassorderdetailstoaPayPalsmartbuttoncheckout【发布时间】:2021-03-2912:22:38【问题描述】:我正在使用django构建一个电子商务网站,并寻找一种将订单详细信息传递给商家... 查看详情

如何将 PayPal 智能按钮添加到 Chrome 扩展程序?

】如何将PayPal智能按钮添加到Chrome扩展程序?【英文标题】:HowdoIaddPayPalSmartButtonstoaChromeExtension?【发布时间】:2020-09-2110:02:35【问题描述】:我认为这将是一个简单的2行集成,如here所示。但是在添加正确的CSP以允许在线执行之... 查看详情

使用智能支付按钮(React + Redux)的 PayPal 结帐创建订单问题

】使用智能支付按钮(React+Redux)的PayPal结帐创建订单问题【英文标题】:PayPalCheckoutwithSmartPaymentButtons(React+Redux)createorderproblem【发布时间】:2020-10-2116:59:24【问题描述】:每当我尝试处理付款时,我都会收到422error:Unprocessableenti... 查看详情

PayPal 智能按钮 - 如何编辑重定向 URL

】PayPal智能按钮-如何编辑重定向URL【英文标题】:PayPalSmartButton-HowToEditRedirectUrl【发布时间】:2021-12-1310:16:16【问题描述】:我正在尝试在我的PayPal智能按钮生成的代码中输入重定向url链接,但不知道该怎么做。我希望我能在这... 查看详情

Paypal 支付按钮和 IPN:如何唯一地链接用户?

】Paypal支付按钮和IPN:如何唯一地链接用户?【英文标题】:PaypalpaymentbuttonsandIPN:howtolinkusersupuniquely?【发布时间】:2011-05-1603:47:18【问题描述】:奇怪的是,Paypal网站上的文档并没有很好地涵盖这一点。我们有一个付款按钮,... 查看详情