MiniProgram.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. namespace app\common\util;
  3. use EasyWeChat\Factory;
  4. use EasyWeChat\Kernel\Exceptions\DecryptException;
  5. use EasyWeChat\Kernel\Exceptions\InvalidConfigException;
  6. use EasyWeChat\Kernel\Support\Collection;
  7. use GuzzleHttp\Exception\GuzzleException;
  8. use Psr\Http\Message\ResponseInterface;
  9. class MiniProgram
  10. {
  11. // public const pay_notify_url = 'https://natapp.sphper.com';
  12. public const pay_notify_url = 'https://test.server.ycxxkj.com/deliver/api/public/index.php';
  13. /**
  14. * 小程序实例
  15. * @return object
  16. */
  17. public static function getApp(): object
  18. {
  19. $wechat = config('wechat');
  20. $config = [
  21. // 必要配置
  22. 'app_id' => $wechat['app_id'],//appId
  23. 'secret' => $wechat['secret'],
  24. // 下面为可选项
  25. // 指定 API 调用返回结果的类型:array(default)/collection/object/raw/自定义类名
  26. 'response_type' => 'array',
  27. ];
  28. return Factory::miniProgram($config);
  29. }
  30. /**
  31. * 支付实例
  32. * @return object
  33. */
  34. public static function getPay(): object
  35. {
  36. $wechat = config('wechat');
  37. $config = [
  38. // 必要配置
  39. 'app_id' => $wechat['app_id'],
  40. 'mch_id' => $wechat['mch_id'],
  41. 'key' => $wechat['key'],
  42. 'cert_path' => $wechat['cert_path'],
  43. 'key_path' => $wechat['key_path'],
  44. 'notify_url' => $wechat['notify_url'],
  45. ];
  46. return Factory::payment($config);
  47. }
  48. /**
  49. * 获取手机号 非解密
  50. * @param $code
  51. * @return array|Collection|object|ResponseInterface|string
  52. * @throws InvalidConfigException
  53. * @throws GuzzleException
  54. */
  55. public static function getPhoneNumber($code)
  56. {
  57. $params = [
  58. 'code' => $code
  59. ];
  60. $app = self::getApp();
  61. return $app->live->httpPostJson('wxa/business/getuserphonenumber', $params);
  62. }
  63. /**
  64. * 获取手机号 解密
  65. * @param $session_key
  66. * @param $iv
  67. * @param $encryptedData
  68. * @return array
  69. * @throws DecryptException
  70. */
  71. public static function getPhoneNumberIv($session_key, $iv, $encryptedData): array
  72. {
  73. $app = self::getApp();
  74. return $app->encryptor->decryptData($session_key, $iv, $encryptedData);
  75. }
  76. /**
  77. * NATIVE
  78. * @param $order_no
  79. * @param $total_fee
  80. * @return false|string
  81. */
  82. public static function nativePay($order_no, $total_fee)
  83. {
  84. $app = self::getPay();
  85. $result = $app->order->unify([
  86. 'trade_type' => 'NATIVE',
  87. 'body' => '快递柜费用',
  88. 'out_trade_no' => $order_no,
  89. 'total_fee' => $total_fee * 100,
  90. 'notify_url' => self::pay_notify_url . '/mobile/payNotify/index',
  91. 'product_id' => 1, // $message['product_id'] 则为生成二维码时的产品 ID
  92. // ...
  93. ]);
  94. if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') {
  95. return QrCode::createEwm($result['code_url']);
  96. }
  97. return false;
  98. }
  99. /**
  100. * JSAPI
  101. * @param $order_no
  102. * @param $total_fee
  103. * @param $openid
  104. * @return mixed
  105. */
  106. public static function JsApiPay($order_no, $total_fee, $openid)
  107. {
  108. $app = self::getPay();
  109. $jsSdk = $app->jssdk;
  110. $result = $app->order->unify([
  111. 'body' => '快递柜费用',
  112. 'out_trade_no' => $order_no,
  113. 'total_fee' => intval($total_fee * 100),
  114. 'notify_url' => self::pay_notify_url . '/api/payNotify/index', // 支付结果通知网址,如果不设置则会使用配置里的默认地址
  115. 'trade_type' => 'JSAPI', // 请对应换成你的支付方式对应的值类型
  116. 'openid' => $openid,
  117. ]);
  118. $pay_json = false;
  119. if ($result['return_code']=='SUCCESS'&&$result['result_code']=='SUCCESS'){
  120. $pay_json = json_decode($jsSdk->bridgeConfig($result['prepay_id']));
  121. }
  122. return $pay_json;
  123. }
  124. /**
  125. * 创建二维码
  126. * @param $param
  127. * @return string
  128. */
  129. public static function createEwm($param): string
  130. {
  131. $app = self::getApp();
  132. $response = $app->app_code->get(config('ewm.path') . '?' . $param);
  133. $file_name = self::rand() . '.png';
  134. $dir = public_path() . 'storage/topic/' . date('Ymd');
  135. if (!is_dir($dir)) {
  136. mkdir($dir);
  137. }
  138. $saveFile = $dir . '/' . $file_name;
  139. $url = request()->domain() . getVirRootDir() . '/storage/topic/' . date('Ymd') . '/' . $file_name;
  140. file_put_contents($saveFile, $response);
  141. return $url;
  142. }
  143. private static function rand()
  144. {
  145. $len = 10;
  146. $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
  147. $string = time();
  148. for (; $len >= 1; $len--) {
  149. $position = rand() % strlen($chars);
  150. $position2 = rand() % strlen($string);
  151. $string = substr_replace($string, substr($chars, $position, 1), $position2, 0);
  152. }
  153. return $string;
  154. }
  155. }