ByteDancePay.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. namespace app\service\byte_dance;
  3. use think\Cache;
  4. use think\Collection;
  5. class ByteDancePay implements ByteDanceInterface {
  6. protected $data=[
  7. 'out_order_no'=>'',
  8. 'total_amount'=>'',
  9. 'subject'=>'',
  10. 'body'=>'',
  11. 'valid_time'=>86400,
  12. 'notify_url'=>'',
  13. 'thirdparty_id'=>''
  14. ];
  15. public function __construct()
  16. {
  17. }
  18. /**
  19. * @param mixed $order_no
  20. * @param $amount
  21. * @param $subject
  22. * @return ByteDancePay
  23. */
  24. public static function setInfo($order_no,$amount,$subject)
  25. {
  26. $class=new self;
  27. $class->data['app_id']=ByteDance::appId();
  28. $class->data['out_order_no'] = $order_no;
  29. $class->data['total_amount'] = $amount*100;
  30. $class->data['subject'] = $subject;
  31. $class->data['body'] = $subject;
  32. $class->data['notify_url']=request()->domain().'/index/payment/notify_dy/order_no/'.$order_no;
  33. return $class;
  34. }
  35. public function get()
  36. {
  37. $data=$this->data;
  38. $data['sign']=$this->sign($data);
  39. $order=ByteDance::httpPost('https://developer.toutiao.com/api/apps/ecpay/v1/create_order',$data);
  40. $obj=ByteDanceResult::make();
  41. foreach ($order as $key=>$item){
  42. $obj->offsetSet($key,$item);
  43. }
  44. return $obj;
  45. }
  46. protected function sign($map) {
  47. $rList = array();
  48. foreach($map as $k =>$v) {
  49. if ($k == "other_settle_params" || $k == "app_id" || $k == "sign" || $k == "thirdparty_id")
  50. continue;
  51. $value = trim(strval($v));
  52. $len = strlen($value);
  53. if ($len > 1 && substr($value, 0,1)=="\"" && substr($value,$len, $len-1)=="\"")
  54. $value = substr($value,1, $len-1);
  55. $value = trim($value);
  56. if ($value == "" || $value == "null")
  57. continue;
  58. array_push($rList, $value);
  59. }
  60. array_push($rList, config('site.byte_dance_mapp_pay_salt'));
  61. sort($rList, 2);
  62. return md5(implode('&', $rList));
  63. }
  64. }
  65. /**
  66. * @property string $order_id
  67. * @property string $order_token
  68. * @property string $url
  69. */
  70. class ByteDanceResult extends Collection {}