2016-10-03 69 views
0

我正在嘗試將貝寶自適應付款集成到laravel中。在laravel控制器中執行多個功能?

我想用類似這樣的代碼的東西: http://proggblo.blogspot.ca/2013/04/paypal-adaptive-payments-parallel.html

問題是,我不會太知道如何做到這一點laravel內。我會使用控制器並通過發佈請求嗎?如果是這樣,它會是什麼樣子?像這樣? 路線:

Route::post('/payment', [ 
'uses' => '\CommendMe\Http\Controllers\[email protected]', 
'as' => 'quote.payment', 
]); 

控制器:

class PaypalController extends Controller { 
    public function payment(Request $request) { 


    $apiUrl = "https://svcs.sandbox.paypal.com/AdaptivePayments/"; 

    $paypalUrl = "https://www.sandbox.paypal.com/webscr?cmd=_ap-payment&paykey="; 


    function __construct() { 

     $this->headers = array(

      "X-PAYPAL-SECURITY-USERID: ".API_USER, 

      "X-PAYPAL-SECURITY-PASSWORD: ".API_PASS, 

      "X-PAYPAL-SECURITY-SIGNATURE: ".API_SIG, 

      "X-PAYPAL-REQUEST-DATA-FORMAT: JSON", 

      "X-PAYPAL-RESPONSE-DATA-FORMAT: JSON", 

      "X-PAYPAL-APPLICATION-ID: ".APP_ID 

    ); 

    } 
    function _paypalSend($data,$call) { 

    $ch = curl_init(); 

    curl_setopt($ch, CURLOPT_URL, $this->apiUrl.$call); 

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 

    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 

    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); 

    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); 

    curl_setopt($ch, CURLOPT_HTTPHEADER, $this->headers); 

    return json_decode(curl_exec($ch), TRUE);  


    } 
} 
} 

等等,等等離開代碼的其餘部分進行。 (你可以在上面的鏈接中找到它)

現在,如果付款功能內的功能將執行,現在這將罰款。

在這種情況下,我應該使用中間件而不是控制器嗎?

回答

0

這與在標準PHP中執行多個方法沒有什麼不同。只需在控制器內創建您的方法,並且您可以在任何時候將其全部呼叫。

您的控制器中最大的問題是您的付款功能中嵌套了__construct方法。此外,如果您在POST請求中觸及付款終端,您需要訪問該方法中的$ data和$ call變量以發送付款。由於您沒有顯示數據來自哪些變​​量,我只能在理論上告訴你如何做到這一點

class PaypalController extends Controller 
{ 
    private $apiUrl; 
    private $paypalUrl; 

    public function payment(Request $request) 
    { 
     $this->apiUrl = "https://svcs.sandbox.paypal.com/AdaptivePayments/"; 
     $this->paypalUrl = "https://www.sandbox.paypal.com/webscr?cmd=_ap-payment&paykey="; 

     $this->send($data, $call); 
    } 

    private function send($data, $call) 
    { 
     $ch = curl_init(); 
     curl_setopt($ch, CURLOPT_URL, $this->apiUrl . $call); 
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
     curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); 
     curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); 
     curl_setopt($ch, CURLOPT_HTTPHEADER, $this->getHeaders()); 
     return json_decode(curl_exec($ch), TRUE);  
    } 


    private function getHeaders() 
    { 
     return [ 

      "X-PAYPAL-SECURITY-USERID: ".API_USER, 

      "X-PAYPAL-SECURITY-PASSWORD: ".API_PASS, 

      "X-PAYPAL-SECURITY-SIGNATURE: ".API_SIG, 

      "X-PAYPAL-REQUEST-DATA-FORMAT: JSON", 

      "X-PAYPAL-RESPONSE-DATA-FORMAT: JSON", 

      "X-PAYPAL-APPLICATION-ID: ".APP_ID 

     ]; 
    } 
}