AlipayF2F.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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->setAppId($this->config['app_id']);
  36. $gateway->setPrivateKey($this->config['private_key']); // 可以是路径,也可以是密钥内容
  37. $gateway->setAlipayPublicKey($this->config['public_key']); // 可以是路径,也可以是密钥内容
  38. $gateway->setNotifyUrl($order['notify_url']);
  39. $gateway->setBizContent([
  40. 'subject' => config('v2board.app_name', 'V2Board') . ' - 订阅',
  41. 'out_trade_no' => $order['trade_no'],
  42. 'total_amount' => $order['total_amount'] / 100
  43. ]);
  44. $gateway->send();
  45. return [
  46. 'type' => 0, // 0:qrcode 1:url
  47. 'data' => $gateway->getQrCodeUrl()
  48. ];
  49. } catch (\Exception $e) {
  50. abort(500, $e->getMessage());
  51. }
  52. }
  53. public function notify($params)
  54. {
  55. $gateway = Omnipay::create('Alipay_AopF2F');
  56. $gateway->setSignType('RSA2'); //RSA/RSA2
  57. $gateway->setAppId($this->config['app_id']);
  58. $gateway->setPrivateKey($this->config['private_key']); // 可以是路径,也可以是密钥内容
  59. $gateway->setAlipayPublicKey($this->config['public_key']); // 可以是路径,也可以是密钥内容
  60. $request = $gateway->completePurchase();
  61. $request->setParams($_POST); //Optional
  62. try {
  63. /** @var \Omnipay\Alipay\Responses\AopCompletePurchaseResponse $response */
  64. $response = $request->send();
  65. if ($response->isPaid()) {
  66. /**
  67. * Payment is successful
  68. */
  69. return [
  70. 'trade_no' => $params['out_trade_no'],
  71. 'callback_no' => $params['trade_no']
  72. ];
  73. } else {
  74. /**
  75. * Payment is not successful
  76. */
  77. return false;
  78. }
  79. } catch (\Exception $e) {
  80. /**
  81. * Payment is not successful
  82. */
  83. return false;
  84. }
  85. }
  86. }