Alipay.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. namespace app\api\controller;
  3. use app\api\controller\Base;
  4. use app\common\model\GoodsOrder;
  5. use app\common\service\OrderCallback;
  6. use think\Controller;
  7. use think\Db;
  8. use think\Model;
  9. use Alipay\EasySDK\Kernel\Factory;
  10. use Alipay\EasySDK\Kernel\Util\ResponseChecker;
  11. use Alipay\EasySDK\Kernel\Config;
  12. require_once env('root_path').'vendor/alipaysdk/easysdk/php/src/Kernel/Factory.php';
  13. require_once env('root_path').'vendor/alipaysdk/easysdk/php/src/Kernel/Config.php';
  14. require_once env('root_path').'vendor/alipaysdk/easysdk/php/src/Kernel/EasySDKKernel.php';
  15. require_once env('root_path').'vendor/alipaysdk/easysdk/php/src/Kernel/AlipayConstants.php';
  16. require_once env('root_path').'vendor/alipaysdk/easysdk/php/src/Kernel/Util/ResponseChecker.php';
  17. require_once env('root_path').'vendor/alipaysdk/easysdk/php/src/Kernel/Util/JsonUtil.php';
  18. require_once env('root_path').'vendor/alipaysdk/easysdk/php/src/Kernel/Util/Signer.php';
  19. require_once env('root_path').'vendor/alipaysdk/easysdk/php/src/Payment/App/Client.php';
  20. require_once env('root_path').'vendor/alipaysdk/easysdk/php/src/Payment/App/Models/AlipayTradeAppPayResponse.php';
  21. require_once env('root_path').'vendor/alipaysdk/easysdk/php/src/Payment/Wap/Client.php';
  22. require_once env('root_path').'vendor/alipaysdk/easysdk/php/src/Payment/Wap/Models/AlipayTradeWapPayResponse.php';
  23. require_once env('root_path').'vendor/alipaysdk/easysdk/php/src/Payment/Page/Client.php';
  24. require_once env('root_path').'vendor/alipaysdk/easysdk/php/src/Kernel/Util/PageUtil.php';
  25. require_once env('root_path').'vendor/alipaysdk/easysdk/php/src/Payment/Page/Models/AlipayTradePagePayResponse.php';
  26. require_once env('root_path').'vendor/alipaysdk/easysdk/php/src/Payment/Common/Client.php';
  27. require_once env('root_path').'vendor/alipaysdk/easysdk/php/src/Kernel/Util/SignContentExtractor.php';
  28. require_once env('root_path').'vendor/alipaysdk/easysdk/php/src/Payment/Common/Models/AlipayTradeRefundResponse.php';
  29. class Alipay extends Base
  30. {
  31. protected static $appid = [
  32. 'H5' =>'2021003173696391',
  33. 'APP' =>'2021003173696391'
  34. ];
  35. protected static $merchantPrivateKey = [
  36. 'H5' =>'MIIEpAIBAAKCAQEAsR+yUSQV5MS+RaWjaR14clB7v+Z49QqC+XlhHGAYzL1k/xZqr4NzFrJfWjbL7T0xW6r3+e4OkiBkjFME4UfGHxRnRdl9Rc7zOLxfXXvqa8R5zUqKKNIk6c6F1kdkY75+ctpdJaEeNvbnbbS6qLktV9YNpoAwJIFTaNwlOBkz67daUkE3sl3i6iqAtLDv9eKV2JR23s75GRkTTN/Us4ULqtOqHG74h/Oe5V/YcNA4cTTpOA+oDvmj6+cfJttOSNbvVtGtoXRZ40vuCeFwQGEJOi8vbxiNkeVVNpCxRUGocYxInrdqrmkG9hjDtaLvUpkJn11Kuu6x+hF3BP15voZuZwIDAQABAoIBAHQJJdKOZ2FlmpkZ+EHGsr+BBnEjVZ6CcmdpIztpoRWQBfgvAVlWLjFyTZFWUKZs7kb1bFrpn9YPLo/XW10SnjKCNV3HK5BuEVzb20XtBhfvsTPSX0x6zv/08iz8vEGz7Dbl4xvhv1+WxOpT+ezK02+1pV+snt1rc+9AOYnUyPTkJDjlIsiGEIrQDQuWtsaP3yUN4PHoK8CEtBPINvHIenpa66aDrtwTpYIQfIY8BtTG3SlxtBa+cYkolFn4Np5r8UK7nwnc2Lgz7dHnHdEJW+rlPz7Tn5OoGnUar8x9lC8Qsgkl7RKs2fT928CAHsZiPKnjlNn2OIkpyhyDlRafvaECgY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8YhZs0QuQyMoi5fe3g8svP90BQOq6TdHU0TBFjg5uYOKuNihKAfaZTqff1O0+1kGRYWO/Pxf1ThwIDAQAB',
  42. ];
  43. /**
  44. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  45. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  46. * @throws \GuzzleHttp\Exception\GuzzleException
  47. * 支付宝支付
  48. */
  49. public static function ali_pay($subject,$outTradeNo,$totalAmount,$notifyUrl,$type = 'APP'){
  50. Factory::setOptions(self::getOptions($notifyUrl,$type));
  51. try {
  52. //2. 发起API调用(以支付能力下的统一收单交易创建接口为例)
  53. //$result = Factory::payment()->common()->create("iPhone6 16G", "20200326235526001", "88.88", "2088002656718920");
  54. $result = Factory::payment()->App()->pay($subject,$outTradeNo , $totalAmount);
  55. $responseChecker = new ResponseChecker();
  56. //3. 处理响应或异常
  57. if ($responseChecker->success($result)) {
  58. return $result->body;
  59. } else {
  60. echo "调用失败,原因:". $result->msg.",".$result->subMsg.PHP_EOL;
  61. }
  62. } catch (Exception $e) {
  63. return false;
  64. }
  65. }
  66. public static function ali_pay_h5($subject,$outTradeNo,$totalAmount,$notifyUrl,$returnUrl)
  67. {
  68. Factory::setOptions(self::getOptions($notifyUrl));
  69. try {
  70. $result = Factory::payment()->Wap()->pay($subject,$outTradeNo,$totalAmount,$returnUrl,$returnUrl);
  71. // $result = Factory::payment()->Page()->pay($subject,$outTradeNo,$totalAmount,$returnUrl);
  72. //var_dump($result);
  73. $responseChecker = new ResponseChecker();
  74. //3. 处理响应或异常
  75. if ($responseChecker->success($result)) {
  76. return $result->body;
  77. } else {
  78. echo "调用失败,原因:". $result->msg.",".$result->subMsg.PHP_EOL;
  79. }
  80. } catch (\Exception $e) {
  81. return false;
  82. }
  83. }
  84. public static function ali_pay_pc($subject,$outTradeNo,$totalAmount,$notifyUrl,$returnUrl){
  85. Factory::setOptions(self::getOptions($notifyUrl,'H5'));
  86. try {
  87. //$result = Factory::payment()->Wap()->pay($subject,$outTradeNo,$totalAmount,$notifyUrl,$returnUrl);
  88. $result = Factory::payment()->Page()->pay($subject,$outTradeNo,$totalAmount,$returnUrl);
  89. //var_dump($result);
  90. $responseChecker = new ResponseChecker();
  91. //3. 处理响应或异常
  92. if ($responseChecker->success($result)) {
  93. return $result->body;
  94. } else {
  95. echo "调用失败,原因:". $result->msg.",".$result->subMsg.PHP_EOL;
  96. }
  97. } catch (\Exception $e) {
  98. return false;
  99. }
  100. }
  101. public static function getOptions($notifyUrl,$type = 'H5'){
  102. $options = new Config();
  103. $options->protocol = 'https';
  104. $options->gatewayHost = 'openapi.alipay.com';
  105. $options->signType = 'RSA2';
  106. $options->appId = static::$appid[$type];
  107. // 为避免私钥随源码泄露,推荐从文件中读取私钥字符串而不是写入源码中
  108. $options->merchantPrivateKey = static::$merchantPrivateKey[$type];
  109. /*$options->alipayCertPath = '<-- 请填写您的支付宝公钥证书文件路径,例如:/foo/alipayCertPublicKey_RSA2.crt -->';
  110. $options->alipayRootCertPath = '<-- 请填写您的支付宝根证书文件路径,例如:/foo/alipayRootCert.crt" -->';
  111. $options->merchantCertPath = '<-- 请填写您的应用公钥证书文件路径,例如:/foo/appCertPublicKey_2019051064521003.crt -->';*/
  112. //注:如果采用非证书模式,则无需赋值上面的三个证书路径,改为赋值如下的支付宝公钥字符串即可
  113. $options->alipayPublicKey = static::$alipayPublicKey[$type];
  114. //可设置异步通知接收服务地址(可选)
  115. $options->notifyUrl = $notifyUrl;
  116. //可设置AES密钥,调用AES加解密相关接口时需要(可选)
  117. // $options->encryptKey = "vrtxdsUskxUow23KHFGRng==";
  118. return $options;
  119. }
  120. // 会员开通回调
  121. public function LevelOrderNotify()
  122. {
  123. $result = input('post.');
  124. if(empty($result['trade_status'])) return true;
  125. $this->payResultLog($result,'dd_level_order');
  126. if ($result['trade_status'] == 'TRADE_SUCCESS' || $result['trade_status'] == 'TRADE_FINISHED') {
  127. $pay_no = $result['out_trade_no'];
  128. $order_info = \app\common\model\LevelOrder::where('pay_no',$pay_no)->find();
  129. if(!$order_info) return false;
  130. $order_info = $order_info->toArray();
  131. if($order_info['status'] != 0) return true;
  132. $back_res = OrderCallback::levelOrderCallBack($order_info,1);// 支付完成后回调
  133. return $back_res['ret_val'];
  134. }
  135. }
  136. // 活动报名回调
  137. public function applyOrderNotify()
  138. {
  139. $result = input('post.');
  140. if(empty($result['trade_status'])) return true;
  141. $this->payResultLog($result,'dd_activity_apply');
  142. if ($result['trade_status'] == 'TRADE_SUCCESS' || $result['trade_status'] == 'TRADE_FINISHED') {
  143. $pay_no = $result['out_trade_no'];
  144. $order_info = \app\common\model\ActivityApply::where('pay_no',$pay_no)->find();
  145. if(!$order_info) return false;
  146. $order_info = $order_info->toArray();
  147. if($order_info['status'] != 0) return true;
  148. $back_res = OrderCallback::activityOrderCallBack($order_info);// 支付完成后回调
  149. return $back_res['ret_val'];
  150. }
  151. }
  152. // 商品支付回调
  153. public function goodsOrderNotify()
  154. {
  155. $result = input('post.');
  156. if(empty($result['trade_status'])) return true;
  157. $this->payResultLog($result,'dd_goods_order');
  158. if ($result['trade_status'] == 'TRADE_SUCCESS' || $result['trade_status'] == 'TRADE_FINISHED') {
  159. $pay_no = $result['out_trade_no'];
  160. $order_info = GoodsOrder::with('orderItem')->where('pay_no',$pay_no)->find();
  161. if(!$order_info) return false;
  162. $order_info = $order_info->toArray();
  163. if($order_info['status'] != 0) return true;
  164. $back_res = OrderCallback::goodsOrderCallBack($order_info,1);// 支付完成后回调
  165. return $back_res['ret_val'];
  166. }
  167. }
  168. /**
  169. * 记录支付日志
  170. * @param $message
  171. * @param $table_name
  172. */
  173. public function payResultLog($message,$table_name)
  174. {
  175. // 回调记录
  176. $ret_arr = [];
  177. $ret_arr['transaction_id'] = isset($message['trade_no']) ?$message['trade_no']: '';
  178. $ret_arr['trade_no'] = isset($message['out_trade_no']) ?$message['out_trade_no']: '';
  179. $ret_arr['return_code'] = isset($message['trade_status']) ?$message['trade_status']: '';
  180. $ret_arr['result_code'] = isset($message['trade_status']) ?$message['trade_status']: '';
  181. $ret_arr['create_at'] = date('Y-m-d H:i:s');
  182. $ret_arr['order_table'] = $table_name;
  183. $ret_arr['result'] = json_encode($message);
  184. $ret_arr['pay_type'] = 'zfb';
  185. Db::name('order_pay_result')->insert($ret_arr);
  186. }
  187. /**
  188. * 支付宝支付退款
  189. */
  190. public static function aliRefund($order_no,$money,$type= 'H5'){
  191. Factory::setOptions(self::getOptions('',$type));
  192. $result = Factory::payment()->common()->refund($order_no,$money);
  193. $responseChecker = new ResponseChecker();
  194. return $responseChecker->success($result);
  195. }
  196. }