import com.github.binarywang.wxpay.bean.notify.WxPayNotifyResponse;
import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.WxPayService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
/**
* @author admin
*/
@Slf4j
@RestController
@RequestMapping("pay")
public class PayController {
/**
* 微信服务回调
*/
@RequestMapping("wxCallPack")
public String wxCallPack(HttpServletRequest request) throws WxPayException, IOException {
// 将异步通知的参数转化为xml
String xmlData = getXml(request);
log.info("微信支付回调:xmlData:{}", xmlData);
WxPayService wxPayService = WxPayAutoConfiguration.getWxPayService();
WxPayOrderNotifyResult notifyResult = wxPayService.parseOrderNotifyResult(xmlData);
// 获取商户订单号
String outTradeNo = notifyResult.getOutTradeNo();
// 通过 outTradeNo 查询系统订单数据
// orderService.updateOrderStatus(outTradeNo, notifyResult.getTransactionId());
return WxPayNotifyResponse.success("成功");
}
private String getXml(HttpServletRequest request) throws IOException {
InputStream inputStream = null;
try {
inputStream = request.getInputStream();
} catch (IOException e) {
e.printStackTrace();
}
//获取请求输入流
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len=inputStream.read(buffer))!=-1){
outputStream.write(buffer,0,len);
}
outputStream.close();
inputStream.close();
return new String(outputStream.toByteArray(), "utf-8");
}
}
#