2013-06-20 133 views
0

以下是我用於處理信用卡的腳本。現在,付款根據$ clientId和$ clientSecret發給我。我需要收件人變得基於用戶貝寶電子郵件。如果我有用戶PayPal電子郵件,我將如何去拉取適當的信息將錢發送給用戶?使用PayPal IPN - 如何選擇付款收款人?

#!/usr/bin/php 
<?php 

# Sandbox 
$host = 'https://api.sandbox.paypal.com'; 
$clientId = 'ATopphBpQ1BOCE3Bi5QLdXH8XjH_btoKoXXHsMNSUVe9cx_nbiB0fpglcwNE'; 
$clientSecret = 'EFvOWhDKf9-F9uEEHaA4LV_t373Zk-B0-h2vT6d-1BjaqsrPo_DWH74wTJ_M'; 

$token = ''; 
// function to read stdin 
function read_stdin() { 
     $fr=fopen("php://stdin","r"); // open our file pointer to read from stdin 
     $input = fgets($fr,128);  // read a maximum of 128 characters 
     $input = rtrim($input);   // trim any trailing spaces. 
     fclose ($fr);     // close the file handle 
     return $input;     // return the text entered 
} 

function get_access_token($url, $postdata) { 
    global $clientId, $clientSecret; 
    $curl = curl_init($url); 
    curl_setopt($curl, CURLOPT_POST, true); 
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); 
    curl_setopt($curl, CURLOPT_USERPWD, $clientId . ":" . $clientSecret); 
    curl_setopt($curl, CURLOPT_HEADER, false); 
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata); 
# curl_setopt($curl, CURLOPT_VERBOSE, TRUE); 
    $response = curl_exec($curl); 
    if (empty($response)) { 
     // some kind of an error happened 
     die(curl_error($curl)); 
     curl_close($curl); // close cURL handler 
    } else { 
     $info = curl_getinfo($curl); 
     echo "Time took: " . $info['total_time']*1000 . "ms<br>"; 
     curl_close($curl); // close cURL handler 
     if($info['http_code'] != 200 && $info['http_code'] != 201) { 
      echo "Received error: " . $info['http_code']. "<br>"; 
      echo "Raw response:".$response."<br>"; 
      die(); 
     } 
    } 

    // Convert the result from JSON format to a PHP array 
    $jsonResponse = json_decode($response); 
    return $jsonResponse->access_token; 
} 

function make_post_call($url, $postdata) { 
    global $token; 
    $curl = curl_init($url); 
    curl_setopt($curl, CURLOPT_POST, true); 
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); 
    curl_setopt($curl, CURLOPT_HEADER, false); 
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($curl, CURLOPT_HTTPHEADER, array(
       'Authorization: Bearer '.$token, 
       'Accept: application/json', 
       'Content-Type: application/json' 
       )); 

    curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata); 
    #curl_setopt($curl, CURLOPT_VERBOSE, TRUE); 
    $response = curl_exec($curl); 
    if (empty($response)) { 
     // some kind of an error happened 
     die(curl_error($curl)); 
     curl_close($curl); // close cURL handler 
    } else { 
     $info = curl_getinfo($curl); 
     echo "Time took: " . $info['total_time']*1000 . "ms<br>"; 
     curl_close($curl); // close cURL handler 
     if($info['http_code'] != 200 && $info['http_code'] != 201) { 
      echo "Received error: " . $info['http_code']. "<br>"; 
      echo "Raw response:".$response."<br>"; 
      die(); 
     } 
    } 

    // Convert the result from JSON format to a PHP array 
    $jsonResponse = json_decode($response, TRUE); 
    return $jsonResponse; 
} 

function make_get_call($url) { 
    global $token; 
    $curl = curl_init($url); 
    curl_setopt($curl, CURLOPT_POST, false); 
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); 
    curl_setopt($curl, CURLOPT_HEADER, false); 
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($curl, CURLOPT_HTTPHEADER, array(
       'Authorization: Bearer '.$token, 
       'Accept: application/json', 
       'Content-Type: application/json' 
       )); 

    #curl_setopt($curl, CURLOPT_VERBOSE, TRUE); 
    $response = curl_exec($curl); 
    if (empty($response)) { 
     // some kind of an error happened 
     die(curl_error($curl)); 
     curl_close($curl); // close cURL handler 
    } else { 
     $info = curl_getinfo($curl); 
     echo "Time took: " . $info['total_time']*1000 . "ms<br>"; 
     curl_close($curl); // close cURL handler 
     if($info['http_code'] != 200 && $info['http_code'] != 201) { 
      echo "Received error: " . $info['http_code']. "<br>"; 
      echo "Raw response:".$response."<br>"; 
      die(); 
     } 
    } 

    // Convert the result from JSON format to a PHP array 
    $jsonResponse = json_decode($response, TRUE); 
    return $jsonResponse; 
} 

echo "<br>"; 
echo "###########################################<br>"; 
echo "Obtaining OAuth2 Access Token.... <br>"; 
$url = $host.'/v1/oauth2/token'; 
$postArgs = 'grant_type=client_credentials'; 
$token = get_access_token($url,$postArgs); 
echo "Got OAuth Token: ".$token; 
echo "<br> <br>"; 
echo "###########################################<br>"; 
echo "Making a Credit Card Payment... <br>"; 
$url = $host.'/v1/payments/payment'; 
$payment = array(
     'intent' => 'sale', 
     'payer' => array(
      'payment_method' => 'credit_card', 
      'funding_instruments' => array (array(
        'credit_card' => array (
         'number' => '5500005555555559', 
         'type' => 'mastercard', 
         'expire_month' => 12, 
         'expire_year' => 2018, 
         'cvv2' => 111, 
         'first_name' => 'Joe', 
         'last_name' => 'Shopper' 
         ) 
        )) 
      ), 
     'transactions' => array (array(
       'amount' => array(
        'total' => '7.47', 
        'currency' => 'USD' 
        ), 
       'description' => 'payment by a credit card using a test script' 
       )) 
     ); 
     echo "<b>Payer Variable Details:</b> :"; 
     print_r($payment['payer']); 
$json = json_encode($payment); 
$json_resp = make_post_call($url, $json); 
foreach ($json_resp['links'] as $link) { 
    if($link['rel'] == 'self'){ 
     $payment_detail_url = $link['href']; 
     $payment_detail_method = $link['method']; 
    } 
} 
$related_resource_count = 0; 
$related_resources = ""; 
foreach ($json_resp['transactions'] as $transaction) { 
    if($transaction['related_resources']) { 
     $related_resource_count = count($transaction['related_resources']); 
     foreach ($transaction['related_resources'] as $related_resource) { 
      if($related_resource['sale']){ 
       $related_resources = $related_resources."sale "; 
       $sale = $related_resource['sale']; 
       foreach ($sale['links'] as $link) { 
        if($link['rel'] == 'self'){ 
         $sale_detail_url = $link['href']; 
         $sale_detail_method = $link['method']; 
        }else if($link['rel'] == 'refund'){ 
         $refund_url = $link['href']; 
         $refund_method = $link['method']; 
        } 
       } 
      } else if($related_resource['refund']){ 
       $related_resources = $related_resources."refund"; 
      } 
     } 
    } 
} 

echo "Payment Created successfully: " . $json_resp['id'] ." with state '". $json_resp['state']."'<br>"; 
echo "Payment related_resources:". $related_resource_count . "(". $related_resources.")"; 
echo "<br> <br>"; 
echo "###########################################<br>"; 
echo "Obtaining Payment Details... <br>"; 
$json_resp = make_get_call($payment_detail_url); 
echo "Payment details obtained for: " . $json_resp['id'] ." with state '". $json_resp['state']. "'"; 
echo "<br> <br>"; 
echo "###########################################<br>"; 
echo "Obtaining Sale details...<br>"; 
$json_resp = make_get_call($sale_detail_url); 
echo "Sale details obtained for: " . $json_resp['id'] ." with state '". $json_resp['state']."'"; 
echo "<br> <br>"; 
echo "###########################################<br>"; 
echo "Refunding a Sale... <br>"; 
$refund = array(
     'amount' => array(
      'total' => '7.47', 
      'currency' => 'USD' 
      ) 
      ); 
$json = json_encode($refund); 
$json_resp = make_post_call($refund_url, $json); 
echo "Refund processed " . $json_resp['id'] ." with state '". $json_resp['state']."'"; 
echo "<br> <br>"; 
echo "###########################################<br>"; 
echo "Obtaining Sale details...<br>"; 
$json_resp = make_get_call($sale_detail_url); 
echo "Sale details obtained for: " . $json_resp['id'] ." with state '". $json_resp['state']."'"; 
echo "<br> <br>"; 
echo "###########################################<br>"; 
echo "Obtaining Payment Details... <br>"; 
$json_resp = make_get_call($payment_detail_url); 
$related_resource_count = 0; 
$related_resources = ""; 
foreach ($json_resp['transactions'] as $transaction) { 
    if($transaction['related_resources']) { 
     $related_resource_count = count($transaction['related_resources']); 
     foreach ($transaction['related_resources'] as $related_resource) { 
      if($related_resource['sale']){ 
       $related_resources = $related_resources."sale "; 
      } else if($related_resource['refund']){ 
       $related_resources = $related_resources."refund"; 
      } 
     } 

    } 
} 

echo "Payment details obtained for: " . $json_resp['id'] ." with state '". $json_resp['state']. "' <br>"; 
echo "Payment related_resources:". $related_resource_count . "(". $related_resources.")"; 
echo "<br> <br>"; 
echo "###########################################<br>"; 
echo "Saving a Credit Card in vault... <br>"; 
$url = $host.'/v1/vault/credit-card'; 
$creditcard = array(
     'payer_id' => '[email protected]', 
     'number' => '4417119669820331', 
     'type' => 'visa', 
     'expire_month' => 11, 
     'expire_year' => 2018, 
     'first_name' => 'John', 
     'last_name' => 'Doe' 
     ); 
$json = json_encode($creditcard); 
$json_resp = make_post_call($url, $json); 
$credit_card_id = $json_resp['id']; 
echo "Credit Card saved ".$credit_card_id." with state '".$json_resp['state']."'"; 
echo "<br> <br>"; 
echo "###########################################<br>"; 
echo "Making a Payment with saved credit card... <br>"; 
$url = $host.'/v1/payments/payment'; 
$payment = array(
       'intent' => 'sale', 
       'payer' => array(
         'payment_method' => 'credit_card', 
         'funding_instruments' => array (array(
             'credit_card_token' => array (
               'credit_card_id' => $credit_card_id, 
               'payer_id' => '[email protected]' 
               ) 
             )) 
         ), 
       'transactions' => array (array(
           'amount' => array(
             'total' => '7.47', 
             'currency' => 'USD' 
             ), 
           'description' => 'payment using a saved card' 
           )) 
       ); 
$json = json_encode($payment); 
$json_resp = make_post_call($url, $json); 
echo "Payment Created successfully: " . $json_resp['id'] ." with state '". $json_resp['state']."'<br>"; 
echo "<br> <br>"; 
echo "###########################################<br>"; 
echo "Obtaining all Payments (list) ... <br>"; 
$payment_list_url = $host.'/v1/payments/payment?start_id=PAY-1JJ14633E59990232KE6QU3I'; 
$json_resp = make_get_call($payment_list_url); 
echo "Number of Payment resources returned: " . count($json_resp['payments']); 
$counter = 0; 
foreach ($json_resp['payments'] as $payment) { 
    echo "<br>" . $counter++ . ". " . $payment['id']; 
} 
echo "<br>Next Payment ID: ". $json_resp['next_id']; 
echo "<br>Obtaining subset (2-4) of the Payments ... <br>"; 
$payment_list_url = $host.'/v1/payments/payment?start_index=1&count=3'; 
$json_resp = make_get_call($payment_list_url); 
echo "Number of Payment resources returned: " . count($json_resp['payments']); 
$counter = 0; 
foreach ($json_resp['payments'] as $payment) { 
     echo "<br>" . $counter++ . ". " . $payment['id']; 
} 
echo "<br>Next Payment ID: ". $json_resp['next_id']; 
echo "<br>Obtaining the next 10 starting from the previous next_id ... <br>"; 
$payment_list_url = $host.'/v1/payments/payment?start_id='.$json_resp['next_id']; 
$json_resp = make_get_call($payment_list_url); 
echo "Number of Payment resources returned: " . count($json_resp['payments']); 
$counter = 0; 
foreach ($json_resp['payments'] as $payment) { 
     echo "<br>" . $counter++ . ". " . $payment['id']; 
} 
echo "<br> <br>"; 
echo "###########################################<br>"; 
echo "Making a Credit Card Authorization... <br>"; 
$url = $host.'/v1/payments/payment'; 
$payment = array(
       'intent' => 'authorize', 
       'payer' => array(
         'payment_method' => 'credit_card', 
         'funding_instruments' => array (array(
             'credit_card' => array (
               'number' => '5500005555555559', 
               'type' => 'mastercard', 
               'expire_month' => 12, 
               'expire_year' => 2018, 
               'cvv2' => 111, 
               'first_name' => 'Joe', 
               'last_name' => 'Shopper' 
               ) 
             )) 
         ), 
       'transactions' => array (array(
           'amount' => array(
             'total' => '7.47', 
             'currency' => 'USD' 
             ), 
           'description' => 'payment by a credit card using a test script' 
           )) 
       ); 
$json = json_encode($payment); 
$json_resp = make_post_call($url, $json); 
foreach ($json_resp['links'] as $link) { 
     if($link['rel'] == 'self'){ 
       $payment_detail_url = $link['href']; 
       $payment_detail_method = $link['method']; 
     } 
} 
$related_resource_count = 0; 
$related_resources = ""; 
foreach ($json_resp['transactions'] as $transaction) { 
     if($transaction['related_resources']) { 
       $related_resource_count = count($transaction['related_resources']); 
       foreach ($transaction['related_resources'] as $related_resource) { 
         if($related_resource['authorization']){ 
           $related_resources = $related_resources."authorization "; 
           $authorization = $related_resource['authorization']; 
           foreach ($authorization['links'] as $link) { 
             if($link['rel'] == 'self'){ 
               $auth_detail_url = $link['href']; 
               $auth_detail_method = $link['method']; 
             }else if($link['rel'] == 'refund'){ 
               $refund_url = $link['href']; 
               $refund_method = $link['method']; 
             }else if($link['rel'] == 'void'){ 
               $void_url = $link['href']; 
               $void_method = $link['method']; 
             }else if($link['rel'] == 'capture'){ 
               $capture_url = $link['href']; 
               $capture_method = $link['method']; 
             } 
           } 
         } else if($related_resource['refund']){ 
           $related_resources = $related_resources."refund"; 
         } 
       } 
     } 
} 

echo "Payment Created successfully: " . $json_resp['id'] ." with state '". $json_resp['state']."'<br>"; 
echo "Payment related_resources:". $related_resource_count . "(". $related_resources.")"; 
echo "<br> <br>"; 
echo "###########################################<br>"; 
echo "Obtaining Payment Details... <br>"; 
$json_resp = make_get_call($payment_detail_url); 
echo "Payment details obtained for: " . $json_resp['id'] ." with state '". $json_resp['state']. "'"; 
echo "<br> <br>"; 
echo "###########################################<br>"; 
echo "Obtaining Authorization details...<br>"; 
$json_resp = make_get_call($auth_detail_url); 
echo "Authorization details obtained for: " . $json_resp['id'] ." with state '". $json_resp['state']."'"; 
echo "<br> <br>"; 
echo "###########################################<br>"; 
echo "Capturing Authorization ...<br>"; 
$capture = array(
       'amount' => array(
         'total' => '5.47', 
         'currency' => 'USD' 
         ) 
       ); 
$json = json_encode($capture); 
$json_resp = make_post_call($capture_url, $json); 
echo "Capture processed " . $json_resp['id'] ." with state '". $json_resp['state']."'"; 
foreach ($json_resp['links'] as $link) { 
     if($link['rel'] == 'self'){ 
       $capture_detail_url = $link['href']; 
       $capture_detail_method = $link['method']; 
     }else if($link['rel'] == 'refund'){ 
        $refund_url = $link['href']; 
        $refund_method = $link['method']; 
      } 
} 
echo "<br> <br>"; 
echo "###########################################<br>"; 
echo "Obtaining Authorization details...<br>"; 
$json_resp = make_get_call($auth_detail_url); 
echo "Authorization details obtained for: " . $json_resp['id'] ." with state '". $json_resp['state']."'"; 
echo "<br> <br>"; 
echo "###########################################<br>"; 
echo "Obtaining Capture details...<br>"; 
$json_resp = make_get_call($capture_detail_url); 
echo "Capture details obtained for: " . $json_resp['id'] ." with state '". $json_resp['state']."'"; 
echo "<br> <br>"; 
echo "###########################################<br>"; 
echo "Refunding a Capture... <br>"; 
$refund = array(
       'amount' => array(
         'total' => '2.47', 
         'currency' => 'USD' 
         ) 
       ); 
$json = json_encode($refund); 
$json_resp = make_post_call($refund_url, $json); 
echo "Refund processed " . $json_resp['id'] ." with state '". $json_resp['state']."'"; 
echo "<br> <br>"; 
echo "###########################################<br>"; 
echo "Obtaining Capture details...<br>"; 
$json_resp = make_get_call($capture_detail_url); 
echo "Capture details obtained for: " . $json_resp['id'] ." with state '". $json_resp['state']."'"; 
echo "<br> <br>"; 
echo "###########################################<br>"; 
echo "Voiding Authorization ...<br>"; 
$void = array(); 
$json = json_encode($void); 
$json_resp = make_post_call($void_url, $json); 
echo "Void processed " . $json_resp['id'] ." with state '". $json_resp['state']."'"; 
echo "<br> <br>"; 
echo "###########################################<br>"; 
echo "Obtaining Authorization details...<br>"; 
$json_resp = make_get_call($auth_detail_url); 
echo "Authorization details obtained for: " . $json_resp['id'] ." with state '". $json_resp['state']."'"; 
echo "<br> <br>"; 
echo "###########################################<br>"; 
echo "Obtaining parent Payment Details for the Authorization ... <br>"; 
$json_resp = make_get_call($payment_detail_url); 
$related_resource_count = 0; 
$related_resources = ""; 
foreach ($json_resp['transactions'] as $transaction) { 
     if($transaction['related_resources']) { 
       $related_resource_count = count($transaction['related_resources']); 
       foreach ($transaction['related_resources'] as $related_resource) { 
         if($related_resource['authorization']){ 
           $related_resources = $related_resources."authorization "; 
         } else if($related_resource['capture']){ 
           $related_resources = $related_resources."capture "; 
         } else if($related_resource['refund']){ 
           $related_resources = $related_resources."refund "; 
          } 
       } 

     } 
} 

echo "Payment details obtained for: " . $json_resp['id'] ." with state '". $json_resp['state']. "' <br>"; 
echo "Payment related_resources:". $related_resource_count . "(". $related_resources.")"; 
echo "<br> <br>"; 

die; 
//END CREDIT CARD FUNCTIONS 

//Paypal Payment 
echo "###########################################<br>"; 
echo "Initiating a Payment with PayPal Account... <br>"; 
$url = $host.'/v1/payments/payment'; 
$payment = array(
       'intent' => 'sale', 
       'payer' => array(
         'payment_method' => 'paypal' 
     ), 
       'transactions' => array (array(
           'amount' => array(
             'total' => '7.47', 
             'currency' => 'USD' 
             ), 
           'description' => 'payment using a PayPal account' 
           )), 
     'redirect_urls' => array (
      'return_url' => 'http://project.aimlessmedia.com/001/return.php?cancelled=0&returned=1', 
      'cancel_url' => 'http://project.aimlessmedia.com/001/return.php?cancelled=1&returned=1' 
     ) 
       ); 
$json = json_encode($payment); 
$json_resp = make_post_call($url, $json); 
foreach ($json_resp['links'] as $link) { 
    if($link['rel'] == 'execute'){ 
     $payment_execute_url = $link['href']; 
     $payment_execute_method = $link['method']; 
    } else if($link['rel'] == 'approval_url'){ 
      $payment_approval_url = $link['href']; 
      $payment_approval_method = $link['method']; 
     } 
} 
echo "Payment Created successfully: " . $json_resp['id'] ." with state '". $json_resp['state']."'<br><br>"; 
echo "Please goto ".$payment_approval_url." in your browser and approve the payment with a PayPal Account.<br>"; 
echo "Enter PayerId from the return url to continue:"; 
$payerId = read_stdin(); 
echo "<br> <br>"; 
echo "###########################################<br>"; 
echo "Executing the PayPal Payment for PayerId (".$payerId.")... <br>"; 
$payment_execute = array(
     'payer_id' => $payerId 
      ); 
$json = json_encode($payment_execute); 
$json_resp = make_post_call($payment_execute_url, $json); 
echo "Payment Execute processed " . $json_resp['id'] ." with state '". $json_resp['state']."'"; 
echo "<br> <br>"; 


?> 

回答