meedu对接易支付 个人免签支付

1、支付SDK服务类 包含发起支付、查询订单、回调验证等功能

<?php
/* * 技术支持QQ 735660248
 * 彩虹易支付SDK服务类
 * 说明:
 * 包含发起支付、查询订单、回调验证等功能
 */
namespace App\Meedu\Payment\Juea;
use Illuminate\Support\Facades\Log;
class EpayCore
{
    
    
	private $pid;
	private $key;
	private $submit_url;
	private $mapi_url;
	private $api_url;
	private $sign_type = 'MD5';

	function __construct($config){
    
    
		$this->pid = $config['pid'];
		$this->key = $config['key'];
		$this->submit_url = $config['apiurl'].'submit.php';
		$this->mapi_url = $config['apiurl'].'mapi.php';
		$this->api_url = $config['apiurl'].'api.php';
	}

	// 发起支付(页面跳转)
	public function pagePay($param_tmp, $button='正在跳转'){
    
    
		$param = $this->buildRequestParam($param_tmp);

		$html = '<form id="dopay" action="'.$this->submit_url.'" method="post">';
		foreach ($param as $k=>$v) {
    
    
			$html.= '<input type="hidden" name="'.$k.'" value="'.$v.'"/>';
		}
		$html .= '<input type="submit" value="'.$button.'"></form><script>document.getElementById("dopay").submit();</script>';

		return $html;
	}

	// 发起支付(获取链接)
	public function getPayLink($param_tmp){
    
    
		$param = $this->buildRequestParam($param_tmp);
		$url = $this->submit_url.'?'.http_build_query($param);
		return $url;
	}

	// 发起支付(API接口)
	public function apiPay($param_tmp){
    
    
		$param = $this->buildRequestParam($param_tmp);
		$response = $this->getHttpResponse($this->mapi_url, http_build_query($param));
		$arr = json_decode($response, true);
		return $arr;
	}

	// 异步回调验证
	public function verifyNotify(){
    
    
			// if(empty($_GET)) return false;
    	Log::error('进入延签的方法里了');
		Log::error(request()->input());
        $parameter = array(
            "pid" => request()->input('pid'),
            "type" => request()->input('type'),
            "notify_url" => 'http://81.68.154.199:9910/payment/callback/juea',
            "return_url" => 'http://81.68.154.199:9912/#/order/success',
            "out_trade_no" => request()->input('out_trade_no'),
            "name" => request()->input('out_trade_no'),
            // "money"	=> $total,
             "money"	=> request()->input('money'),
            // 'clientip'=>request()->ip()
        );
		$sign = $this->getSign($parameter);
	    Log::error($sign);

		if($sign ===request()->input('sign')){
    
    
			$signResult = true;
		}else{
    
    
			$signResult = false;
		}

		return $signResult;
	}

	// 同步回调验证
	public function verifyReturn(){
    
    
		if(empty($_GET)) return false;

		$sign = $this->getSign($_GET);

		if($sign === $_GET['sign']){
    
    
			$signResult = true;
		}else{
    
    
			$signResult = false;
		}

		return $signResult;
	}

	// 查询订单支付状态
	public function orderStatus($trade_no){
    
    
		$result = $this->queryOrder($trade_no);
		if($result['status']==1){
    
    
			return true;
		}else{
    
    
			return false;
		}
	}

	// 查询订单
	public function queryOrder($trade_no){
    
    
		$url = $this->api_url.'?act=order&pid=' . $this->pid . '&key=' . $this->key . '&trade_no=' . $trade_no;
		$response = $this->getHttpResponse($url);
		$arr = json_decode($response, true);
		return $arr;
	}

	private function buildRequestParam($param){
    
    
		$mysign = $this->getSign($param);
		$param['sign'] = $mysign;
		$param['sign_type'] = $this->sign_type;
		return $param;
	}

	// 计算签名
	private function getSign($param){
    
    
		ksort($param);
		reset($param);
		$signstr = '';
	
		foreach($param as $k => $v){
    
    
			if($k != "sign" && $k != "sign_type" && $v!=''){
    
    
				$signstr .= $k.'='.$v.'&';
			}
		}
		$signstr = substr($signstr,0,-1);
		$signstr .= $this->key;
		$sign = md5($signstr);
		return $sign;
	}

	// 请求外部资源
	private function getHttpResponse($url, $post = false, $timeout = 10){
    
    
		$ch = curl_init($url);
		curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
		$httpheader[] = "Accept: */*";
		$httpheader[] = "Accept-Language: zh-CN,zh;q=0.8";
		$httpheader[] = "Connection: close";
		curl_setopt($ch, CURLOPT_HTTPHEADER, $httpheader);
		curl_setopt($ch, CURLOPT_HEADER, false);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		if($post){
    
    
			curl_setopt($ch, CURLOPT_POST, true);
			curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
		}
		$response = curl_exec($ch);
		curl_close($ch);
		return $response;
	}
}
<?php
namespace App\Http\Controllers\Api\V2;
use Illuminate\Http\Request;
use App\Constant\FrontendConstant;
use App\Exceptions\SystemException;
use App\Meedu\Payment\Wechat\WechatScan;
use App\Services\Base\Services\CacheService;
use App\Meedu\Payment\Contract\PaymentStatus;
use App\Services\Base\Services\ConfigService;
use App\Services\Order\Services\OrderService;
use App\Services\Base\Interfaces\CacheServiceInterface;
use App\Services\Base\Interfaces\ConfigServiceInterface;
use App\Services\Order\Interfaces\OrderServiceInterface;
use Illuminate\Support\Facades\Log;
class PaymentController extends BaseController
{
    
    

    /**
     * @var OrderService
     */
    protected $orderService;

    /**
     * @var CacheService
     */
    protected $cacheService;

    public function __construct(OrderServiceInterface $orderService, CacheServiceInterface $cacheService)
    {
    
    
        $this->orderService = $orderService;
        $this->cacheService = $cacheService;
    }

    /**
     * @api {get} /api/v2/order/payments 支付网关列表
     * @apiGroup 订单
     * @apiName Payments
     * @apiVersion v2.0.0
     * @apiHeader Authorization Bearer+空格+token
     *
     * @apiParam {String} scene 支付场景[h5,wechat]
     *
     * @apiSuccess {Number} code 0成功,非0失败
     * @apiSuccess {Object} data 数据
     * @apiSuccess {String} data.sign 网关值
     * @apiSuccess {String} data.name 网关名
     */
    public function payments(Request $request)
    {
    
    
        $scene = $request->input('scene');
        if (!$scene) {
    
    
            return $this->error(__('参数错误'));
        }

        $payments = get_payments($scene)->map(function ($val) {
    
    
            return [
                'sign' => $val['sign'],
                'name' => $val['name'],
                'icon' => url($val['logo']),
            ];
        })->toArray();
        sort($payments);
        return $this->data($payments);
    }

    /**
     * @api {get} /api/v2/order/pay/redirect 跳转到第三方支付
     * @apiGroup 订单
     * @apiName PayRedirect
     * @apiVersion v2.0.0
     * @apiHeader Authorization Bearer+空格+token
     *
     * @apiParam {String=h5:手机浏览器,wechat:微信浏览器} payment_scene 支付场景
     * @apiParam {String=alipay:支付宝支付,wechat-jsapi:微信jsapi支付,handPay:手动打款} payment 支付方式
     * @apiParam {String} order_id 订单编号
     * @apiParam {String} redirect 支付完成回跳地址
     *
     * @apiSuccess {Number} code 0成功,非0失败
     * @apiSuccess {Object} data 数据
     */
    public function payRedirect(Request $request)
    {
    
    
        $payment = $request->input('payment');
        $paymentScene = $request->input('payment_scene');
        $orderId = $request->input('order_id');

        if (!$payment || !$paymentScene || !$orderId) {
    
    
            return $this->error(__('参数错误'));
        }

        $payments = get_payments($paymentScene);
        if (!$payments) {
    
    
            return $this->error(__('错误'));
        }
        if (!isset($payments[$payment])) {
    
    
            return $this->error(__('错误'));
        }

        $order = $this->orderService->findUser($this->id(), $orderId);
        if ($order['status'] !== FrontendConstant::ORDER_UN_PAY) {
    
    
            return $this->error(__('订单状态错误'));
        }
      Log::error('准备进行易支付');
        Log::error($order);
        $updateData = [
            'payment' => $payment,
            'payment_method' => $payments[$payment][$paymentScene],
        ];
        $this->orderService->change2Paying($order['id'], $updateData);
        $order = array_merge($order, $updateData);
          //易支付
        $paymentHandler = app()->make(\App\Meedu\Payment\Juea\Juea::class);
        $createResult = $paymentHandler->create($order);
        return $createResult;
        // // 创建远程订单
        // $paymentHandler = app()->make($payments[$payment]['handler']);
        // // var_dump($payments[$payment]['handler']);
        // //   var_dump('这是啥');
        // // die;
        // $createResult = $paymentHandler->create($order);
        // if ($createResult->status === false) {
    
    
        //     throw new SystemException(__('系统错误'));
        // }

        // return $createResult->data;
    }

    /**
     * @api {get} /api/v2/order/pay/handPay 手动打款支付
     * @apiGroup 订单
     * @apiName PaymentHandPayV2
     * @apiVersion v2.0.0
     * @apiHeader Authorization Bearer+空格+token
     *
     * @apiSuccess {Number} code 0成功,非0失败
     * @apiSuccess {Object} data 数据
     * @apiSuccess {String} data.text 手动打款支付信息
     */
    public function handPay()
    {
    
    
        /**
         * @var ConfigService $configService
         */
        $configService = app()->make(ConfigServiceInterface::class);

        $text = $configService->getHandPayIntroducation();

        return $this->data(['text' => $text]);
    }

    /**
     * @api {post} /api/v2/order/pay/wechatScan 微信扫码支付[PC]
     * @apiGroup 订单
     * @apiName PaymentWechatScan
     * @apiVersion v2.0.0
     * @apiHeader Authorization Bearer+空格+token
     *
     * @apiParam {String} order_id 订单编号
     *
     * @apiSuccess {Number} code 0成功,非0失败
     * @apiSuccess {Object} data 数据
     * @apiSuccess {String} data.code_url 微信支付二维码的文本值,用该值生成二维码
     */
    public function wechatScan(Request $request)
    {
    
    
        $orderId = $request->input('order_id');
        if (!$orderId) {
    
    
            return $this->error(__('参数错误'));
        }

        $order = $this->orderService->findUser($this->id(), $orderId);
        if ($order['status'] !== FrontendConstant::ORDER_UN_PAY) {
    
    
            return $this->error(__('订单状态错误'));
        }

        $updateData = [
            'payment' => FrontendConstant::PAYMENT_SCENE_WECHAT,
            'payment_method' => FrontendConstant::PAYMENT_SCENE_WECHAT_SCAN,
        ];
        $this->orderService->change2Paying($order['id'], $updateData);
        $order = array_merge($order, $updateData);

        // 创建远程订单
        $paymentHandler = app()->make(WechatScan::class);

        /**
         * @var PaymentStatus $createResult
         */
        $createResult = $paymentHandler->create($order);
        if ($createResult->status === false) {
    
    
            throw new SystemException(__('系统错误'));
        }

        $data = $createResult->data;

        return $this->data([
            'code_url' => $data['code_url'],
        ]);
    }
}

2、在meedu后台配置上 你自己的个人易支付账号信息 商户ID 商户秘钥
在这里插入图片描述3、选择你的课程
在这里插入图片描述4、选择你要的支付方式
在这里插入图片描述5、自动跳转到易支付的 支付页面了
在这里插入图片描述
6、在meedu的个人中心那边可以看到刚刚支付了1块钱的课程订单
在这里插入图片描述
7、在易支付的控制台可以看到刚刚支付成功了的订单金额,接下来就去进行提现到个人收款账号就好了在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/withkai44/article/details/128556686