alipay.class.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. class AlipayService
  3. {
  4. public $appId;
  5. public $returnUrl;
  6. public $notifyUrl;
  7. public $signType;
  8. public $privateKey;
  9. public $publicKey;
  10. public function __construct($config)
  11. {
  12. $this->appId = isset($config['appId']) ? $config['appId'] : '';
  13. $this->notifyUrl = isset($config['notifyUrl']) ? $config['notifyUrl'] : '';
  14. $this->signType = $config['signType'];
  15. $this->privateKey = isset($config['privateKey']) ? $config['privateKey'] : '';
  16. $this->publicKey = $config['publicKey'];
  17. }
  18. /**
  19. * AopClient初始化
  20. * @return AopClient
  21. */
  22. private function aop_init($isData = true)
  23. {
  24. require_once __DIR__ . '/aop/AopClient.php';
  25. $aop = new AopClient();
  26. if ($isData) {
  27. $this->data_init($aop);
  28. }
  29. return $aop;
  30. }
  31. private function data_init($aop)
  32. {
  33. $aop->appId = $this->appId;
  34. $aop->rsaPrivateKey = $this->privateKey;
  35. $aop->alipayrsaPublicKey = $this->publicKey;
  36. $aop->signType = $this->signType;
  37. return $aop;
  38. }
  39. public function qrpay(array $data)
  40. {
  41. require_once __DIR__ . '/AopSdk.php';
  42. $aop = $this->aop_init();
  43. $request = new AlipayTradePrecreateRequest();
  44. $request->setNotifyUrl($this->notifyUrl);
  45. $body = [
  46. 'body' => $data['body'],
  47. 'subject' => $data['subject'],
  48. 'out_trade_no' => $data['out_trade_no'],
  49. 'timeout_express' => '5m',
  50. 'total_amount' => $data['total_amount'],
  51. 'store_id' => 'FF_001'
  52. ];
  53. $request->setBizContent(json_encode($body));
  54. $result = $aop->execute($request, "POST");
  55. $response = $result->alipay_trade_precreate_response;
  56. return ['code' => $response->code, 'msg' => $response->msg, 'out_trade_no' => $response->out_trade_no, 'qr_code' => $response->qr_code];
  57. }
  58. /**
  59. * 验签方法
  60. * @param $arr 验签支付宝返回的信息,使用支付宝公钥。
  61. * @return boolean
  62. */
  63. public function check($arr)
  64. {
  65. $aop = $this->aop_init(false);
  66. $aop->alipayrsaPublicKey = $this->publicKey;
  67. //dump($aop);exit;
  68. $result = $aop->rsaCheckV1($arr, $this->publicKey, $this->signType);
  69. return $result;
  70. }
  71. }