if( $pay_type == 1 ){
//微信
$return_url = 'https://api.abc.cn/api/charge_notify/wx_charge_notify';
$config = pay_config( $pay_type , $return_url );
try {
$client = new \Payment\Client(\Payment\Client::WECHAT , $config);
$pay_res = $client->pay(\Payment\Client::WX_CHANNEL_APP, $payData);
} catch (InvalidArgumentException $e) {
echo $e->getMessage();
exit;
} catch (\Payment\Exceptions\GatewayException $e) {
echo $e->getMessage();
var_dump($e->getRaw());
exit;
} catch (\Payment\Exceptions\ClassNotFoundException $e) {
echo $e->getMessage();
exit;
} catch (Exception $e) {
echo $e->getMessage();
exit;
}
}elseif( $pay_type == 2 ){
//支付宝
$return_url = 'https://api.abc.cn/api/charge_notify/ali_charge_notify';
$config = pay_config($pay_type, $return_url);
$payData = [];
$payData = [
'body' => '互动传媒',
'subject' => '钻石充值',
'trade_no' => $order_sn,// 自己实现生成
'time_expire' => time() + 600, // 表示必须 600s 内付款
'amount' => $num, // 微信沙箱模式,需要金额固定为3.01
'goods_type' => 0,
'client_ip' => isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '127.0.0.1', // 客户地址
];
try {
$client = new \Payment\Client(\Payment\Client::ALIPAY , $config);
$pay_res = $client->pay(\Payment\Client::ALI_CHANNEL_APP, $payData);
} catch (InvalidArgumentException $e) {
echo $e->getMessage();
exit;
} catch (\Payment\Exceptions\GatewayException $e) {
echo $e->getMessage();
var_dump($e->getRaw());
exit;
} catch (\Payment\Exceptions\ClassNotFoundException $e) {
echo $e->getMessage();
exit;
} catch (Exception $e) {
echo $e->getMessage();
exit;
}
}
配置文件
/**
* 支付配置信息
* @param type $return_url
*/
function pay_config( $type , $return_url ){
if( $type == 1 ){
// wx
$config = [
'use_sandbox' => false, // 是否使用 微信支付仿真测试系统
'app_id' => 'wx74ba3', // 公众账号ID
'sub_appid' => '', // 公众子商户账号ID
'mch_id' => '1952', // 商户id
'sub_mch_id' => '', // 子商户id
'md5_key' => '53e5500775', // md5 秘钥
'app_cert_pem' => EXTEND_PATH. 'lib/wx_cert/apiclient_cert.pem',
'app_key_pem' => EXTEND_PATH. 'lib/wx_cert/apiclient_key.pem',
'sign_type' => 'MD5', // MD5 HMAC-SHA256
'limit_pay' => [
//'no_credit',
], // 指定不能使用信用卡支付 不传入,则均可使用
'fee_type' => 'CNY', // 货币类型 当前仅支持该字段
'notify_url' => $return_url,
'redirect_url' => '', // 如果是h5支付,可以设置该值,返回到指定页面
];
}elseif( $type == 2 ){
//ali
$config = [
'use_sandbox' => false, // 是否使用沙盒模式
'app_id' => '202161',
'sign_type' => 'RSA2', // RSA RSA2
// 支付宝公钥字符串
'ali_public_key' => Ii4KgH7uoH9BKXpEPMrVOAKcXg/OIJAQUZf0E30OiNox239Efz2Blpi0K7Uia7ldP1sZXRSvQIDAQAB',
// 自己生成的密钥字符串
'rsa_private_key' => 'MIIEowIBAAKCAQEAsJf6wukH5m3pFyX5y1gn0x/Im2l4Ghbg3ik1NYxg2jVtWN573f40uzH7X5R8',
'limit_pay' => [
// 'balance',// 余额
//'moneyFund',// 余额宝
// 'debitCardExpress',// 借记卡快捷
//'creditCard',//信用卡
//'creditCardExpress',// 信用卡快捷
// 'creditCardCartoon',//信用卡卡通
// 'credit_group',// 信用支付类型(包含信用卡卡通、信用卡快捷、花呗、花呗分期)
], // 用户不可用指定渠道支付当有多个渠道时用“,”分隔
// 与业务相关参数
'notify_url' => $return_url,
'return_url' => '',
];
}
return $config;
}
回调
ChargeNotify.php
$channel,
"notifyType" => $notifyType,
"notifyWay" => $notifyWay,
"notifyData" => $notifyData,
];
file_put_contents("wx_charge_notify2.txt", json_encode($data));
$callback = new YuyueNotify();
$callback->charge_logic( $notifyData );
return true;
}
}
// 自己实现一个类,继承该接口
class AliChargeNotify implements \Payment\Contracts\IPayNotify {
/**
* 处理自己的业务逻辑,如更新交易状态、保存通知数据等等
* @param string $channel 通知的渠道,如:支付宝、微信、招商
* @param string $notifyType 通知的类型,如:支付、退款
* @param string $notifyWay 通知的方式,如:异步 async,同步 sync
* @param array $notifyData 通知的数据
* @return bool
*/
public function handle(string $channel, string $notifyType, string $notifyWay, array $notifyData) {
//var_dump($channel, $notifyType, $notifyWay, $notifyData);exit;
$data = [
"channel" => $channel,
"notifyType" => $notifyType,
"notifyWay" => $notifyWay,
"notifyData" => $notifyData,
];
file_put_contents("ali_charge_notify2.txt", json_encode($data));
$callback = new YuyueNotify();
$callback->charge_logic( $notifyData );
return true;
}
}
/**
* 支付回调
*/
class ChargeNotify extends Api {
protected $noNeedLogin = ['*'];
protected $noNeedRight = ['*'];
/**
* 微信支付回调
*/
public function wx_charge_notify() {
$return_url = 'https://api.abc.cn/api/charge_notify/wx_charge_notify';
$config = pay_config( 1 , $return_url );
// 实例化继承了接口的类
$callback = new WxChargeNotify();
file_put_contents("wx_charge_notify1.txt", json_encode($callback));
try {
$client = new \Payment\Client(\Payment\Client::WECHAT, $config);
$xml = $client->notify($callback);
$xml = $xml[ "notifyData" ];
file_put_contents("wx_charge_notify3.txt", json_encode($xml));
} catch (InvalidArgumentException $e) {
echo $e->getMessage();
exit;
} catch (\Payment\Exceptions\GatewayException $e) {
echo $e->getMessage();
exit;
} catch (\Payment\Exceptions\ClassNotFoundException $e) {
echo $e->getMessage();
exit;
} catch (Exception $e) {
echo $e->getMessage();
exit;
}
}
/**
* 微信支付回调
*/
public function ali_charge_notify() {
$return_url = 'https://api.abc.cn/api/charge_notify/ali_charge_notify';
$config = pay_config( 2 , $return_url );
// 实例化继承了接口的类
$callback = new AliChargeNotify();
file_put_contents("ali_charge_notify1.txt", json_encode($callback));
try {
$client = new \Payment\Client(\Payment\Client::ALIPAY, $config);
$xml = $client->notify($callback);
$xml = $xml[ "notifyData" ];
$this->charge_logic( $xml );
file_put_contents("ali_charge_notify3.txt", json_encode($xml));
} catch (InvalidArgumentException $e) {
echo $e->getMessage();
exit;
} catch (\Payment\Exceptions\GatewayException $e) {
echo $e->getMessage();
exit;
} catch (\Payment\Exceptions\ClassNotFoundException $e) {
echo $e->getMessage();
exit;
} catch (Exception $e) {
echo $e->getMessage();
exit;
}
}
/**
* 充值逻辑
* $type 1微信 2支付宝
*/
public function charge_logic( $xml_arr ) {
// wx -- ali
if ( (isset($xml_arr['return_code']) && $xml_arr['return_code'] == "SUCCESS") || (isset($xml_arr['trade_status']) && $xml_arr['trade_status']=="TRADE_SUCCESS") ) {
$pay_type = 0;
$pay_str = '';
if( isset($xml_arr['return_code']) ){
$pay_type = 1;
$order_sn = $xml_arr['out_trade_no'];
$transaction_id = $xml_arr['transaction_id'];
$pay_str .= "微信充值";
}elseif( isset($xml_arr['trade_status']) ){
$pay_type = 2;
$order_sn = $xml_arr['out_trade_no'];
$transaction_id = $xml_arr['trade_no'];
$pay_str .= "支付宝充值";
}
// echo "";var_dump( $xml_arr );die;
Db::startTrans();
try {
Db::commit();
echo "success";die;
} catch (Exception $e) {
Db::rollback();
echo "fail";die;
}
}
}
}
这是app支付
其他支付自己改参数
巨坑----这个参数不要用
api\vendor\riverslei\payment\src\Gateways\Alipay\AppCharge.php