Transaction.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <?php
  2. namespace app\common\library;
  3. use think\Db;
  4. /**
  5. * 苹果server to server
  6. */
  7. class Transaction
  8. {
  9. /**
  10. * 续费or升级
  11. *
  12. * @param $transaction_info
  13. * @param bool $is_upgrade
  14. * @return bool
  15. */
  16. public static function autoRenew($transaction_info, $is_upgrade = false)
  17. {
  18. $original_transaction_id = isset($transaction_info['originalTransactionId']) ? $transaction_info['originalTransactionId'] : ''; // 订阅原始ID
  19. $transaction_id = isset($transaction_info['transactionId']) ? $transaction_info['transactionId'] : ''; // 苹果订单号
  20. $product_id = isset($transaction_info['productId']) ? $transaction_info['productId'] : ''; // 产品id
  21. if (!$original_transaction_id || !$transaction_id || !$product_id) {
  22. return '参数有误';
  23. }
  24. filePut("\r\n\r\n".'新的S2S');
  25. $prefix = '[S2S]originalTransactionId:'.$original_transaction_id.',transactionId:'.$transaction_id.',productId:'.$product_id.'。';
  26. //检查重复订单
  27. $check_map = [
  28. 'original_transaction_id' => $original_transaction_id,
  29. 'transaction_id' => $transaction_id,
  30. ];
  31. $check_order = Db::name('user_vipxufei_task')->where($check_map)->field('id')->find();
  32. if($check_order){
  33. filePut($prefix.'续费早已完成');
  34. return '续费早已完成';
  35. }
  36. Db::startTrans();
  37. //查找订单,可能找到以前的。只看同一个用户的,其他用户的这里不处理
  38. $order_map = [
  39. 'original_transaction_id' => $original_transaction_id,
  40. ];
  41. $order_info = Db::name('user_vipxufei_task')->where($order_map)->order('expires_date_ms desc')->find();
  42. if (!$order_info) {
  43. Db::rollback();
  44. filePut($prefix.'不存在的订单');
  45. return '不存在的订单';
  46. }
  47. //续订,但是换了产品了,重新定义order_info
  48. //原始id换给别人用了
  49. $order_info_bundle_id = $order_info['bundle_id'];
  50. $order_info_user_id = $order_info['user_id'];
  51. if($product_id != $order_info['bundle_id']){
  52. $pay_order_map = [
  53. 'user_id' => $order_info['user_id'],
  54. 'bundle_id' => $product_id,
  55. 'order_status' => 0,
  56. 'table_name' => 'vip_recharge',
  57. ];
  58. $pay_order = Db::name('pay_order')->where($pay_order_map)->order('id desc')->lock(true)->find();
  59. if(!$pay_order){
  60. Db::rollback();
  61. filePut($prefix.'未找到匹配的交易,新订单找不到');
  62. return '未找到匹配的交易,新订单找不到';
  63. }
  64. // 修改订单状态
  65. $update_order = [
  66. 'notifytime'=>time(),
  67. 'order_status'=>1,
  68. 'original_transaction_id' => $original_transaction_id,
  69. 'transaction_info' => json_encode($transaction_info),
  70. ];
  71. $ros = Db::name('pay_order')->where(['id' => $pay_order['id']])->update($update_order);
  72. if($ros === false) {
  73. filePut($prefix.'修改订单状态失败');
  74. Db::rollback();
  75. return '修改订单状态失败';
  76. }
  77. $args = json_decode($pay_order['args'],true);
  78. //修改order_info
  79. $order_info['order_id'] = $pay_order['id'];
  80. $order_info['user_id'] = $pay_order['user_id'];
  81. $order_info['bundle_id'] = $pay_order['bundle_id'];
  82. $order_info['days'] = $args['days'];
  83. $order_info['original_transaction_id'] = $original_transaction_id;//多余
  84. }
  85. //验证时间,不得小于最新的一条预定信息
  86. if($transaction_info['purchaseDate'] <= $order_info['purchase_date_ms'] || $transaction_info['expiresDate'] <= $order_info['expires_date_ms']){
  87. Db::rollback();
  88. filePut($prefix.'时间对不上,返回成功,finish掉');
  89. return '时间对不上,返回成功,finish掉';
  90. }
  91. //逻辑开始
  92. //先充值
  93. $user_info = Db::name('user_wallet')->where('user_id',$order_info['user_id'])->lock(true)->find();
  94. if($user_info['vip_endtime'] < time()){
  95. //过期了
  96. $vip_endtime = time() + (intval($order_info['days']) * 86400);
  97. }else{
  98. //追加vip
  99. $vip_endtime = $user_info['vip_endtime'] + (intval($order_info['days']) * 86400);
  100. }
  101. $update_data = [
  102. 'vip_endtime'=>$vip_endtime,
  103. ];
  104. $result = Db::name('user_wallet')->where('user_id',$order_info['user_id'])->update($update_data);
  105. if($result === false)
  106. {
  107. filePut($prefix.'逻辑续费vip时间失败');
  108. Db::rollback();
  109. return '逻辑续费vip时间失败';
  110. }
  111. //逻辑结束
  112. //添加新的一个task
  113. $task_data = $order_info;
  114. unset($task_data['id']);
  115. $task_data['createtime'] = time();
  116. $task_data['apple_receipt'] = '';
  117. $task_data['in_app_one'] = '';
  118. $task_data['transaction_id'] = $transaction_id;
  119. $task_data['times'] = $order_info['times'] + 1;
  120. $task_data['original_purchase_date_ms'] = $transaction_info['originalPurchaseDate'];
  121. $task_data['purchase_date_ms'] = $transaction_info['purchaseDate'];
  122. $task_data['expires_date_ms'] = $transaction_info['expiresDate'];
  123. $task_data['transaction_info'] = json_encode($transaction_info);
  124. if($product_id != $order_info_bundle_id){
  125. $task_data['times'] = 1;//回归1
  126. }
  127. $task_id = Db::name('user_vipxufei_task')->insertGetId($task_data);
  128. if(!$task_id)
  129. {
  130. filePut($prefix.'用户添加vipxufei_task失败');
  131. Db::rollback();
  132. return '用户添加vipxufei_task失败';
  133. }
  134. Db::commit();
  135. filePut($prefix.'充值成功'.$task_id);
  136. return '充值成功'.$task_id;
  137. //逻辑结束
  138. }
  139. /**
  140. * 退款
  141. *
  142. * @param $transaction_info
  143. * @return bool
  144. */
  145. public static function refund($transaction_info)
  146. {
  147. return true;
  148. /*$original_transaction_id = $transaction_info['originalTransactionId']; // 订阅ID
  149. $web_order_line_item_id = $transaction_info['webOrderLineItemId']; // 交易ID
  150. $base_msg = self::_getBaseMsg($original_transaction_id, $web_order_line_item_id) . '- 退款:';
  151. $transaction_info_data = Transaction::query()
  152. ->where('web_order_line_item_id', '=', $transaction_info['webOrderLineItemId'])
  153. ->where('original_transaction_id', '=', $transaction_info['originalTransactionId'])
  154. ->first();
  155. if (!$transaction_info_data) {
  156. Log::channel('transaction')->warning($base_msg . ' not find transaction info');
  157. return false;
  158. }
  159. // 修改退款时间以及状态
  160. Transaction::query()
  161. ->where('web_order_line_item_id', '=', $transaction_info['webOrderLineItemId'])
  162. ->where('original_transaction_id', '=', $transaction_info['originalTransactionId'])
  163. ->update([
  164. 'cancellation_date_ms' => date('Y-m-d H:i:s', $transaction_info['revocationDate'] / 1000),
  165. 'is_cancellation' => 1,
  166. ]);
  167. // 修改用户会员为到期
  168. User::saveExpireVip($transaction_info_data->user_id);
  169. return true;*/
  170. }
  171. /**
  172. * 订阅
  173. *
  174. * @param $transaction_info
  175. * @param $subtype
  176. * @return bool
  177. */
  178. public static function updateSubscribed($transaction_info, $subtype)
  179. {
  180. return true;
  181. }
  182. /**
  183. * 续订失败
  184. *
  185. * @param $transaction_info
  186. * @return bool
  187. */
  188. public static function updateExpiredTrans($transaction_info)
  189. {
  190. return true;
  191. }
  192. /**
  193. * 续订失败
  194. *
  195. * @param $transaction_info
  196. * @return bool
  197. */
  198. public static function updateRenewFailTrans($transaction_info)
  199. {
  200. return true;
  201. }
  202. /**
  203. * 自动续费状态修改
  204. *
  205. * @param $transaction_info
  206. * @param $subtype
  207. * @return bool
  208. */
  209. public static function changeRenewStatus($transaction_info, $subtype)
  210. {
  211. return true;
  212. }
  213. }