2017-03-24 6 views
0

私はPaypalとPHPを初めて使用しているので、多分私は何か間違ったことをやっていますか? 私はPaypalを-context checkout v4サーバー側を使用して作業し、支払いが完了したときに操作できるようにします。私はPaypalガイド(https://github.com/paypal/paypal-checkout/blob/master/docs/button.mdhttps://github.com/paypal/paypal-checkout/blob/master/docs/paypal-rest-api.md)と多くの研究を続けてきましたが、今のところ役に立たなかったのです。Paypalのコンテキスト内でのチェックに問題があります.js v4はREST APIを使用しています

私が作成した支払いが呼び出された後、ペイパルウィンドウがメッセージ 「はこの時商人 に戻るペイパルのログイン情報を提示し、代わりに私を提示していない、我々はあなたの要求を処理することができません問題がありますもう一度やり直してみてください。 "

これが私のサーバー側からのコードです:

class ExpressCheckout { 

    private $request = null; 

    function createPayment(){ 

     $token = $this->getConnectionToken(); 

     $this->request = curl_init(); 
     $paypalmode = (PPL_MODE=='sandbox') ? '.sandbox' : ''; 

     $data = $this->getDataFromSession(); 
     $url = 'https://api'.$paypalmode.'.paypal.com/v1/payments/payment'; 
     $this->setDefaultRequest(); 
     curl_setopt($this->request, CURLOPT_HTTPHEADER, array("Authorization: Bearer $token", 
                  'Content-Type: application/json')); 

     curl_setopt($this->request, CURLOPT_POSTFIELDS, $data); 
     curl_setopt($this->request, CURLOPT_URL, $url); 
     $response = $this->sendHttp($url); 
     return '{"paymentID":"'.$response->id.'"}'; 
    } 

    function executePayment(){ 

     $token = $this->getConnectionToken(); 

     $entryData = json_decode(file_get_contents('php://input'), true); 
     $paymentId = $entryData["paymentID"]; 
     $payerId = $entryData["payerID"]; 

     $this->request = curl_init(); 
     $paypalmode = (PPL_MODE=='sandbox') ? '.sandbox' : ''; 

     $data = '{"payer_id":"'.$payerID.'"}'; 
     $url = 'https://api'.$paypalmode.'.paypal.com/v1/payments/payment/'.$paymentId.'/execute'; 
     $this->setDefaultRequest(); 
     curl_setopt($this->request, CURLOPT_HTTPHEADER, array("Authorization: Bearer $token", 
                'Content-Type: application/json')); 

     curl_setopt($this->request, CURLOPT_POSTFIELDS, $data); 
     curl_setopt($this->request, CURLOPT_URL, $url); 
     $response = $this->sendHttp($url); 
     return $response; 
    } 

    function getConnectionToken(){ 
     $this->request = curl_init(); 
     $userName = PPL_REST_API_CLIENT_ID; 
     $password = PPL_REST_API_SECRET; 
     $paypalmode = (PPL_MODE=='sandbox') ? '.sandbox' : ''; 
     $url = 'https://api'.$paypalmode.'.paypal.com/v1/oauth2/token'; 
     $data = 'grant_type=client_credentials'; 

     $this->setDefaultRequest(); 
     curl_setopt($this->request, CURLOPT_USERPWD, "$userName:$password"); 
     curl_setopt($this->request, CURLOPT_POSTFIELDS, $data); 

     $response = $this->sendHttp($url); 
     return $response->access_token; 
    } 

    private function setDefaultRequest(){ 
     curl_setopt($this->request, CURLOPT_VERBOSE, 1); 
     curl_setopt($this->request, CURLOPT_SSL_VERIFYPEER, FALSE); 
     curl_setopt($this->request, CURLOPT_SSL_VERIFYHOST, FALSE); 
     curl_setopt($this->request, CURLOPT_TIMEOUT, 45); 
     curl_setopt($this->request, CURLOPT_RETURNTRANSFER, 1); 
     curl_setopt($this->request, CURLOPT_POST, 1); 
    } 

    private function sendHttp($url){ 
     curl_setopt($this->request, CURLOPT_URL, $url); 
     $responseJson = json_decode(curl_exec($this->request)); 
     curl_close ($this->request); 
     return $responseJson; 
    } 
} 

私のクライアントのためのコードは次のとおりです。あなたがサンドボックスを呼び出している場合は、おそらく、paypal.Button.render()env: 'sandbox'を渡す必要があり

<!DOCTYPE HTML> 
<html> 
<head> 
    <meta charset="UTF-8" /> 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
    <meta http-equiv="X-UA-Compatible" content="IE=edge" /> 
    <script src="https://code.jquery.com/jquery-3.2.0.min.js"></script> 
</head> 
<body> 
    <div id="paypal-button2"></div> 
    <script src="https://www.paypalobjects.com/api/checkout.js" data-version-4></script> 


    <script> 
     paypal.Button.render({ 
      locale: 'en_US', 
      style: { 
       size: 'small', 
       color: 'gold', 
       shape: 'pill', 
       label: '' 
      }, 

      payment: function(resolve, reject) { 
       jQuery.post('http://dummy_url_site.com/create_payment.php') 
       .done(function(data){ 
        alert(data); 
        var myJson = JSON.parse(data); 
        alert(myJson.paymentID); 
        resolve(myJson.paymentID); 
       }) 
       .fail(function(err){reject(err); }); 
      }, 

      onAuthorize: function(data, actions) { 
       console.log('The payment was authorized!'); 
       console.log('Payment ID = ', data.paymentID); 
       console.log('PayerID = ', data.payerID); 
       // At this point, the payment has been authorized, and you will need to call your back-end to complete the 
       // payment. Your back-end should invoke the PayPal Payment Execute api to finalize the transaction. 
       /* 
       jQuery.post('http://dummy_url_site.com/execute_payment.php', { paymentID: data.paymentID, payerID: data.payerID }) 
        .done(function(data) { alert(data.toString()); }) 
        .fail(function(err) { }); 
      }, 
      onCancel: function(data, actions) { 
       return actions.redirect(); 
      }, 
onError: function(data, actions) { 
// Show an error page here. You may try restarting payment execution. 
alert('Something went wrong with payment approval' Data: ' + data.toSource()); 
return actions.restart(); 
      } 
     }, '#paypal-button2'); 
    </script> 
</body> 

</html> 

答えて

0

支払いを設定します。

+0

それはそれでした!あなたの答えをありがとう、本当にありがとう –

関連する問題