2015-03-31 22 views
1

我努力工作,Omnipay沒有多少文檔。我已成功將其用於其他支付網關,但未與Sagepay一起使用。我試圖將它集成到CodeIgniter中,但可以從其他框架中的示例中工作 - 我越來越絕望了!有沒有人有Omnipay和Sagepay Server或Sagepay Direct(使用3D Secure)的工作示例?

+0

剛剛發現模擬器不再支持這是我用於測試。我不知道是否這是我的問題的原因,因爲現在必須建立一個合作伙伴帳戶進行測試,但認爲我會更新這個以防其他人在此期間遇到此問題。 – JoJo 2015-04-01 13:53:20

+0

還有一個關於Sagepay協議3和Omnipay的線索https://github.com/thephpleague/omnipay-sagepay/issues/19 – JoJo 2015-04-01 14:48:42

+0

此主題讓我走上了正軌:https://github.com/thephpleague/omnipay/issues/255 – JoJo 2015-04-07 14:52:02

回答

1

感謝github上的一些很好的幫助(請參閱我的原始帖子中的主題鏈接評論),我現在有一些可行的代碼,我將在此分享以防將來幫助其他人。

<?php 

use Omnipay\Omnipay; 

class PaymentGateway { 

    //live details 
    private $live_vendor = 'xxx'; 
    //test details 
    private $test_vendor= 'xxx'; 

    //payment settings 
    private $testMode = true; 
    private $api_vendor = ''; 
    private $gateway = null; 

    public function __construct() 
    { 
     parent::__construct(); 
     //setup api details for test or live 
     if ($this->testMode) : 
      $this->api_vendor = $this->test_vendor; 
     else : 
      $this->api_vendor = $this->live_vendor; 
     endif; 

     //initialise the payment gateway 
     $this->gateway = Omnipay::create('SagePay_Server'); 
     $this->gateway->setVendor($this->api_vendor); 
     $this->gateway->setTestMode($this->testMode); 



    } 


    public function initiate() 
    { 

     //get order details 
     $orderNo = customFunctionToGetOrderNo(); //get the order number from your system however you store and retrieve it 

     $params = array(
      'description'=> 'Online order', 
      'currency'=> 'GBP', 
      'transactionId'=> $orderNo, 
      'amount'=> customFunctionToGetOrderTotal($orderNo) 
     ); 


     $customer = customFunctionToGetCustomerDetails($orderNo); 

     $params['returnUrl'] = '/payment-gateway-process/' . $orderNo . '/'; //this is the Sagepay NotificationURL 

     $params['card'] = array(
      'firstName' => $customer['billing_firstname'], 
      'lastName' => $customer['billing_lastname'], 
      'email' => $customer['billing_email'], 
      'billingAddress1' => $customer['billing_address1'], 
      'billingAddress2' => $customer['billing_address2'], 
      'billingCity' => $customer['billing_town'], 
      'billingPostcode' => $customer['billing_postcode'], 
      'billingCountry' => $customer['billing_country'], 
      'billingPhone' => $customer['billing_telephone'], 
      'shippingAddress1' => $customer['delivery_address1'], 
      'shippingAddress2' => $customer['delivery_address2'], 
      'shippingCity' => $customer['delivery_town'], 
      'shippingPostcode' => $customer['delivery_postcode'], 
      'shippingCountry' => $customer['delivery_country'] 
     ); 


     try { 
      $response = $this->gateway->purchase($params)->send(); 


      if ($response->isSuccessful()) : 

       //not using this part 

      elseif ($response->isRedirect()) : 

       $reference = $response->getTransactionReference(); 
       customFunctionToSaveTransactionReference($orderNo, $reference); 
       $response->redirect(); 

      else : 
       //do something with an error 
       echo $response->getMessage(); 

      endif; 

     } catch (\Exception $e) { 

      //do something with this if an error has occurred 
      echo 'Sorry, there was an error processing your payment. Please try again later.'; 
     } 



    } 


    public function processPayment($orderNo) 
    { 

     $params = array(
      'description'=> 'Online order', 
      'currency'=> 'GBP', 
      'transactionId'=> $orderNo, 
      'amount'=> customFunctionToGetOrderTotal($orderNo) 
     ); 

     $customer = customFunctionToGetCustomerDetails($orderNo); 


     $transactionReference = customFunctionToGetTransactionReference($orderNo); 


     try { 
      $response = $this->gateway->completePurchase(array(
       'transactionId' => $orderNo, 
       'transactionReference' => $transactionReference, 
      ))->send(); 

      customFunctionToSaveStatus($orderNo, array('payment_status' => $response->getStatus())); 
      customFunctionToSaveMessage($orderNo, array('gateway_response' => $response->getMessage())); 

      //encrypt it to stop anyone being able to view other orders 
      $encodeOrderNo = customFunctionToEncodeOrderNo($orderNo); 
      $response->confirm('/payment-gateway-response/' . $encodeOrderNo); 

     } catch(InvalidResponseException $e) { 
      // Send "INVALID" response back to SagePay. 
      $request = $this->gateway->completePurchase(array()); 
      $response = new \Omnipay\SagePay\Message\ServerCompleteAuthorizeResponse($request, array()); 

      customFunctionToSaveStatus($orderNo, array('payment_status' => $response->getStatus())); 
      customFunctionToSaveMessage($orderNo, array('gateway_response' => $response->getMessage())); 

      redirect('/payment-error-response/'); 
     } 



    } 


    public function paymentResponse($encodedOrderNo) 
    { 
     $orderNo = customFunctionToDecode($encodedOrderNo); 
     $sessionOrderNo = customFunctionToGetOrderNo(); 
     if ($orderNo != $sessionOrderNo) : 
      //do something here as someone is trying to fake a successful order 
     endif; 
     $status = customFunctionToGetOrderStatus($orderNo); 

     switch(strtolower($status)) : 
      case 'ok' : 
       customFunctionToHandleSuccess($orderNo); 
      break; 

      case 'rejected' : 
      case 'notauthed' : 
       //do something to handle failed payments 
      break; 

      case 'error' : 
       //do something to handle errors 

      break; 

      default: 

       //do something if it ever reaches here 

     endswitch; 


    } 

} 
+0

我看到訂單號在返回URL中被加密。另一種方法是不在該URL上提供任何參數,而是在調用通知處理程序之前依靠訂單號保存到會話中。關於錯誤的「重定向」也是錯誤的 - 在這裏沒有重定向。您必須調用'$ response-> error('your url','您的可選信息');'而不是,然後將URL返回給SagePay,SagePay會將用戶發送給您。 – Jason 2015-07-07 18:37:39

1

我昨晚做了談這一點,已經把工作演示腳本在GitHub上的位置:

https://github.com/academe/OmniPay-SagePay-Demo

SagePay Direct是一次性的行動 - OmniPay發送交易細節並立即得到迴應。

SagePay服務器涉及用戶重定向到SagePay網站,以使用他們的卡詳細信息授權交易。此API使用通知消息,其中SagePay將直接使用授權結果調用您的應用程序。這發生在用戶會話之外,因此需要將事務存儲在數據庫中,以便可以在兩個事務之間共享。

所有這些都在上面鏈接的腳本中。 authorize.php將執行授權。編輯該文件以使用SagePay\DirectSagePay\Server來查看它是如何工作的。 SagePay\Server的通知處理程序是sagepay-confirm.php,最終將用戶發送到final.php,其中可以從存儲在數據庫中的事務中讀取結果。

腳本都是註釋,應該是有道理的,但隨時在這裏或在該github存儲庫的問題跟蹤器中提出更多問題。

雖然我沒有嘗試使用3D-Secure的SagePay\Direct。這些腳本可能需要一些修改才能支持,假設組合是的東西

相關問題