123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- <?php
- namespace app\api\controller;
- use app\api\controller\Base;
- use app\common\model\GoodsOrder;
- use app\common\service\OrderCallback;
- use think\Controller;
- use think\Db;
- use think\Model;
- use Alipay\EasySDK\Kernel\Factory;
- use Alipay\EasySDK\Kernel\Util\ResponseChecker;
- use Alipay\EasySDK\Kernel\Config;
- require_once env('root_path').'vendor/alipaysdk/easysdk/php/src/Kernel/Factory.php';
- require_once env('root_path').'vendor/alipaysdk/easysdk/php/src/Kernel/Config.php';
- require_once env('root_path').'vendor/alipaysdk/easysdk/php/src/Kernel/EasySDKKernel.php';
- require_once env('root_path').'vendor/alipaysdk/easysdk/php/src/Kernel/AlipayConstants.php';
- require_once env('root_path').'vendor/alipaysdk/easysdk/php/src/Kernel/Util/ResponseChecker.php';
- require_once env('root_path').'vendor/alipaysdk/easysdk/php/src/Kernel/Util/JsonUtil.php';
- require_once env('root_path').'vendor/alipaysdk/easysdk/php/src/Kernel/Util/Signer.php';
- require_once env('root_path').'vendor/alipaysdk/easysdk/php/src/Payment/App/Client.php';
- require_once env('root_path').'vendor/alipaysdk/easysdk/php/src/Payment/App/Models/AlipayTradeAppPayResponse.php';
- require_once env('root_path').'vendor/alipaysdk/easysdk/php/src/Payment/Page/Client.php';
- require_once env('root_path').'vendor/alipaysdk/easysdk/php/src/Kernel/Util/PageUtil.php';
- require_once env('root_path').'vendor/alipaysdk/easysdk/php/src/Payment/Page/Models/AlipayTradePagePayResponse.php';
- class Alipay extends Base
- {
- protected static $appid = [
- 'H5'=>'2021003155604810',
- 'APP'=>'2021003155604810'
- ];
- protected static $merchantPrivateKey = [
- 'H5'=>'MIIEpQIBAAKCAQEAkfh1oGMZSI8t3WbvpgQhAiK5dk1aSUafBl8AEj+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PBEt7U/XP7yh89+UDOZQPknxJjpfo53ciOAowEeGFH9O8NimtvSFMMF9iCQijmlhXyOCfZstLyrVRojcZTgv4//AjV877DrUBTsonpeGGSi92Ssl8COPWbr461UtYBCWjAK+UiZsh01ZI5LNTvt8bVyOWPhWDEcPvxxrkTqUF6qRO9Bvr169V0tZxiJgDLu891Ofim/TlD+tHgnOI9NfWQafRddylPCTzIHbaxZyQ0JmNJleROSCMNOpVZiWZX8+mQmhEpeodJ3MqYmAqN4Dla7fN54Y2wWHaRNkGwIDAQAB',
- 'APP'=>'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAidau5/jnsnry1+3VDvTOO7S+Zsi1u+a0uNvacXXHAh78TQIWPBEt7U/XP7yh89+UDOZQPknxJjpfo53ciOAowEeGFH9O8NimtvSFMMF9iCQijmlhXyOCfZstLyrVRojcZTgv4//AjV877DrUBTsonpeGGSi92Ssl8COPWbr461UtYBCWjAK+UiZsh01ZI5LNTvt8bVyOWPhWDEcPvxxrkTqUF6qRO9Bvr169V0tZxiJgDLu891Ofim/TlD+tHgnOI9NfWQafRddylPCTzIHbaxZyQ0JmNJleROSCMNOpVZiWZX8+mQmhEpeodJ3MqYmAqN4Dla7fN54Y2wWHaRNkGwIDAQAB',
- ];
- /**
- * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
- * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
- * @throws \GuzzleHttp\Exception\GuzzleException
- * 支付宝支付
- */
- public static function ali_pay($subject,$outTradeNo,$totalAmount,$notifyUrl){
- Factory::setOptions(self::getOptions($notifyUrl,"APP"));
- try {
- //2. 发起API调用(以支付能力下的统一收单交易创建接口为例)
- //$result = Factory::payment()->common()->create("iPhone6 16G", "20200326235526001", "88.88", "2088002656718920");
- $result = Factory::payment()->App()->pay($subject,$outTradeNo , $totalAmount);
- $responseChecker = new ResponseChecker();
- //3. 处理响应或异常
- if ($responseChecker->success($result)) {
- return $result->body;
- } else {
- echo "调用失败,原因:". $result->msg.",".$result->subMsg.PHP_EOL;
- }
- } catch (Exception $e) {
- return false;
- }
- }
- public static function ali_pay_pc($subject,$outTradeNo,$totalAmount,$notifyUrl,$returnUrl){
- Factory::setOptions(self::getOptions($notifyUrl,'H5'));
- try {
- $result = Factory::payment()->Page()->pay($subject,$outTradeNo,$totalAmount,$returnUrl);
- $responseChecker = new ResponseChecker();
- //3. 处理响应或异常
- if ($responseChecker->success($result)) {
- return $result->body;
- } else {
- echo "调用失败,原因:". $result->msg.",".$result->subMsg.PHP_EOL;
- }
- } catch (Exception $e) {
- return false;
- }
- }
- public static function getOptions($notifyUrl,$type = 'H5'){
- $options = new Config();
- $options->protocol = 'https';
- $options->gatewayHost = 'openapi.alipay.com';
- $options->signType = 'RSA2';
- $options->appId = static::$appid[$type];
- // 为避免私钥随源码泄露,推荐从文件中读取私钥字符串而不是写入源码中
- $options->merchantPrivateKey = static::$merchantPrivateKey[$type];
- /*$options->alipayCertPath = '<-- 请填写您的支付宝公钥证书文件路径,例如:/foo/alipayCertPublicKey_RSA2.crt -->';
- $options->alipayRootCertPath = '<-- 请填写您的支付宝根证书文件路径,例如:/foo/alipayRootCert.crt" -->';
- $options->merchantCertPath = '<-- 请填写您的应用公钥证书文件路径,例如:/foo/appCertPublicKey_2019051064521003.crt -->';*/
- //注:如果采用非证书模式,则无需赋值上面的三个证书路径,改为赋值如下的支付宝公钥字符串即可
- $options->alipayPublicKey = static::$merchantPrivateKey[$type];
- //可设置异步通知接收服务地址(可选)
- $options->notifyUrl = $notifyUrl;
- //可设置AES密钥,调用AES加解密相关接口时需要(可选)
- $options->encryptKey = "";
- return $options;
- }
- // 会员开通回调
- public function LevelOrderNotify()
- {
- $result = input('post.');
- $this->payResultLog($result,'dd_level_order');
- if ($result['trade_status'] == 'TRADE_SUCCESS' || $result['trade_status'] == 'TRADE_FINISHED') {
- $pay_no = $result['out_trade_no'];
- $order_info = \app\common\model\LevelOrder::where('pay_no',$pay_no)->find();
- if(!$order_info) return false;
- $order_info = $order_info->toArray();
- if($order_info['status'] != 0) return true;
- $back_res = OrderCallback::levelOrderCallBack($order_info,1);// 支付完成后回调
- return $back_res['ret_val'];
- }
- }
- // 活动报名回调
- public function applyOrderNotify()
- {
- $result = input('post.');
- $this->payResultLog($result,'dd_activity_apply');
- if ($result['trade_status'] == 'TRADE_SUCCESS' || $result['trade_status'] == 'TRADE_FINISHED') {
- $pay_no = $result['out_trade_no'];
- $order_info = \app\common\model\ActivityApply::where('pay_no',$pay_no)->find();
- if(!$order_info) return false;
- $order_info = $order_info->toArray();
- if($order_info['status'] != 0) return true;
- $back_res = OrderCallback::activityOrderCallBack($order_info);// 支付完成后回调
- return $back_res['ret_val'];
- }
- }
- // 商品支付回调
- public function goodsOrderNotify()
- {
- $result = input('post.');
- $this->payResultLog($result,'dd_goods_order');
- if ($result['trade_status'] == 'TRADE_SUCCESS' || $result['trade_status'] == 'TRADE_FINISHED') {
- $pay_no = $result['out_trade_no'];
- $order_info = GoodsOrder::with('orderItem')->where('pay_no',$pay_no)->find();
- if(!$order_info) return false;
- $order_info = $order_info->toArray();
- if($order_info['status'] != 0) return true;
- $back_res = OrderCallback::goodsOrderCallBack($order_info,1);// 支付完成后回调
- return $back_res['ret_val'];
- }
- }
- /**
- * 记录支付日志
- * @param $message
- * @param $table_name
- */
- public function payResultLog($message,$table_name)
- {
- // 回调记录
- $ret_arr = [];
- $ret_arr['transaction_id'] = isset($message['trade_no']) ?$message['trade_no']: '';
- $ret_arr['trade_no'] = isset($message['out_trade_no']) ?$message['out_trade_no']: '';
- $ret_arr['return_code'] = isset($message['trade_status']) ?$message['trade_status']: '';
- $ret_arr['result_code'] = isset($message['trade_status']) ?$message['trade_status']: '';
- $ret_arr['create_at'] = date('Y-m-d H:i:s');
- $ret_arr['order_table'] = $table_name;
- $ret_arr['result'] = json_encode($message);
- $ret_arr['pay_type'] = 'zfb';
- Db::name('order_pay_result')->insert($ret_arr);
- }
- }
|