AliPay.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. namespace common\components\payment;
  3. use common\helpers\ArrayHelper;
  4. use yii\web\UnprocessableEntityHttpException;
  5. use Yansongda\Pay\Pay;
  6. use Yansongda\Pay\Exception\InvalidResponseException;
  7. /**
  8. * Class AliPay
  9. * @doc https://pay.yansongda.cn/docs/v3/
  10. * @package common\components\payment
  11. */
  12. class AliPay
  13. {
  14. public function __construct($config)
  15. {
  16. // 初始化
  17. Pay::config($config);
  18. }
  19. /**
  20. * @return \Yansongda\Supports\Collection
  21. * @throws \Yansongda\Pay\Exception\ContainerException
  22. * @throws \Yansongda\Pay\Exception\InvalidParamsException
  23. */
  24. public function callback()
  25. {
  26. return Pay::alipay()->callback();
  27. }
  28. /**
  29. * @return \Psr\Http\Message\ResponseInterface
  30. */
  31. public function success()
  32. {
  33. return Pay::alipay()->success();
  34. }
  35. /**
  36. * 网页支付
  37. *
  38. * @param $config
  39. *
  40. * 参数说明
  41. * $config = [
  42. * 'subject' => 'test',
  43. * 'out_trade_no' => date('YmdHis') . mt_rand(1000, 9999),
  44. * 'total_amount' => '0.01',
  45. * ]
  46. *
  47. * @return string
  48. */
  49. public function web($order)
  50. {
  51. return Pay::alipay()->web($order)->getBody()->getContents();
  52. }
  53. /**
  54. * H5支付
  55. *
  56. * @param $order
  57. * @return mixed
  58. */
  59. public function wap($order)
  60. {
  61. return Pay::alipay()->wap($order)->getBody()->getContents();
  62. }
  63. /**
  64. * 小程序
  65. *
  66. * @param $order
  67. * @return mixed
  68. */
  69. public function mini($order)
  70. {
  71. $result = Pay::alipay()->mini($order);
  72. return $result->get('trade_no'); // 支付宝交易号
  73. }
  74. /**
  75. * APP支付
  76. *
  77. * 参数说明
  78. * $config = [
  79. * 'subject' => 'test',
  80. * 'out_trade_no' => date('YmdHis') . mt_rand(1000, 9999),
  81. * 'total_amount' => '0.01',
  82. * ]
  83. *
  84. * iOS 客户端
  85. * [[AlipaySDK defaultService] payOrder:orderString fromScheme:appScheme callback:^(NSDictionary *resultDic) {
  86. * NSLog(@"reslut = %@",resultDic);
  87. * }];
  88. *
  89. * Android 客户端
  90. * PayTask alipay = new PayTask(PayDemoActivity.this);
  91. * Map<String, String> result = alipay.payV2(orderString, true);
  92. * @param $config
  93. * @param $notifyUrl
  94. * @return mixed
  95. */
  96. public function app($order)
  97. {
  98. return Pay::alipay()->app($order)->getBody()->getContents();
  99. }
  100. /**
  101. * 扫码支付
  102. *
  103. * @param $order
  104. * @return mixed
  105. */
  106. public function scan($order)
  107. {
  108. $result = Pay::alipay()->scan($order);
  109. $this->getError(ArrayHelper::toArray($result));
  110. return $result->qr_code; // 二维码 url
  111. }
  112. /**
  113. * 扫码收款
  114. *
  115. * $info = [
  116. * 'out_trade_no' => '',
  117. * 'auth_code' => '', // 付款码
  118. * 'total_amount' => 18.4, // 金额
  119. * 'subject' => '' // 说明
  120. * ]
  121. *
  122. * @return mixed
  123. */
  124. public function pos(array $info)
  125. {
  126. return Pay::alipay()->pos($info);
  127. }
  128. /**
  129. * 转账
  130. *
  131. * $info = [
  132. * 'out_biz_no' => '转账单号',
  133. * 'trans_amount' => '收款金额',
  134. * 'payee_info' => [
  135. * 'identity_type' => 'ALIPAY_LOGON_ID', // ALIPAY_USER_ID:支付宝唯一号;ALIPAY_LOGON_ID:支付宝登录号
  136. * 'identity' => '收款人账号',
  137. * 'name' => '收款方真实姓名', // 非必填
  138. * ],
  139. * 'remark' => '账业务的标题,用于在支付宝用户的账单里显示', // 非必填
  140. * 'order_title' => '转账业务的标题,用于在支付宝用户的账单里显示 ', // 非必填
  141. * ]
  142. *
  143. * identity_type
  144. * 1、ALIPAY_USER_ID :支付宝账号对应的支付宝唯一用户号。以2088开头的16位纯数字组成。
  145. * 2、ALIPAY_LOGON_ID:支付宝登录号,支持邮箱和手机号格式。
  146. *
  147. * @return mixed
  148. */
  149. public function transfer(array $info)
  150. {
  151. !isset($info['product_code']) && $info['product_code'] = 'TRANS_ACCOUNT_NO_PWD';
  152. !isset($info['biz_scene']) && $info['biz_scene'] = 'DIRECT_TRANSFER';
  153. !isset($info['payee_info']['identity_type']) && $info['payee_info']['identity_type'] = 'ALIPAY_LOGON_ID';
  154. return Pay::alipay()->transfer($info);
  155. }
  156. /**
  157. * 订单查询
  158. *
  159. * $info = [
  160. * 'out_biz_no' => '转账单号',
  161. * ]
  162. *
  163. * or
  164. *
  165. * $info = '1514027114';
  166. * @return mixed
  167. */
  168. public function find($info)
  169. {
  170. return Pay::alipay()->find($info);
  171. }
  172. /**
  173. * 退款
  174. *
  175. * $info = [
  176. * 'out_trade_no' => 'The existing Order ID',
  177. * 'trade_no' => 'The Transaction ID received in the previous request',
  178. * 'refund_amount' => 18.4,
  179. * 'out_request_no' => date('YmdHis') . mt_rand(1000, 9999)
  180. * ]
  181. */
  182. public function refund(array $info)
  183. {
  184. return Pay::alipay()->refund($info);
  185. }
  186. /**
  187. * @param $error
  188. * @return mixed
  189. * @throws UnprocessableEntityHttpException
  190. */
  191. protected function getError($error)
  192. {
  193. if (!is_array($error)) {
  194. return false;
  195. }
  196. if ($error['code'] != '10000') {
  197. if (isset($error['sub_msg'])) {
  198. throw new UnprocessableEntityHttpException($error['sub_msg']);
  199. }
  200. throw new UnprocessableEntityHttpException($error['msg']);
  201. }
  202. return true;
  203. }
  204. }
粤ICP备19079148号