AlipayF2F.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. /**
  3. * 自己写别抄,抄NMB抄
  4. */
  5. namespace App\Payments;
  6. use Omnipay\Omnipay;
  7. class AlipayF2F {
  8. public function __construct($config)
  9. {
  10. $this->config = $config;
  11. }
  12. public function form()
  13. {
  14. return [
  15. 'app_id' => [
  16. 'label' => '支付宝APPID',
  17. 'description' => '',
  18. 'type' => 'input',
  19. ],
  20. 'private_key' => [
  21. 'label' => '支付宝私钥',
  22. 'description' => '',
  23. 'type' => 'input',
  24. ],
  25. 'public_key' => [
  26. 'label' => '支付宝公钥',
  27. 'description' => '',
  28. 'type' => 'input',
  29. ]
  30. ];
  31. }
  32. public function pay($order)
  33. {
  34. $gateway = Omnipay::create('Alipay_AopF2F');
  35. $gateway->setSignType('RSA2'); //RSA/RSA2
  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. $request = $gateway->purchase();
  41. $request->setBizContent([
  42. 'subject' => config('v2board.app_name', 'V2Board') . ' - 订阅',
  43. 'out_trade_no' => $order['trade_no'],
  44. 'total_amount' => $order['total_amount'] / 100
  45. ]);
  46. /** @var \Omnipay\Alipay\Responses\AopTradePreCreateResponse $response */
  47. $response = $request->send();
  48. $result = $response->getAlipayResponse();
  49. if ($result['code'] !== '10000') {
  50. abort(500, $result['sub_msg']);
  51. }
  52. return [
  53. 'type' => 0, // 0:qrcode 1:url
  54. 'data' => $response->getQrCode()
  55. ];
  56. }
  57. public function notify($params)
  58. {
  59. $gateway = Omnipay::create('Alipay_AopF2F');
  60. $gateway->setSignType('RSA2'); //RSA/RSA2
  61. $gateway->setAppId($this->config['app_id']);
  62. $gateway->setPrivateKey($this->config['private_key']); // 可以是路径,也可以是密钥内容
  63. $gateway->setAlipayPublicKey($this->config['public_key']); // 可以是路径,也可以是密钥内容
  64. $request = $gateway->completePurchase();
  65. $request->setParams($_POST); //Optional
  66. try {
  67. /** @var \Omnipay\Alipay\Responses\AopCompletePurchaseResponse $response */
  68. $response = $request->send();
  69. if ($response->isPaid()) {
  70. /**
  71. * Payment is successful
  72. */
  73. return [
  74. 'trade_no' => $params['out_trade_no'],
  75. 'callback_no' => $params['trade_no']
  76. ];
  77. } else {
  78. /**
  79. * Payment is not successful
  80. */
  81. return false;
  82. }
  83. } catch (\Exception $e) {
  84. /**
  85. * Payment is not successful
  86. */
  87. return false;
  88. }
  89. }
  90. }