BitpayX.php 4.1 KB

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