openpayu/openpayu_php_sdk

OpenPayU PHP 库

2.3.6 2023-07-27 07:47 UTC

README

OpenPayU PHP 库提供对 REST API 2.1 的集成访问

依赖关系

PHP >= 5.3,需要扩展 cURLhash

文档

完整实现指南: 英文波兰语

要处理以下操作

您需要提供一个名为 orderId 的参数。orderId 的值是 PayU 支付系统设置的订单标识符,用于调用远程方法。

获取 orderId 有两种方式

  1. 它存在于 PayU 支付系统支付结果通知消息中。
  2. 在 OpenPayU_Order::create 方法的响应中。

在两种情况下,您都可以使用以下语句找到 orderId: $response->getResponse()->orderId

安装

Composer

要使用 Composer 安装,只需将依赖项添加到 composer.json 文件中

{
  "require" : {
    "openpayu/openpayu" : "2.3.*"
  }
}

然后运行以下命令进行安装

composer.phar install

手动安装

使用以下命令获取 openpayu_php SDK 最新版本

git clone https://github.com/PayU/openpayu_php.git

入门

如果您使用 Composer,请使用自动加载功能

include "vendor/autoload.php";

或者在应用程序的任何位置添加以下行

    require_once 'lib/openpayu.php';
    require_once realpath(dirname(__FILE__)) . '/../../config.php';

配置

重要:SDK 只与 'REST API'(结账)销售点(POS)一起使用。如果您还没有 PayU 商户账户,请在生产环境中注册在沙盒环境中注册

来自商户面板的“配置密钥”示例

pos_configuration

要配置 OpenPayU 环境,您必须在 config.php 文件中提供一组必填数据。

生产环境

    //set Production Environment
    OpenPayU_Configuration::setEnvironment('secure');

    //set POS ID and Second MD5 Key (from merchant admin panel)
    OpenPayU_Configuration::setMerchantPosId('145227');
    OpenPayU_Configuration::setSignatureKey('13a980d4f851f3d9a1cfc792fb1f5e50');

    //set Oauth Client Id and Oauth Client Secret (from merchant admin panel)
    OpenPayU_Configuration::setOauthClientId('145227');
    OpenPayU_Configuration::setOauthClientSecret('12f071174cb7eb79d4aac5bc2f07563f');    

沙盒环境

    //set Sandbox Environment
    OpenPayU_Configuration::setEnvironment('sandbox');

    //set POS ID and Second MD5 Key (from merchant admin panel)
    OpenPayU_Configuration::setMerchantPosId('300046');
    OpenPayU_Configuration::setSignatureKey('0c017495773278c50c7b35434017b2ca');

    //set Oauth Client Id and Oauth Client Secret (from merchant admin panel)
    OpenPayU_Configuration::setOauthClientId('300046');
    OpenPayU_Configuration::setOauthClientSecret('c8d4b7ac61758704f38ed5564d8c0ae0');

如果您想使用沙盒环境,请在此链接注册https://secure.snd.payu.com/cp/register?lang=en

OAuth 配置

SDK 支持两种 PayU OAuth 授权类型: client_credentialstrusted_merchant。默认为 client_credentials

如果您想更改授权类型,请使用

    OpenPayU_Configuration::setOauthGrantType('grant_type');

grant_type 可以是以下之一 OauthGrantType::CLIENT_CREDENTIALOauthGrantType::TRUSTED_MERCHANT

对于 client_credentials 需要的参数

    //set Oauth Client Id and Oauth Client Secret (from merchant admin panel)
    OpenPayU_Configuration::setOauthClientId('300046');
    OpenPayU_Configuration::setOauthClientSecret('c8d4b7ac61758704f38ed5564d8c0ae0');

对于 trusted_merchant 需要的参数

    //set Oauth Client Id and Oauth Client Secret (from merchant admin panel)
    OpenPayU_Configuration::setOauthClientId('clent_id');
    OpenPayU_Configuration::setOauthClientSecret('clent_secret');

    //set Oauth Email and Oauth Ext Customer Id
    OpenPayU_Configuration::setOauthEmail('email');
    OpenPayU_Configuration::setOauthExtCustomerId('ext_customer_id');

通过代理连接

    OpenPayU_Configuration::setProxyHost('address');
    OpenPayU_Configuration::setProxyPort(8080);
    OpenPayU_Configuration::setProxyUser('user');
    OpenPayU_Configuration::setProxyPassword('password');

缓存

OpenPayU 库自动将 OAuth 认证数据存储在缓存中。

OpenPayU 库实现了两个类来管理缓存

  • OauthCacheFile - 数据存储在文件系统中。这是默认和自动缓存方法,数据存储在 lib/Cache 文件夹中。**注意:出于安全原因,建议更改缓存文件夹,以便它无法从网络浏览器访问。**

    配置

    OpenPayU_Configuration::setOauthTokenCache(new OauthCacheFile($directory));

    $directory - 数据文件夹的绝对路径;如果参数不存在,则文件夹为 lib/Cache

  • OauthCacheMemcached - 数据存储在Memcached中。此方法需要服务器上安装Memcached(https://memcached.org.cn/)以及Memcached PHP模块(https://php.ac.cn/manual/en/book.memcached.php

    配置

    OpenPayU_Configuration::setOauthTokenCache(new OauthCacheMemcached($host, $port, $weight));

    $host - Memcached服务器地址 - 默认为 localhost $port - Memcached服务器端口 - 默认为 11211 $weight - Memcached服务器优先级 - 默认为 0

可以实现另一种缓存管理方法。在这种情况下,需要实现 OauthCacheInterface

用法

记住:在“顺序数组”中的所有键必须是小写。

使用REST API创建订单

包含工作示例的文件:examples/v2/order/OrderCreate.php

要在后端使用REST API创建订单,必须在控制器中提供一个包含订单数据的数组

    $order['continueUrl'] = 'http://localhost/'; //customer will be redirected to this page after successfull payment
    $order['notifyUrl'] = 'http://localhost/';
    $order['customerIp'] = $_SERVER['REMOTE_ADDR'];
    $order['merchantPosId'] = OpenPayU_Configuration::getMerchantPosId();
    $order['description'] = 'New order';
    $order['currencyCode'] = 'PLN';
    $order['totalAmount'] = 3200;
    $order['extOrderId'] = '1342'; //must be unique!

    $order['products'][0]['name'] = 'Product1';
    $order['products'][0]['unitPrice'] = 1000;
    $order['products'][0]['quantity'] = 1;

    $order['products'][1]['name'] = 'Product2';
    $order['products'][1]['unitPrice'] = 2200;
    $order['products'][1]['quantity'] = 1;

    //optional section buyer
    $order['buyer']['email'] = 'dd@ddd.pl';
    $order['buyer']['phone'] = '123123123';
    $order['buyer']['firstName'] = 'Jan';
    $order['buyer']['lastName'] = 'Kowalski';

    $response = OpenPayU_Order::create($order);

    header('Location:'.$response->getResponse()->redirectUri); //You must redirect your client to PayU payment summary page.

从OpenPayU检索订单

包含工作示例的文件:examples/v2/order/OrderRetrieve.php

您可以通过其PayU订单_id检索订单

    $response = OpenPayU_Order::retrieve('Z963D5JQR2230925GUEST000P01'); //as parameter use orderId

从OpenPayU检索订单的交易

包含工作示例的文件:examples/v2/order/OrderTransactionRetrieve.php

您可以通过其PayU订单_id检索订单的交易

    $response = OpenPayU_Order::retrieveTransaction('Z963D5JQR2230925GUEST000P01'); //as parameter use orderId

取消订单

包含工作示例的文件:examples/v2/order/OrderCancel.php

您可以通过其PayU订单_id取消订单

    $response = OpenPayU_Order::cancel('Z963D5JQR2230925GUEST000P01'); //as parameter use orderId

更新订单状态

包含工作示例的文件:examples/v2/order/OrderStatusUpdate.php

您可以将订单状态更新为接受订单。

    $status_update = array(
        "orderId" => 'Z963D5JQR2230925GUEST000P01', //as value use ORDER_ID
        "orderStatus" => 'COMPLETED'
    );

    $response = OpenPayU_Order::statusUpdate($status_update);

处理来自PayU的通知

包含工作示例的文件:examples/v2/order/OrderNotify.php

当订单状态改变时,PayU会向您的应用程序发送请求

    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
        $body = file_get_contents('php://input');
        $data = trim($body);

        $response = OpenPayU_Order::consumeNotification($data);
        $response->getResponse()->order->status; //NEW PENDING CANCELED REJECTED COMPLETED WAITING_FOR_CONFIRMATION

        header("HTTP/1.1 200 OK");
    }

退款

包含工作示例的文件:examples/v2/refund/RefundCreate.php

您可以为买家账户创建退款。

    $refund = OpenPayU_Refund::create(
        'Z963D5JQR2230925GUEST000P01', //as a value use ORDER_ID
        'Money refund', //Description - required
        '100', //Amount - If not provided, returns whole transaction, optional
        'ext-customer-id', // External submerchant ID, required only for marketplace
        'ext-refund-id' // External refund ID, required only for marketplace
    );

从POS获取支付方式

包含工作示例的文件:examples/v2/retrieve/RetrievePaymethods.php

您可以从POS检索支付方式。

    $response = OpenPayU_Retrieve::payMethods();

您可以在payMethods()中添加可选参数lang

    $response = OpenPayU_Retrieve::payMethods('en');

删除卡令牌

包含工作示例的文件:examples/v2/token/TokenDelete.php

您可以删除用户的卡令牌。

令牌删除仅适用于trusted_merchant授权类型。

    $refund = OpenPayU_Token::delete(
        'TOKC_EXAMPLE_TOKEN' // as a value use user card token
    );

获取商店

包含工作示例的文件:examples/v2/shops/Get.php

您可以检索商店数据。

    $shop = OpenPayU_Shop::get(
        'PUBLIC_SHOP_ID' // Shop ID from Merchant Panel
    );

贡献

  1. 分叉它
  2. 创建您的功能分支(git checkout -b my-new-feature
  3. 提交您的更改(git commit -am 'Add some feature'
  4. 将更改推送到分支(git push origin my-new-feature
  5. 创建新的Pull Request