FaceMiniForm.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\client\models\v1\third\ali;
  8. use app\models\AggregateQrcode;
  9. use app\models\StoreAliMini;
  10. use app\models\StoreAliMiniVersion;
  11. use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenAgentConfirmRequest;
  12. use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenAgentCreateRequest;
  13. use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenAgentFacetofaceSignRequest;
  14. use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenAgentOrderQueryRequest;
  15. class FaceMiniForm extends BaseForm
  16. {
  17. /**
  18. * 当面付form
  19. */
  20. public function inits()
  21. {
  22. try {
  23. $mini_id = $this->mini_id;
  24. if (!empty($mini_id)) {
  25. $store_mini = StoreAliMini::findOne($mini_id);
  26. if (empty($store_mini)) {
  27. return [
  28. 'code' => 1,
  29. 'msg' => "查找不到小程序信息"
  30. ];
  31. }
  32. $this->store_mini = $store_mini;
  33. $this->mini_version = StoreAliMiniVersion::find()->where(['mini_id' => $mini_id])->orderBy('id desc')->one();
  34. }
  35. } catch (\Exception $e) {
  36. return [
  37. 'code' => 1,
  38. 'msg' => $e->getMessage()
  39. ];
  40. }
  41. }
  42. //创建事务 alipay.open.agent.create(开启代商户签约、创建应用事务)
  43. public function openAgentCreate()
  44. {
  45. try {
  46. $result = $this->inits();
  47. if ($result['code'] === 1) {
  48. return $result;
  49. }
  50. //小程序model
  51. $store_mini = $this->store_mini;
  52. //接口参数
  53. $param = json_encode([
  54. //支付宝账户
  55. 'account' => $store_mini->alipay_account,
  56. 'contact_info' => [
  57. //商家姓名
  58. 'contact_name' => $store_mini->contact_name,
  59. //商家手机号
  60. 'contact_mobile' => $store_mini->service_phone,
  61. //商家邮箱
  62. 'contact_email' => $store_mini->service_email
  63. ]
  64. ]);
  65. //调用接口配置信息
  66. $request = new AlipayOpenAgentCreateRequest();
  67. //调用接口
  68. $result = $this->miniCommon($request, $param);
  69. if ($result->code == 10000) {
  70. //保存事务编号,用作签约等操作
  71. $store_mini->batch_no = $result->batch_no;
  72. //保存状态信息
  73. $store_mini->batch_status = 1;
  74. //保存信息
  75. if (!$store_mini->save()) {
  76. throw new \Exception(json_encode($store_mini->errors));
  77. }
  78. return [
  79. 'code' => 0,
  80. 'msg' => "创建事务成功"
  81. ];
  82. } else {
  83. throw new \Exception($result->sub_msg);
  84. }
  85. } catch (\Exception $e) {
  86. return [
  87. 'code' => 0,
  88. 'msg' => $e->getMessage()
  89. ];
  90. }
  91. }
  92. //当面付签约 alipay.open.agent.facetoface.sign(代签约当面付产品)
  93. public function openFaceToFaceSign()
  94. {
  95. try {
  96. $result = $this->inits();
  97. if ($result['code'] === 1) {
  98. return $result;
  99. }
  100. //小程序model
  101. $store_mini = $this->store_mini;
  102. //业务编号
  103. $batch_no = $store_mini->batch_no;
  104. //获取请求配置
  105. $request = new AlipayOpenAgentFacetofaceSignRequest();
  106. //设置业务编号
  107. $request->setBatchNo($batch_no);
  108. //设置商家经营类目编码
  109. $request->setMccCode("A0002_B0201");
  110. //设置服务费率(%)
  111. $request->setRate('0.38');
  112. //签约且授权标识,默认为false,只进行签约操作; 如果设置为true,则表示签约成功后,会自动进行应用授权操作。
  113. $request->setSignAndAuth(false);
  114. //营业执照图片。被代创建商户运营主体为个人账户必填,企业账户无需填写
  115. $request->setShopSignBoardPic('@' . $this->saveTempImage($store_mini->app_logo));
  116. //调用接口
  117. $result = $this->miniCommon($request,null, 1);
  118. //如果请求成功或者报同类产品已经签约就显示成功
  119. if (($result->code == 10000) || $result->sub_code == "ANT_PRODUCT_CONFLICT") {
  120. $store_mini->batch_status = 2;
  121. if (!$store_mini->save()) {
  122. throw new \Exception(json_encode($store_mini->errors));
  123. }
  124. return [
  125. 'code' => 0,
  126. 'msg' => "操作成功",
  127. 'batch_status' => $store_mini->batch_status
  128. ];
  129. }
  130. throw new \Exception($result->sub_msg);
  131. } catch (\Exception $e) {
  132. return [
  133. 'code' => 1,
  134. 'msg' => $e->getMessage()
  135. ];
  136. }
  137. }
  138. //提交事务 alipay.open.agent.confirm(提交代商户签约、创建应用事务)
  139. public function openAgentConfirm()
  140. {
  141. try {
  142. $result = $this->inits();
  143. if ($result['code'] === 1) {
  144. return $result;
  145. }
  146. //小程序model
  147. $store_mini = $this->store_mini;
  148. //业务编号
  149. $batch_no = $store_mini->batch_no;
  150. //请求参数
  151. $param = json_encode([
  152. //业务编号
  153. 'batch_no' => $batch_no
  154. ]);
  155. //请求配置信息
  156. $request = new AlipayOpenAgentConfirmRequest();
  157. $result = $this->miniCommon($request, $param, 1);
  158. //请求成功
  159. if ($result->code == 10000) {
  160. $store_mini->batch_status = 3;
  161. if (!$store_mini->save()) {
  162. throw new \Exception($store_mini->errors);
  163. }
  164. return [
  165. 'code' => 0,
  166. 'msg' => "成功",
  167. 'data' => $result
  168. ];
  169. }
  170. throw new \Exception($result->sub_msg);
  171. } catch (\Exception $e) {
  172. return [
  173. 'code' => 1,
  174. 'msg' => $e->getMessage(),
  175. ];
  176. }
  177. }
  178. //查询状态 alipay.open.agent.order.query(查询申请单状态)
  179. public function openAgentOrderStatus()
  180. {
  181. try {
  182. $result = $this->inits();
  183. if ($result['code'] === 1) {
  184. return $result;
  185. }
  186. //小程序model
  187. $store_mini = $this->store_mini;
  188. //业务编号
  189. $batch_no = $store_mini->batch_no;
  190. //请求参数
  191. $param = json_encode([
  192. //业务编号
  193. 'batch_no' => $batch_no
  194. ]);
  195. $request = new AlipayOpenAgentOrderQueryRequest();
  196. $result = $this->miniCommon($request, $param, 1);
  197. if ($result->code == 10000) {
  198. $status = $result->order_status;
  199. $msg = "成功";
  200. switch ($status) {
  201. case "MERCHANT_INFO_HOLD": //异常单 4
  202. $store_mini->batch_status = 4;
  203. $msg = "订单异常";
  204. break;
  205. case "MERCHANT_AUDITING": //审核中 3
  206. $store_mini->batch_status = 3;
  207. $msg = "订单审核中";
  208. break;
  209. case "MERCHANT_CONFIRM": //待确认 5
  210. $store_mini->batch_status = 5;
  211. $msg = "等待商户确认,请注意查收支付宝消息通知";
  212. break;
  213. case "MERCHANT_CONFIRM_SUCCESS": //商户确认成功 6
  214. $store_mini->batch_status = 2;
  215. $msg = "商户确认成功";
  216. break;
  217. case "MERCHANT_CONFIRM_TIME_OUT": //商户超时未确认 7
  218. $store_mini->batch_status = 7;
  219. $msg = "商户超时未确认";
  220. break;
  221. case "MERCHANT_APPLY_ORDER_CANCELED": //审核失败或商户拒绝 8
  222. $store_mini->batch_status = 8;
  223. $msg = "审核失败或商户拒绝";
  224. break;
  225. }
  226. if (!$store_mini->save()) {
  227. throw new \Exception($store_mini->errors);
  228. }
  229. return [
  230. 'code' => 0,
  231. 'msg' => $msg,
  232. 'data' => $result
  233. ];
  234. }
  235. throw new \Exception($result->sub_msg);
  236. } catch (\Exception $e) {
  237. return [
  238. 'code' => 1,
  239. 'msg' => $e->getMessage(),
  240. ];
  241. }
  242. }
  243. }