1.定义接口 //仅供个人参考 //切忌
<?php
namespace App\Http\Itface;Interface Pay
{
public function pay(); //接口,限定子类实现方法
}
2.创建工厂
<?phpnamespace App\Http\Factory;use App\Http\Controllers\Api\AlipayController;class PayFactory
{
const ALIPAY = 'alipay';const WXPAY = 'wxpay';const YUEPAY = 'yuepay';public function pay($type){
switch ($type){
case self::ALIPAY:$obj= new AlipayController();break;case self::WXPAY:$obj=2;break;case self::YUEPAY:$obj=3;break;default:return null;}return $obj;}
}
2.创建支付类 这里先创建一个,当做demo
<?php
namespace OssSdk\service; //命名空间use OssSdk\Defineinterface\Cloud; //接口的目录路径class TencentCloud implements Pay{
public function pay($file,$config){
//写支付的方法}
}
3.控制器调用即可
//支付public function pay(Request $request){
$data=$request->input();
// $config=[
// 'ak'=>'',
// 'sk'=>'',
// ];$obj=new PayFactory();$res=$obj->pay($data['pay'])->index($data['price']);if ($res){
return $res;}}