BitpayX.php 4.1 KB

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