soul-key / smallpay
小微支付配套PHP库
v2.0.0
2021-12-09 14:07 UTC
Requires
- php: >=7.1.0
This package is auto-updated.
Last update: 2025-09-09 22:48:26 UTC
README
小微支付的PHP版本SDK
一、安装
- 使用Composer v2安装,仓库
https://github.com/soul-key/smallpay
composer require soul-key/smallpay
/** * 发起支付示例 */ require __DIR__ . '/../vendor/autoload.php'; use SmallPay\Payment; //支付地址 $payUrl = "http://api.weituma.cn/api/pay/go"; //应用ID $appId = "10000002"; //应用秘钥 $appKey = "de872154ffbf91a5dcc0e539dd2d5106"; //设置支付相关 $client = new Payment($appId, $appKey); $client->SetPayCode(2001);//设置支付能力 $client->SetOutTradeNo("IMD".time());//设置商户订单号 $client->SetMoney(1); $client->SetGoodsName("测试商品"); $client->SetNotifyUrl("http://www.baidu.com"); $client->SetReturnUrl("http://www.baidu.com"); //发送支付请求(正常返回json格式数据) $res = $client->Send($payUrl); $res = json_decode($res, true); if (isset($res['data']['url'])) { //返回的支付url 经过了 urlencode 转码,故此处要解码 $pay_url = urldecode($res['data']['url']); header("location:" . $pay_url); } else { var_dump($res); } die;
/** * 支付异步通知示例 */ require __DIR__ . '/../vendor/autoload.php'; use SmallPay\Payment; //应用ID $appId = "10000002"; //应用秘钥 $appKey = "de872154ffbf91a5dcc0e539dd2d5106"; $client = new Payment($appId, $appKey); //验签 $check = $client->VerifySign($_POST); if ($check) { //执行商户的逻辑 //处理完成返回如下 json echo json_encode(['state'=>"SUCCESS"]); }else { //验签失败 echo json_encode(['state' => "FAIL"]); exit(); }
/** * 退款示例 */ require __DIR__ . '/../vendor/autoload.php'; use SmallPay\Refund; $notifyUrl = "http://api.weituma.cn/api/pay/refund"; //应用ID $appId = "10000002"; //应用秘钥 $appKey = "de872154ffbf91a5dcc0e539dd2d5106"; $client = new Refund($appId, $appKey); $client->SetOutTradeNo("IMD121545112545"); $res = $client->Send($notifyUrl); var_dump($res); //{"code":1,"msg":"订单无效","data":""} //{"code":0,"msg":"申请成功","data":""} exit();