简体   繁体   English

在laravel控制器中执行多项功能?

[英]Executing multiple functions in a laravel controller?

I'm trying to integrate Paypal adaptive payments within laravel. 我正在尝试在laravel中集成Paypal自适应支付。

I want to use something similar to this code: http://proggblo.blogspot.ca/2013/04/paypal-adaptive-payments-parallel.html 我想使用类似于以下代码的内容: http : //proggblo.blogspot.ca/2013/04/paypal-adaptive-payments-parallel.html

Problem is, I wouldn't be too sure how to do that within laravel. 问题是,我不太确定如何在laravel中执行此操作。 Would I use a controller and pass through a post request? 我会使用控制器并通过发布请求吗? If so, how would it look like? 如果是这样,它将是什么样? Something like this? 像这样吗 Route: 路线:

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

controller: 控制器:

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);      


  }
 }
}

etc. etc. Leaving the rest of the code out. 等等。省去了其余的代码。 (you can find it in the link above) (您可以在上面的链接中找到它)

Now this would would fine if the functions within the payment function would execute. 现在,如果将执行支付功能中的功能,这将很好。

Should I use middleware instead of a controller in this situation? 在这种情况下,我应该使用中间件而不是控制器吗?

It would be no different than executing multiple methods in standard PHP. 这与在标准PHP中执行多个方法没有什么不同。 Just create your methods inside the controller and you can call them all whenever you want to. 只需在控制器内创建方法,就可以随时调用它们。

The biggest problem in your controller is you have a __construct method nested inside your payment function. 控制器中最大的问题是您的付款功能中嵌套了__construct方法。 Also, if you are hitting the payment endpoint on the POST request, you need access to the $data and $call variables inside that method in order to send the payment.Since you don't show where the data comes from of those variables, I can only you show you in theory how to do this 另外,如果您要在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  

        ];
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM