2013-09-28 72 views
0

我创建一个PayPal支付用PHP,这是到目前为止的代码:宝PHP REST API

$ch = curl_init(); 
     $clientId = "clientID"; 
     $secret = "secret"; 

     curl_setopt($ch, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/oauth2/token"); 
     curl_setopt($ch, CURLOPT_HEADER, false); 
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
     curl_setopt($ch, CURLOPT_POST, true); 
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
     curl_setopt($ch, CURLOPT_USERPWD, $clientId.":".$secret); 
     curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=client_credentials"); 

     $result = curl_exec($ch); 

     if(empty($result))die("Error: No response."); 
     else 
     { 
       $json = json_decode($result); 
        //print_r($json->access_token); 
        if ($_POST['creditcard'] == 'paypal') { 
         $data = '{ 
          "intent":"sale", 
          "redirect_urls":{ 
          "return_url":"http://returnurl.com", 
          "cancel_url":"http://returnurl.com" 
          }, 
          "payer":{ 
          "payment_method":"paypal" 
          }, 
          "transactions":[ 
          { 
          "amount":{ 
          "total":"' . preg_replace("/[^0-9,.]/", "", $order_price) .'", 
          "currency":"' . $currency_code .'" 
          }, 
          "description":"' . ucfirst($type) . ' Logo Purchase. ' . '" 
          } 
          ] 
          }'; 

          curl_setopt($ch, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/payments/payment"); 
          curl_setopt($ch, CURLOPT_POST, true); 
          curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 
          curl_setopt($ch, CURLOPT_HTTPHEADER, array(
          'Content-Type:application/json', 
          "Authorization: Bearer " . $json->access_token, 
          "Content-length: ".strlen($data)) 
          ); 
          print_r($data); 
          $result = curl_exec($ch); 

          if(empty($result))die("Error: No response."); 
          else { 
           $json_output = json_decode($result); 
           print_r($result); 

           if(isset($json_output->{'links'}[2]->{'href'})) 
            $_SESSION['execute_url'] = $json_output->{'links'}[2]->{'href'}; 
           if(isset($json_output->{'links'}[1]->{'href'})) 
            header('Location:' .$json_output->{'links'}[1]->{'href'}); 
          } 

        } 

       } 

     curl_close($ch); 

我签我的测试帐号,我重定向到重定向URL。所以我假设一切工作正常。接下来的部分是我无法弄清楚的。我正在尝试在这里执行付款。

我是否使用从上述以前代码收到的付款网址?

我试着这样做:

      $data = '{ "payer_id" : "' .$_GET['PayerID'] . '" }'; 
          curl_setopt($ch, CURLOPT_URL, $stored_execute_url; 
          curl_setopt($ch, CURLOPT_POST, true); 
          curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 
          curl_setopt($ch, CURLOPT_HTTPHEADER, array(
          'Content-Type:application/json', 
          "Authorization: Bearer " . $_GET['token'], 
          "Content-length: ".strlen($data)) 
          ); 
          print_r($data); 
          $result = curl_exec($ch); 

          if(empty($result))die("Error: No response."); 

所有我得到的是“错误:没有响应”

我一直在关注这个链接https://developer.paypal.com/webapps/developer/docs/integration/web/accept-paypal-payment/,我能够得到,直到最后一部分。

+0

我想通了。我使用PayPal返回网址中给出的令牌。我尝试使用原始的令牌,并购买了。贝宝返回您的网站时,令牌是否有目的? – Marquisk2

回答

0

在对OAuth2端点的调用中返回的访问令牌标识调用者和特权(可以调用哪些api调用)。这是任何api调用(REST)创建付款,执行,退款等所必需的。某些调用需要比其他调用更多的权限,即您需要在创建访问令牌时请求更多信息或提供更多凭据。

URL中返回的令牌标识付款,这是为了当用户重定向回您的站点时,您可以确定哪些付款已重定向回您的站点并确定正确的信息,即执行正确的付款。

0

“授权:持票人”。 $ _GET ['token'],将此令牌替换为之前的令牌,方法是将其存储到会话“Authorization:Bearer”中。 $ json-> access_token,这一个