Example #1
0
 /**
  *红包发送代码
  */
 public function sendRed()
 {
     //发送红包钱判断用户是否有发送机会
     $customer = Customer::where('openid', Session::get('logged_user')['openid'])->firstOrFail();
     if ($customer->chances <= 0) {
         $result['return_code'] = 'FAIL';
         $result['return_msg'] = '红包机会已用完';
         return $result;
     }
     //$appId, $appSecret, $mchId, $mchKey
     /**
      * 第 1 步:定义商户
      */
     $business = new Business($this->appid, $this->secret, $this->mch_id, $this->mch_key);
     /**
      * 第 2 步:设置证书路径
      * CLIENTCERT_PATH即证书apiclient_cert.pem的路径
      * CLIENTkey_PATH即证书apiclient_key.pem的路径
      */
     $business->setClientCert(storage_path('cert/apiclient_cert.pem'));
     $business->setClientKey(storage_path('cert/apiclient_key.pem'));
     /**
      * 第 3 步:创建LuckMoney实例
      */
     $luckMoneyServer = new LuckMoney($business);
     /**
      * 第 4 步:要发送的红包相关数据(本代码以发送现金红包为例)
      */
     $luckMoneyData['mch_billno'] = time();
     //红包记录对应的商户订单号
     $luckMoneyData['send_name'] = '犟骨头';
     //红包发送者名称
     $luckMoneyData['re_openid'] = Session::get('logged_user')['openid'];
     //红包接收者的openId
     $luckMoneyData['total_amount'] = 100;
     //红包总额(单位为分),现金红包至少100,裂变红包至少300
     $luckMoneyData['total_num'] = 1;
     //现金红包时为1,裂变红包时至少为3
     $luckMoneyData['wishing'] = '恭喜发财';
     $luckMoneyData['act_name'] = '关注有礼';
     $luckMoneyData['remark'] = 'test';
     /**
      * 第 5 步:发送红包
      * 第二个参数表示发送的红包类型,有现金红包('CASH_LUCK_MONEY')和裂变红包('GROUP_LUCK_MONEY')可选,红包工具类中已定义相关常量。
      */
     $result = $luckMoneyServer->send($luckMoneyData);
     //判断红包是否发送成功
     if ($result['return_code'] == 'SUCCESS') {
         //将用户存入数据库
         $customer->chances -= 1;
         $customer->save();
     }
     return $result;
 }
Example #2
0
 /**
  * 查询红包信息
  *
  * @param string $mchBillNumber
  *
  * @return array
  */
 public function query($mchBillNumber)
 {
     if (empty($mchBillNumber)) {
         throw new Exception('mch_id is required');
     }
     $param['mch_billno'] = $mchBillNumber;
     $param['nonce_str'] = uniqid('pre_');
     $param['mch_id'] = $this->business->mch_id;
     $param['appid'] = $this->business->appid;
     $param['bill_type'] = 'MCHT';
     $signGenerator = new SignGenerator($param);
     $me = $this;
     $signGenerator->onSortAfter(function (SignGenerator $that) use($me) {
         $that->key = $me->business->mch_key;
     });
     $sign = $signGenerator->getResult();
     $param['sign'] = $sign;
     $request = XML::build($param);
     //设置Http使用的证书
     $options['sslcert_path'] = $this->business->getClientCert();
     $options['sslkey_path'] = $this->business->getClientKey();
     $http = new Http();
     $response = $http->request(static::API_QUERY, Http::POST, $request, $options);
     if (empty($response)) {
         throw new Exception('Get LuckMoneyInfo failed.');
     }
     $result = XML::parse($response);
     return $result;
 }
Example #3
0
 /**
  * 获取退款结果
  * @return array
  * @throws Exception
  */
 public function getResponse()
 {
     if (is_null($this->business)) {
         throw new Exception('Business is required');
     }
     static::$params['appid'] = $this->business->appid;
     static::$params['mch_id'] = $this->business->mch_id;
     $this->checkParams();
     $signGenerator = new SignGenerator(static::$params);
     $signGenerator->onSortAfter(function (SignGenerator $that) {
         $that->key = $this->business->mch_key;
     });
     static::$params['sign'] = $signGenerator->getResult();
     $request = XML::build(static::$params);
     //设置Http使用的证书
     $options['sslcert_path'] = $this->business->getClientCert();
     $options['sslkey_path'] = $this->business->getClientKey();
     $http = new Http();
     $response = $http->request(static::REFUNDORDER_URL, Http::POST, $request, $options);
     if (empty($response)) {
         throw new Exception('Get Refund Failure:');
     }
     $refundOrder = XML::parse($response);
     if (isset($refundOrder['return_code']) && $refundOrder['return_code'] === 'FAIL') {
         throw new Exception($refundOrder['return_code'] . ': ' . $refundOrder['return_msg']);
     }
     //返回签名数据校验
     if (empty($refundOrder) || empty($refundOrder['sign'])) {
         throw new Exception('param sign is missing or empty');
     }
     $sign = $refundOrder['sign'];
     unset($refundOrder['sign']);
     $signGenerator = new SignGenerator($refundOrder);
     $signGenerator->onSortAfter(function (SignGenerator $that) {
         $that->key = $this->business->mch_key;
     });
     if ($sign !== $signGenerator->getResult()) {
         throw new Exception('check sign error');
     }
     //返回结果判断
     if (isset($refundOrder['result_code']) && $refundOrder['result_code'] === 'FAIL') {
         throw new Exception($refundOrder['err_code'] . ': ' . $refundOrder['err_code_des']);
     }
     if (isset($refundOrder['return_code']) && $refundOrder['return_code'] === 'FAIL') {
         throw new Exception($refundOrder['return_code'] . ': ' . $refundOrder['return_msg']);
     }
     return $this->refundInfo = $refundOrder;
 }