2015-03-31 24 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。这些脚本可能需要一些修改才能支持,假设组合是的东西

相关问题