PHP code example of orq / wxpay
1. Go to this page and download the library: Download orq/wxpay library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
orq / wxpay example snippets
// 支付
public function makeOrder() {
$prepay_id = $this->makeUnifiedOder($order, 'test_prod', $_GET['code']);
$JsApi = new \Orq\Wxpay\JsApi();
$jsApi->setPrepayId($prepay_id);
return response()->json(json_decode($jsApi->getParameters(), true));
}
/**
* 统一下单
*/
protected function makeUnifiedOder($order, $item_title, $code) {
$pay_amount = $order->pay_amount * 100;
$unifiedOrder = new \Orq\Wxpay\UnifiedOrder();
$JsApi = new \Orq\Wxpay\JsApi();
$jsApi->setCode($code);
$openId = $JsApi->getOpenId();
$unifiedOrder->setParameter("openid", $openID);
$unifiedOrder->setParameter("body", $item_title);//商品描述
//自定义订单号,此处仅作举例
// $timeStamp = time();
$unifiedOrder->setParameter("out_trade_no", $order->no);//商户订单号
$unifiedOrder->setParameter("total_fee", $pay_amount);//总金额以分位单位必须是整数
$unifiedOrder->setParameter("notify_url",\Orq\Wxpay\$this->config->getAppSecret()NOTIFY_URL);//通知地址
$unifiedOrder->setParameter("trade_type","JSAPI");//交易类型
//非必填参数,商户可根据实际情况选填
// file_put_contents('Debug.txt', date('Y-m-d H:i:s').' Finished settingData:'."\r\n", FILE_APPEND);
$prepay_id = $unifiedOrder->getPrepayId();
// file_put_contents('Debug.txt', date('Y-m-d H:i:s').' prepay_id '.$prepay_id."\r\n", FILE_APPEND);
return $prepay_id;
}
// 假如已经有openId则可以使用这个来统一下单
protected function makeUnifiedOder($order, $item_title) {
$unifiedOrder = new \Orq\Wxpay\UnifiedOrder();
$unifiedOrder->setParameter("openid", session('openid'));//用户openid
$unifiedOrder->setParameter("body", $item_title);//商品描述
//自定义订单号,此处仅作举例
$timeStamp = time();
$unifiedOrder->setParameter("out_trade_no", $order->no);//商户订单号
$unifiedOrder->setParameter("total_fee", $order->pay_amount_);//总金额
$unifiedOrder->setParameter("notify_url",$this->config->getAppSecret()NOTIFY_URL);//通知地址
$unifiedOrder->setParameter("trade_type","JSAPI");//交易类型
//非必填参数,商户可根据实际情况选填
$prepay_id = $unifiedOrder->getPrepayId();
return $prepay_id;
}
// 支付通知处理
public function handleNotify() {
$str = file_get_contents('php://input');
$arr = \Orq\Wxpay\Utility::xmlToArray($str);
$okMsg = ['return_code'=>'SUCCESS', 'return_msg'=>'OK'];
$orderRepository = new OrderRepository();
$order = $orderRepository->findByOrderNumber($arr['out_trade_no']);
if (!$order || $order->getPayStatus()->getKey() == '2') {
return \Orq\Wxpay\Utility::arrayToXml($okMsg);
}
$sign = $arr['sign'];
$aSign = \Orq\Wxpay\Utility::getSign(array_diff_key($arr, ['sign'=>'']));
if ($aSign === $sign) {
if ($arr['total_fee'] == $order->getPayAmount()*100) {
try {
$order->setPayStatus('2');
$order->setUpdatedAt(date('Y-m-d H:i:s'));
$orderRepository->updateOrder($order);
return $util->arrayToXml($okMsg);
} catch (RepositoryException $e) {
Log::error('更新订单支付状态失败!OrderNumber: '.$order->getOrderNumber().' Message: '.$e->getMessage());
}
}
} else {
throw new \Exception('签名不符');
}
}