Coder Social home page Coder Social logo

yurunsoft / paysdk Goto Github PK

View Code? Open in Web Editor NEW
888.0 40.0 186.0 408 KB

PHP 集成支付 SDK ,集成了支付宝、微信支付的支付接口和其它相关接口的操作。支持 php-fpm 和 Swoole,所有框架通用。宇润PHP全家桶技术支持群:17916227

License: MIT License

PHP 99.93% HTML 0.07%
php payment paysdk swoole coroutine alipaysdk alipay-sdk weixin-pay

paysdk's Introduction

PaySDK

Latest Version Php Version IMI Doc IMI License

介绍

PaySDK 是 PHP 集成支付 SDK ,集成了支付宝、微信支付的支付接口和其它相关接口的操作。

无框架依赖,支持所有框架,支持 Swoole 协程环境。

我们有完善的在线技术文档:http://doc.yurunsoft.com/PaySDK

API 文档:https://apidoc.gitee.com/yurunsoft/PaySDK

此项目进入维护阶段,不会支持微信V3接口,懒得跟着他们折腾,如有需要请找别处!

作者本人不提供任何技术支持,如有需要可以加群讨论:17916227 点击加群,有没有人回复就不一定了。

大家在开发中肯定会对接各种各样的支付平台,我个人精力有限,欢迎各位来 Github 提交 PR,一起完善 PaySDK ,让它能够支持更多的支付平台,更加稳定可靠好用。

支持的支付接口

支付宝

  • 即时到账-电脑网站支付(老)
  • 即时到账-手机网站支付(老)
  • 当面付
  • 手机网站支付
  • 电脑网站支付
  • APP支付服务端
  • 小程序支付
  • 单笔转账到支付宝账户
  • 海外支付(电脑网站、手机网站、APP、扫码)
  • 海关报关
  • 其它辅助交易接口(退款、查询等)

微信支付

  • 刷卡支付
  • 公众号支付
  • 扫码支付
  • APP支付
  • H5支付
  • 小程序支付
  • 企业付款到零钱
  • 企业付款到银行卡
  • 海外支付(刷卡、公众号、扫码、APP)
  • 海关报关
  • 其它辅助交易接口(退款、查询等)

安装

在您的composer.json中加入配置:

PHP >= 5.5

{
    "require": {
        "yurunsoft/pay-sdk": "~3.0"
    }
}

PHP >= 5.4

{
    "require": {
        "yurunsoft/pay-sdk": "~2.0"
    }
}

3.x 版本支持 PHP >= 5.5,持续迭代维护中

2.x 版本支持 PHP >= 5.4,支持长期 BUG 维护,保证稳定可用,停止功能性更新

然后执行composer update命令。

代码示例

支付宝即时到账

// SDK实例化,传入公共配置
$pay = new \Yurun\PaySDK\Alipay\SDK($params);

// 支付接口
$request = new \Yurun\PaySDK\Alipay\Params\Pay\Request;
$request->notify_url = ''; // 支付后通知地址(作为支付成功回调,这个可靠)
$request->return_url = ''; // 支付后跳转返回地址
$request->businessParams->seller_id = $GLOBALS['PAY_CONFIG']['appid']; // 卖家支付宝用户号
$request->businessParams->out_trade_no = 'test' . mt_rand(10000000,99999999); // 商户订单号
$request->businessParams->total_fee = 0.01; // 价格
$request->businessParams->subject = '测试商品'; // 商品标题

// 跳转到支付页面
// $pay->redirectExecute($request);

// 获取跳转url
$pay->prepareExecute($request, $url);
var_dump($url);

支付宝手机网站支付

// SDK实例化,传入公共配置
$pay = new \Yurun\PaySDK\AlipayApp\SDK($params);

// 支付接口
$request = new \Yurun\PaySDK\AlipayApp\Wap\Params\Pay\Request;
$request->notify_url = ''; // 支付后通知地址(作为支付成功回调,这个可靠)
$request->return_url = ''; // 支付后跳转返回地址
$request->businessParams->out_trade_no = 'test' . mt_rand(10000000,99999999); // 商户订单号
$request->businessParams->total_amount = 0.01; // 价格
$request->businessParams->subject = '小米手机9黑色陶瓷尊享版'; // 商品标题

// 跳转到支付页面
// $pay->redirectExecute($request);

// 获取跳转url
$pay->prepareExecute($request, $url);
var_dump($url);

微信H5支付

// SDK实例化,传入公共配置
$pay = new \Yurun\PaySDK\Weixin\SDK($params);

// 支付接口
$request = new \Yurun\PaySDK\Weixin\H5\Params\Pay\Request;
$request->body = 'test'; // 商品描述
$request->out_trade_no = 'test' . mt_rand(10000000,99999999); // 订单号
$request->total_fee = 1; // 订单总金额,单位为:分
$request->spbill_create_ip = '127.0.0.1'; // 客户端ip
$request->notify_url = ''; // 异步通知地址

// 调用接口
$result = $pay->execute($request);
if($pay->checkResult())
{
    // 跳转支付界面
    header('Location: ' . $result['mweb_url']);
}
else
{
    var_dump($pay->getErrorCode() . ':' . $pay->getError());
}
exit;

Swoole 协程环境支持

在支付、退款异步通知中,需要赋值 SwooleRequestResponse 对象,或者遵循 PSR-7 标准的对象即可。

主流框架的 RequestResponse 对象,一般都遵循 PSR-7 标准,可以直接使用。

imi 框架中使用

imi 是基于 PHP Swoole 的高性能协程应用开发框架,它支持 HttpApi、WebSocket、TCP、UDP 服务的开发。

在 Swoole 的加持下,相比 php-fpm 请求响应能力,I/O密集型场景处理能力,有着本质上的提升。

imi 框架拥有丰富的功能组件,可以广泛应用于互联网、移动通信、企业软件、云计算、网络游戏、物联网(IOT)、车联网、智能家居等领域。可以使企业 IT 研发团队的效率大大提升,更加专注于开发创新产品。

https://www.imiphp.com/

/**
 * 这是一个在控制器中的动作方法
 * @Action
 */
public function test()
{
    $payNotify = new class extends \Yurun\PaySDK\Weixin\Notify\Pay
    {
        /**
         * 后续执行操作
         * @return void
         */
        protected function __exec()
        {

        }
    };
    $context = RequestContext::getContext();
    // 下面两行很关键
    $payNotify->swooleRequest = $context['request'];
    $payNotify->swooleResponse = $context['response'];

    $sdk->notify($payNotify);

    // 这句话必须填写
    return $payNotify->swooleResponse;
}

其它框架(Swoole 对象)

$payNotify = new class extends \Yurun\PaySDK\Weixin\Notify\Pay
{
    /**
     * 后续执行操作
     * @return void
     */
    protected function __exec()
    {

    }
};
// 下面两行很关键,$request、$response 从 request 中获取
// 或者查阅如何从你使用的框架中获取
$payNotify->swooleRequest = $request;
$payNotify->swooleResponse = $response;

$sdk->notify($payNotify);

其它框架(PSR-7 对象)

$payNotify = new class extends \Yurun\PaySDK\Weixin\Notify\Pay
{
    /**
     * 后续执行操作
     * @return void
     */
    protected function __exec()
    {

    }
};
// 目前主流 Swoole 基本都支持 PSR-7 标准的对象
// 所以可以直接传入,如何获取请查阅对应框架的文档
$payNotify->swooleRequest = $request;
$payNotify->swooleResponse = $response;

$sdk->notify($payNotify);

// 处理完成后需要将 $response 从控制器返回或者赋值给上下文
// 不同框架的操作不同,请自行查阅对应框架的文档
return $payNotify->swooleResponse;

商业服务

服务内容:

  • 低费率开户(仅合法商户)
  • 问题排查及咨询
  • 代接支付
  • 其它合作

如有需要加QQ:369124067 微信:wx_zhangrunyu(注明来意)

捐赠

开源不求盈利,多少都是心意,生活不易,随缘随缘……

paysdk's People

Contributors

anxiaowen avatar lanshe avatar lrrun avatar phperben avatar sy-records avatar yurunsoft avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

paysdk's Issues

支付宝使用证书公钥匙提示

PHP Deprecated: Invalid characters passed for attempted conversion, these have been ignored in /vendor/yurunsoft/pay-sdk/src/Lib/CertUtil.php on line 86


/**
* 0x转高精度数字
* @param $hex
* @return int|string
*/
public static function hex2dec($hex)
{
$dec = 0;
$len = strlen($hex);
for ($i = 1; $i <= $len; $i++)
{
$dec = bcadd($dec, bcmul(strval(hexdec($hex[$i - 1])), bcpow('16', strval($len - $i))));
}
return $dec;
}

支付宝 AES 的支持有计划吗?

image

接支付宝的时候,感觉又一层加密会更安全,所以设置了 AES 的key。发现包不支持,支付宝还不支持取消。框架用的hyperf,现在只能用 class_map 替换需要加密和解密的类,总有种不踏实的感觉,希望官方可以原生支持。

另外还有几个接口要求强制开启 AES,如果业务有涉及用到。支付改起来也是灾难性的

没有HttpRequest类

base.php文件的构造方法中实例化HttpRequest类的时候报错,没有对应的类。

微信NATIVE支付问题

1.在Weixin\Notify\Base::getNotifyData()方法中,->getBody() ,应该要加上->getContents()

2.在Weixin\SDK::verifyCallback() ,if(!isset($data['return_code']) || 'SUCCESS' !== $data['return_code'] || !isset($data['sign'])) 这段代码, 实际在通知的时候微信并没有返回return_code给我,导致没有通过

开启aes加密时,支付宝方报参数错误

https://doc.yurunsoft.com/PaySDK/74
对接手机网站支付:在关掉aes加密时,一切正常,在打开加密时,支付宝方 报 参数出错。
https://static.dingtalk.com/media/lADPDgQ9rfGPQqnNBQDNAoA_640_1280.jpg_620x10000q90g.jpg?bizType=im
与支付宝客服沟通后,他们也不能判断paySDK中的加密方式是否有问题,并给了一个他们官方SDK的加密代码,如下:
I)0 K6032)I3WJ Y9RX5HOY
支付宝官方SDK地址:
https://docs.open.alipay.com/54/103419

回调通知的一点疑问

在hyperf中使用sdk,
微信回调通知中,
逻辑/业务都能正常处理,唯独一点
调用 $this->reply(true, 'OK')后,微信还是继续回调同一个通知.
不知道是不是我哪里用错了

在 WechatPayService中封装一个公共的handleNotify方法,

    /**
     * 处理微信回调
     * @param  int  $appId
     * @param  string  $payMethod
     * @param $client
     * @param  string  $handler
     * @return void
     * @throws \Throwable
     * @author Wayne<[email protected]>
     */
    public function handleNotify(int $appId, string $payMethod, string $client, string $handler)
    {
        $this->factory->initGateway($appId, $payMethod, null, $client)->notify($handler);
    }

然后在 WechatPayFactory中封装 初始化SDK的initGateway 和 通知处理方法 notify

nofity方法如下

public function notify(string $handler)
    {
        $handler = "\App\Service\PayNotifyHandlers\\$handler";
        $notify = new $handler();
        $notify->swooleRequest = $this->request;
        $notify->swooleResponse = $this->response;
        try {
            $this->gateway->notify($notify);
        } catch (\Throwable $exception) {
            throw $exception;
        }
    }

handler是自己封装的处理业务的通知类,
$this->gateway是 SDK的实例
其中的__exec方法中会有调用

$this->reply(true, 'OK');

但是调用了此方法后,微信依旧会继续对同一个订单进行回调

才刚学习swoole类框架,还请多执教

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.