AlipayProfit.php 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\utils\Alipay;
  8. use app\models\Goods;
  9. use app\models\Option;
  10. use app\constants\OptionSetting;
  11. use app\models\OrderDetail;
  12. use app\models\OrderUnion;
  13. use app\models\SharingReceiver;
  14. use app\models\Store;
  15. use app\models\StoreAliMini;
  16. use app\models\StoreMiniToken;
  17. use app\models\User;
  18. use app\modules\admin\models\AlipayThirdAppForm;
  19. use app\utils\Alipay\alipaySdk\aop\request\AlipayTradeRoyaltyRelationBindRequest;
  20. use app\utils\OrderNo;
  21. use Symfony\Component\HttpFoundation\Response;
  22. use Yansongda\Pay\Exceptions\GatewayException;
  23. use Yansongda\Pay\Exceptions\InvalidConfigException;
  24. use Yansongda\Pay\Exceptions\InvalidSignException;
  25. use Yansongda\Pay\Pay;
  26. use Yansongda\Supports\Collection;
  27. use yii\base\BaseObject;
  28. use yii\helpers\ArrayHelper;
  29. use yii\helpers\Json;
  30. use app\utils\CurlHelper;
  31. include_once 'alipaySdk/aop/AopClient.php';
  32. include_once 'alipaySdk/aop/request/AlipayMerchantItemFileUploadRequest.php';
  33. /**
  34. * Class Alipay 证书公钥模式
  35. * 支付宝支付相关操作
  36. * @package app\utils
  37. */
  38. class AlipayProfit
  39. {
  40. /**
  41. * @var \AopClient $aop
  42. */
  43. private static $aop;
  44. private static $gatewayUrl = 'https://openapi.alipay.com/gateway.do';
  45. public static $alipay_config;
  46. /**
  47. * 授权地址前缀
  48. */
  49. const AUTH_URL = 'https://openauth.alipay.com/oauth2/appToAppAuth.htm';
  50. const FLAG_REFUND = 'refund';
  51. const FLAG_NORMAL = 'wap';
  52. const FLAG_TRANSFER = 'transfer';
  53. private static $validFlag = [
  54. self::FLAG_NORMAL,
  55. self::FLAG_REFUND,
  56. self::FLAG_TRANSFER
  57. ];
  58. /**
  59. * 授权应用类型
  60. * @var string[]
  61. */
  62. private static $validAuthType = [
  63. 'TINYAPP',
  64. 'WEBAPP',
  65. 'MOBILEAPP',
  66. 'PUBLICAPP'
  67. ];
  68. /**
  69. * @var \Yansongda\Pay\Gateways\Alipay
  70. */
  71. public static $alipay;
  72. /**
  73. * @var string
  74. * 回调url
  75. */
  76. private static $notify_url = 'alipay/notify';
  77. public static $h5_url = 'order/order/order';
  78. public $mini_id;
  79. public $store_id;
  80. /**
  81. * @param $return_url
  82. * @param $level
  83. * 获取支付宝实例
  84. */
  85. private function __construct($store_id, $return_url = 'https://www.baidu.com', $is_app = false, $type = 0)
  86. {
  87. $store_id = !empty($store_id) ? $store_id : get_store_id();
  88. $this->store_id = $store_id;
  89. // 第三方应用代理调用支付宝相关接口
  90. // 相关配置为第三方应用所属,但必须加上授权token(app_auth_token)
  91. $alipay_config = Option::getSaasAlipay();
  92. $data = ['store_id' => $store_id, 'is_cancel' => 0];
  93. if (\Yii::$app->prod_is_dandianpu()) {
  94. if (self_mini() === true) {
  95. $data['is_use'] = 1;
  96. }
  97. } else {
  98. $data['is_use'] = 1;
  99. }
  100. //兼容支付宝平台运营app支付
  101. if ($is_app) {
  102. $store_token = StoreMiniToken::find()->where(['batch_status' => [2, 6], 'store_id' => $store_id])->select('app_auth_token')->asArray()->one();
  103. $store_mini['auth_token'] = $store_token['app_auth_token'];
  104. if (empty($store_mini['auth_token'])) {
  105. $store_mini = StoreAliMini::find()->where($data)->orderBy('id desc')->select('auth_token')->asArray()->one();
  106. }
  107. } else {
  108. $store_mini = StoreAliMini::find()->where($data)->orderBy('id desc')->select('auth_token')->asArray()->one();
  109. }
  110. self::$alipay_config = $alipay_config;
  111. $config = [
  112. 'app_id' => $alipay_config['app_id'],
  113. 'return_url' => $return_url,
  114. 'notify_url' => pay_notify_url(self::$notify_url),
  115. 'private_key' => $alipay_config['app_private_key'],
  116. 'log' => [ // optional
  117. 'file' => \Yii::$app->basePath . '/runtime/logs/alipay.log',
  118. 'level' => 'debug', // 建议生产环境等级调整为 info,开发环境为 debug
  119. 'type' => 'single', // optional, 可选 daily.
  120. 'max_file' => 30, // optional, 当 type 为 daily 时有效,默认 30 天
  121. ],
  122. 'ali_public_key' => $alipay_config['alipay_public_key'],
  123. 'app_auth_token' => $store_mini['auth_token'] ?: Store::findOne($store_id)->app_auth_token
  124. // 'app_cert_public_key' => \Yii::$app->basePath. '/utils/Alipay/CRT/app_cert_public_key.crt', // 应用公钥证书路径
  125. // 'ali_public_key' => \Yii::$app->basePath. '/utils/Alipay/CRT/alipay_cert_public_key.crt', // 支付宝公钥证书路径
  126. // 'alipay_root_cert' => \Yii::$app->basePath. '/utils/Alipay/CRT/alipay_root_cert.crt', // 支付宝根证书路径
  127. // 'mode' => 'dev' // optional,设置此参数,将进入沙箱模式
  128. ];
  129. if ($type) {
  130. $key = Option::ALIPAY_CONFIG_CERT;
  131. if (\Yii::$app->prod_is_dandianpu() && is_h5()){
  132. $key = OptionSetting::ALIPAY_PAY_DANDIANPUH5;
  133. if(!Store::hasIncoming($store_id, 'alipay', is_h5())){
  134. $store_id = 0;
  135. }
  136. }
  137. $config_cache = Option::get($key, $store_id, 'alipay')['value'];
  138. $config_cache = json_decode($config_cache, true);
  139. if (!empty($config_cache)) {
  140. $config = [
  141. 'app_id' => $config_cache['app_id'],
  142. 'notify_url' => pay_notify_url(self::$notify_url),
  143. 'return_url' => $return_url,
  144. 'log' => [ // optional
  145. 'file' => \Yii::$app->basePath . '/runtime/logs/alipay.log',
  146. 'level' => 'debug', // 建议生产环境等级调整为 info,开发环境为 debug
  147. 'type' => 'single', // optional, 可选 daily.
  148. 'max_file' => 30, // optional, 当 type 为 daily 时有效,默认 30 天
  149. ],
  150. 'private_key' => $config_cache['app_private_key'],
  151. 'app_cert_public_key' => $this->saveTempImage($config_cache['app_public_cert']), // 应用公钥证书路径
  152. 'ali_public_key' => $this->saveTempImage($config_cache['alipay_public_cert']), // 支付宝公钥证书路径
  153. 'alipay_root_cert' => $this->saveTempImage($config_cache['alipay_root_cert']), // 支付宝根证书路径
  154. ];
  155. }
  156. }
  157. self::$alipay = Pay::alipay($config);
  158. }
  159. /**
  160. * 静态方法调用construct方法
  161. * @param $return_url
  162. */
  163. public static function init($store_id, $return_url = '', $is_app = false, $type = 0) {
  164. return new self($store_id, $return_url, $is_app, $type);
  165. }
  166. //获取网络图片到临时目录
  167. public function saveTempImage($url)
  168. {
  169. if (strpos($url, 'http') === false) {
  170. $url = 'http:' . trim($url);
  171. }
  172. if (!is_dir(\Yii::$app->runtimePath . '/crt')) {
  173. mkdir(\Yii::$app->runtimePath . '/crt');
  174. }
  175. $save_path = \Yii::$app->runtimePath . '/crt/' . md5($url) . '.crt';
  176. CurlHelper::download($url, $save_path);
  177. return $save_path;
  178. }
  179. /**
  180. * app支付
  181. * @param $order
  182. * @param User $user
  183. * @param $subject
  184. * @param $balance_price
  185. * @param $return_url
  186. * @return Response | false | string | array
  187. */
  188. public static function app($order, $subject, $user, $type = '', $total_pay_price = 0,$balance_price = 0, $return_url = '') {
  189. self::init($order->store_id, $return_url, true);
  190. $order1 = $order;
  191. if($type == OrderNo::ORDER_UNION){
  192. $order = [
  193. 'body' => '订单合并支付',
  194. 'out_trade_no' => OrderNo::getOrderNo(OrderNo::ORDER_UNION),
  195. 'total_amount' => $balance_price > 0 ? $total_pay_price - $balance_price : $total_pay_price,
  196. 'subject' => count($order1) . '笔订单合并支付',
  197. 'extend_params' => [
  198. 'royalty_freeze' => 'true'
  199. ]
  200. ];
  201. $order_union = new OrderUnion();
  202. $order_union->store_id = get_store_id();
  203. $order_union->user_id = get_user()->id;
  204. $order_union->order_no = $order['out_trade_no'];
  205. $order_union->price = $order['total_amount'];
  206. $order_union->is_pay = 0;
  207. $order_union->created_at = time();
  208. $order_union->is_delete = 0;
  209. $order_id_list = [];
  210. foreach ($order1 as $value) {
  211. $order_id_list[] = $value->id;
  212. }
  213. $order_union->order_id_list = json_encode($order_id_list);
  214. if (!$order_union->save()) {
  215. foreach ($order_union->errors as $error) {
  216. return [
  217. 'code' => 1,
  218. 'msg' => $error
  219. ];
  220. }
  221. }
  222. }else{
  223. /** 逻辑开始 **/
  224. $order = [
  225. 'body' => $subject,
  226. 'out_trade_no' => $order->order_no,
  227. 'total_amount' => $balance_price > 0 ? ($order->pay_price - $balance_price) : $order->pay_price,
  228. 'subject' => $subject,
  229. 'extend_params' => [
  230. 'royalty_freeze' => 'true'
  231. ]
  232. ];
  233. }
  234. \Yii::error($order);
  235. try {
  236. $res = self::$alipay->app($order)->getContent();
  237. } catch (\Exception $e) {
  238. return [
  239. 'code' => 1,
  240. 'msg' => $e->getMessage()
  241. ];
  242. }
  243. if($type == OrderNo::ORDER_UNION){
  244. foreach($order1 as $value){
  245. $value->order_union_id = $order_union->id;
  246. $value->save();
  247. }
  248. }
  249. return [
  250. 'code' => 0,
  251. 'msg' => 'success',
  252. 'data' => $res,
  253. 'res' => $res
  254. ];
  255. }
  256. /**
  257. * 订单查询
  258. * @param $order
  259. * @param $type 默认:wap: 普通订单,refund:退款订单,transfer: 转账订单
  260. * @return array | Collection
  261. */
  262. public static function find($order, $type = 'wap') {
  263. if (!in_array($type, self::$validFlag)) {
  264. return [
  265. 'code' => 1,
  266. 'msg' => 'Params are wrong'
  267. ];
  268. }
  269. self::init($order->store_id);
  270. /** 逻辑开始 **/
  271. $order = [
  272. 'out_trade_no' => $order->order_no,
  273. 'query_options' => ['trade_settle_info']
  274. ];
  275. if ($type == self::FLAG_REFUND) {
  276. // 后端生成,唯一即可
  277. $order['out_request_no'] = $order->order_no;
  278. }
  279. try {
  280. $res = self::$alipay->find($order, $type);
  281. if ($res && $res->code === '10000' && $res->msg === 'Success') {
  282. return [
  283. 'code' => 0,
  284. 'msg' => 'success',
  285. 'data' => $res->toArray()
  286. ];
  287. }
  288. return [
  289. 'code' => $res->code,
  290. 'msg' => $res->msg,
  291. ];
  292. } catch (GatewayException $e) {
  293. return [
  294. 'code' => 1,
  295. 'msg' => $e->getMessage()
  296. ];
  297. } catch (InvalidSignException $e) {
  298. return [
  299. 'code' => 1,
  300. 'msg' => $e->getMessage()
  301. ];
  302. } catch (InvalidConfigException $e) {
  303. return [
  304. 'code' => 1,
  305. 'msg' => $e->getMessage()
  306. ];
  307. }
  308. }
  309. /**
  310. * wap支付
  311. * @param $order
  312. * @param $subject
  313. * @param $return_url
  314. * @return array
  315. */
  316. public static function wap($order, $subject, $return_url = '', $type = '', $total_pay_price = 0, $balance_price = 0, $payData = []) {
  317. if(!$return_url){
  318. $return_url = pay_return_url(self::$h5_url);
  319. }
  320. self::init($order->store_id, $return_url, false, 1);
  321. $order1 = $order;
  322. if($type == OrderNo::ORDER_UNION){
  323. $order = [
  324. 'body' => '订单合并支付',
  325. 'out_trade_no' => OrderNo::getOrderNo(OrderNo::ORDER_UNION),
  326. 'total_amount' => $balance_price > 0 ? $total_pay_price - $balance_price : $total_pay_price,
  327. 'subject' => count($order1) . '笔订单合并支付',
  328. 'buyer_id' => get_user()->alipay_open_id
  329. ];
  330. $order_union = new OrderUnion();
  331. $order_union->store_id = get_store_id();
  332. $order_union->user_id = get_user()->id;
  333. $order_union->order_no = $order['out_trade_no'];
  334. $order_union->price = $order['total_amount'];
  335. $order_union->is_pay = 0;
  336. $order_union->created_at = time();
  337. $order_union->is_delete = 0;
  338. $order_id_list = [];
  339. foreach ($order1 as $value) {
  340. $order_id_list[] = $value->id;
  341. }
  342. $order_union->order_id_list = json_encode($order_id_list);
  343. if (!$order_union->save()) {
  344. foreach ($order_union->errors as $error) {
  345. return [
  346. 'code' => 1,
  347. 'msg' => $error
  348. ];
  349. }
  350. }
  351. }else{
  352. /** 逻辑开始 **/
  353. $order = [
  354. 'body' => $subject,
  355. 'out_trade_no' => $order->order_no,
  356. 'total_amount' => $order->pay_price,
  357. 'subject' => $subject,
  358. 'buyer_id' => get_user()->alipay_open_id
  359. ];
  360. }
  361. try {
  362. $res = self::$alipay->wap($order);
  363. if ($res->getStatusCode() == 40004 || $res->getStatusCode() == 'ACQ.CONTEXT_INCONSISTENT') {
  364. $order->order_no = OrderNo::getOrderNo(OrderNo::ORDER_MALL);
  365. $order->save();
  366. return self::wap($order, $subject, $return_url, $type, $total_pay_price, $balance_price, $payData);
  367. }
  368. if($type == OrderNo::ORDER_UNION){
  369. foreach($order1 as $value){
  370. $value->order_union_id = $order_union->id;
  371. $value->save();
  372. }
  373. }
  374. return [
  375. 'code' => 0,
  376. 'msg' => 'success',
  377. 'data' => $res->getContent()
  378. ];
  379. } catch (\Exception $e) {
  380. return [
  381. 'code' => 1,
  382. 'msg' => '支付失败'. $e->getMessage()
  383. ];
  384. }
  385. }
  386. /**
  387. * web支付
  388. * @param $order
  389. * @param $subject
  390. * @param $return_url
  391. * @return Response | false | string
  392. */
  393. public static function web($order, $subject, $return_url = '') {
  394. self::init($order->store_id, $return_url);
  395. /** 逻辑开始 **/
  396. $order = [
  397. 'body' => $subject,
  398. 'out_trade_no' => $order->order_no,
  399. 'total_amount' => $order->pay_price,
  400. 'subject' => $subject,
  401. ];
  402. $res = self::$alipay->web($order)->send();
  403. return $res->getContent();
  404. }
  405. /**
  406. * 扫码支付
  407. * @param $order
  408. * @param $subject
  409. * @param $return_url
  410. * @return Collection | array
  411. */
  412. public static function scan($order, $subject, $return_url = '') {
  413. self::init($order->store_id, $return_url);
  414. /** 逻辑开始 **/
  415. $order = [
  416. 'body' => $subject,
  417. 'out_trade_no' => $order->order_no,
  418. 'total_amount' => $order->pay_price,
  419. 'subject' => $subject,
  420. ];
  421. try {
  422. $res = self::$alipay->scan($order);
  423. if ($res && $res->code === '10000' && $res->msg === 'Success') {
  424. return [
  425. 'code' => 0,
  426. 'msg' => 'success',
  427. 'data' => $res->toArray()
  428. ];
  429. }
  430. return [
  431. 'code' => $res->code,
  432. 'msg' => $res->msg,
  433. ];
  434. } catch (GatewayException $e) {
  435. return [
  436. 'code' => 1,
  437. 'msg' => $e->getMessage()
  438. ];
  439. } catch (InvalidSignException $e) {
  440. return [
  441. 'code' => 1,
  442. 'msg' => $e->getMessage()
  443. ];
  444. } catch (InvalidConfigException $e) {
  445. return [
  446. 'code' => 1,
  447. 'msg' => $e->getMessage()
  448. ];
  449. }
  450. }
  451. /**
  452. * 付款码支付
  453. * @param $order
  454. * @param $subject 订单标题
  455. * @param $return_url
  456. * @return Collection | array
  457. */
  458. public static function scanNew($order, $subject, $return_url = '', $auth_code,$total_pay_price = 0,$balance_price = 0) {
  459. self::init($order->store_id,$return_url);
  460. /** 逻辑开始 **/
  461. $data = [
  462. 'out_trade_no' => $order->order_no,//订单号
  463. 'total_amount' => $balance_price > 0 ? $total_pay_price - $balance_price : $total_pay_price,
  464. 'subject' => $subject,// 订单标题
  465. 'auth_code' => $auth_code,
  466. 'scene' => 'bar_code',
  467. ];
  468. try {
  469. $res = self::$alipay->pos($data);
  470. if ($res && $res->code === '10000' && $res->msg === 'Success') {
  471. return [
  472. 'code' => 0,
  473. 'msg' => 'success',
  474. 'data' => $res->toArray()
  475. ];
  476. }
  477. return [
  478. 'code' => $res->code,
  479. 'msg' => $res->msg,
  480. ];
  481. } catch (GatewayException $e) {
  482. return [
  483. 'code' => 1,
  484. 'msg' => $e->getMessage()
  485. ];
  486. } catch (InvalidSignException $e) {
  487. return [
  488. 'code' => 1,
  489. 'msg' => $e->getMessage()
  490. ];
  491. } catch (InvalidConfigException $e) {
  492. return [
  493. 'code' => 1,
  494. 'msg' => $e->getMessage()
  495. ];
  496. }
  497. }
  498. /**
  499. * 转账
  500. * @param $order
  501. * @param $mail
  502. * @param $return_url
  503. * @return Collection | array
  504. */
  505. public static function transfer($order, $mail, $return_url = '') {
  506. if (empty($mail)) {
  507. return [
  508. 'code' => 1,
  509. 'msg' => 'Params are wrong'
  510. ];
  511. }
  512. self::init($order->store_id, $return_url);
  513. /** 逻辑开始 **/
  514. $order = [
  515. 'out_biz_no' => $order->order_no,
  516. 'total_amount' => $order->pay_price,
  517. 'product_code' => 'TRANS_ACCOUNT_NO_PWD',
  518. 'payee_info' => [
  519. 'identity' => $mail,
  520. 'identity_type' => 'ALIPAY_LOGON_ID',
  521. ],
  522. ];
  523. try {
  524. $res = self::$alipay->transfer($order);
  525. if ($res && $res->code === '10000' && $res->msg === 'Success') {
  526. return [
  527. 'code' => 0,
  528. 'msg' => 'success',
  529. 'data' => $res->toArray()
  530. ];
  531. }
  532. return [
  533. 'code' => $res->code,
  534. 'msg' => $res->msg,
  535. ];
  536. } catch (GatewayException $e) {
  537. return [
  538. 'code' => 1,
  539. 'msg' => $e->getMessage()
  540. ];
  541. } catch (InvalidSignException $e) {
  542. return [
  543. 'code' => 1,
  544. 'msg' => $e->getMessage()
  545. ];
  546. } catch (InvalidConfigException $e) {
  547. return [
  548. 'code' => 1,
  549. 'msg' => $e->getMessage()
  550. ];
  551. }
  552. }
  553. /**
  554. * 批量转账
  555. * @param $params
  556. * @return Collection | array
  557. */
  558. public static function batchTransfer($params) {
  559. self::init('');
  560. /** 逻辑开始 **/
  561. $order = [
  562. 'out_batch_no' => $params['out_batch_no'],
  563. 'product_code' => 'BATCH_API_TO_ACC_NO_PWD',
  564. 'biz_scene' => 'MESSAGE_BATCH_PAY',
  565. 'total_trans_amount' => $params['total_trans_amount'],
  566. 'total_count' => 1,
  567. 'trans_order_list' => [
  568. [
  569. 'out_biz_no' => $params['out_batch_no'],
  570. 'trans_amount' => $params['total_trans_amount'],
  571. 'payee_info' => [
  572. 'identity' => $params['account'],
  573. 'identity_type' => 'ALIPAY_LOGON_ID',
  574. 'name' => $params['name']
  575. ],
  576. 'remark' => $params['title']
  577. ],
  578. ],
  579. 'order_title' => $params['title'],
  580. 'remark' => '转账',
  581. ];
  582. try {
  583. $res = self::$alipay->batchtransfer($order);
  584. \Yii::error($res);
  585. if ($res && $res->code === '10000' && $res->msg === 'Success') {
  586. return [
  587. 'code' => 0,
  588. 'msg' => 'success',
  589. 'data' => $res->toArray()
  590. ];
  591. }
  592. return [
  593. 'code' => $res->code,
  594. 'msg' => $res->msg,
  595. ];
  596. } catch (GatewayException $e) {
  597. return [
  598. 'code' => 1,
  599. 'msg' => $e->getMessage()
  600. ];
  601. } catch (InvalidSignException $e) {
  602. return [
  603. 'code' => 1,
  604. 'msg' => $e->getMessage()
  605. ];
  606. } catch (InvalidConfigException $e) {
  607. return [
  608. 'code' => 1,
  609. 'msg' => $e->getMessage()
  610. ];
  611. }
  612. }
  613. /**
  614. * 退款
  615. * @param $order
  616. * @param $return_url
  617. * @param $refund_fee
  618. * @return Collection | array
  619. */
  620. public static function refund($order, $refund_fee, $return_url = '') {
  621. self::init($order->store_id, $return_url);
  622. /** 逻辑开始 **/
  623. $data = [
  624. 'out_trade_no' => $order->order_no,
  625. 'refund_amount' => $refund_fee,
  626. ];
  627. try {
  628. $res = self::$alipay->refund($data);
  629. if ($res && $res->code === '10000' && $res->msg === 'Success') {
  630. return [
  631. 'code' => 0,
  632. 'msg' => 'success',
  633. 'data' => $res->toArray()
  634. ];
  635. }
  636. return [
  637. 'code' => $res->code,
  638. 'msg' => $res->msg,
  639. ];
  640. } catch (GatewayException $e) {
  641. return [
  642. 'code' => 1,
  643. 'msg' => $e->getMessage()
  644. ];
  645. } catch (InvalidSignException $e) {
  646. return [
  647. 'code' => 1,
  648. 'msg' => $e->getMessage()
  649. ];
  650. } catch (InvalidConfigException $e) {
  651. return [
  652. 'code' => 1,
  653. 'msg' => $e->getMessage()
  654. ];
  655. }
  656. }
  657. /**
  658. * 小程序支付
  659. * @param $order
  660. * @param $subject
  661. * @param User $user
  662. * @param $return_url
  663. * @param $is_food
  664. * @return Collection | array
  665. */
  666. public static function mini($order, $subject, $user, $return_url = '', $type = '', $is_food = false, $total_pay_price = 0, $balance_price = 0, $payData = []) {
  667. $self = self::init($order->store_id, $return_url);
  668. if (!$order || ($type != OrderNo::ORDER_UNION && empty($subject)) || !$user) {
  669. return [
  670. 'code' => 1,
  671. 'msg' => '参数有误'
  672. ];
  673. }
  674. $order1 = $order;
  675. if ($type == OrderNo::ORDER_UNION) {
  676. $order = [
  677. 'body' => '订单合并支付',
  678. 'out_trade_no' => OrderNo::getOrderNo(OrderNo::ORDER_UNION),
  679. 'total_amount' => $balance_price > 0 ? $total_pay_price - $balance_price : $total_pay_price,
  680. 'subject' => count($order1) . '笔订单合并支付',
  681. 'buyer_id' => $user->alipay_open_id
  682. ];
  683. $order_union = new OrderUnion();
  684. $order_union->store_id = get_store_id();
  685. $order_union->user_id = get_user()->id;
  686. $order_union->order_no = $order['out_trade_no'];
  687. $order_union->price = $order['total_amount'];
  688. $order_union->is_pay = 0;
  689. $order_union->created_at = time();
  690. $order_union->is_delete = 0;
  691. $order_id_list = [];
  692. foreach ($order1 as $value) {
  693. $order_id_list[] = $value->id;
  694. }
  695. $order_union->order_id_list = json_encode($order_id_list);
  696. if (!$order_union->save()) {
  697. foreach ($order_union->errors as $error) {
  698. return [
  699. 'code' => 1,
  700. 'msg' => $error
  701. ];
  702. }
  703. }
  704. } else {
  705. try{
  706. $goods_detail = [];
  707. $details = OrderDetail::findAll(['order_id' => $order->id]);
  708. foreach ($details as $detail) {
  709. $goods = Goods::findOne($detail->goods_id);
  710. $goods_detail[] = [
  711. 'goods_id' => $goods->goods_no,
  712. 'goods_name' => $detail->goods_name,
  713. 'quantity' => $detail->num,
  714. 'price' => $detail->total_price / $detail->num,
  715. ];
  716. }
  717. } catch (\Exception $e) {
  718. \Yii::error([__METHOD__, $order, $e]);
  719. }
  720. $order = [
  721. 'body' => $subject,
  722. 'out_trade_no' => $order->order_no,
  723. 'total_amount' => $balance_price > 0 ? ($order->pay_price - $balance_price) : $order->pay_price,
  724. 'subject' => $subject,
  725. 'buyer_id' => ($user->ali_openId ?: $user->alipay_open_id),
  726. 'store_id' => $order->store_id
  727. ];
  728. $goods_detail && $order['goods_detail'] = $goods_detail;
  729. if ($is_food) {
  730. $order['product_code'] = 'FACE_TO_FACE_PAYMENT';
  731. $order['extend_params'] = [
  732. 'food_order_type' => 'qr_order',
  733. 'sys_service_provider_id' => self::$alipay_config['user_id']
  734. ];
  735. } else {
  736. $store = Store::findOne($order['store_id']);
  737. if($store->transfer_profit > 0){
  738. $order['extend_params'] = [
  739. 'royalty_freeze' => 'true'
  740. ];
  741. }
  742. $order['product_code'] = 'JSAPI_PAY';
  743. }
  744. }
  745. try {
  746. if($payData->ali_requireOrder && $payData->ali_sourceId){
  747. $form = new \app\modules\admin\models\AlipayThirdForm();
  748. $form->bind_store_id = $order->store_id ?: get_store_id();
  749. $form->mini_id = $self->mini_id;
  750. $openOrder = $form->AlipayOpenMiniOrderCreate($order1, $order, $payData);
  751. if($openOrder['code'] != 0){
  752. return $openOrder;
  753. }
  754. $order['product_code'] = 'JSAPI_PAY';
  755. $order['extend_params'] = array_merge((array)$order['extend_params'], [
  756. 'trade_component_order_id' => $openOrder['data'],
  757. ]);
  758. foreach($openOrder['biz_content']['order_detail']['item_infos'] as $item){
  759. $order['goods_detail'][] = [
  760. 'goods_id' => $item['goods_id'],
  761. 'goods_name' => $item['goods_name'],
  762. 'quantity' => $item['item_cnt'],
  763. 'price' => $item['sale_price'],
  764. 'out_item_id' => $item['out_item_id'],
  765. 'out_sku_id' => $item['out_sku_id'],
  766. ];
  767. }
  768. }
  769. $res = self::$alipay->mini($order);
  770. if ($res && $res->code === '10000' && $res->msg === 'Success') {
  771. return [
  772. 'code' => 0,
  773. 'msg' => 'success',
  774. 'data' => $res->toArray()
  775. ];
  776. }
  777. if ($type == OrderNo::ORDER_UNION) {
  778. foreach ($order1 as $value) {
  779. $value->order_union_id = $value->id;
  780. $value->save();
  781. }
  782. }
  783. return [
  784. 'code' => $res->code,
  785. 'msg' => $res->msg,
  786. ];
  787. } catch (GatewayException $e) {
  788. return [
  789. 'code' => 1,
  790. 'msg' => $e->getMessage()
  791. ];
  792. } catch (InvalidSignException $e) {
  793. return [
  794. 'code' => 1,
  795. 'msg' => $e->getMessage()
  796. ];
  797. } catch (InvalidConfigException $e) {
  798. return [
  799. 'code' => 1,
  800. 'msg' => $e->getMessage()
  801. ];
  802. }
  803. }
  804. /**
  805. * token
  806. * @param $authCode
  807. * @return array | Collection
  808. */
  809. public static function token($authCode) {
  810. if (empty($authCode)) {
  811. return [
  812. 'code' => 1,
  813. 'msg' => 'code不能为空'
  814. ];
  815. }
  816. $alipay_config = Option::getSaasAlipay();
  817. $config = [
  818. 'app_id' => $alipay_config['app_id'],
  819. 'return_url' => 'https://quanqudao.we10.cn',
  820. 'notify_url' => pay_notify_url(self::$notify_url),
  821. 'private_key' => $alipay_config['app_private_key'],
  822. 'log' => [ // optional
  823. 'file' => \Yii::$app->basePath . '/runtime/logs/alipay.log',
  824. 'level' => 'debug', // 建议生产环境等级调整为 info,开发环境为 debug
  825. 'type' => 'single', // optional, 可选 daily.
  826. 'max_file' => 30, // optional, 当 type 为 daily 时有效,默认 30 天
  827. ],
  828. 'ali_public_key' => $alipay_config['alipay_public_key'],
  829. // 'app_cert_public_key' => \Yii::$app->basePath. '/utils/Alipay/CRT/app_cert_public_key.crt', // 应用公钥证书路径
  830. // 'ali_public_key' => \Yii::$app->basePath. '/utils/Alipay/CRT/alipay_cert_public_key.crt', // 支付宝公钥证书路径
  831. // 'alipay_root_cert' => \Yii::$app->basePath. '/utils/Alipay/CRT/alipay_root_cert.crt', // 支付宝根证书路径
  832. // 'mode' => 'dev' // optional,设置此参数,将进入沙箱模式
  833. ];
  834. $alipay = Pay::alipay($config);
  835. $params = [
  836. 'grant_type' => 'authorization_code',
  837. 'code' => $authCode
  838. ];
  839. /** 逻辑开始 **/
  840. try {
  841. $res = $alipay->token($params)->get('alipay_open_auth_token_app_response');
  842. if ($res && $res['code'] === '10000' && $res['msg'] === 'Success') {
  843. return [
  844. 'code' => 0,
  845. 'msg' => 'success',
  846. 'data' => $res['tokens'][0]['app_auth_token']
  847. ];
  848. }
  849. return [
  850. 'code' => 1,
  851. 'msg' => $res['sub_msg'],
  852. 'res' => $res
  853. ];
  854. } catch (GatewayException $e) {
  855. return [
  856. 'code' => 1,
  857. 'msg' => $e->getMessage()
  858. ];
  859. } catch (InvalidSignException $e) {
  860. return [
  861. 'code' => 1,
  862. 'msg' => $e->getMessage()
  863. ];
  864. } catch (InvalidConfigException $e) {
  865. return [
  866. 'code' => 1,
  867. 'msg' => $e->getMessage()
  868. ];
  869. }
  870. }
  871. /**
  872. * 拼接授权链接供商户提供授权
  873. */
  874. public static function authLink() {
  875. $store_id = get_store_id();
  876. $app_id = Option::get('alipay_appid', 0, 'saas')['value'];
  877. if (empty($app_id)) {
  878. return [
  879. 'code' => 1,
  880. 'msg' => '服务商appid不存在'
  881. ];
  882. }
  883. $type = join(',', self::$validAuthType);
  884. $redirect_uri = \Yii::$app->request->hostInfo . '/index.php/alipay/callback';
  885. $callback_url = self::AUTH_URL . '?app_id=' . $app_id . '&application_type='. $type . '&redirect_uri=' . $redirect_uri . '&state=' . $store_id;
  886. return [
  887. 'code' => 0,
  888. 'data' => ['url' => $callback_url]
  889. ];
  890. }
  891. /**
  892. * 绑定关系
  893. * @param string $out_trade_no
  894. * @param array $receiver_list
  895. * @return array
  896. */
  897. public static function bind($out_trade_no, $receiver_list, $is_app = false) {
  898. // 绑定关系必须为真实姓名。个人账户为个人真实姓名,企业账户为企业名称
  899. \Yii::warning($receiver_list);
  900. $valid_receive_type = array_keys(SharingReceiver::$validAlipayReceiveType);
  901. foreach ($receiver_list as $index => $receiver) {
  902. if (empty($receiver['account']) || !in_array($receiver['type'], $valid_receive_type) || empty($receiver['name'])) {
  903. unset($receiver_list[$index]);
  904. \Yii::warning('<========================> 订单号:' . $out_trade_no . '添加分账关系校验参数错误:' . json_encode($receiver));
  905. continue;
  906. }
  907. $receiver_list[$index]['type'] = SharingReceiver::$validAlipayReceiveType[$receiver['type']];
  908. }
  909. if (empty($receiver_list)) {
  910. return [
  911. 'code' => 1,
  912. 'msg' => '接收方为空'
  913. ];
  914. }
  915. $store_id = get_store_id();
  916. // self::init($store_id, '', $is_app);
  917. $params = [
  918. 'receiver_list' => $receiver_list,
  919. 'out_request_no' => $out_trade_no
  920. ];
  921. //原来逻辑会报错验签失败,所以用新款的 START
  922. $data = ['store_id' => $store_id, 'is_cancel' => 0];
  923. if (\Yii::$app->prod_is_dandianpu()) {
  924. if (self_mini() === true) {
  925. $data['is_use'] = 1;
  926. }
  927. } else {
  928. $data['is_use'] = 1;
  929. }
  930. if ($is_app) {
  931. $store_token = StoreMiniToken::find()->where(['batch_status' => [2, 6], 'store_id' => $store_id])->select('app_auth_token')->asArray()->one();
  932. $store_mini['auth_token'] = $store_token['app_auth_token'];
  933. if (empty($store_mini['auth_token'])) {
  934. $store_mini = StoreAliMini::find()->where($data)->orderBy('id desc')->select('auth_token')->asArray()->one();
  935. }
  936. } else {
  937. $store_mini = StoreAliMini::find()->where($data)->orderBy('id desc')->select('auth_token')->asArray()->one();
  938. }
  939. $form = new AlipayThirdAppForm();
  940. $request = new AlipayTradeRoyaltyRelationBindRequest();
  941. $request->setBizContent(json_encode($params, JSON_UNESCAPED_UNICODE));
  942. $result = $form->miniCommon($request, null, 0, $store_mini['auth_token']);
  943. debug_log(ArrayHelper::toArray($result), 'Alipaybind.log');
  944. if ($result->code == 10000) {
  945. return [
  946. 'code' => 0,
  947. 'msg' => '绑定关系成功'
  948. ];
  949. } else {
  950. return [
  951. 'code' => 1,
  952. 'msg' => $result->sub_msg
  953. ];
  954. }
  955. //END
  956. \Yii::warning(json_encode($params));
  957. /** 逻辑开始 **/
  958. try {
  959. $res = self::$alipay->bind($params)->get('alipay_trade_royalty_relation_bind_response');
  960. \Yii::warning($res);
  961. if ($res && $res['code'] === '10000' && $res['msg'] === 'Success' && $res['result_code'] == 'SUCCESS') {
  962. return [
  963. 'code' => 0,
  964. 'msg' => '绑定关系成功'
  965. ];
  966. }
  967. return [
  968. 'code' => 1,
  969. 'msg' => $res['sub_msg'],
  970. 'res' => $res
  971. ];
  972. } catch (GatewayException $e) {
  973. return [
  974. 'code' => 1,
  975. 'msg' => $e->getMessage()
  976. ];
  977. } catch (InvalidSignException $e) {
  978. return [
  979. 'code' => 1,
  980. 'msg' => $e->getMessage()
  981. ];
  982. } catch (InvalidConfigException $e) {
  983. return [
  984. 'code' => 1,
  985. 'msg' => $e->getMessage()
  986. ];
  987. }
  988. }
  989. /**
  990. * 解绑关系
  991. * @param string $out_trade_no
  992. * @param array $receiver_list
  993. * @return array
  994. */
  995. public static function unbind($out_trade_no, $receiver_list) {
  996. $valid_receive_type = array_keys(SharingReceiver::$validAlipayReceiveType);
  997. foreach ($receiver_list as $index => $receiver) {
  998. if (empty($receiver['account']) || !in_array($receiver['type'], $valid_receive_type)) {
  999. unset($receiver_list[$index]);
  1000. \Yii::warning('<========================> 订单号:' . $out_trade_no . '添加分账关系校验参数错误:' . json_encode($receiver));
  1001. }
  1002. }
  1003. if (empty($receiver_list)) {
  1004. return [
  1005. 'code' => 1,
  1006. 'msg' => '接收方为空'
  1007. ];
  1008. }
  1009. self::init(get_store_id());
  1010. $params = [
  1011. 'receiver_list' => $receiver_list,
  1012. 'out_request_no' => $out_trade_no
  1013. ];
  1014. /** 逻辑开始 **/
  1015. try {
  1016. $res = self::$alipay->unbind($params)->get('alipay_trade_royalty_relation_unbind_response');
  1017. \Yii::warning($res);
  1018. if ($res && $res['code'] === '10000' && $res['msg'] === 'Success' && $res['result_code'] == 'SUCCESS') {
  1019. return [
  1020. 'code' => 0,
  1021. 'msg' => '关系解绑成功'
  1022. ];
  1023. }
  1024. return [
  1025. 'code' => 1,
  1026. 'msg' => $res['sub_msg'],
  1027. 'res' => $res
  1028. ];
  1029. } catch (GatewayException $e) {
  1030. return [
  1031. 'code' => 1,
  1032. 'msg' => $e->getMessage()
  1033. ];
  1034. } catch (InvalidSignException $e) {
  1035. return [
  1036. 'code' => 1,
  1037. 'msg' => $e->getMessage()
  1038. ];
  1039. } catch (InvalidConfigException $e) {
  1040. return [
  1041. 'code' => 1,
  1042. 'msg' => $e->getMessage()
  1043. ];
  1044. }
  1045. }
  1046. /**
  1047. * queryBind
  1048. * @param integer $page
  1049. * @param integer $page_size
  1050. * @param string $out_trade_no
  1051. * @return array
  1052. */
  1053. public static function queryBind($out_trade_no, $page = 1, $page_size = 10) {
  1054. if (empty($out_trade_no)) {
  1055. return [
  1056. 'code' => 1,
  1057. 'msg' => '商户订单号不能为空'
  1058. ];
  1059. }
  1060. self::init(get_store_id());
  1061. $params = [
  1062. 'page_num' => $page,
  1063. 'page_size' => $page_size,
  1064. 'out_request_no' => $out_trade_no
  1065. ];
  1066. /** 逻辑开始 **/
  1067. try {
  1068. $res = self::$alipay->queryBind($params)->get('alipay_trade_royalty_relation_batchquery_response');
  1069. \Yii::warning($res);
  1070. if ($res && $res['code'] === '10000' && $res['msg'] === 'Success' && $res['result_code'] == 'SUCCESS') {
  1071. return [
  1072. 'code' => 0,
  1073. 'msg' => 'success',
  1074. 'data' => [
  1075. 'data' => $res['receiver_list'],
  1076. 'total_page_num' => $res['total_page_num'],
  1077. 'total_record_num' => $res['total_record_num'],
  1078. 'current_page_num' => $res['current_page_num'],
  1079. 'current_page_size' => $res['current_page_size']
  1080. ]
  1081. ];
  1082. }
  1083. return [
  1084. 'code' => 1,
  1085. 'msg' => $res['sub_msg'],
  1086. 'res' => $res
  1087. ];
  1088. } catch (GatewayException $e) {
  1089. return [
  1090. 'code' => 1,
  1091. 'msg' => $e->getMessage()
  1092. ];
  1093. } catch (InvalidSignException $e) {
  1094. return [
  1095. 'code' => 1,
  1096. 'msg' => $e->getMessage()
  1097. ];
  1098. } catch (InvalidConfigException $e) {
  1099. return [
  1100. 'code' => 1,
  1101. 'msg' => $e->getMessage()
  1102. ];
  1103. }
  1104. }
  1105. /**
  1106. * settle 分账结算
  1107. * @param integer $page
  1108. * @param integer $page_size
  1109. * @param string $out_trade_no
  1110. * @param array $parameters
  1111. * @param string $is_end
  1112. * @return array
  1113. */
  1114. public static function settle($out_trade_no, $alipay_trade_no, $parameters, $store_id, $is_end = 'false', $is_app = false) {
  1115. if (empty($out_trade_no) || empty($alipay_trade_no) || empty($parameters)) {
  1116. return [
  1117. 'code' => 1,
  1118. 'msg' => '参数有误'
  1119. ];
  1120. }
  1121. $params = [
  1122. 'out_request_no' => $out_trade_no,
  1123. 'trade_no' => $alipay_trade_no,
  1124. 'royalty_parameters' => $parameters,
  1125. 'extend_params' => [
  1126. 'royalty_finish' => $is_end
  1127. ]
  1128. ];
  1129. self::init($store_id, '', $is_app);
  1130. /** 逻辑开始 **/
  1131. try {
  1132. $res = self::$alipay->settle($params)->get('alipay_trade_order_settle_response');
  1133. \Yii::warning($res);
  1134. if ($res && $res['code'] === '10000' && $res['msg'] === 'Success') {
  1135. return [
  1136. 'code' => 0,
  1137. 'msg' => 'success'
  1138. ];
  1139. }
  1140. return [
  1141. 'code' => 1,
  1142. 'msg' => $res['sub_msg'],
  1143. 'res' => $res
  1144. ];
  1145. } catch (GatewayException $e) {
  1146. return [
  1147. 'code' => 1,
  1148. 'msg' => $e->getMessage()
  1149. ];
  1150. } catch (InvalidSignException $e) {
  1151. return [
  1152. 'code' => 1,
  1153. 'msg' => $e->getMessage()
  1154. ];
  1155. } catch (InvalidConfigException $e) {
  1156. return [
  1157. 'code' => 1,
  1158. 'msg' => $e->getMessage()
  1159. ];
  1160. }
  1161. }
  1162. /**
  1163. * qrcode 小程序二维码
  1164. * @param string $url_param 页面地址 page/index/index
  1165. * @param string $query_param 页面参数,示例:x=1
  1166. * @param string $describe 二维码描述
  1167. * @return array
  1168. */
  1169. public static function qrcode($url_param, $query_param, $describe) {
  1170. if (empty($url_param) || empty($query_param) || empty($describe)) {
  1171. return [
  1172. 'code' => 1,
  1173. 'msg' => '参数有误'
  1174. ];
  1175. }
  1176. $params = [
  1177. 'url_param' => $url_param,
  1178. 'query_param' => $query_param,
  1179. 'describe' => $describe
  1180. ];
  1181. self::init(get_store_id());
  1182. /** 逻辑开始 **/
  1183. try {
  1184. $res = self::$alipay->qrcode($params)->get('alipay_open_app_qrcode_create_response');
  1185. \Yii::warning($res);
  1186. if ($res && $res['code'] === '10000' && $res['msg'] === 'Success') {
  1187. return [
  1188. 'code' => 0,
  1189. 'msg' => 'success',
  1190. 'data' => $res['qr_code_url']
  1191. ];
  1192. }
  1193. return [
  1194. 'code' => 1,
  1195. 'msg' => $res['sub_msg'],
  1196. 'res' => $res
  1197. ];
  1198. } catch (GatewayException $e) {
  1199. return [
  1200. 'code' => 1,
  1201. 'msg' => $e->getMessage()
  1202. ];
  1203. } catch (InvalidSignException $e) {
  1204. return [
  1205. 'code' => 1,
  1206. 'msg' => $e->getMessage()
  1207. ];
  1208. } catch (InvalidConfigException $e) {
  1209. return [
  1210. 'code' => 1,
  1211. 'msg' => $e->getMessage()
  1212. ];
  1213. }
  1214. }
  1215. /**
  1216. * 店铺信息图片上传
  1217. * @param string $image_type 图片类型
  1218. * @param string $image_content 图片内容 二进制
  1219. * @return array
  1220. */
  1221. public static function image($image_type, $image_content) {
  1222. if (empty($image_type) || empty($image_content)) {
  1223. return [
  1224. 'code' => 1,
  1225. 'msg' => '参数有误'
  1226. ];
  1227. }
  1228. $params = [
  1229. 'image_type' => $image_type,
  1230. 'image_content' => $image_content
  1231. ];
  1232. self::init(get_store_id());
  1233. /** 逻辑开始 **/
  1234. try {
  1235. $res = self::$alipay->image($params)->get('ant_merchant_expand_indirect_image_upload_response');
  1236. \Yii::warning($res);
  1237. if ($res && $res['code'] === '10000' && $res['msg'] === 'Success') {
  1238. return [
  1239. 'code' => 0,
  1240. 'msg' => 'success',
  1241. 'data' => $res
  1242. ];
  1243. }
  1244. return [
  1245. 'code' => 1,
  1246. 'msg' => $res['sub_msg'],
  1247. 'res' => $res
  1248. ];
  1249. } catch (GatewayException $e) {
  1250. return [
  1251. 'code' => 1,
  1252. 'msg' => $e->getMessage()
  1253. ];
  1254. } catch (InvalidSignException $e) {
  1255. return [
  1256. 'code' => 1,
  1257. 'msg' => $e->getMessage()
  1258. ];
  1259. } catch (InvalidConfigException $e) {
  1260. return [
  1261. 'code' => 1,
  1262. 'msg' => $e->getMessage()
  1263. ];
  1264. }
  1265. }
  1266. /**
  1267. * 创建店铺
  1268. * @param array $params
  1269. * @return array
  1270. */
  1271. public static function shop_create($params) {
  1272. if (empty($params)) {
  1273. return [
  1274. 'code' => 1,
  1275. 'msg' => '参数有误'
  1276. ];
  1277. }
  1278. self::init(get_store_id());
  1279. /** 逻辑开始 **/
  1280. try {
  1281. $res = self::$alipay->shop($params)->get('ant_merchant_expand_shop_create_response');
  1282. \Yii::warning($res);
  1283. if ($res && $res['code'] === '10000' && $res['msg'] === 'Success') {
  1284. return [
  1285. 'code' => 0,
  1286. 'msg' => 'success',
  1287. 'data' => $res['order_id']
  1288. ];
  1289. }
  1290. return [
  1291. 'code' => 1,
  1292. 'msg' => $res['sub_msg'],
  1293. 'res' => $res
  1294. ];
  1295. } catch (GatewayException $e) {
  1296. return [
  1297. 'code' => 1,
  1298. 'msg' => $e->getMessage()
  1299. ];
  1300. } catch (InvalidSignException $e) {
  1301. return [
  1302. 'code' => 1,
  1303. 'msg' => $e->getMessage()
  1304. ];
  1305. } catch (InvalidConfigException $e) {
  1306. return [
  1307. 'code' => 1,
  1308. 'msg' => $e->getMessage()
  1309. ];
  1310. }
  1311. }
  1312. /**
  1313. * 查询门店创建结果
  1314. * @param string $order_id 订单id
  1315. * @return array
  1316. */
  1317. public static function shop_query($order_id) {
  1318. if (empty($order_id)) {
  1319. return [
  1320. 'code' => 1,
  1321. 'msg' => '参数有误'
  1322. ];
  1323. }
  1324. $params = [
  1325. 'order_id' => $order_id
  1326. ];
  1327. self::init(get_store_id());
  1328. /** 逻辑开始 **/
  1329. try {
  1330. $res = self::$alipay->queryShop($params)->get('ant_merchant_expand_order_query_response');
  1331. \Yii::warning($res);
  1332. if ($res && $res['code'] === '10000' && $res['msg'] === 'Success') {
  1333. return [
  1334. 'code' => 0,
  1335. 'msg' => 'success',
  1336. 'data' => $res
  1337. ];
  1338. }
  1339. return [
  1340. 'code' => 1,
  1341. 'msg' => $res['sub_msg'],
  1342. 'res' => $res
  1343. ];
  1344. } catch (GatewayException $e) {
  1345. return [
  1346. 'code' => 1,
  1347. 'msg' => $e->getMessage()
  1348. ];
  1349. } catch (InvalidSignException $e) {
  1350. return [
  1351. 'code' => 1,
  1352. 'msg' => $e->getMessage()
  1353. ];
  1354. } catch (InvalidConfigException $e) {
  1355. return [
  1356. 'code' => 1,
  1357. 'msg' => $e->getMessage()
  1358. ];
  1359. }
  1360. }
  1361. /**
  1362. * 同步订单
  1363. * @param array $params 订单信息
  1364. * @return array
  1365. */
  1366. public static function order_sync($params, $store_id) {
  1367. if (empty($params)) {
  1368. return [
  1369. 'code' => 1,
  1370. 'msg' => '参数有误'
  1371. ];
  1372. }
  1373. self::init($store_id);
  1374. /** 逻辑开始 **/
  1375. try {
  1376. $res = self::$alipay->orderSync($params)->get('alipay_merchant_order_sync_response');
  1377. \Yii::warning($res);
  1378. if ($res && $res['code'] === '10000' && $res['msg'] === 'Success') {
  1379. return [
  1380. 'code' => 0,
  1381. 'msg' => 'success',
  1382. 'data' => $res
  1383. ];
  1384. }
  1385. return [
  1386. 'code' => 1,
  1387. 'msg' => $res['sub_msg'],
  1388. 'res' => $res
  1389. ];
  1390. } catch (GatewayException $e) {
  1391. return [
  1392. 'code' => 1,
  1393. 'msg' => $e->getMessage()
  1394. ];
  1395. } catch (InvalidSignException $e) {
  1396. return [
  1397. 'code' => 1,
  1398. 'msg' => $e->getMessage()
  1399. ];
  1400. } catch (InvalidConfigException $e) {
  1401. return [
  1402. 'code' => 1,
  1403. 'msg' => $e->getMessage()
  1404. ];
  1405. }
  1406. }
  1407. /**
  1408. * 上传商品文件
  1409. * @param string $scene 业务场景描述
  1410. * @param string $file_content 文件二进制字节流
  1411. * @return array
  1412. */
  1413. public static function file_upload_old($file_content, $scene = 'SYNC_ORDER') {
  1414. if (empty($file_content)) {
  1415. return [
  1416. 'code' => 1,
  1417. 'msg' => '参数有误'
  1418. ];
  1419. }
  1420. self::init(get_store_id());
  1421. /** 逻辑开始 **/
  1422. $params = [
  1423. 'scene' => $scene,
  1424. 'file_content' => $file_content
  1425. ];
  1426. try {
  1427. $res = self::$alipay->itemUpload($params)->get('alipay_merchant_item_file_upload_response');
  1428. \Yii::warning($res);
  1429. if ($res && $res['code'] === '10000' && $res['msg'] === 'Success') {
  1430. return [
  1431. 'code' => 0,
  1432. 'msg' => 'success',
  1433. 'data' => $res
  1434. ];
  1435. }
  1436. return [
  1437. 'code' => 1,
  1438. 'msg' => $res['sub_msg'],
  1439. 'res' => $res
  1440. ];
  1441. } catch (GatewayException $e) {
  1442. return [
  1443. 'code' => 1,
  1444. 'msg' => $e->getMessage()
  1445. ];
  1446. } catch (InvalidSignException $e) {
  1447. return [
  1448. 'code' => 1,
  1449. 'msg' => $e->getMessage()
  1450. ];
  1451. } catch (InvalidConfigException $e) {
  1452. return [
  1453. 'code' => 1,
  1454. 'msg' => $e->getMessage()
  1455. ];
  1456. }
  1457. }
  1458. /**
  1459. * 原生上传图片
  1460. * @param $image
  1461. * @return array
  1462. * @throws \Exception
  1463. */
  1464. public static function file_upload($image) {
  1465. self::init(get_store_id());
  1466. self::aopClient();
  1467. $request = new \AlipayMerchantItemFileUploadRequest();
  1468. $request->setScene("SYNC_ORDER");
  1469. $request->setFileContent("@". $image);
  1470. $result = self::$aop->execute ( $request, null, Store::findOne(get_store_id())->app_auth_token);
  1471. $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
  1472. $resultNode = $result->$responseNode;
  1473. return (array)($resultNode);
  1474. }
  1475. private static function aopClient() {
  1476. $aop = new \AopClient();
  1477. $aop->gatewayUrl = self::$gatewayUrl;
  1478. $aop->appId = self::$alipay_config['app_id'];
  1479. $aop->rsaPrivateKey = self::$alipay_config['app_private_key'];
  1480. $aop->alipayrsaPublicKey = self::$alipay_config['alipay_public_key'];
  1481. $aop->apiVersion = '1.0';
  1482. $aop->signType = 'RSA2';
  1483. $aop->postCharset='utf-8';
  1484. $aop->format='json';
  1485. self::$aop = $aop;
  1486. }
  1487. }