Browse Source

payment

master
林一峰 7 years ago
parent
commit
6f3c6bcd8d
  1. 78
      app/Common/Payment.php
  2. 71
      app/Http/Controllers/Api/PaymentNotifyController.php

78
app/Common/Payment.php

@ -0,0 +1,78 @@
<?php
namespace App\Common;
class Payment
{
//微信APP支付
public static function WeixinPayApp($attributes2,$config = config('weixin.app'))
{
//1.配置
//$config = config('weixin.app');
$app = new \EasyWeChat\Foundation\Application($config);
$payment = $app->payment;
//2.创建订单
$attributes = array(
'trade_type' => 'APP', // JSAPI,NATIVE,APP...
'body' => '订单支付',
'detail' => '订单支付',
'out_trade_no' => 'fghfdgfg789',
'total_fee' => '123.00', // 单位:分
'notify_url' => 'http://www.baidu.com/aaa.php', // 支付结果通知网址,如果不设置则会使用配置里的默认地址
//'openid' => '当前用户的 openid', // trade_type=JSAPI,此参数必传,用户在商户appid下的唯一标识,
// ...
);
$order = new \EasyWeChat\Payment\Order(array_merge($attributes, $attributes2));
//3.统一下单
$result = $payment->prepare($order);
if ($result->return_code == 'SUCCESS' && $result->result_code == 'SUCCESS')
{
$prepayId = $result->prepay_id;
$res = $payment->configForAppPayment($prepayId);
return $res;
}
return false;
}
/**
* 支付宝APP支付
* bizcontent
* 支付宝APP支付
*/
public function AlipayApp($bizcontent,$notify_url,$config = config('alipay.app_alipay'))
{
require_once base_path('resources/org/alipay_app').'/AopClient.php';
require_once base_path('resources/org/alipay_app').'/AlipayTradeAppPayRequest.php';
$aop = new \AopClient;
$aop->gatewayUrl = "https://openapi.alipay.com/gateway.do";
$aop->appId = $config['appId'];
$aop->rsaPrivateKey = $config['rsaPrivateKey'];
$aop->format = "json";
$aop->charset = "UTF-8";
$aop->signType = "RSA2";
$aop->alipayrsaPublicKey = $config['alipayrsaPublicKey'];
//实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.trade.app.pay
$request = new \AlipayTradeAppPayRequest();
//SDK已经封装掉了公共参数,这里只需要传入业务参数
/* $bizcontent = "{\"body\":\"订单支付\","
. "\"subject\": \"订单支付\","
. "\"out_trade_no\": \""."jklkjlkj54654165"."\","
. "\"total_amount\": \""."123.00"."\","
. "\"timeout_express\": \"30m\","
. "\"product_code\":\"QUICK_MSECURITY_PAY\""
. "}"; */
$request->setNotifyUrl($notify_url);
$request->setBizContent($bizcontent);
//这里和普通的接口调用不同,使用的是sdkExecute
$response = $aop->sdkExecute($request);
//htmlspecialchars是为了输出到页面时防止被浏览器将关键参数html转义,实际打印到日志以及http传输不会有这个问题
return $response;//就是orderString 可以直接给客户端请求,无需再做处理。
}
}

71
app/Http/Controllers/Api/PaymentNotifyController.php

@ -0,0 +1,71 @@
<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Api\CommonController;
use Illuminate\Http\Request;
use App\Common\ReturnData;
use App\Common\Token;
use App\Http\Model\UserMoney;
class PaymentNotifyController extends CommonController
{
public function __construct()
{
parent::__construct();
}
public function WxPayAppNotify(Request $request)
{
$options = config('weixin.abmapp');
$app = new \EasyWeChat\Foundation\Application($options);
$response = $app->payment->handleNotify(function($notify, $successful)
{
// 使用通知里的 "微信支付订单号" 或者 "商户订单号" 去自己的数据库找到订单
$order = 查询订单($notify->out_trade_no);
if (!$order) { // 如果订单不存在
return 'Order not exist.'; // 告诉微信,我已经处理完了,订单没找到,别再通知我了
}
// 如果订单存在
// 检查订单是否已经更新过支付状态
if ($order->paid_at) // 假设订单字段“支付时间”不为空代表已经支付
{
return true; // 已经支付成功了就不再更新了
}
// 用户是否支付成功
if ($successful)
{
// 不是已经支付状态则修改为已经支付状态
$order->paid_at = time(); // 更新支付时间为当前时间
$order->status = 'paid';
}
else
{
// 用户支付失败
$order->status = 'paid_fail';
}
$order->save(); // 保存订单
return true; // 返回处理完成
});
return $response;
}
public function AlipayAppNotify($config('alipay.app_alipay'))
{
require_once base_path('resources/org/alipay_app').'/AopClient.php';
$aop = new \AopClient;
$aop->alipayrsaPublicKey = $config['alipayrsaPublicKey'];
$flag = $aop->rsaCheckV1($_REQUEST, NULL, "RSA2");
//支付成功
if($flag)
{
$out_trade_no = $_REQUEST['out_trade_no'];
}
}
}
Loading…
Cancel
Save