当前位置:首页 > PHP教程 > php高级应用 > 列表

纯PHP代码实现支付宝批量付款

发布:smiling 来源: PHP粉丝网  添加日期:2021-07-01 16:28:48 浏览: 评论:0 

最近在做一个使用支付宝转账的项目,其中有需求把我难到了:批量支付成功后不知道怎么接收系统返回的通知,经过朋友帮忙,此功能实现,下面小编把具体代码整理分享给大家,供大家参考。

废话不多说了,直接给大家贴php代码了,具体代码如下所示:

  1. //批量付款异步通知处理 
  2. class Notify 
  3. { 
  4.   public $notifyParams; 
  5.   //处理成功的信息 
  6.   protected $success = []; 
  7.   //处理失败的信息 
  8.   protected $fail = []; 
  9.   //批次号 
  10.   protected $batchNo; 
  11.   public function save() 
  12.   { 
  13.     if (!is_array($this->notifyParams)) { 
  14.       return false; 
  15.     } 
  16.     $alipayNotify = new AlipayNotify(); 
  17.     $alipayNotify->notifyParams = $this->notifyParams; 
  18.     $alipayNotify->partner = Yii::$app->params['Alipay.appid']; 
  19.     $alipayNotify->key = Yii::$app->params['Alipay.appKey']; 
  20.     if (!$alipayNotify->verify()) { 
  21.       return false; 
  22.     } 
  23.     $this->batchNo = $this->notifyParams['batch_no']; 
  24.     $this->parseResult(); 
  25.     //转账成功的 
  26.     if (!emptyempty($this->success)) { 
  27.       foreach ($this->success as $item) { 
  28.         //......... 
  29.       } 
  30.     } 
  31.     //转账失败的 
  32.     if (!emptyempty($this->fail)) { 
  33.       foreach ($this->fail as $item) { 
  34.         //........ 
  35.       } 
  36.     } 
  37.     return true; 
  38.   } 
  39.   //解析结果 
  40.   protected function parseResult() 
  41.   { 
  42.     if (!emptyempty($this->notifyParams['success_details'])) { 
  43.       $suArray = explode('|', $this->notifyParams['success_details']); 
  44.       foreach ($suArray as $item) { 
  45.         $this->success[] = explode('^', $item); 
  46.       } 
  47.     } 
  48.     if (!emptyempty($this->notifyParams['fail_detail'])) { 
  49.       $faArray = explode('|', $this->notifyParams['fail_detail']); 
  50.       foreach ($faArray as $item) { 
  51.         $this->fail[] = explode('^', $item); 
  52.       } 
  53.     } 
  54.   } 
  55. } 
  56. //用法 
  57. $model = new Notify(); 
  58. $model->notifyParams = $_POST; 
  59. if ($model->save()) { 
  60.   return 'success'; 
  61. } 
  62. return 'fail'; 

以上内容给大家讲解了纯PHP代码实现支付宝批量付款的功能,希望对大家有所帮助。

Tags: PHP支付宝批量付款

分享到: