2013-10-09 6 views
2

필자는 curl에 익숙하지 않아 Paypal 개발자 블로그에있는 코드를 사용하고 있으며, 저를 위해 일하는 데 어려움을 겪고 있습니다. 여기에 내가Paypal REST API 임 플리 멘 테이션

class paypal { 
    private $access_token; 
    private $token_type; 

    /** 
    * Constructor 
    * 
    * Handles oauth 2 bearer token fetch 
    * @link https://developer.paypal.com/webapps/developer/docs/api/#authentication--headers 
    */ 
    public function __construct(){ 
     $postvals = "grant_type=client_credentials"; 
     $uri = PAYMENT_URI . "v1/oauth2/token"; 

     $auth_response = self::curl($uri, 'POST', $postvals, true); 
     $this->access_token = $auth_response['body']->access_token; 
     $this->token_type = $auth_response['body']->token_type; 
    } 

    /** 
    * cURL 
    * 
    * Handles GET/POST requests for auth requests 
    * @link http://php.net/manual/en/book.curl.php 
    */ 
    private function curl($url, $method = 'GET', $postvals = null, $auth = false){ 
     $ch = curl_init($url); 

     //if we are sending request to obtain bearer token 
     if ($auth){ 
      $headers = array("Accept: application/json", "Accept-Language: en_US"); 
      curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); 
      curl_setopt($ch, CURLOPT_USERPWD, CLIENT_ID . ":" .CLIENT_SECRET); 
      curl_setopt($ch, CURLOPT_SSLVERSION, 3); 
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
      curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); 
     //if we are sending request with the bearer token for protected resources 
     } else { 
      $headers = array("Content-Type:application/json", "Authorization:{$this->token_type} {$this->access_token}"); 
     } 

     $options = array(
      CURLOPT_HEADER => true, 
      CURLINFO_HEADER_OUT => true, 
      CURLOPT_HTTPHEADER => $headers, 
      CURLOPT_RETURNTRANSFER => true, 
      CURLOPT_VERBOSE => true, 
      CURLOPT_TIMEOUT => 10 
     ); 

     if ($method == 'POST'){ 
      $options[CURLOPT_POSTFIELDS] = $postvals; 
      $options[CURLOPT_CUSTOMREQUEST] = $method; 
     } 

     curl_setopt_array($ch, $options); 

     $response = curl_exec($ch); 
     $header = substr($response, 0, curl_getinfo($ch,CURLINFO_HEADER_SIZE)); 
     $body = json_decode(substr($response, curl_getinfo($ch,CURLINFO_HEADER_SIZE))); 
     curl_close($ch); 

     return array('header' => $header, 'body' => $body); 
    } 

    // Function for Processing Payment 
    function process_payment($request) { 
     $postvals = $request; 
     $uri = PAYMENT_URI . "v1/payments/payment"; 
     return self::curl($uri, 'POST', $postvals); 
    } 
} 

if (isset($_SESSION['payment_type']) && ($_SESSION['payment_type'] == 'paypal')) { // User has chosen to pay with Paypal. 


    // Retrive Shopping cart contents 
    $r = mysqli_query($dbc, "CALL get_shopping_cart_contents('$uid')"); 

    $request = array(
     'intent' => 'sale', 
     'redirect_urls' => array(
      'return_url' =>'http://store.example.com/final', 
      'cancel_url' =>'http://store.example.com/payment' 
     ), 
     'payer' => array(
      'payment_method' =>'paypal' 
     ), 
     'transactions' => array(
      'amount' => array(
       'total' =>''.number_format($order_total,2).'', 
       'currency' =>'USD', 
       'details' => array(
        'subtotal' =>''.number_format($subtotal,2).'', 
        'shipping' =>''.number_format($shipping,2).'' 
       ), 
       'item_list' => array(

       ) 
      ), 
      'description' =>'Mike and Maureen Photography - Order ID #'.$order_id.'' 
     ) 
    ); 

    while ($items = mysqli_fetch_array($r, MYSQLI_ASSOC)) { 

     $newitems = array(
      'quantity' =>''.$items['quantity'].'', 
      'name' =>''.$items['name'].'', 
      'price' =>''.get_price($items['price'],$items['sales_price']).'', 
      'currency' =>'USD' 
     ); 
     $request['transactions']['amount']['item_list']['items'][] = $newitems; 
    } 

    $request = json_encode($request); 

    process_payment($request); 
} 

내가 PHP 좋은 오전을 사용하고 코드 만이 모든 클래스는, 공개/개인 물건이 날을 던지고있다. 이 코드를 사용하지 않거나 문제가 생길 수 있습니까? 오류를 발생시키지 않고 process_payment 함수를 실행하려면 어떻게해야합니까? "치명적인 오류 : 정의되지 않은 함수 process_payment() 호출"

Didnt 그냥 함수를 paypal 클래스에 정의 했습니까? 나는 페이팔에 대한 문서를 읽었으며 내가 잘못하고있는 것에 대해 이해할 수 없다. 어떤 도움이라도 좋을 것입니다.

+1

'process_payment()'를 호출 할 수 없습니다. 'paypal' 클래스의 인스턴스를 사용하거나 클래스 밖에서 호출 될 수 있도록 함수를 정적으로 만들어야합니다. –

+0

이제 그 부분을 알아 냈습니다. "들어오는 JSON 요청이 API 요청에 매핑되지 않습니다."라는 응답에서 오류가 발생했습니다. 코드를 확인하거나 여기에 코드를 게시해야합니까? 나는 $ paypal = 새로운 PayPal()을 사용했다. \t \t $ result = $ paypal-> process_payment ($ request); – McCoy

답변

관련 문제