PayPal.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2024 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\api\service\recharge;
  13. use app\api\model\Payment as PaymentModel;
  14. use app\api\model\recharge\Order as OrderModel;
  15. use app\api\model\PaymentTrade as PaymentTradeModel;
  16. use app\api\service\User as UserService;
  17. use app\api\service\Order as OrderService;
  18. use app\api\service\recharge\PaySuccess as RechargePaySuccesService;
  19. use app\common\service\BaseService;
  20. use app\common\enum\Client as ClientEnum;
  21. use app\common\enum\OrderType as OrderTypeEnum;
  22. use app\common\enum\payment\Method as PaymentMethodEnum;
  23. use app\common\library\payment\Facade as PaymentFacade;
  24. use cores\exception\BaseException;
  25. use think\facade\Cache;
  26. /**
  27. * 余额充值订单付款服务类
  28. * Class Payment
  29. * @package app\api\controller
  30. */
  31. class PayPal extends BaseService
  32. {
  33. // 提示信息
  34. private string $message = '';
  35. // 订单信息
  36. private OrderModel $orderInfo;
  37. // 支付方式 (微信支付、支付宝)
  38. private string $method = '';
  39. // 下单的客户端
  40. private string $client = '';
  41. /**
  42. * 设置当前支付方式
  43. * @param string $method 支付方式
  44. * @return $this
  45. */
  46. public function setMethod(string $method): PayPal
  47. {
  48. $this->method = $method;
  49. return $this;
  50. }
  51. /**
  52. * 设置下单的客户端
  53. * @param string $client 客户端
  54. * @return $this
  55. */
  56. public function setClient(string $client): PayPal
  57. {
  58. $this->client = $client;
  59. return $this;
  60. }
  61. /**
  62. * 确认订单支付事件
  63. * @param int|null $planId 方案ID
  64. * @param string|null $customMoney 自定义金额
  65. * @param array $extra 附加数据
  66. * @return array[]
  67. * @throws BaseException
  68. * @throws \think\db\exception\DataNotFoundException
  69. * @throws \think\db\exception\DbException
  70. * @throws \think\db\exception\ModelNotFoundException
  71. */
  72. public function orderPay(?int $planId = null, string $customMoney = null, array $extra = []): array
  73. {
  74. // 创建余额订单信息
  75. $this->orderInfo = $this->createOrder($planId, $customMoney);
  76. // 构建第三方支付请求的参数
  77. $payment = $this->unifiedorder($extra);
  78. // 记录第三方交易信息
  79. $this->recordPaymentTrade($payment);
  80. // 返回结果
  81. return compact('payment');
  82. }
  83. /**
  84. * 创建充值订单
  85. * @param int|null $planId 方案ID
  86. * @param string|null $customMoney 自定义金额
  87. * @return OrderModel
  88. * @throws BaseException
  89. * @throws \think\db\exception\DataNotFoundException
  90. * @throws \think\db\exception\DbException
  91. * @throws \think\db\exception\ModelNotFoundException
  92. */
  93. private function createOrder(?int $planId = null, string $customMoney = null): OrderModel
  94. {
  95. $model = new OrderModel;
  96. if (!$model->createOrder($planId, $customMoney)) {
  97. throwError($model->getError() ?: '创建充值订单失败');
  98. }
  99. $model['order_id'] = (int)$model['order_id'];
  100. return $model;
  101. }
  102. /**
  103. * 查询订单是否支付成功 (仅限第三方支付订单)
  104. * @param string $outTradeNo 商户订单号
  105. * @return bool
  106. * @throws BaseException
  107. * @throws \think\db\exception\DataNotFoundException
  108. * @throws \think\db\exception\DbException
  109. * @throws \think\db\exception\ModelNotFoundException
  110. */
  111. public function tradeQuery(string $outTradeNo): bool
  112. {
  113. // 判断支付方式是否合法
  114. if (!in_array($this->method, [PaymentMethodEnum::WECHAT, PaymentMethodEnum::ALIPAY])) {
  115. return false;
  116. }
  117. // 获取支付方式的配置信息
  118. $options = $this->getPaymentConfig();
  119. // 构建支付模块
  120. $Payment = PaymentFacade::store($this->method)->setOptions($options, $this->client);
  121. // 执行第三方支付查询API
  122. $result = $Payment->tradeQuery($outTradeNo);
  123. // 订单支付成功事件
  124. if (!empty($result) && $result['paySuccess']) {
  125. // 获取第三方交易记录信息
  126. $tradeInfo = PaymentTradeModel::detailByOutTradeNo($outTradeNo);
  127. // 订单支付成功事件
  128. $this->orderPaySucces($tradeInfo['order_no'], $tradeInfo['trade_id'], $result);
  129. }
  130. // 返回订单状态
  131. return $result ? $result['paySuccess'] : false;
  132. }
  133. /**
  134. * 记录第三方交易信息
  135. * @param array $payment 第三方支付数据
  136. * @throws BaseException
  137. * @throws \think\db\exception\DataNotFoundException
  138. * @throws \think\db\exception\DbException
  139. * @throws \think\db\exception\ModelNotFoundException
  140. */
  141. private function recordPaymentTrade(array $payment): void
  142. {
  143. if ($this->method != PaymentMethodEnum::BALANCE) {
  144. PaymentTradeModel::record(
  145. $this->orderInfo,
  146. $this->method,
  147. $this->client,
  148. OrderTypeEnum::RECHARGE,
  149. $payment
  150. );
  151. }
  152. }
  153. /**
  154. * 返回消息提示
  155. * @return string
  156. */
  157. public function getMessage(): string
  158. {
  159. return $this->message;
  160. }
  161. /**
  162. * 构建第三方支付请求的参数
  163. * @param array $extra 附加数据
  164. * @return array
  165. * @throws BaseException
  166. * @throws \think\db\exception\DataNotFoundException
  167. * @throws \think\db\exception\DbException
  168. * @throws \think\db\exception\ModelNotFoundException
  169. */
  170. private function unifiedorder(array $extra = []): array
  171. {
  172. // 判断支付方式是否合法
  173. if (!in_array($this->method, [PaymentMethodEnum::WECHAT, PaymentMethodEnum::ALIPAY])) {
  174. return [];
  175. }
  176. // 生成第三方交易订单号 (并非主订单号)
  177. $outTradeNo = OrderService::createOrderNo();
  178. // 获取支付方式的配置信息
  179. $options = $this->getPaymentConfig();
  180. // 整理下单接口所需的附加数据
  181. $extra = $this->extraAsUnify($extra);
  182. // 构建支付模块
  183. $Payment = PaymentFacade::store($this->method)->setOptions($options, $this->client);
  184. // 执行第三方支付下单API
  185. if (!$Payment->unify($outTradeNo, (string)$this->orderInfo['pay_price'], $extra)) {
  186. throwError('第三方支付下单API调用失败');
  187. }
  188. // 返回客户端需要的支付参数
  189. return $Payment->getUnifyResult();
  190. }
  191. /**
  192. * 获取支付方式的配置信息
  193. * @return mixed
  194. * @throws BaseException
  195. * @throws \think\db\exception\DataNotFoundException
  196. * @throws \think\db\exception\DbException
  197. * @throws \think\db\exception\ModelNotFoundException
  198. */
  199. private function getPaymentConfig()
  200. {
  201. $PaymentModel = new PaymentModel;
  202. $templateInfo = $PaymentModel->getPaymentInfo($this->method, $this->client, $this->getStoreId());
  203. return $templateInfo['template']['config'][$this->method];
  204. }
  205. /**
  206. * 整理下单接口所需的附加数据
  207. * @param array $extra
  208. * @return array
  209. * @throws BaseException
  210. */
  211. private function extraAsUnify(array $extra = []): array
  212. {
  213. // 微信支付时需要的附加数据
  214. if ($this->method === PaymentMethodEnum::WECHAT) {
  215. // 微信小程序端需要openid
  216. if (in_array($this->client, [ClientEnum::MP_WEIXIN])) {
  217. $extra['openid'] = $this->getWechatOpenid();
  218. }
  219. }
  220. // 支付宝支付时需要的附加数据
  221. if ($this->method === PaymentMethodEnum::ALIPAY) {
  222. }
  223. return $extra;
  224. }
  225. /**
  226. * 订单支付成功事件
  227. * @param string $orderNo 当前订单号
  228. * @param int|null $tradeId 第三方交易记录ID
  229. * @param array $paymentData 第三方支付成功返回的数据
  230. * @return void
  231. * @throws BaseException
  232. */
  233. private function orderPaySucces(string $orderNo, ?int $tradeId = null, array $paymentData = []): void
  234. {
  235. // 获取订单详情
  236. $service = new RechargePaySuccesService;
  237. // 订单支付成功业务处理
  238. $service->setOrderNo($orderNo)->setMethod($this->method)->setTradeId($tradeId)->setPaymentData($paymentData);
  239. if (!$service->handle()) {
  240. throwError($service->getError() ?: '订单支付失败');
  241. }
  242. $this->message = '恭喜您,余额充值成功';
  243. }
  244. protected $appId;
  245. protected $appSecret;
  246. protected $baseUrl = 'https://api.sandbox.paypal.com';
  247. //protected $baseUrl = 'https://api-m.paypal.com';
  248. /*构造函数,把实例化Paypal类时传进来的appId和appSecret保存起来
  249. *
  250. *
  251. * */
  252. public function __construct($appId, $appSecret)
  253. {
  254. $this->appId = $appId;
  255. $this->appSecret = $appSecret;
  256. }
  257. /*curl post
  258. *param
  259. * $extreUrl string 接口地址
  260. * $data array 提交的数据 为空时为get请求
  261. * $header array 请求头信息
  262. * $Oauth bool 是否使用Oauth验证
  263. * return array 响应信息
  264. * */
  265. protected function httpRequest($extreUrl, $data, $header, $Oauth = false)
  266. {
  267. $url = $this->baseUrl . $extreUrl;
  268. $curl = curl_init();
  269. curl_setopt($curl, CURLOPT_URL, $url);
  270. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
  271. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
  272. if ($Oauth) {
  273. curl_setopt($curl, CURLOPT_USERPWD, $this->appId . ":" . $this->appSecret);
  274. }
  275. //获取请求头信息
  276. //curl_setopt($curl, CURLINFO_HEADER_OUT, TRUE);
  277. curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
  278. if ($data) {
  279. curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  280. curl_setopt($curl, CURLOPT_POST, 1);
  281. }
  282. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  283. $output = curl_exec($curl);
  284. //获取请求头信息
  285. //var_dump(curl_getinfo($curl)["request_header"]);
  286. curl_close($curl);
  287. return json_decode($output, true);
  288. }
  289. /*请求头部加上Authorization
  290. *param
  291. * $extreUrl string 接口地址
  292. * $data array 请求的参数
  293. * return array 返回信息
  294. * */
  295. protected function httpRequestWithAuth($extreUrl, $data = [])
  296. {
  297. $header[] = 'Content-Type:application/json';
  298. $header[] = 'Authorization:Bearer ' . $this->getAccessToken();
  299. return $this->httpRequest($extreUrl, $data, $header);
  300. }
  301. /*转化数组为键值对形式
  302. *param
  303. * $array array 需要转化的数组
  304. * return string 转化后的字符串:参数1=值1&参数2=值2
  305. * */
  306. protected function array2keyvalue($array)
  307. {
  308. $res = [];
  309. foreach ($array as $k => $v) {
  310. $res[] = $k . '=' . $v;
  311. }
  312. return implode('&', $res);
  313. }
  314. /*获取accesstoken
  315. * 判断存储的accesstoken是否过期,过期则调用接口获取新的accesstoken否则直接返回。
  316. * 可以把文件读取改成缓存读取 数据库读取。
  317. * return string accesstoken值
  318. * */
  319. public function getAccessToken()
  320. {
  321. // $clientId = 'AS0FH780ZGtSAdpT1NTjwkFzryCPf69rZb_FR9Rt_rZdasB80cmjqTQ6CQELWiFVh_MU9e31CSnyz7Ai';
  322. // $clientSecret = 'EDqRQhgLNHCb5bxld98T8-JJJZKvMIeqxudO7lMwDFOxBfy138PjM5A21FnDNyb3q4yYUh8r7Qr2BnVi';
  323. //从paypal_access_token.json文件中读取accesstoken和过期时间
  324. $access_token = Cache::get('paypal_access_token', null);
  325. //$data = json_decode(file_get_contents("paypal_access_token.json"),true);
  326. if (empty($access_token)) {
  327. $extreUrl = '/v1/oauth2/token';
  328. $postdata['grant_type'] = 'client_credentials';
  329. $postdata = $this->array2keyvalue($postdata);
  330. $header[] = "Content-type: application/x-www-form-urlencoded";
  331. $res = $this->httpRequest($extreUrl, $postdata, $header, true);
  332. \think\facade\Log::info('pay::' . json_encode($res));
  333. $access_token = $res['access_token'];
  334. if ($access_token) {
  335. Cache::set('paypal_access_token', $access_token, $res['expires_in'] - 20);
  336. }
  337. }
  338. return $access_token;
  339. }
  340. public function createOrderV2()
  341. {
  342. $extreUrl = '/v2/checkout/orders';
  343. $body = [
  344. "intent" => "CAPTURE",
  345. "purchase_units" => [
  346. [
  347. "amount" => [
  348. "currency_code" => "USD",//USD
  349. "value" => "100.00"
  350. ]
  351. ]
  352. ],
  353. "application_context" => [
  354. "return_url" => "https://lar.lmm.gold/store/index.html",//todo 自己的结算详情,通过接口获取然后转到location
  355. "cancel_url" => "https://lar.lmm.gold/store/index.html"
  356. ]
  357. ];
  358. return $this->httpRequestWithAuth($extreUrl, json_encode($body));
  359. }
  360. public function captureOrder($id)
  361. {
  362. $extreUrl = '/v2/checkout/orders/' . $id . '/capture';
  363. return $this->httpRequestWithAuth($extreUrl);
  364. }
  365. /*创建payment
  366. *
  367. */
  368. public function CreatePayment($array)
  369. {
  370. $extreUrl = '/v1/payments/payment';
  371. $data['intent'] = 'sale';
  372. $data['payer'] = ['payment_method' => 'paypal'];
  373. $transactions['amount'] = $array['amount'];
  374. $transactions['description'] = $array['description'];
  375. $transactions['custom'] = $array['orderid'];
  376. $transactions['invoice_number'] = $array['orderid'];
  377. $transactions['item_list']['items'] = $array['items'];
  378. $transactions['payment_options']['allowed_payment_method'] = 'INSTANT_FUNDING_SOURCE';
  379. $transactions['item_list']['shipping_address'] = $array['shipping_address'];
  380. $data['transactions'][] = $transactions;
  381. $data['note_to_payer'] = $array['note_to_payer'];
  382. $data['redirect_urls'] = $array['redirect_urls'];
  383. return $this->httpRequestWithAuth($extreUrl, json_encode($data));
  384. }
  385. /* 获取 完成payment
  386. *param
  387. * paymentId 创建payment后返回的ID
  388. * PayerID 用户id 用户完成授权后跳转的excute页面中url带的参数
  389. **/
  390. public function ExcutePayment($array)
  391. {
  392. $extreUrl = '/v1/payments/payment/' . $array['paymentId'] . '/execute';
  393. $data['payer_id'] = $array['PayerID'];
  394. return $this->httpRequestWithAuth($extreUrl, json_encode($data));
  395. }
  396. /* 获取payment详情
  397. *param
  398. * paymentId 创建payment后返回的ID
  399. **/
  400. public function PaymentInfo($array)
  401. {
  402. $extreUrl = '/v1/payments/payment/' . $array['paymentId'];
  403. return $this->httpRequestWithAuth($extreUrl);
  404. }
  405. }