CryptoLogic.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <?php
  2. namespace App\Http\logic\api;
  3. use App\dao\Pay\AccountPayInfo;
  4. use App\dao\Pay\PayController;
  5. use App\Facade\TableName;
  6. use App\Http\helper\CreateOrder;
  7. use App\Http\helper\HttpCurl;
  8. use App\Http\helper\NumConfig;
  9. use App\Jobs\Order;
  10. use App\Models\Cpf;
  11. use App\Notification\TelegramBot;
  12. use App\Services\CreateLog;
  13. use App\Services\Crypto;
  14. use App\Services\OrderServices;
  15. use App\Services\PayConfig;
  16. use App\Util;
  17. use Illuminate\Support\Facades\DB;
  18. use Illuminate\Support\Facades\Log;
  19. use Illuminate\Support\Facades\Redis;
  20. class CryptoLogic extends BaseApiLogic
  21. {
  22. public $result;
  23. public function pay_order($userId, $pay_amount, $userPhone, $userEmail, $userName, $GiftsID, $buyIP,$AdId,$eventType)
  24. {
  25. $dao = new AccountPayInfo();
  26. [$userPhone, $userName, $userEmail] = $dao->payInfo($userId);
  27. $pay_amount = (int)$pay_amount;
  28. // 礼包类型验证
  29. $PayVerify = new PayController();
  30. $pay_amount = $PayVerify->verify($userId, $GiftsID, $pay_amount) ;
  31. if ($PayVerify->verify($userId, $GiftsID, $pay_amount) === false) {
  32. $this->error = $PayVerify->getError();
  33. return false;
  34. }
  35. if ($pay_amount < 0) {
  36. $this->error = 'Payment error_4';
  37. return false;
  38. }
  39. $payConfigService = new PayConfig();
  40. $config = $payConfigService->getConfig('Crypto');
  41. $order_sn = CreateOrder::order_sn($userId);
  42. // 生成订单信息
  43. $logic = new OrderLogic();
  44. $amount = $pay_amount * NumConfig::NUM_VALUE;
  45. $logic->orderCreate($order_sn, $amount, 'Crypto', $userId, '', $GiftsID,$AdId,$eventType);
  46. $service = new Crypto();
  47. $data = [
  48. "totalPrice" => $pay_amount, // Total price of the purchase (should match sum of line items)
  49. "fiatCurrency" => "EUR", // Fiat currency (e.g., EUR, USD)
  50. "cryptoAllowedSlippage" => 0.1, // Allowed slippage for the crypto conversion
  51. "externalOrderId" => $order_sn, // Unique order ID
  52. "lineItems" => [
  53. [
  54. "quantity" => 1, // Quantity of the product
  55. "price" => $pay_amount, // Price of the product
  56. "name" => "Test Item", // Name of the item
  57. "description" => "Description of the item" // Description of the item
  58. ]
  59. ],
  60. "timeout" => 3600, // Timeout for the purchase in seconds
  61. "transactionStatusWebhookUrl" => $config['notify'],
  62. "kyc" => [
  63. "email" => $userEmail, // User's email for KYC (Know Your Customer) verification
  64. "phoneNumber" => $userPhone, // User's phone number
  65. "firstName" => $userName, // User's first name
  66. "lastName" => $userName, // User's last name
  67. "countryTax" => "EU", // User's country tax information
  68. ]
  69. ];
  70. // 生成用户请求信息日志
  71. if (is_array($data)) {
  72. $request_extra = \GuzzleHttp\json_encode($data);
  73. } else {
  74. $request_extra = '';
  75. }
  76. CreateLog::pay_request($userPhone, $request_extra, $order_sn, $userEmail, $userId, $userName);
  77. $result = $service->curlPost('/merchant/v1/purchase/init', $data);
  78. // $this->result=compact('result','data');
  79. Util::WriteLog('Crypto','Crypto支付请求'.$request_extra);
  80. Util::WriteLog('Crypto','Crypto支付结果'.json_encode($result));
  81. try {
  82. $data = $result;
  83. $data['mchOrderNo'] = $order_sn;
  84. }catch (\Exception $e){
  85. Util::WriteLog("Crypto_error",[$result,$e->getMessage(),$e->getTraceAsString()]);
  86. return false;
  87. }
  88. return $data;
  89. }
  90. public function notify($post)
  91. {
  92. $order_sn = $post['externalOrderId'];
  93. try {
  94. // 查询订单信息
  95. $order = DB::connection('write')->table('agent.dbo.order')->where('order_sn', $order_sn)->first();
  96. if (!$order) {
  97. Util::WriteLog('Crypto','订单不存在');
  98. return '{"success":false,"message":"商户自定义出错信息"}';
  99. }
  100. if ((!empty($order->pay_at) || !empty($order->finished_at)) ) {
  101. return 'SUCCESS';
  102. }
  103. $body = [
  104. 'payment_sn' => $post['cryptoTransactionId'],
  105. 'updated_at' => date('Y-m-d H:i:s'),
  106. ];
  107. $apply_data = [
  108. 'order_id' => $order->id,
  109. 'payment_sn' => $post['cryptoTransactionId'],
  110. 'payment_code' => 'Crypto',
  111. 'return' => \GuzzleHttp\json_encode($post),
  112. 'is_error' => 0,
  113. 'created_at' => date('Y-m-d H:i:s'),
  114. 'updated_at' => date('Y-m-d H:i:s'),
  115. ];
  116. $ordStatus = $post['status']=='Confirmed'?3:4;
  117. $GiftsID = $order->GiftsID ?: '';
  118. $userID = $order->user_id ?: '';
  119. $AdId = $order->AdId ?: '';
  120. $eventType = $order->eventType ?: '';
  121. $payAmt = (int)$post['totalPrice'];
  122. if(!$AdId){
  123. $redis = Redis::connection();
  124. $AdId = $redis->get('user_ad_'.$userID);
  125. }
  126. // 说明:除了成功,失败和退款状态,都可以认为是处理中
  127. switch ($ordStatus) {
  128. case 3: // 支付成功
  129. $body['pay_status'] = 1;
  130. $body['pay_at'] = date('Y-m-d H:i:s');
  131. $body['finished_at'] = date('Y-m-d H:i:s');
  132. $body['amount'] = $payAmt * NumConfig::NUM_VALUE;
  133. $config = (new PayConfig())->getConfig('Crypto');
  134. $body['payment_fee']=$body['amount']*$config['payin_fee'];
  135. try {
  136. // 获取金额
  137. $service = new OrderServices();
  138. if ($order->amount != $body['amount']) {
  139. $body['GiftsID'] = 0;
  140. $body['amount'] = $payAmt * NumConfig::NUM_VALUE;
  141. $Recharge = $payAmt;
  142. $give = 0;
  143. $favorable_price = $Recharge + $give;
  144. $czReason = 1;
  145. $cjReason = 45;
  146. } else {
  147. [$give, $favorable_price, $Recharge, $czReason, $cjReason] = $service->getPayInfo($GiftsID, $userID, $payAmt);
  148. }
  149. // 增加充值记录
  150. [$Score] = $service->addRecord($userID, $payAmt, $favorable_price, $order_sn, $GiftsID, $Recharge, $czReason, $give, $cjReason, $AdId, $eventType);
  151. // 成功处理回调
  152. Order::dispatch([$userID, $payAmt, $Score, $favorable_price, $GiftsID, $order_sn]);
  153. }catch (\Exception $exception) {
  154. TelegramBot::getDefault()->sendProgramNotify("OrderService Except ",$exception->getMessage() );
  155. }
  156. // $cpf = @$post['reference'];
  157. // if($cpf){
  158. // Cpf::insertCpf($userID,$cpf);
  159. // }
  160. break;
  161. case 4: // 支付失败
  162. $body['pay_status'] = 2;
  163. $apply_data['is_error'] = 1;
  164. break;
  165. }
  166. $order_up = DB::connection('write')->table('agent.dbo.order')
  167. ->where('order_sn', $order_sn)
  168. ->update($body);
  169. $apply = DB::connection('write')->table('agent.dbo.payment_apply')
  170. ->insert($apply_data);
  171. if (($order_up && $apply) != true) {
  172. Util::WriteLog('SitoBank','订单更新失败');
  173. return '{"success":false,"message":"商户自定义出错信息"}';
  174. }
  175. //Util::WriteLog('SitoBank','success');
  176. return 'SUCCESS';
  177. } catch (\Exception $exception) {
  178. Util::WriteLog('SitoBank','Goopago 支付失败' . $exception->getMessage());
  179. $this->error = $exception->getMessage();
  180. return '{"success":false,"message":"商户自定义出错信息"}';
  181. }
  182. }
  183. public function pay_search($orderNo){
  184. $payConfigService = new PayConfig();
  185. $config = $payConfigService->getConfig('Goopago');
  186. $service = new Goopago();
  187. $data = [
  188. 'mchId' =>$config['merchantID'],
  189. 'nonceStr' => $service->getNonceStr(),
  190. 'mchOrderNo' => $orderNo,
  191. ];
  192. $sign = $service->sign($data);
  193. $data['sign'] = $sign;
  194. $url = $config['pay_search'];
  195. $result = $service->curlPost($url, $data, 60);
  196. Log::info(var_export('Goopago 支付结果查询:', true), [$result]);
  197. try {
  198. $data = json_decode($result, true);
  199. if (json_last_error() !== JSON_ERROR_NONE) {
  200. return false;
  201. }
  202. if ($data['resCode'] != 'SUCCESS') {
  203. return false;
  204. }
  205. $orderInfo = $data['orderInfo'];
  206. if(is_array($orderInfo)){
  207. $orderStatus = $orderInfo['status'] == 2?1:0;
  208. }else{
  209. $orderInfo = json_decode($orderInfo,true);
  210. $orderStatus = $orderInfo['status'] == 2?1:0;
  211. }
  212. return $orderStatus;
  213. } catch (\Exception $e) {
  214. return false;
  215. }
  216. }
  217. }