收银呗隶属于武汉融商创银科技有限公司,融商创银是国内移动支付解决方案服务商,以支付为切点,涵盖商户收银、会员营销、财务管理、卡券核销、店铺运营分析和金融服务等功能,为商户开展数字化经营提供合理的移动支付解决方案和技术支持。
CRMEB开源商城系统是一款全开源可商用的系统,前后端分离开发,全部100%开源,在小程序、公众号、H5、APP、PC端都能用,使用方便,二开方便!
本章内容主要讲解crmeb接入收银呗支付平台的在线支付功能。收银呗支付可以完美解决crmeb在小程序、公众号、H5、APP、PC端上的支付服务。
以下以微信小程序的对接为例,代码如下:
namespace crmeb\services\pay\storage;
use app\services\pay\PayServices;
use crmeb\exceptions\AdminException;
use crmeb\exceptions\PayException;
use crmeb\services\pay\BasePay;
use crmeb\services\pay\PayInterface;
use crmeb\services\pay\extend\shouyinbeipay\ShouyinbeiPay as ShouyinbeiPayService;
use EasyWeChat\Payment\Order;
use think\facade\Event;
use think\facade\Log;
/**
* 收银呗
* Class ShouyinbeiPay
* @author 模板之家
* @V xu08290201
* @date 2023/2/1
* @package crmeb\services\pay\storage
*/
class ShouyinbeiPay extends BasePay implements PayInterface
{
/**
* @var ShouyinbeiPayService
*/
protected $pay;
/**
* @param array $config
* @return mixed|void
* @author 模板之家
* @V xu08290201
* @date 2024-05-10
*/
protected function initialize(array $config)
{
$this->pay = new ShouyinbeiPayService([
'orgId' => sys_config('shouyinbei_orgId'),
'appId' => sys_config('shouyinbei_appId'),
'privateKey' => sys_config('shouyinbei_private_key'),
'publicKey' => sys_config('shouyinbei_public_key'),
'businessCode' => sys_config('shouyinbei_businessCode'),
'subAppid' => sys_config('routine_appId'),
'notifyUrl' => trim(sys_config('site_url')) . '/api/pay/notify/shouyinbei',
'isBeta' => false,
]);
}
/**
* 创建支付
* @param string $orderId
* @param string $totalFee
* @param string $attach
* @param string $body
* @param string $detail
* @param array $options
* @return array|mixed
* @author 模板之家
* @V xu08290201
* @date 2024-05-10
*/
public function create(string $orderId, string $totalFee, string $attach, string $body, string $detail, array $options = [])
{
$this->authSetPayType();
$options['returl'] = sys_config('site_url') . '/pages/index/index';
if ($options['returl']) {
$options['returl'] = str_replace('http://', 'https://', $options['returl']);
}
$options['appid'] = sys_config('routine_appId');
switch ($this->payType) {
case Order::APP:
return $this->pay->appPay($totalFee, $orderId, $body, '', '', false, $attach);
case Order::JSAPI:
if (request()->isRoutine()) {
return $this->pay->miniproPay($totalFee, $orderId, $body, $attach, $options);
} else {
return $this->pay->h5Pay($totalFee, $orderId, $body, $options['returl'] ?? '', $attach);
}
case Order::NATIVE:
return $this->pay->pcPay($totalFee, $orderId, $body, $attach, !empty($options['wechat']));
default:
throw new PayException('收银呗:支付类型错误或者暂不支持此环境下支付');
}
}
public function merchantPay(string $openid, string $orderId, string $amount, array $options = [])
{
throw new PayException('收银呗:暂不支持商家转账');
}
/**
* 发起退款
* @param string $outTradeNo
* @param array $options
* @return array|mixed
* @author 模板之家
* @V xu08290201
* @date 2024-05-10
*/
public function refund(string $outTradeNo, array $options = [])
{
$result = $this->pay->refund($options, $outTradeNo);
if (isset($result) && $result['data']['status'] === 'REFUND_FAIL') {
throw new AdminException('退款失败');
}
}
public function queryRefund(string $outTradeNo, string $outRequestNo, array $other = [])
{
// TODO: Implement queryRefund() method.
}
/**
* 异步回调
* @return mixed|string
* @author 模板之家
* @V xu08290201
* @date 2024-05-10
*/
public function handleNotify()
{
return $this->pay->handleNotify(function ($notify) {
if (isset($notify['data']['orderNumber'])) {
$data = [
'attach' => $notify['data']['attach'],
'out_trade_no' => $notify['data']['mercOrderNo'],
'transaction_id' => $notify['data']['orderNumber'],
'mercCode' => $notify['data']['mercCode']
];
return Event::until('NotifyListener', [$data, PayServices::SHOUYINBEI_PAY]);
}
return false;
});
}
/**
* 退款异步回调
* @return mixed|string
*/
public function refundNotify()
{
$params = request()->post();
if (count($params) < 1) {
//如果参数为空,则不进行处理
return "error";
}
$params['data'] = json_decode($params['bizContent'], true);
if (isset($params['code']) && $params['code'] === '0000' && $params['data']['status'] === 'REFUND_SUCCESS') {
//验签成功
return 'success';
} else {
return "error";
}
}
}
因开发内容较多,在这里就不帖出全部代码了,欢迎各位开发者加我(V:xu08290201)一起交流开发经验。收银呗支付对于退款接口等是有异步回调通知的,但crmeb本身只在支付交易中有异步通知,所以开发者需要自行再开发退款异步通知接口。好了,大概就写这些吧,大家在开发中有遇到问题,可以私信联系,一起共享交流。