dongqiang2069 2013-09-19 23:12
浏览 57
已采纳

PayPal API无法连接

Here is my situation, I am trying to plug in my PayPal API details into an application that was developed for me. PayPal API details were provided as a placeholder, and it worked fine. When I plugged mine in, however, it would just fail each time. I realize this is extremely vague, and you probably wont be able to help me with so much generalization, however any tips would help! The code is below. In addition, if you know of a way I can get more details about what is going on (an error code, etc) it would also help so I could do more research as well.

Finally, the solution I had come up with before is that I would need to be an upgraded account, however I just need to know whether I need to add a credit card or a bank.

<?php 
class PaymentController extends BaseController {
    public function PPHttpPost($methodName_, $nvpStr_) {

            $PayPalMode         = 'live';
            $PayPalApiUsername  = 'usergame';
            $PayPalApiPassword  = 'passhole';
            $PayPalApiSignature     = 'siggypoo';
            $PayPalCurrencyCode     = 'USD';
            $PayPalReturnURL    = URL::to('/paypal/enter');
            $PayPalCancelURL    = URL::to('/paypal/enter');


            $API_UserName = urlencode($PayPalApiUsername);
            $API_Password = urlencode($PayPalApiPassword);
            $API_Signature = urlencode($PayPalApiSignature);

            $str = '';
            foreach ($nvpStr_ as $key => $nvp) {
                $str .= '&' . $key . '=' . urlencode($nvp);
            }

            if($PayPalMode=='sandbox')
            {
                $paypalmode     =   '.sandbox';
            }
            else
            {
                $paypalmode     =   '';
            }

            $API_Endpoint = "https://api-3t".$paypalmode.".paypal.com/nvp";
            $version = urlencode('76.0');

            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $API_Endpoint);
            curl_setopt($ch, CURLOPT_VERBOSE, 1);

            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);

            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_POST, 1);

            $nvpreq = "METHOD=$methodName_&VERSION=$version&PWD=$API_Password&USER=$API_UserName&SIGNATURE=$API_Signature$str";

            curl_setopt($ch, CURLOPT_POSTFIELDS, $nvpreq);

            $httpResponse = curl_exec($ch);

            if(!$httpResponse) {
                exit("$methodName_ failed: ".curl_error($ch).'('.curl_errno($ch).')');
            }

            $httpResponseAr = explode("&", $httpResponse);

            $httpParsedResponseAr = array();
            foreach ($httpResponseAr as $i => $value) {
                $tmpAr = explode("=", $value);
                if(sizeof($tmpAr) > 1) {
                    $httpParsedResponseAr[$tmpAr[0]] = $tmpAr[1];
                }
            }

            if((0 == sizeof($httpParsedResponseAr)) || !array_key_exists('ACK', $httpParsedResponseAr)) {
                exit("Invalid HTTP Response for POST request($nvpreq) to $API_Endpoint.");
            }

        return $httpParsedResponseAr;
    }

    public function PaymentController() {


        $this->data = array(
            'PAYMENTREQUEST_0_AMT' => Config::get('app.sponsor_price_per_month'),
            'PAYMENTREQUEST_0_CURRENCY' => 'USD',
            'RETURNURL' => URL::to('/paypal/enter'),
            'CANCELURL' => URL::to('/paypal/enter'),
            'NOSHIPPING' => 1,
            'REQCONFIRMSHIPPING' => 0,
            'L_PAYMENTREQUEST_0_ITEMCATEGORY0' => 'Digital', 
            'L_PAYMENTREQUEST_0_NAME0' => '30 Days Sponsorship',
            'L_PAYMENTREQUEST_0_DESC0' => 'Sponsored status on Nae for 30 days.',
            'L_PAYMENTREQUEST_0_AMT0' => Config::get('app.sponsor_price_per_month'),
            'L_PAYMENTREQUEST_0_QTY0' => 1,
            'L_PAYMENTREQUEST_0_TAXAMT0' => 0,
        );
    }

    public function exitpage() {

        if (!Auth::check()) {
            return Redirect::to('/account/login');
        } else {
            $user = Auth::user();
        }

        if (!$server = $user->server) {
            return Redirect::to('/account/server');
        }

        if (strtotime($server->sponsored) > time()) {
            return Redirect::to('/account/server');
        }

        $out = $this->PPHttpPost('SetExpressCheckout', $this->data);

        if (strtoupper($out["ACK"]) == "SUCCESS" || strtoupper($out["ACK"]) == "SUCCESSWITHWARNING") {

            return Redirect::to('https://www.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=' . $out["TOKEN"]);
        } else {

            return View::make('standard')->
                with('title', 'Sponsor Server')->
                nest('content', 'pages.sponsor', array(
                    'level' => 'warning',
                    'message' => 'Error processing payment! You have not been charged, please try again. TOKEN'
                ));

        }
    }

    public function enter() {

        if (!Auth::check()) {
            return Redirect::to('/account/login');
        } else {
            $user = Auth::user();
        }

        if (!$server = $user->server) {
            return Redirect::to('/account/server');
        }

        if (strtotime($server->sponsored) > time()) {
            return Redirect::to('/account/server');
        }

        $data = array(
            'TOKEN' => Input::get('token'),
            'PAYERID' => Input::get('PayerID'),
            'PAYMENTACTION' => 'SALE',
            'AMT' => Config::get('app.sponsor_price_per_month'),
            'CURRENCYCODE' => 'USD'
        );

        $out = $this->PPHttpPost('DoExpressCheckoutPayment', $data);

        if (strtoupper($out["ACK"]) == "SUCCESS" || strtoupper($out["ACK"]) == "SUCCESSWITHWARNING") {

            $server->sponsored = date('Y-m-d H:i:s', time() + (30 * 24 * 60 * 60));
            $server->save();

            return Redirect::to('/account/server');
        } else {
            return View::make('standard')->
                with('title', 'Sponsor Server')->
                nest('content', 'pages.sponsor', array(
                    'level' => 'warning',
                    'message' => 'Error processing payment! You have not been charged, please try again. 2nd'
                ));
        }
    }
}
  • 写回答

1条回答 默认 最新

  • dousi1097 2013-09-20 09:03
    关注

    You were probably mixing up live and sandbox credentials. The code snippet assumes live mode by default.

    As for troubleshooting errors, you can look at the response and error code section and the detailed error code documentation

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来