PHP - PayPal 集成 - 向多个帐户付款

Posted

技术标签:

【中文标题】PHP - PayPal 集成 - 向多个帐户付款【英文标题】:PHP - PayPal integration - Payments to multiple accounts 【发布时间】:2012-03-10 09:19:30 【问题描述】:

我和另一位程序员已经为此工作了几天,没有任何乐趣。在这一点上,我们想要完成的只是接收来自 PayPal 的成功响应。不幸的是,PayPal API 的文档很糟糕。

这是我们的代码:

    <?php
    class Paypal 
            /**
            * Last error message(s)
            * @var array
            */
            protected $_errors = array();

            /**
            * API Credentials
            * Use the correct credentials for the environment in use (Live / Sandbox)
            * @var array
            */
            protected $_credentials = array(
                    'USER' => '*************************',
                    'PWD' => '****************',
                    'SIGNATURE' => '************************************************',
            );

            /**
            * API endpoint
            * Live - https://api-3t.paypal.com/nvp
            * Sandbox - https://api-3t.sandbox.paypal.com/nvp
            * @var string
            */
            protected $_endPoint = 'https://svcs.sandbox.paypal.com/AdaptivePayments/Pay';

            /**
            * API Version
            * @var string
            */
            protected $_version = '74.0';


            public function newstartpayment() 
                    //set PayPal Endpoint to sandbox
                    $url = trim("https://svcs.sandbox.paypal.com/AdaptivePayments/Pay");

            //PayPal API Credentials
            $API_UserName = $_credentials['USER'];
            $API_Password = $_credentials['PWD'];
            $API_Signature = $_credentials['SIGNATURE'];

            //Default App ID for Sandbox    
            $API_AppID = "******************";

            $API_RequestFormat = "NV";
            $API_ResponseFormat = "NV";

            //Create request payload with minimum required parameters
            $bodyparams = array (
                    "requestEnvelope.errorLanguage" => "en_US",
                    "actionType" => "PAY",
                    "currencyCode" => "USD",
                    "cancelUrl" => "http://www.paypal.com",
                    "returnUrl" => "http://www.paypal.com",
                    "receiverList.receiver(0).email" => "************@paypal.com", //TODO
                    "receiverList.receiver(0).amount" => "40", //TODO
                    "receiverList.receiver(0).primary" => "true", //TODO
                    "receiverList.receiver(1).email" => "****************@paypal.com", //TODO
                    "receiverList.receiver(1).amount" => "30", //TODO
                    "receiverList.receiver(1).primary" => "false", //TODO
                    'USER' => '************************',
                    'PWD' => '***********',
                    'SIGNATURE' => '*************************************'
            );

            // convert payload array into url encoded query string
            $body_data = http_build_query($bodyparams, "", chr(38));

            try
            
                    //create request and add headers
                    $params = array("http" => array( 
                    "method" => "POST",
                    "content" => $body_data,
                    "header" => "X-PAYPAL-SECURITY-USERID: " . $API_UserName . "\r\n" .
                    "X-PAYPAL-SECURITY-SIGNATURE: " . $API_Signature . "\r\n" .
                    "X-PAYPAL-SECURITY-PASSWORD: " . $API_Password . "\r\n" .
                    "X-PAYPAL-APPLICATION-ID: " . $API_AppID . "\r\n" .
                    "X-PAYPAL-REQUEST-DATA-FORMAT: " . $API_RequestFormat . "\r\n" .
                    "X-PAYPAL-RESPONSE-DATA-FORMAT: " . $API_ResponseFormat . "\r\n" 
                    ));

                    //create stream context
                    $ctx = stream_context_create($params);

                    //open the stream and send request
                    $fp = @fopen($url, "r", false, $ctx);

                    //get response
                    $response = stream_get_contents($fp);

                    //check to see if stream is open
                    if ($response === false) 
                            throw new Exception("php error message = " . $php_errormsg);
                    

                    //close the stream
                    fclose($fp);

                    return $response;
            
                    catch (Exception $e) 
                            error_log($e->getMessage());
                            print_r($e);
                            return false;
                    
    

    $paypal = new Paypal();

    ?>

好的,现在执行这段代码,我们得到以下错误:

Paypal 返回:2012-02-19T15:07:01.883-08:00Failureff3b99b56fcb72486531520003PLATFORMApplicationErrorApplicationAuthentication 失败。 API 凭据不正确。

我们已经走到了尽头,请上帝保佑..

【问题讨论】:

"API 凭据不正确。" 错误似乎很明显。 API 凭据彻底检查了 1000 次,它们都是正确的。 它们是来自真实账户的凭据吗?这对沙盒端点不起作用,反之亦然。通过developer.paypal.com > 测试账户 > 预配置获取测试卖家账户,并改用这些 API 凭据。 【参考方案1】:

您正在使用 pypal 沙箱或实时 paypal API 凭据。

【讨论】:

沙盒。我们正在沙盒中进行测试。所以帐户是沙盒

以上是关于PHP - PayPal 集成 - 向多个帐户付款的主要内容,如果未能解决你的问题,请参考以下文章

如何从 iOS 应用程序内的单个企业帐户向多个 paypal 或 venmo 或支票帐户付款

多个接收方和一个发送方的 Paypal 集成

使用 PHP 自动执行 PayPal 付款

将 PayPal 集成为付款方式 - 管理不同的帐户

如何将paypal与laravel集成

PHP:贝宝集成