PayHelper.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. namespace App\Helper;
  3. use Omnipay\Omnipay;
  4. use App\Models\OrderInfoModel;
  5. use Config, Request,QrCode;
  6. trait PayHelper
  7. {
  8. private function prepare() {
  9. // Pingpp::setApiKey(env('PINGPP_APP_KEY'));
  10. // Pingpp::setPrivateKeyPath(config_path() . 'ppp_pri_key.pem');
  11. }
  12. /**
  13. * 验签
  14. * @param $data
  15. * @param $sig
  16. * @return int
  17. */
  18. public function checkPppSignature($data, $sig) {
  19. $pubKey = file_get_contents(config_path() . 'ppp_pub_key.pem');
  20. return openssl_verify($data, $sig, $pubKey, 'sha256');
  21. }
  22. private function saveOrder($data) {
  23. $order = new OrderInfoModel();
  24. $order->code = $data['code'];
  25. $order->transaction_id = $data['transaction_id'];
  26. $order->user_id = $data['user_id'];
  27. $order->goods_id = $data['goods_id'];
  28. $order->price = $data['price'];
  29. $order->number = $data['number'];
  30. $order->amount = $data['amount'];
  31. $order->pay_type = $data['pay_type'];
  32. if (isset($data['ext_info'])) {
  33. $order->ext_info = $data['ext_info'];
  34. }
  35. if ($order->save()) {
  36. return $order->find($order->id);
  37. }
  38. return false;
  39. }
  40. public function createAlipayCharge($data, $profile = 'alipay') {
  41. $gateways = Config::get('laravel-omnipay.gateways');
  42. if (array_key_exists($profile, $gateways)) {
  43. $config = $gateways[$profile];
  44. } else {
  45. \Log::error('alipay: 不支持的支付类型, ' . $profile);
  46. return false;
  47. }
  48. $gateway = Omnipay::create($config['driver']);
  49. $gateway->setEnvironment($config['options']['environment']);
  50. $gateway->setAppId($config['options']['appid']);
  51. $gateway->setEncryptKey($config['options']['encrypt_key']);
  52. $gateway->setPrivateKey($config['options']['prikey']);
  53. $gateway->setAlipayPublicKey($config['options']['ali_pubkey']);
  54. $gateway->setNotifyUrl($config['options']['notify_url']);
  55. $goodsTypes = OrderInfoModel::getAllGoodsTypes();
  56. if (!isset($data['subject'])) {
  57. $data['subject'] = sprintf("瞄喵%s充值%d", $goodsTypes[$data['goods']], $data['number']);
  58. }
  59. if (!isset($data['goods_id'])) {
  60. $data['goods_id'] = 0;
  61. }
  62. $data['transaction_id'] = date('YmdHis') . mt_rand(1000, 9999);
  63. $data['code'] = 'ALIPAY_' . $data['transaction_id'];
  64. $data['price'] = 1;
  65. $data['amount'] = $data['number'] * $data['price']*100;
  66. $data['pay_type'] = OrderInfoModel::PAY_TYPE_ALIPAY;
  67. $request = $gateway->purchase();
  68. $request->setBizContent([
  69. 'subject' => $data['subject'],
  70. 'out_trade_no' => $data['transaction_id'],
  71. 'total_amount' => round($data['amount'] / 100, 2),
  72. 'product_code' => 'QUICK_MSECURITY_PAY',//固定值
  73. ]);
  74. if ($profile == 'alipay_f2f') {
  75. $request->setBizContent([
  76. 'subject' => $data['subject'],
  77. 'out_trade_no' => $data['transaction_id'],
  78. 'total_amount' => round($data['amount'] / 100, 2),
  79. ]);
  80. }
  81. // dd($request);
  82. $response = $request->send();
  83. if ($response->isSuccessful()) {
  84. if ($profile == 'alipay_app' || $profile == 'alipay') {
  85. $orderString = $response->getOrderString();
  86. }
  87. if ($profile == 'alipay_f2f') {
  88. $code_url = $response->getQrCode();
  89. $code_path = public_path('qrcodes/'.$data['code'].'.png');
  90. QrCode::format('png')->size(500)->generate($code_url,$code_path);
  91. $orderString = env('APP_URL').'/qrcodes/'.$data['code'].'.png';
  92. }
  93. \Log::info("支付宝生成订返回:".$orderString);
  94. //写订单
  95. $order = $this->saveOrder($data);
  96. if ($order !== false) {
  97. $result = $order->toArray();
  98. $result['orderString'] = $orderString;
  99. return $result;
  100. }
  101. \Log::error('save order failed');
  102. return false;
  103. } else {
  104. \Log::error('alipay response failed'.$response->getMessage());
  105. return false;
  106. }
  107. }
  108. public function createWechatpayCharge($data, $profile = 'wechatpay') {
  109. $gateways = Config::get('laravel-omnipay.gateways');
  110. if (array_key_exists($profile, $gateways)) {
  111. $config = $gateways[$profile];
  112. } else {
  113. \Log::error('wechatpay: 不支持的支付设置, ' . $profile);
  114. return false;
  115. }
  116. $gateway = Omnipay::create($config['driver']);
  117. // $gateway->setEnvironment('sandbox');
  118. $gateway->setAppId($config['options']['appid']);
  119. $gateway->setMchId($config['options']['merchant_id']);
  120. $gateway->setApiKey($config['options']['apikey']);
  121. $gateway->setNotifyUrl($config['options']['notify_url']);
  122. $goodsTypes = OrderInfoModel::getAllGoodsTypes();
  123. if (!isset($data['subject'])) {
  124. $data['subject'] = sprintf("瞄喵%s充值%d", $goodsTypes[$data['goods']], $data['number']);
  125. }
  126. if (!isset($data['goods_id'])) {
  127. $data['goods_id'] = 0;
  128. }
  129. $data['transaction_id'] = date('YmdHis') . mt_rand(1000, 9999);
  130. $data['code'] = 'WECHATPAY_' . $data['transaction_id'];
  131. $data['price'] = 1;
  132. $data['amount'] = $data['number'] * $data['price']*100;
  133. $data['pay_type'] = OrderInfoModel::PAY_TYPE_WECHATPAY;
  134. $order = [
  135. 'body' => $data['subject'],
  136. 'out_trade_no' => $data['transaction_id'],
  137. 'total_fee' => $data['amount'],
  138. 'spbill_create_ip' => Request::ip(),
  139. 'fee_type' => 'CNY'
  140. ];
  141. $request = $gateway->purchase($order);
  142. $response = $request->send();
  143. // dd($response);
  144. if ($response->isSuccessful()) {
  145. if ($profile == 'wechatpay_app' || $profile == 'wechatpay') {
  146. $orderString = $response->getAppOrderData();
  147. }
  148. // if ($profile == 'wechatpay_native') {
  149. // $code_url = $response->getCodeUrl();
  150. // $code_path = public_path('qrcodes/'.$data['code'].'.png');
  151. // QrCode::format('png')->size(500)->generate($code_url,$code_path);
  152. // $orderString = env('APP_URL').'/qrcodes/'.$data['code'].'.png';
  153. // }
  154. \Log::debug($orderString);
  155. //写订单
  156. $order = $this->saveOrder($data);
  157. if ($order !== false) {
  158. $result = $order->toArray();
  159. $result['orderString'] = $orderString;
  160. return $result;
  161. }
  162. \Log::error('save order failed');
  163. return false;
  164. } else {
  165. \Log::error($response->getData());
  166. return false;
  167. }
  168. //available methods
  169. // $response->isSuccessful();
  170. // $response->getData(); //For debug
  171. // $response->getAppOrderData(); //For WechatPay_App
  172. // $response->getJsOrderData(); //For WechatPay_Js
  173. // $response->getCodeUrl(); //For Native Trade Type
  174. }
  175. }