WechatPayNative.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. namespace App\Payments;
  3. use Omnipay\Omnipay;
  4. use Omnipay\WechatPay\Helper;
  5. class WechatPayNative {
  6. public function __construct($config)
  7. {
  8. $this->config = $config;
  9. }
  10. public function form()
  11. {
  12. return [
  13. 'app_id' => [
  14. 'label' => 'APPID',
  15. 'description' => '绑定微信支付商户的APPID',
  16. 'type' => 'input',
  17. ],
  18. 'mch_id' => [
  19. 'label' => '商户号',
  20. 'description' => '微信支付商户号',
  21. 'type' => 'input',
  22. ],
  23. 'api_key' => [
  24. 'label' => 'APIKEY(v1)',
  25. 'description' => '',
  26. 'type' => 'input',
  27. ]
  28. ];
  29. }
  30. public function pay($order)
  31. {
  32. $gateway = Omnipay::create('WechatPay_Native');
  33. $gateway->setAppId($this->config['app_id']);
  34. $gateway->setMchId($this->config['mch_id']);
  35. $gateway->setApiKey($this->config['api_key']);
  36. $gateway->setNotifyUrl($order['notify_url']);
  37. $params = [
  38. 'body' => $order['trade_no'],
  39. 'out_trade_no' => $order['trade_no'],
  40. 'total_fee' => $order['total_amount'],
  41. 'spbill_create_ip' => '0.0.0.0',
  42. 'fee_type' => 'CNY'
  43. ];
  44. $request = $gateway->purchase($params);
  45. $response = $request->send();
  46. $response = $response->getData();
  47. if ($response['return_code'] !== 'SUCCESS') {
  48. abort(500, $response['return_msg']);
  49. }
  50. return [
  51. 'type' => 0,
  52. 'data' => $response['code_url'],
  53. 'custom_result' => '<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>'
  54. ];
  55. }
  56. public function notify($params)
  57. {
  58. $data = Helper::xml2array(file_get_contents('php://input'));
  59. $gateway = Omnipay::create('WechatPay');
  60. $gateway->setAppId($this->config['app_id']);
  61. $gateway->setMchId($this->config['mch_id']);
  62. $gateway->setApiKey($this->config['api_key']);
  63. $response = $gateway->completePurchase([
  64. 'request_params' => file_get_contents('php://input')
  65. ])->send();
  66. if (!$response->isPaid()) {
  67. die('FAIL');
  68. }
  69. return [
  70. 'trade_no' => $data['out_trade_no'],
  71. 'callback_no' => $data['transaction_id']
  72. ];
  73. }
  74. }