php网站接入微信支付,PHP快速接入微信支付和支付宝支付

做支付我都是直接用大神封装好的工具包的,简单省事!这里推荐:

composer require yansongda/pay -vvv

引入

use Yansongda\Pay\Pay;

这里仅示例简单的支付,具体的其他用法请看官方文档

微信支付:

//调起支付(仅示例扫码支付)

$wechat_config = Config::get('wechat_pay');

$config = [

'app_id' => $wechat_config['app_id'],

'mch_id' => $wechat_config['mch_id'],

'key' => $wechat_config['key'],

'notify_url' => 'https://'.$_SERVER['HTTP_HOST'].'/portal/Order/wechat_notify',

'http' => [

'timeout' => 5.0,

'connect_timeout' => 5.0,

],

];

$pay_order = [

'out_trade_no' => $order['order_sn'],

'total_fee' => $order['price']*100, //微信支付以分为单位

'body' => '测试订单',

];

try {

//网站扫码支付

$pay = Pay::wechat($config)->scan($pay_order);

$qrcode = new Qrcode($pay->code_url);

$image = $qrcode->getString();

return response($image, 200, ['Content-Type' => 'image/jpeg']);

} catch (\Exception $e) {

echo $e->getMessage();

}

//回调验签(验签失败是会跑出异常的,最好写个捕获异常)

$pay = Pay::wechat($config);

$data = $pay->verify();

$param = $data->all();

//处理订单

if ($param['result_code']=='SUCCESS'&&$param['return_code']=='SUCCESS') {

(new OrderService($param['out_trade_no'], 'wechat'))->call_notice();

}

//退款(退款需要用到安全证书)

$wechat_config = Config::get('wechat_pay');

$config = [

'app_id' => $wechat_config['app_id'],

'mch_id' => $wechat_config['mch_id'],

'key' => $wechat_config['key'],

'cert_client' => $wechat_config['cert_client'],

'cert_key' => $wechat_config['cert_key'],

'http' => [

'timeout' => 5.0,

'connect_timeout' => 5.0,

],

];

$wechat_order = [

'out_trade_no' => $order['order_sn'],

'out_refund_no' => $order['order_sn'],

'total_fee' => $order['price']*100,

'refund_fee' => $order['price']*100

];

try {

$pay = Pay::wechat($config)->refund($wechat_order);

if ($pay->return_code != 'SUCCESS') {

$this->error($pay->return_msg);

}

} catch (Exception $e) {

$this->error($e->getMessage());

}

支付宝支付:

//调起支付(网站跳转支付)

$alipay_config = Config::get('alipay');

$config = [

'app_id' => $alipay_config['app_id'],

'notify_url' => 'https://'.$_SERVER['HTTP_HOST'].'/portal/Order/alipay_notify',

'return_url' => 'https://'.$_SERVER['HTTP_HOST'].'/portal/Pay/pay_success',

'ali_public_key' => $alipay_config['ali_public_key'],

'private_key' => $alipay_config['private_key'],

'http' => [

'timeout' => 5.0,

'connect_timeout' => 5.0,

]

];

$pay_order = [

'out_trade_no' => $order['order_sn'],

'total_amount' => $order['price'],

'subject' => '测试订单',

];

try {

$alipay = Pay::alipay($config)->web($pay_order);

return $alipay->send();

} catch (\Exception $e) {

$this->error($e->getMessage());

}

//回调验签(buyer_id退款要用到,最好存起来)

$alipay = Pay::alipay($config);

$data = $alipay->verify();

$param = $data->all();

if (isset($param['out_trade_no'])&&!empty($param['out_trade_no'])&&$param['trade_status']=='TRADE_SUCCESS') {

(new OrderService($param['out_trade_no'], 'alipay', $param['buyer_id']))->call_notice();

}

//退款(用回原来的配置就行)

$alipay_order = [

'out_trade_no' => $order['order_sn'],

'refund_amount' => $order['price'],

'goods_id' => time(),

'goods_name' => '测试商品',

'quantity' => 1,

'price' => $order['price'],

'trans_in' => $order['buyer_id'],

];

try{

$alipay = Pay::alipay($config)->refund($alipay_order);

if ($alipay->code!='10000'||$alipay->buyer_user_id!=$order['buyer_id']||$alipay->out_trade_no!=$order['order_sn']) {

$this->error($alipay->msg);

}

} catch (Exception $e) {

$this->error($e->getMessage());

}

话不多少,真香就完事了!!

你可能感兴趣的:(php网站接入微信支付)