AlipayF2F.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. $gateway = new \Library\AlipayF2F();
  57. $gateway->setAppId($this->config['app_id']);
  58. $gateway->setPrivateKey($this->config['private_key']); // 可以是路径,也可以是密钥内容
  59. $gateway->setAlipayPublicKey($this->config['public_key']); // 可以是路径,也可以是密钥内容
  60. try {
  61. if ($gateway->verify($params)) {
  62. /**
  63. * Payment is successful
  64. */
  65. return [
  66. 'trade_no' => $params['out_trade_no'],
  67. 'callback_no' => $params['trade_no']
  68. ];
  69. } else {
  70. /**
  71. * Payment is not successful
  72. */
  73. return false;
  74. }
  75. } catch (\Exception $e) {
  76. /**
  77. * Payment is not successful
  78. */
  79. return false;
  80. }
  81. }
  82. }