<?php
namespace Payment; use Payment\Common\BaseStrategy; use Payment\Common\PayException; use Payment\Trans\AliTransfer; use Payment\Trans\WxTransfer; class TransferContext { protected $transfer; public function initTransfer($channel, array $config) { try { switch ($channel) { case Config::ALI_TRANSFER: $this->transfer = new AliTransfer($config); break; case Config::WX_TRANSFER: $this->transfer = new WxTransfer($config); break; default: throw new PayException('当前仅支持：ALI WEIXIN两个常量'); } } catch (PayException $e) { throw $e; } } public function transfer(array $data) { if (! $this->transfer instanceof BaseStrategy) { throw new PayException('请检查初始化是否正确'); } try { return $this->transfer->handle($data); } catch (PayException $e) { throw $e; } } } 