Pay.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. namespace app\home\controller;
  3. use think\Controller;
  4. use think\Db;
  5. use app\user\model\PayPlugin;
  6. use app\user\model\User;
  7. use app\user\model\UserPay;
  8. class Pay extends Controller{
  9. private $payPluginModel, $userModel;
  10. public function _initialize(){
  11. $this->payPluginModel= new PayPlugin();
  12. $this->userModel = new User();
  13. }
  14. public function pay(){
  15. $data = decode($this->request->post());
  16. // $data['i'] = 'com.dashgame.garden.packfresh';
  17. // $data['t'] = 1;
  18. // $data['u'] = 1704251601802555535;
  19. $ipaInfo = Db::name('iap_config')->where(['item_id'=>$data['i']])->find();
  20. if(empty($ipaInfo)) exit(json(['error'=>1111]));
  21. $plugin = $this->payPluginModel->getPayPluginByType($data['t']);
  22. if(empty($plugin)) exit(json(['error'=>1111]));
  23. $biz_content = $this->getAliTradeInfo($ipaInfo, $data['u']);
  24. $val = $this->getAliPayInfo($plugin, $biz_content);
  25. return http_build_query($val);
  26. }
  27. public function notify(){
  28. echo 'success';
  29. }
  30. private function getAliTradeInfo($ipa, $user){
  31. $content = [];
  32. $content['subject'] = $ipa['desc'];
  33. // $content['subject'] = "Player Pay Money!";
  34. $content['out_trade_no'] = $tradeNo = 'D'.getId();
  35. $content['total_amount'] = $ipa['price'];
  36. // $content['total_amount'] = 0.01;
  37. $content['product_code'] = 'QUICK_MSECURITY_PAY';
  38. $content['timeout_express'] = '90m';
  39. // $content['body'] = "Temp Body";
  40. $content['body'] = $ipa['desc'];
  41. $userPay = new UserPay();
  42. $userPay->add(['user_id'=>$user, 'cost'=>$ipa['price'], 'pay_id'=>$ipa['id'], 'out_trade_no'=>$tradeNo]);
  43. return json_encode($content);
  44. }
  45. private function getAliPayInfo($plugin, $biz_content){
  46. $params = [];
  47. // $params['gatewayUrl'] = "https://openapi.alipaydev.com/gateway.do";
  48. // $params['gatewayUrl'] = "https://openapi.alipay.com/gateway.do ";
  49. $params['app_id'] = $plugin['appid'];
  50. $params['method'] = 'alipay.trade.app.pay';
  51. $params['format'] = "JSON";
  52. $params['charset'] = "UTF-8";
  53. $params['version'] = "1.0";
  54. $params['timestamp'] = getCurrentTime();
  55. $params['sign_type'] = "RSA2";
  56. $params['notify_url'] = 'http://'.$_SERVER['SERVER_NAME']._PHP_FILE_.'/'.$this->request->module().'/'.$this->request->controller().'/notify';
  57. $params['biz_content'] = $biz_content;
  58. ksort($params);
  59. $sign = aliSign($plugin['private_key'], getAliSignContent($params));
  60. $params['sign'] = $sign;
  61. foreach ($params as &$value) {
  62. // $value = characet($value, $params['charset']);
  63. $value = urlencode($value);
  64. }
  65. return $params;
  66. }
  67. }