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. $this->customResult = '<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>';
  10. }
  11. public function form()
  12. {
  13. return [
  14. 'app_id' => [
  15. 'label' => 'APPID',
  16. 'description' => '绑定微信支付商户的APPID',
  17. 'type' => 'input',
  18. ],
  19. 'mch_id' => [
  20. 'label' => '商户号',
  21. 'description' => '微信支付商户号',
  22. 'type' => 'input',
  23. ],
  24. 'api_key' => [
  25. 'label' => 'APIKEY(v1)',
  26. 'description' => '',
  27. 'type' => 'input',
  28. ]
  29. ];
  30. }
  31. public function pay($order)
  32. {
  33. $gateway = Omnipay::create('WechatPay_Native');
  34. $gateway->setAppId($this->config['app_id']);
  35. $gateway->setMchId($this->config['mch_id']);
  36. $gateway->setApiKey($this->config['api_key']);
  37. $gateway->setNotifyUrl($order['notify_url']);
  38. $params = [
  39. 'body' => $order['trade_no'],
  40. 'out_trade_no' => $order['trade_no'],
  41. 'total_fee' => $order['total_amount'],
  42. 'spbill_create_ip' => '0.0.0.0',
  43. 'fee_type' => 'CNY'
  44. ];
  45. $request = $gateway->purchase($params);
  46. $response = $request->send();
  47. $response = $response->getData();
  48. if ($response['return_code'] !== 'SUCCESS') {
  49. abort(500, $response['return_msg']);
  50. }
  51. return [
  52. 'type' => 0,
  53. 'data' => $response['code_url']
  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. }