PHP code example of altairaki / easy-pay
1. Go to this page and download the library: Download altairaki/easy-pay library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
altairaki / easy-pay example snippets
use AltairAki\EasyPay\Pay;
class PayController
{
$config = [
'appid' => 'wxc9d7aa3c5f3c5123', // APP APPID
'app_id' => 'wx21111df6eb31d123', // 公众号 APPID
'mini_id' => 'wx0cb12db05346eb50', // 小程序 APPID
'mch_id' => '',
'key' => '', //
'cert_client' => './cert/apiclient_cert.pem', // 可选项,退款等情况时用到
'cert_key' => './cert/apiclient_key.pem', // 可选项,退款等情况时用到
];
public function payByWechat(Order $order, Request $request) {
// 校验权限
$this->authorize('own', $order);
// 校验订单状态
if ($order->paid_at || $order->closed) {
throw new InvalidRequestException('订单状态不正确');
}
$app = Pay::wechat($this->config);
// scan 方法为拉起微信扫码支付
return $app->scan([
'out_trade_no' => time(), // 商户订单号
'total_fee' => 101, // 微信支付的金额单位是分。
'body' => 'Test Body', // 订单描述
]);
}
public function wechatNotify()
{
$app = Pay::wechat($this->config);
// 校验回调参数是否正确
$data = $app->verify();
// 找到对应的订单
$order = Order::where('no', $data->out_trade_no)...;
// 订单不存在则告知微信支付
if (!$order) {
return 'fail';
}
// 订单已支付
if ($order->paid_at) {
// 告知微信支付此订单已处理
return app('wechat_pay')->success()->send();
}
// 将订单标记为已支付
$order->update([
'paid_at' => time(),
'payment_method' => 'wechat',
'payment_no' => $data->transaction_id,
]);
return app('wechat_pay')->success()->send();
}
}
use AltairAki\EasyPay\Pay;
class PayController{
public function pay()
{
$config = [
'app_id' => '2016092400583112',
'ali_public_key' => 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsHBArzr7jdkPF63gGbcnCQy1/oa5Pg/Qgrvg+bwl1O8wDFu3SXz4RHM12680V5E5IM07PRzc4svwjxj6jjS+vPmGOqRe1GRnvdaRE5XUDWCCEw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2NXCRIWMezj67YOpMKeUhycc4ENbop8E0/+3Bl8nFul253fhOPHBOeNkBOPfSvLlftHubTph8ZvjIWgHBV0owKBgQC+/phBE+v1yyH0HYovMOLqTHg1U0ZperMQjBGE11VpD9NL7NigZ+/3isdh5TolIqOKQtWvmKP92lDvqjAzJOrN+pYRDLoW9meECeQG9gCgsP1QyVLxu/ibUG4WYvwKggaQrG+MiqdRxM1/ndgH2ZR0kmZr58GyL9EV0+ApqCjRAwKBgF9IQcDPNlIhY7Ejwy91f3TPGHW9D+PFA8mSr3T76MUs9WYe3BD25Sm7ZB0drkgGilCM786BWWXA37eyh2bnPyN2iFrX376rjNtj6+1IetVZFgf/DZ8Ay7EkAtYXjflD8jUIIDqfizpzgmvqAceNUijrm9uROg/hUZU+E9SnnAlrAoGADKvfdhHYScpcSlHbZR4dL+Y6427O8RiO4L4qO2H97KZ8IkFobdv3c7jlWX1XyjbuGrIscyXxW1osnHnyELKWUWwaoK7zeaqHW588XanciMy1QbLZqegKqmM/qoSOrDPMM7T9AZoBV89ywtC6EGtDCijcWrRZiXTarlQMPzdE3fkCgYB/1TswTkmSn4aALTVlSBcDey3vDUvJfCMV0QsWEHb5yytKKvHpq7lsTvw3qHTSm7fbItfDzXu7Pw9Nns2co17hAyGes8TOBUGk1DBev49lbhkbKUODCK523W8V+p8N9mNje8rXu60cnqXf4/5xeaL8dtUi7MYh9fERF1Qu+OFYBg==',
// 使用公钥证书模式,请配置下面两个参数,同时修改ali_public_key为以.crt结尾的支付宝公钥证书路径,如(./cert/alipayCertPublicKey_RSA2.crt)
// 'app_cert_public_key' => './cert/appCertPublicKey.crt', //应用公钥证书路径
// 'alipay_root_cert' => './cert/alipayRootCert.crt', //支付宝根证书路径
];
Pay::wechat($config)->web([
'out_trade_no' => time(),
'body' => 'Body',
'total_fee' => 101,
'notify_url' => 'http://dev.com/notify.php',
'return_url' => 'http://dev.com/return.php',
'openid' => 'owufD0Wzz6oZX1AUiYcHzYafPDFA',
]);
}
// 前端回调页面
public function alipayReturn()
{
try {
app('alipay')->verify();
} catch (\Exception $e) {
return view('pages.error', ['msg' => '数据不正确']);
}
return view('pages.success', ['msg' => '付款成功']);
}
// 服务器端回调
public function alipayNotify()
{
// 校验输入参数
$data = app('alipay')->verify();
// 如果订单状态不是成功或者结束,则不走后续的逻辑
// 所有交易状态:https://docs.open.alipay.com/59/103672
if (!in_array($data->trade_status, ['TRADE_SUCCESS', 'TRADE_FINISHED'])) {
return app('alipay')->success()->send();
}
$order = Order::where('no', $data->out_trade_no)->first();
// 正常来说不太可能出现支付了一笔不存在的订单,这个判断只是加强系统健壮性。
if (!$order) {
return 'fail';
}
// 如果这笔订单的状态已经是已支付
if ($order->paid_at) {
// 返回数据给支付宝
return app('alipay')->success();
}
$order->update([
'paid_at' => time(), // 支付时间
'payment_method' => 'alipay', // 支付方式
'payment_no' => $data->trade_no, // 支付宝订单号
]);
return app('alipay')->success()->send(); //lavavel 直接 return app('alipay')->success();
}
}
'wecaht' => [
'appid' => 'wxc9d7aa3c5f3c5123', // APP APPID
'app_id' => 'wx21111df6eb31d123', // 公众号 APPID
'mini_id' => 'wx0cb12db05346eb50', // 小程序 APPID
'mch_id' => '1222040111',
'key' => 'your key', //商户平台设置的密钥key
'cert_client' => './cert/apiclient_cert.pem', // optional,退款等情况时用到
'cert_key' => './cert/apiclient_key.pem',// optional,退款等情况时用到
],
'wecaht' => [
...
'gateway' => 'app', //APP-app,小程序-mini,公众号-oa
],