2017-10-10 6 views
0

다음은 지불 게이트웨이의 나머지 API와 상호 작용하는 클래스 세트 인 "Paytrail_Module_Rest.php"의 예제 코드입니다. 일부 클래스는 자격 증명이있는 Paytrail_Module_rest와 같이 미리 인스턴스화 할 수 있지만 일부는 컨트롤러에서만 사용할 수있는 정보로 인스턴스화해야합니다 (예 : 가격과 같은 지불 세부 정보를 설정하는 Paytrail_Module_Rest_Payment_S1)Paytrail과 같은 slim3에 복잡한 서비스를 주입하는 방법

Can 누구든지 slim3에 주입하는 깨끗한 방법을 제안합니까? 나는 표준 컨테이너 주입 방법으로 그것을하는 좋은 방법을 볼 수 없습니다.

$urlset = new\App\Service\Paytrail\Paytrail_Module_Rest_Urlset(
    "https://www.demoshop.com/sv/success", // return address for successful payment 
    "https://www.demoshop.com/sv/failure", // return address for failed payment 
    "https://www.demoshop.com/sv/notify", // address for payment confirmation from Paytrail server 
    "" // pending url not in use 
); 

$orderNumber = '1'; 
$price = 99.00; 
$payment = new \App\Service\Paytrail\Paytrail_Module_Rest_Payment_S1($orderNumber, $urlset, $price); 

$payment->setLocale('en_US'); 

$module = new \App\Service\Paytrail\Paytrail_Module_Rest(13466, '6pKF4jkv97zmqBJ3ZL8gUw5DfT2NMQ'); 

try { 
    $result = $module->processPayment($payment); 
} 
catch (\App\Service\Paytrail\Paytrail_Exception $e) { 
    die('Error in creating payment to Paytrail service:'. $e->getMessage()); 
} 

echo $result->getUrl(); 

(여기에 나열된 자격 증명이 공공 시험 자격 증명)

+0

Slim은 모든 PSR-7 HTTP 메시지 구현을 지원합니다. 이를 위해 미들웨어를 작성하고 작업에서 속성을 사용할 수 있습니다. – DanielO

답변

1

모듈과 같은 컨테이너로 변경하고 urlset이

$container[\App\Service\Paytrail\Paytrail_Module_Rest_Urlset::class] = function($c) { 
    return new \App\Service\Paytrail\Paytrail_Module_Rest_Urlset(
     "https://www.demoshop.com/sv/success", // return address for successful payment 
     "https://www.demoshop.com/sv/failure", // return address for failed payment 
     "https://www.demoshop.com/sv/notify", // address for payment confirmation from Paytrail server 
     "" // pending url not in use 
    ); 
}; 

$container[\App\Service\Paytrail\Paytrail_Module_Rest::class] = function($c) { 
    return new \App\Service\Paytrail\Paytrail_Module_Rest(13466, '6pKF4jkv97zmqBJ3ZL8gUw5DfT2NMQ'); 
}; 

을 말았하지 않는 물건을 추가하고 다음 중 하나를 수 필요할 때마다 지불을 인스턴스화하거나 어댑터와 같은 도우미 클래스를 추가하십시오.

class PaymentAdapter { 

    public function __construct(
      \App\Service\Paytrail\Paytrail_Module_Rest $module, 
      \App\Service\Paytrail\Paytrail_Module_Rest_Urlset $urlset) 
    { 
     $this->module = $module; 
     $this->urlset = $urlset; 
    } 

    function createAndProcessPayment($orderNumber, $price) 
    { 
     $payment = new \App\Service\Paytrail\Paytrail_Module_Rest_Payment_S1($orderNumber, $this->urlset, $price); 

     $payment->setLocale('en_US'); 
     try { 
      $result = $module->processPayment($payment); 
     } 
     catch (\App\Service\Paytrail\Paytrail_Exception $e) { 
      die('Error in creating payment to Paytrail service:'. $e->getMessage()); 
     } 
     return $result; 
    } 

} 

그런 다음 어댑터를 컨테이너에 추가하십시오.

$container[\yournamespace\PaymentAdapter::class] = function($c) { 
    return new \yournamespace\PaymentAdapter(
     $c[\App\Service\Paytrail\Paytrail_Module_Rest::class], 
     $c[\App\Service\Paytrail\Paytrail_Module_Rest_Urlset::class] 
    ); 
};