| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201 |
- <?php
- // +----------------------------------------------------------------------
- // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2016~2024 https://www.crmeb.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
- // +----------------------------------------------------------------------
- // | Author: CRMEB Team <admin@crmeb.com>
- // +----------------------------------------------------------------------
- namespace crmeb\services\easywechat\pay;
- use crmeb\services\easywechat\BaseClient;
- use think\exception\ValidateException;
- use think\facade\Log;
- class Client extends BaseClient
- {
- protected $isService = false;
- public function handleNotify($callback)
- {
- $request = request();
- $success = $request->post('event_type') === 'TRANSACTION.SUCCESS';
- $data = $this->decrypt($request->post('resource', []));
- Log::info('支付回调参数v3:'.var_export($request->post(),1));
- $handleResult = call_user_func_array($callback, [json_decode($data, true), $success]);
- if (is_bool($handleResult) && $handleResult) {
- $response = [
- 'code' => 'SUCCESS',
- 'message' => 'OK',
- ];
- } else {
- $response = [
- 'code' => 'FAIL',
- 'message' => $handleResult,
- ];
- }
- return response($response, 200, [], 'json');
- }
- public function pay($type, $order)
- {
- $params = [
- 'appid' => $this->app['config']['app_id'],
- 'mchid' => $this->app['config']['payment']['merchant_id'],
- 'description' => $order['body'],
- 'out_trade_no' => $order['out_trade_no'],
- 'attach' => $order['attach'],
- 'notify_url' => $this->app['config']['payment']['notify_url'],
- 'amount' => [
- 'total' => intval($order['total_fee']),
- 'currency' => 'CNY'
- ],
- 'scene_info' => [
- 'device_id' => 'shop system',
- 'payer_client_ip' => request()->ip(),
- ],
- ];
- if ($type === 'h5') {
- $params['scene_info']['h5_info'] = [
- 'type' => $order['h5_type'] ?? 'Wap'
- ];
- }
- if (isset($order['openid'])) {
- $params['payer'] = [
- 'openid' => $order['openid']
- ];
- }
- Log::info('微信v3支付:'.var_export($params,true));
- $content = json_encode($params, JSON_UNESCAPED_UNICODE);
- $res = $this->request('/v3/pay/transactions/' . $type, 'POST', ['sign_body' => $content]);
- if (isset($res['code'])) {
- throw new ValidateException('微信接口报错:' . $res['message']);
- }
- return $res;
- }
- public function payApp($options)
- {
- $res = $this->pay('app', $options);
- return $this->configForAppPayment($res['prepay_id']);
- }
- /**
- * @param string $type 场景类型,枚举值: iOS:IOS移动应用; Android:安卓移动应用; Wap:WAP网站应用
- */
- public function payH5($options, $type = 'Wap')
- {
- $options['h5_type'] = $type;
- return $this->pay('h5', $options);
- }
- public function payJsapi($options)
- {
- $res = $this->pay('jsapi', $options);
- return $this->configForJSSDKPayment($res['prepay_id']);
- }
- public function payNative($options)
- {
- unset($options['openid']);
- return $this->pay('native', $options);
- }
- public function refund($orderNo, $refundNo, $totalFee, $refundFee, $opUserId = null, $type, $refundAccount, $refundReason)
- {
- $params = [
- $type => $orderNo,
- 'out_refund_no' => $refundNo,
- 'amount' => [
- 'refund' => intval($refundFee),
- 'total' => intval($totalFee),
- 'currency' => 'CNY'
- ]
- ];
- if (isset($refundReason)) {
- $params['reason'] = $refundReason;
- }
- // if (isset($refundAccount)) {
- // $params['refund_account'] = $refundAccount;
- // }
- $content = json_encode($params);
- $res = $this->request('/v3/refund/domestic/refunds', 'POST', ['sign_body' => $content], true);
- if (isset($res['code'])) {
- throw new ValidateException('微信接口报错:' . $res['message']);
- }
- return $res;
- }
- public function configForPayment($prepayId, $json = true)
- {
- $params = [
- 'appId' => $this->app['config']['app_id'],
- 'timeStamp' => strval(time()),
- 'nonceStr' => uniqid(),
- 'package' => "prepay_id=$prepayId",
- 'signType' => 'RSA',
- ];
- $message = $params['appId'] . "\n" .
- $params['timeStamp'] . "\n" .
- $params['nonceStr'] . "\n" .
- $params['package'] . "\n";
- openssl_sign($message, $raw_sign, $this->getPrivateKey(), 'sha256WithRSAEncryption');
- $sign = base64_encode($raw_sign);
- $params['paySign'] = $sign;
- return $json ? json_encode($params) : $params;
- }
- /**
- * Generate app payment parameters.
- *
- * @param string $prepayId
- *
- * @return array
- */
- public function configForAppPayment($prepayId)
- {
- $params = [
- 'appid' => $this->app['config']['app_id'],
- 'partnerid' => $this->app['config']['payment']['merchant_id'],
- 'prepayid' => $prepayId,
- 'noncestr' => uniqid(),
- 'timestamp' => time(),
- 'package' => 'Sign=WXPay',
- ];
- $message = $params['appid'] . "\n" .
- $params['timestamp'] . "\n" .
- $params['noncestr'] . "\n" .
- $params['prepayid'] . "\n";
- openssl_sign($message, $raw_sign, $this->getPrivateKey(), 'sha256WithRSAEncryption');
- $sign = base64_encode($raw_sign);
- $params['sign'] = $sign;
- return $params;
- }
- public function configForJSSDKPayment($prepayId)
- {
- $config = $this->configForPayment($prepayId, false);
- $config['timestamp'] = $config['timeStamp'];
- unset($config['timeStamp']);
- return $config;
- }
- }
|