BitpayX.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. namespace App\Http\Controllers\Gateway;
  3. use App\Models\Payment;
  4. use Auth;
  5. use Illuminate\Http\Request;
  6. use Response;
  7. class BitpayX extends AbstractPayment {
  8. private $bitpayGatewayUri = 'https://api.mugglepay.com/v1/';
  9. /**
  10. * @param Request $request
  11. *
  12. * @return mixed
  13. */
  14. public function purchase(Request $request) {
  15. $payment = new Payment();
  16. $payment->trade_no = self::generateGuid();
  17. $payment->user_id = Auth::id();
  18. $payment->oid = $request->input('oid');
  19. $payment->amount = $request->input('amount');
  20. $payment->save();
  21. $data = [
  22. 'merchant_order_id' => $payment->trade_no,
  23. 'price_amount' => (float) $request->input('amount'),
  24. 'price_currency' => 'CNY',
  25. 'pay_currency' => $request->input('type') == 1? 'ALIPAY' : 'WECHAT',
  26. 'title' => '支付单号:'.$payment->trade_no,
  27. 'description' => parent::$systemConfig['subject_name']?: parent::$systemConfig['website_name'],
  28. 'callback_url' => (parent::$systemConfig['website_callback_url']?: parent::$systemConfig['website_url']).'/callback/notify?method=bitpayx',
  29. 'success_url' => parent::$systemConfig['website_url'].'/invoices',
  30. 'cancel_url' => parent::$systemConfig['website_url'],
  31. 'token' => $this->sign($this->prepareSignId($payment->trade_no)),
  32. ];
  33. $result = json_decode($this->mprequest($data), true);
  34. if($result['status'] === 200 || $result['status'] === 201){
  35. $result['payment_url'] .= '&lang=zh';
  36. Payment::whereId($payment->id)->update(['url' => $result['payment_url']]);
  37. return Response::json([
  38. 'status' => 'success',
  39. 'url' => $result['payment_url'] .= '&lang=zh',
  40. 'message' => '创建订单成功!'
  41. ]);
  42. }
  43. return Response::json(['status' => 'fail', 'data' => $result, 'message' => '创建订单失败!']);
  44. }
  45. private function sign($data) {
  46. return strtolower(md5(md5($data).parent::$systemConfig['bitpay_secret']));
  47. }
  48. private function prepareSignId($tradeno) {
  49. $data_sign = [
  50. 'merchant_order_id' => $tradeno,
  51. 'secret' => parent::$systemConfig['bitpay_secret'],
  52. 'type' => 'FIAT',
  53. ];
  54. ksort($data_sign);
  55. return http_build_query($data_sign);
  56. }
  57. private function mprequest($data, $type = 'pay') {
  58. $headers = ['content-type: application/json', 'token: '.parent::$systemConfig['bitpay_secret']];
  59. $curl = curl_init();
  60. if($type === 'pay'){
  61. $this->bitpayGatewayUri .= 'orders';
  62. curl_setopt($curl, CURLOPT_URL, $this->bitpayGatewayUri);
  63. curl_setopt($curl, CURLOPT_POST, 1);
  64. $data_string = json_encode($data);
  65. curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);
  66. }elseif($type === 'query'){
  67. $this->bitpayGatewayUri .= 'orders/merchant_order_id/status?id='.$data['merchant_order_id'];
  68. curl_setopt($curl, CURLOPT_URL, $this->bitpayGatewayUri);
  69. curl_setopt($curl, CURLOPT_HTTPGET, 1);
  70. }
  71. curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
  72. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  73. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  74. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  75. $data = curl_exec($curl);
  76. curl_close($curl);
  77. return $data;
  78. }
  79. public function notify(Request $request) {
  80. $inputString = file_get_contents('php://input', 'r');
  81. $inputStripped = str_replace(["\r", "\n", "\t", "\v"], '', $inputString);
  82. $inputJSON = json_decode($inputStripped, true); //convert JSON into array
  83. $data = [];
  84. if($inputJSON !== null){
  85. $data = [
  86. 'status' => $inputJSON['status'],
  87. 'order_id' => $inputJSON['order_id'],
  88. 'merchant_order_id' => $inputJSON['merchant_order_id'],
  89. 'price_amount' => $inputJSON['price_amount'],
  90. 'price_currency' => $inputJSON['price_currency'],
  91. 'created_at_t' => $inputJSON['created_at_t'],
  92. ];
  93. }
  94. // 准备待签名数据
  95. $str_to_sign = $this->prepareSignId($inputJSON['merchant_order_id']);
  96. $resultVerify = $this->verify($str_to_sign, $inputJSON['token']);
  97. $isPaid = $data !== null && $data['status'] !== null && $data['status'] === 'PAID';
  98. if($resultVerify && $isPaid){
  99. $this->postPayment($inputJSON['merchant_order_id'], 'BitPayX');
  100. $return['status'] = 200;
  101. echo json_encode($return);
  102. }else{
  103. $return['status'] = 400;
  104. echo json_encode($return);
  105. }
  106. exit();
  107. }
  108. private function verify($data, $signature) {
  109. $mySign = $this->sign($data);
  110. return $mySign === $signature;
  111. }
  112. }