AlipayF2F.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. /**
  3. * 自己写别抄,抄NMB抄
  4. */
  5. namespace App\Payments;
  6. class AlipayF2F {
  7. public function __construct($config)
  8. {
  9. $this->config = $config;
  10. }
  11. public function form()
  12. {
  13. return [
  14. 'app_id' => [
  15. 'label' => '支付宝APPID',
  16. 'description' => '',
  17. 'type' => 'input',
  18. ],
  19. 'private_key' => [
  20. 'label' => '支付宝私钥',
  21. 'description' => '',
  22. 'type' => 'input',
  23. ],
  24. 'public_key' => [
  25. 'label' => '支付宝公钥',
  26. 'description' => '',
  27. 'type' => 'input',
  28. ]
  29. ];
  30. }
  31. public function pay($order)
  32. {
  33. try {
  34. $gateway = new \Library\AlipayF2F();
  35. $gateway->setMethod('alipay.trade.precreate');
  36. $gateway->setAppId($this->config['app_id']);
  37. $gateway->setPrivateKey($this->config['private_key']); // 可以是路径,也可以是密钥内容
  38. $gateway->setAlipayPublicKey($this->config['public_key']); // 可以是路径,也可以是密钥内容
  39. $gateway->setNotifyUrl($order['notify_url']);
  40. $gateway->setBizContent([
  41. 'subject' => config('v2board.app_name', 'V2Board') . ' - 订阅',
  42. 'out_trade_no' => $order['trade_no'],
  43. 'total_amount' => $order['total_amount'] / 100
  44. ]);
  45. $gateway->send();
  46. return [
  47. 'type' => 0, // 0:qrcode 1:url
  48. 'data' => $gateway->getQrCodeUrl()
  49. ];
  50. } catch (\Exception $e) {
  51. abort(500, $e->getMessage());
  52. }
  53. }
  54. public function notify($params)
  55. {
  56. if ($params['trade_status'] !== 'TRADE_SUCCESS') return false;
  57. $gateway = new \Library\AlipayF2F();
  58. $gateway->setAppId($this->config['app_id']);
  59. $gateway->setPrivateKey($this->config['private_key']); // 可以是路径,也可以是密钥内容
  60. $gateway->setAlipayPublicKey($this->config['public_key']); // 可以是路径,也可以是密钥内容
  61. try {
  62. if ($gateway->verify($params)) {
  63. /**
  64. * Payment is successful
  65. */
  66. return [
  67. 'trade_no' => $params['out_trade_no'],
  68. 'callback_no' => $params['trade_no']
  69. ];
  70. } else {
  71. /**
  72. * Payment is not successful
  73. */
  74. return false;
  75. }
  76. } catch (\Exception $e) {
  77. /**
  78. * Payment is not successful
  79. */
  80. return false;
  81. }
  82. }
  83. }