Pay.Class.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: phperstar
  5. * Date: 2020/6/22
  6. * Time: 2:30 PM
  7. */
  8. namespace Util\ByteDance;
  9. use Mall\Framework\Core\ResultWrapper;
  10. use Mall\Framework\Core\ErrorCode;
  11. class Pay
  12. {
  13. /**
  14. * 开发者后台支付设置页的appId
  15. * @var string $appid
  16. */
  17. private $appid;
  18. /**
  19. * 开发者后台支付设置页的商户号
  20. * @var string $merchant_id
  21. */
  22. private $merchant_id;
  23. /**
  24. * 开发者后台支付设置页的支付secret
  25. * @var string $app_secret
  26. */
  27. private $app_secret;
  28. public function __construct($appid, $merchant_id, $app_secret)
  29. {
  30. $this->appid = $appid;
  31. $this->merchant_id = $merchant_id;
  32. $this->app_secret = $app_secret;
  33. }
  34. /**
  35. * 官方文档地址:https://microapp.bytedance.com/dev/cn/mini-app/develop/api/open-interface/payment/tt.pay
  36. * @param $orderNo
  37. */
  38. public function pay($orderNo, $total_fee, $shopName, $ip, $wx_url)
  39. {
  40. $orderInfo = [
  41. 'app_id' => $this->appid,
  42. 'sign_type' => 'MD5',
  43. 'out_order_no' => $orderNo,
  44. 'merchant_id' => $this->merchant_id, // 开发者后台支付设置页的商户号
  45. 'timestamp' => (string)time(),
  46. 'product_code' => 'pay',
  47. 'payment_type' => 'direct',
  48. 'total_amount' => yuanToFen($total_fee), // 金额
  49. 'trade_type' => 'H5',
  50. 'uid' => $this->appid,
  51. 'version' => '2.0',
  52. 'currency' => 'CNY',
  53. 'subject' => $shopName.'-'.'线上商城', // 商户订单名称
  54. 'body' => $shopName.'-'.'线上商城', // 商户订单详情
  55. 'trade_time' => (string)time(),
  56. 'valid_time' => (string)3600, // 订单有效时间
  57. 'notify_url' => 'https://tp-pay.snssdk.com/paycallback',
  58. 'wx_url' => $wx_url,
  59. 'risk_info' => json_encode(['ip'=>$ip]),
  60. ];
  61. if(!empty($orderInfo['wx_url'])){
  62. $orderInfo['wx_type'] = 'MWEB';
  63. }
  64. $orderInfo['sign'] = self::getSign($orderInfo);
  65. return ResultWrapper::success($orderInfo);
  66. }
  67. /**
  68. * 生成签名方法
  69. * 官方接口地址: https://microapp.bytedance.com/dev/cn/mini-app/develop/api/open-interface/payment/mini-app-pay-plugin-reference/server-sign
  70. * 官方签名测试地址: https://pay-doc.toutiao.com/tools/sign
  71. * @param $parm
  72. * @return string
  73. */
  74. public function getSign($parm)
  75. {
  76. // 剔除risk_info
  77. unset($parm['risk_info']);
  78. //非空参数值的参数按照参数名ASCII码从小到大排序(字典序)
  79. if(ksort($parm)){
  80. $stringA = '';
  81. //使用URL键值对的格式拼接成字符串stringA
  82. $i = 1;
  83. foreach($parm as $key => $value){
  84. if($value === 0 || !empty($value)){
  85. $stringA .=$key.'='.$value;
  86. if($i != count($parm)){
  87. $stringA .= '&';
  88. }
  89. }
  90. $i++;
  91. }
  92. }else{
  93. return ResultWrapper::fail('对参数排序出错', ErrorCode::$paramError);
  94. }
  95. //在stringA最后拼接上key=商户支付密钥
  96. $stringSignTemp = $stringA.$this->app_secret;
  97. $signValue = md5($stringSignTemp);
  98. return $signValue;
  99. }
  100. }