itvisionsy/payfort-php-sdk

PayFort PHP SDK

dev-master 2017-04-20 08:24 UTC

This package is not auto-updated.

Last update: 2024-09-15 02:25:43 UTC


README

1 安装

1.1 使用 Composer 自动安装

只需执行以下命令 composer require itvisionsy/payfort-php

1.2 手动使用 Composer

您需要将 ItvisionSy\Payment\PayFort\ 添加为 PSR-4 命名空间,并指向库根目录的路径,同时在文件部分添加 autoload.php 文件。假设您有以下结构

-/              <= Project root
  - Libraries/
    - PayFort/  <= This is the payment library
  composer.json <= The composer.json config file

composer.json 文件中,您需要以下定义

  "autoload": {
    "psr-4": {
      "ItvisionSy\\Payment\\PayFort\\": "Libraries/PayFort/src/"
    },
    "files": [
      "Libraries/PayFort/autoload.php"
    ]
  }

1.3 手动使用 Autoload.php

在使用库之前,您需要引入 autoload.php 文件。

2 配置

您可以通过以下方式之一配置支付

  1. 在服务器根目录下创建名为 payfort_config.php 的文件。该文件应返回一个配置数组。以下是该文件内容的示例。
    <?php return [
        'sandbox' => true,
        'merchant_identifier' => 'payfort_merchant_identifier_string',
        'access_code' => 'payfort_merchant_access_code_here',
        'language' => \ItvisionSy\Payment\PayFort\Config::LANG_EN,
        'sha_type' => \ItvisionSy\Payment\PayFort\Config::SHA_TYPE_SHA256,
        'sha_request_phrase' => 'request_phrase_here',
        'sha_response_phrase' => 'response_phrase_here',
        'response_url_tokenization' => 'http://your_redirect_back_url_to_receive_tokenization_result',
        'response_url_purchase' => 'http://your_redirect_back_url_to_receive_purchase_result',
        'response_url_authorization' => 'http://your_redirect_back_url_to_receive_authorize_result',
        'send_as_normal_http_post' => true,
        'model_loader' => $your_model_loader_callable
    ];
  2. 在使用任何支付功能或方法之前,使用公共静态方法 ItvisionSy\Payment\PayFort\Config::setDefaultConfig() 并传入配置值数组。以下是该方法的用法示例
    <?php
    \ItvisionSy\Payment\PayFort\Config::setDefaultConfig([
        'sandbox' => true,
        'merchant_identifier' => 'payfort_merchant_identifier_string',
        'access_code' => 'payfort_merchant_access_code_here',
        'language' => \ItvisionSy\Payment\PayFort\Config::LANG_EN,
        'sha_type' => \ItvisionSy\Payment\PayFort\Config::SHA_TYPE_SHA256,
        'sha_request_phrase' => 'request_phrase_here',
        'sha_response_phrase' => 'response_phrase_here',
        'response_url_tokenization' => 'http://your_redirect_back_url_to_receive_tokenization_result',
        'response_url_purchase' => 'http://your_redirect_back_url_to_receive_purchase_result',
        'response_url_authorization' => 'http://your_redirect_back_url_to_receive_authorize_result',
        'send_as_normal_http_post' => true,
        'model_loader' => $your_model_loader_callable
    ]);
  3. 您可以手动初始化配置对象并将其存储在 $GLOBALS 数组中的 payfort_config。以下是该方法的示例
    <?php
    $GLOBALS['payfort_config'] = \ItvisionSy\Payment\PayFort\Config::make()
        ->setSandbox(true)
        ->setMerchantIdentifier('payfort_merchant_identifier_string')
        ->setAccessCode('payfort_merchant_access_code_string')
        ->setLanguage(\ItvisionSy\Payment\PayFort\Config::LANG_AR)
        ->setShaType(\ItvisionSy\Payment\PayFort\Config::SHA_TYPE_SHA256)
        ->setShaRequestPhrase('request_phrase_here')
        ->setShaResponsePhrase('response_phrase_here')
        ->setResponseUrlTokenization('http://your_redirect_back_url_to_receive_tokenization_result')
        ->setResponseUrlPurchase('http://your_redirect_back_url_to_receive_purchase_result')
        ->setResponseUrlAuthorization('http://your_redirect_back_url_to_receive_authorize_result')
        ->setSendAsNormalHttpPost(true)
        ->setModelLoader($your_model_loader_callable);
  4. 您可以定义一个公共辅助函数,称为 payfort_config,该函数始终返回 \ItvisionSy\Payment\PayFort\Config 对象。以下是一个示例
    <?php
    function payfort_config(){
        return \ItvisionSy\Payment\PayFort\Config::make()
            ->setSandbox(true)
            ->setMerchantIdentifier('payfort_merchant_identifier_string')
            ->setAccessCode('payfort_merchant_access_code_string')
            ->setLanguage(\ItvisionSy\Payment\PayFort\Config::LANG_AR)
            ->setShaType(\ItvisionSy\Payment\PayFort\Config::SHA_TYPE_SHA256)
            ->setShaRequestPhrase('request_phrase_here')
            ->setShaResponsePhrase('response_phrase_here')
            ->setResponseUrlTokenization('http://your_redirect_back_url_to_receive_tokenization_result')
            ->setResponseUrlPurchase('http://your_redirect_back_url_to_receive_purchase_result')
            ->setResponseUrlAuthorization('http://your_redirect_back_url_to_receive_authorize_result')
            ->setSendAsNormalHttpPost(true)
            ->setModelLoader($your_model_loader_callable);
    }

3 使用

3.1 理解逻辑流程

PayFort 有两种主要类型的操作:基于令牌的操作和基于 ID 的操作。

  • 基于令牌的操作是 AUTHORIZEPURCHASE,它们用于向信用卡收费。成功后,它们返回一个 fort_id 字符串,该字符串用于基于 ID 的操作。
  • 基于 ID 的操作主要是任何修改、完成、查询或取消基于令牌操作的操作。它们是 CAPTURE AUTHORIZE 操作的全部或部分金额,或 VOID 它。REFUND 任何已捕获的金额。以及其他维护操作。

基于令牌的操作需要一个特定信用卡信息的令牌。为此,需要向 PayFort 发送一个包含信用卡信息和商家身份的全 HTTP POST 请求。如果成功,它将返回该信用卡的令牌字符串。此令牌可以在以后的操作中使用,以相同用户的身份。之后,所有剩余的操作都是正常的 HTTP API 调用操作,除非有第三方安全检查,这将需要重定向周期。PURCHASEAUTHORIZE 操作都需要使用令牌。如果您已经为用户的信用卡生成了一个令牌,您可以直接跳过令牌化周期并直接进入购买过程。

3.2 如何使用

有两种方法

  1. 扩展所需操作的抽象类并实现成功/失败函数。
  2. 调用所需操作的实现并传递成功/失败的可调用对象。

授权

所有内容均根据 (MIT 许可证)[LICENSE] 发布

感谢

JetBrains 为开源项目提供的所有产品许可证。