chriskivaze/iveri-php

Iveri API的包装器

0.6.2 2016-11-14 14:38 UTC

This package is auto-updated.

Last update: 2024-09-28 22:47:53 UTC


README

Latest Stable Version Latest Unstable Version Total Downloads License

Iveri Enterprise集成API包

  • 由Stephen Lake编写和编写文档,采用MIT许可证
  • 版权所有 Stephen Lake 2016

要求

依赖项(由Composer处理)

  • guzzlehttp/guzzle ^6.2
  • illuminate/validation ^5.3
  • illuminate/support ^5.3
  • illuminate/translation ^5.3
  • ramsey/uuid ^3.5
  • stephenlake/centinel ^1.3

用法

完整3DSecured交易的示例

创建一个包含您的Iveri账户详情的新配置实例

use StephenLake\Iveri\Objects\Configuration;

$configuration = new Configuration;
$configuration->setIveriUserGroupId('<your-user-group>')
              ->setIveriUsername('<your-backoffice-username>')
              ->setIveriPassword('<your-backoffice-password>')
              ->setIveriApplicationId('<your-test-application-id>')
              ->setIveriCertificateId('<your-test-certificate-id>')
              ->setIveriMerchantId('<your-merchant-id>')
              ->setIveriApiLive(false)
              ->setIveriCmpiProcessorId(1000)
              ->setIveriCmpiPassword('<your-cmpi-password>')
              ->build();

注意:在构建之前,Configuration将不会构建,并且不能在Transaction实例中使用。如果未设置必需参数,您将收到一个描述缺少必需参数的ConfigurationValidateException

创建一个新的交易实例,具有标准交易事件监听器

use StephenLake\Iveri\Objects\Transactions\ThreeDomainLookup;
use StephenLake\Iveri\Listeners\TransactionListener;

$ThreeDomainLookup = new ThreeDomainLookup(new TransactionListener());
$ThreeDomainLookup->setTransactionAmount('<amount-in-decimal-format'>)
                  ->setTransactionPanNumber('<pan>')
                  ->setTransactionPanExpiryMonth('<pan-expiry-month>') // MM
                  ->setTransactionPanExpiryYear('<pan-expiry-year>') // YYYY
                  ->setTransactionReference('<unique-transaction-reference>')
                  ->setTransactionCurrency('<currency-iso>') // Alpha ISO
                  ->build();

注意:在构建之前,Transaction将不会构建,并且不能在Iveri实例中使用。如果未设置必需参数,您将收到一个描述缺少必需参数的TransactionValidateException

创建Iveri实例并将配置和交易相关联

use use StephenLake\Iveri\Iveri;

$IveriServiceAPI = new Iveri($configuration);
$IveriServiceAPI->setTransaction($ThreeDomainLookup)
                ->submitTransaction();

此时,您的查找请求已准备好并返回结果。

处理结果

if ($ThreeDomainLookup->succeeds()) {

  if($ThreeDomainLookup->isThreeDomainSecured()) {
  
    // Handle 3DSecure Process
    // See 'Handling 3DSecure'
    
  } else {
    
    // Submit Transaction Request
    // See 'Submitting Debit Transactions'
    
  }

} else {

  $errorCode    = $ThreeDomainLookup->getTransactionResult()->getErrorCode();
  $errorMessage = $ThreeDomainLookup->getTransactionResult()->getErrorMessage();
  
}

处理3DSecure

注意此包不使用Iveri的3DSecure Lookup API,而是使用Centinel API。这是由于从Iveri收到的支持不足,自2015年12月以来,我已经多次亲自联系,但尚未收到回复(当前日期为2016年10月19日)。请参阅要求以获取3DSecure查找和认证的凭据。

万事达卡的名为“SecureCode”,维萨卡的名为“Verified by Visa”。3D-Secure指的是涉及安全的三个领域。它们是获取方或商家的银行、卡协会的金融网络(即万事达卡和维萨卡)以及发行方或持卡人的银行。

当我们对PAN执行3DSecure查找并通知我们PAN已注册3DSecure时,我们将收到包含threeDomainEnrolledthreeDomainACSUrlthreeDomainPAREQ的有效负载。

为了向客户显示3DSecure授权页面,我们需要向上述接收到的threeDomainACSUrl提交表单数据,这将在一个iframe中渲染3DSecure视图。

一旦客户完成3DSecure身份验证,3DSecure过程将提交一个HTTP POST将结果返回到您提供的端点,我们将在下面进行描述。

创建必要的变量

重要 在此阶段,您需要保存交易的记录,因为您需要将用户重定向到另一个端点,该端点将销毁您在此点的任何变量。首选方法是存储在临时缓存中的交易,因为它会自动销毁,通常您不想处理敏感数据的存储。

  $threeDomainSecureACSUrl       = $ThreeDomainLookup->getTransactionResult()->getThreeDomainACSUrl();
  $threeDomainSecurePAREQ        = $ThreeDomainLookup->getTransactionResult()->getThreeDomainPAREQ();
  $threeDomainSecureID           = $ThreeDomainLookup->getTransactionIdentifier();
  $threeDomainSecureTerminateURL = 'https://your-domain-name.moc/3dsecure/terminate';
  
  
  // Cache the transaction data, because we're about to lose it and we need it later
  // Pseudocode Example:
  cache('identifer', $threeDomainSecureID)->setData([
      'panHolderName'                => $ThreeDomainLookup->getTransactionPanHolderName(),
      'panNumber'                    => $ThreeDomainLookup->getTransactionPanNumber(),
      'panSecurityCode'              => $ThreeDomainLookup->getTransactionPanSecurityCode),
      'panExpiryMonth'               => $ThreeDomainLookup->getTransactionPanExpiryMonth(),
      'panExpiryYear'                => $ThreeDomainLookup->getTransactionPanExpiryYear(),
      'transactionIndex'             => $ThreeDomainLookup->getTransactionIndex(),
      'transactionReference'         => $ThreeDomainLookup->getTransactionReference(),
      'currency'                     => $ThreeDomainLookup->getTransactionCurrency(),
      'amount'                       => $ThreeDomainLookup->getTransactionAmount(),
  ]);
  • threeDomainSecureACSUrl是表单必须POST到的3DSecure URL。这是在执行PAN上的3DSecure查找时接收到的。

  • threeDomainSecurePAREQ 是在执行对 PAN 的 3DSecure 查询时接收的请求令牌,必须作为表单的一部分提交。

  • threeDomainSecureTerminateURL 是 3DSecure 流程将提交包含 3DSecure 结果的 HTTP POST 的 URL。请确保您拥有此端点并且它接受 POST。

  • threeDomainSecureID 应该是一个您定义的唯一标识符,它将在客户完成认证后从 3DSecure 流程中返回,并通过发送到您的 threeDomainSecureTerminateURLHTTP POST 接收。

创建视图

创建 HTML IFrame,该 IFrame 将包含在提交到 threeDomainSecureACSUrl 的 POST 之后接收到的 3DSecure 表单。

<iframe 
  id="3dsecure_iframe" 
  name="3dsecure_iframe" 
  marginwidth="0" 
  marginheight="0" 
  hspace="0" 
  vspace="0" 
  frameborder="0" 
  scrolling="no" 
  frameBorder="0" 
  width="420px" 
  height="700px"
>
</iframe>

创建 HTML 表单,该表单将提交 POSTthreeDomainSecureACSUrl 并初始化 IFrame 中的 3DSecure 表单。

出于清晰起见,此示例使用模板引擎在视图中渲染变量,在您的代码中,您可以使用如 <?php echo $exampleVar ?> 之类的纯 PHP 语法或您可能正在使用的任何框架。

<form method="POST" action="{{ $threeDomainSecureACSUrl }}" target="3dsecure_iframe">

    <input type="hidden" name="PaReq" value="{{ $threeDomainSecurePAREQ }}">
    
    <input type="hidden" name="TermUrl" value="{{ $threeDomainSecureTerminateURL }}"> />
    <input type="hidden" name="MD" value="{{ $threeDomainSecureID }}" />
   
    <br/>
    
    <button type="submit">PROCEED TO 3DSECURE</button>
</form>
完成 3DSecure 交易

无论 3DSecure 流程是否失败,有效负载都将提交到提供的 threeDomainSecureTerminateURL。在此端点,您的代码必须处理接收到的响应,该响应将包含两个字段

  • MD:您提供的用于检索已保存交易并完成它的唯一标识符。
  • PaRes:将用于授权交易的 Payment Authorization Response。

此时,已通过 HTTP POST 向您的 URL(threeDomainSecureTerminateURL)发送,您的 Iveri API、配置和交易实例已丢失,因此我们需要使用 MD 来重建,它是我们交易的唯一标识符。

您的 threeDomainSecureTerminateURL 应包含如下内容(使用纯 PHP)

  use StephenLake\Iveri\Iveri;
  use StephenLake\Iveri\Objects\Configuration;
  use StephenLake\Iveri\Objects\Transactions\ThreeDomainAuthorise;
  use StephenLake\Iveri\Objects\Transactions\Debit;
  use StephenLake\Iveri\Listeners\TransactionListener;

  // Fetch the data that was posted to this endpoint
  $transactionIdentifier  = $_POST['MD'];
  $threeDomainSecurePARES = $_POST['PaRes'];
    
  // Fetched your cached transaction data - Pseudocode Example:
  // Depending on how you stored your lost transaction data, fetch it.
  $cachedTransactionData = cache->getWhere('identifier', '=', $transactionIdentifier);
  
  // Create new transaction instance for 3DS_AUTHORIZE
  $ThreeDomainAuthorise = new ThreeDomainAuthorise(new TransactionListener());
  $ThreeDomainAuthorise->setTransactionThreeDomainServerPARES(Input::get('PaRes'))
                       ->setTransactionIndex($transactionIdentifier)
                       ->setTransactionAmount($cachedTransactionData['amount'])
                       ->setTransactionPanNumber($cachedTransactionData['pan'])
                       ->setTransactionPanExpiryMonth($cachedTransactionData['panExpiryMonth']) 
                       ->setTransactionPanExpiryYear($cachedTransactionData['panExpiryYear']) 
                       ->setTransactionReference($cachedTransactionData['transactionReference'])
                       ->setTransactionCurrency($cachedTransactionData['currency'])
                       ->build();
   
  // Submit the 3DS_AUTHORIZE request
  $IveriServiceAPI->setTransaction($ThreeDomainLookup)
                  ->submitTransaction();
         
  if ($ThreeDomainAuthorise->fails()) {

      // Something went wrong with the 3DSecure Authorization
      // Cannot continue with transaction
      
      $errorCode    = $ThreeDomainLookup->getTransactionResult()->getErrorCode();
      $errorMessage = $ThreeDomainLookup->getTransactionResult()->getErrorMessage();

  } else {

     // Perform the transction settlement
     $Debit = new Debit(new TransactionListener);
     $Debit->setTransactionPanHolderName($cachedTransactionData['panHolderName'])
           ->setTransactionReference($cachedTransactionData['transactionReference'])
           ->setTransactionPanCode($cachedTransactionData['panSecurityCode'])
           ->setTransactionCurrency($cachedTransactionData['currency'])
           ->setTransactionAmount($cachedTransactionData['amount'])                   
           ->setTransactionPanNumber($cachedTransactionData['panNumber'])            
           ->setTransactionPanExpiryMonth($cachedTransactionData['panExpiryMonth'])
           ->setTransactionPanExpiryYear($cachedTransactionData['panExpiryYear'])
           ->setTransactionIndex($cachedTransactionData['transactionIndex'])  
           ->build();

     $IveriServiceAPI->setTransaction($Debit)
                     ->submitTransaction();

   if ($Debit->succeeds()) {
   
      // The payment succeeded
      $paymentDetail = $Debit->getTransactionResult()->getTransactionDetail();
      
   } else {
   
      // Something went wrong with the settlement
      // handle errors as you like
      
      $errorCode    = $ThreeDomainLookup->getTransactionResult()->getErrorCode();
      $errorMessage = $ThreeDomainLookup->getTransactionResult()->getErrorMessage();
      
   }
   
  }
   

扩展交易监听器

在构建 Transaction 实例时,您必须传递一个 TransactionListener 实例,该实例在特定的交易条件下触发事件。您可以创建自己的 TransactionListener 并通过扩展默认的 TransactionListener 接收这些事件的通知,如下所示。

class CustomTransactionListener extends TransactionListener {
  public function threeDomainLookupPrepared(Transaction $transaction){}
  public function threeDomainLookupInitiated(Transaction $transaction){}
  public function threeDomainLookupFailed(Transaction $transaction){}
  public function threeDomainLookupSucceeded(Transaction $transaction){}
  public function threeDomainAuthorizePrepared(Transaction $transaction){}
  public function threeDomainAuthorizeInitiated(Transaction $transaction){}
  public function threeDomainAuthorizeFailed(Transaction $transaction){}
  public function threeDomainAuthorizeSucceeded(Transaction $transaction){}
  public function debitPrepared(Transaction $transaction){}
  public function debitInitiated(Transaction $transaction){}
  public function debitFailed(Transaction $transaction){}
  public function debitSucceeded(Transaction $transaction){}
}

然后按如下方式设置交易以使用您自定义的 TransactionListener

new ThreeDomainLookup(new CustomTransactionListener);

$ThreeDomainLookup->setTransactionListener(new CustomTransactionListener)