PHP - PayPal集成 - 向多个账户支付款项

3

我和另一位程序员已经花了几天时间,但仍无法成功地从PayPal获得响应。目前我们所尝试的只是接收来自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月19日T15:07:01.883-08:00失败FF3B99B56FCB72486531520003平台应用程序错误应用身份验证失败。API凭据不正确。

我们已经到了绝境,请帮帮我们..


API凭据不正确。错误很明显。 - user557846
1
API凭据已经被彻底检查了数千次,它们是正确的。 - Craig Barben
这些凭据是来自实际账户吗?那在沙盒端点或反之亦然是行不通的。请通过 https://developer.paypal.com/ > 测试账户 > 预配置 获取测试卖家账户,并使用那些 API 凭据代替。 - Robert
1个回答

0
您正在使用Pypal沙盒或Live Paypal API凭据。

沙盒。我们正在沙盒中进行测试,因此帐户是沙盒。 - Craig Barben

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接