How to write the Alipay interface for the Douyin mini program?

How to write the Alipay interface for the Douyin mini program?

The process of connecting Douyin to Alipay

First, obtain the SP order number provided by Douyin through the server;

Then send the SP order number and other data to Alipay.

Then pass the information returned by Alipay back to the Douyin side so that the payment is connected.

include 'alipay.class.php';
include 'AopSdk.php';
$risk_info=json_encode(array('ip'=>$_SERVER['REMOTE_ADDR']));
$array=array(
'app_id'=>'800117472149',
'method'=>'tp.trade.create',
'charset'=>'utf-8',
'sign_type'=>'MD5',
'timestamp'=>time(),
'version'=>'1.0',
'out_order_no'=>'20190727000',
'uid'=>'4ybyfXwNMulvUhBT',
'merchant_id'=>'1900011759',
'total_amount'=>100,
'currency'=>'CNY',
'subject'=>'Merchant order name',
'body'=>'Merchant order details',
'trade_time'=>time(),
'valid_time'=>'20',
'notify_url'=>'https://www.juxunapp.com/',
'risk_info'=>$risk_info
);

$aplipay = new alipay($array);

$res=$aplipay->pay();
app_id = isset($args['app_id']) ? $args['app_id'] : null;
$this->method = isset($args['method']) ? $args['method'] : null;
$this->charset = isset($args['charset']) ? $args['charset'] : null;
$this->sign_type = isset($args['sign_type']) ? $args['sign_type'] : null;
$this->timestamp = isset($args['timestamp']) ? $args['timestamp'] : null;
$this->version = isset($args['version']) ? $args['version'] : null;
$this->out_order_no = isset($args['out_order_no']) ? $args['out_order_no'] : null;
$this->uid = isset($args['uid']) ? $args['uid'] : null;
$this->merchant_id = isset($args['merchant_id']) ? $args['merchant_id'] : null;
$this->total_amount = isset($args['total_amount']) ? $args['total_amount'] : null;
$this->currency = isset($args['currency']) ? $args['currency'] : null;
$this->subject = isset($args['subject']) ? $args['subject'] : null;
$this->body = isset($args['body']) ? $args['body'] : null;
$this->trade_time = isset($args['trade_time']) ? $args['trade_time'] : null;
$this->valid_time = isset($args['valid_time']) ? $args['valid_time'] : null;
$this->notify_url = isset($args['notify_url']) ? $args['notify_url'] : null;
$this->risk_info = isset($args['risk_info']) ? $args['risk_info'] : null;

$biz_content = json_encode(array(
'out_order_no'=>$this->out_order_no,
'uid'=>$this->uid,
'merchant_id'=>$this->merchant_id,
'total_amount'=>$this->total_amount,
'currency'=>$this->currency,
'subject'=>$this->subject,
'body'=>$this->body,
'trade_time'=>$this->trade_time,
'valid_time'=>$this->valid_time,
'notify_url'=>$this->notify_url,
'risk_info'=>$this->risk_info
));

$this->biz_content=$biz_content;

$sign['app_id']=$this->app_id;
$sign['method']=$this->method;
$sign['charset']=$this->charset;
$sign['sign_type']=$this->sign_type;
$sign['timestamp']=$this->timestamp;
$sign['version']=$this->version;
$sign['biz_content']=$this->biz_content;

$this->sign = md5($this->getSignContent($sign,'utf-8','gx9j1saqpjkjgp024kzm6ypncfufruaxk76xk12b'));

}

public function pay() {
$url="https://tp-pay.snssdk.com/gateway";
$data=array('app_id'=>$this->app_id,'method'=>$this->method,'charset'=>$this->charset,'sign_type'=>$this->sign_type,'sign'=>$this->sign,'timestamp'=>$this->timestamp,'version'=>$this->version,'biz_content'=>$this->biz_content);
$res = json_decode($this->send_post($url,$data),true);
//Here we need to return the data to the front end, and pull up the Alipay cashier through the front end

$ali_data=array(
'body'=>$this->body,
'subject'=>$this->subject,
'body'=>$this->body,
'fee'=>$this->total_amount
);
$ali_data2=array(
'trade_no'=>$res['response']['trade_no']
);

$config=array(
'ali_app_app_id'=>'2019121069845600',
'ali_app_rsa_pri_key'=>'MIFEowIBAAKCAQEAxAfJcO3atT9LofwrDJuZP09nzUgNW5VKdZmSxHnXLqjzo+w2AIbBVmURTLtyDFAJAB19QZ2BXNtll2pb8rXonqQHZFi+5+cyB2/pLKRRNdaqGlQTbLShpbFhPrVNUFgg/zb2O5XiL1PXH3abNY45fVTiyseDJbIlGq1SeH12A5f5UZTA+/bsUTe2HLgupGltTNA3/OwBsw2u3zgr/aYjsj9mf029+HbzZtVMpJlQ2If80tcE9k8U75u+21H/ujDjKYThVbAKQPDSoUmLWlFPasExqRsKffIZdxbANNsKnXpJAP00NA0Nk06d3xmjfYdZiQtkpMo0myKF5JWUGRz7wwIDA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4+P9lMlyT9J953lJdGUdj7rQI87mg8NamNrAhcfdg5HuXSDlfLhlnW9rT16pc2yCi3n5qZDCpydAmBB2MS91vteRvg05p17tzRIghxQuFSyl+djeEK1KtHcvMcblFL56G7YEAoBhRlkWeiVr0B',
'ali_app_pay_rsa_pub_key'=>'MIIAIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAxAfJcO3atT9LofwrDJuZP09nzUgNW5VKdZmSxHnXLqjzo+w2AIbBVmURTLtyDFAJAB19QZ2BXNtll2pb8rXonqQHZFi+5+cyB2/pLKRRNdaqGlQTbLShpbFhPrVNUFgg/zb2O5XiL1PXH3abNY45fVTiyseDJbIlGq1SeH12A5f5UZTA+/bsUTe2HLgupGltTNA3/OwBsw2u3zgr/aYjsj9mf029+HbzZtVMpJlQ2If80tcE9k8U75u+21H/ujDjKYThVbAKQPDSoUmLWlFPasExqRsKffIZdxbANNsKnXpJAP00NA0Nk06d3xmjfYdZiQtkpMo0myKF5JWUGRz7wwIDAQAB',
);

$website="https://www.juxunapp.com/";
$data2 = [];
$data2['trade_no'] = $res['response']['trade_no']; //The trade_no order number just obtained
$data2['app_id'] = $this->app_id; //ID assigned by Toutiao Payment to the business party
$data2['sign_type'] = $this->sign_type; //MD5 used by Toutiao
$data2['timestamp'] = (string)$this->timestamp; //Time when the request is sent
$data2['merchant_id'] = $this->merchant_id; //Merchant ID assigned by Toutiao Payment to the business party
$data2['uid'] = $this->uid; //Uniquely identifies the user open_id (this is Toutiao's)
$data2['total_amount'] = $this->total_amount; //Amount, divided into units, should be passed as integer
$data2['params'] = json_encode(['url' => $this->aliapppaytest($ali_data , $biz_content , $ali_data2 , $website , $config)]); //Here are the request parameters for Alipay APP payment. I used Alipay SDK here

$stringToBeSigned = $this->getSignContent($data2, $this->charset, 'gx9j1saqpjkjgp024kzm6ypncfufruaxk76xk12b'); //Sign the above parameters again
$data2["sign"] = md5($stringToBeSigned);

$data2['pay_channel'] = "ALIPAY_NO_SIGN";
$data2["pay_type"] = "ALIPAY_APP";
$data2['method'] = 'tp.trade.confirm';
$data2['risk_info'] = $this->risk_info;
return ['error'=>0 , 'msg'=>'success' , 'data'=>$data2]; // Return these parameters to the front end for processing
//return $res;
//return json_encode($this->alipayData($res));
}

public function aliapppaytest($data, $biz_content, $data2, $website, $config){
require 'AopSdk.php';
$aop = new \AopClient();
$aop->gatewayUrl = "https://openapi.alipay.com/gateway.do";
$aop->appId = $config['ali_app_app_id']; //Alipay APPID
$aop->rsaPrivateKey = $config['ali_app_rsa_pri_key']; //Alipay secret key
$aop->format = "json";
$aop->charset = "UTF-8";
$aop->signType = "RSA2";
$aop->alipayrsaPublicKey = $config['ali_app_pay_rsa_pub_key']; //Alipay public key

//Instantiate the request class corresponding to the specific API. The class name corresponds to the interface name. The current calling interface name is: alipay.trade.app.pay
$request = new AlipayTradeAppPayRequest(); //SDK has encapsulated the public parameters, only the business parameters need to be passed in here
$data2 = [
'body' => $data['body'],
'subject' => $data['subject'],
'out_trade_no' => $data2['trade_no'],
'timeout_express' => '30m',
'total_amount' => $data['fee']/1000,
'product_code' => 'QUICK_MSECURITY_PAY',
];
$postdata = json_encode($data2);
$request->setNotifyUrl($website . '/payment/toutiao/notify.php'); //Server asynchronous notification http address);
$request->setBizContent($postdata);
//$response = $aop->sdkExecute($request); //This is different from the normal interface call, using sdkExecute

$response = $aop->sdkExecute($request);

//htmlspecialchars is used to prevent the browser from escaping the key parameter HTML when outputting to the page. This problem will not occur when printing to the log or transmitting via HTTP.
// return htmlspecialchars($str); // orderString can be directly requested to the client without further processing.
return $response; //OrderString can be directly sent to the client request without further processing.
}

public function alipayData($res){

$biz_content=json_encode(array(
'subject'=>$this->subject,
'out_trade_no'=>$this->out_order_no,
'total_amount'=>$this->total_amount,
'product_code'=>"QUICK_MSECURITY_PAY"

));

$url=array(
'app_id'=>$this->app_id,
'biz_content'=>$biz_content,
'charset'=>$this->charset,
'method'=>'alipay.trade.app.pay',
'notify_url'=>$this->notify_url,
'sign'=>$res['sign'],
'sign_type'=>'RSA2',
'timestamp'=>time(),
'version'=>$this->version
);

$url = $this->getSignContent($url,'utf-8','');

$data=array(
'app_id'=>$this->app_id,
'sign_type'=>'MD5',
'timestamp'=>(string)$this->timestamp,
'trade_no'=>$res['response']['trade_no'],
'merchant_id'=>'1900011747',
'uid'=>'4ybyfXwNMulvUhBT',
'total_amount'=>100,
'params'=>json_encode(array('url'=>$url)),
'sign'=>$this->sign,
'method'=>'tp.trade.confirm',
'pay_channel'=>'ALIPAY_NO_SIGN',
'pay_type'=>'ALIPAY_APP',
'risk_info' => $this->risk_info
);

return $data;
}

public function getSignContent($params, $charset,$app_secret) {
ksort($params);
$stringToBeSigned = "";
$i = 0;
foreach ($params as $k => $v) {
if (false === $this->checkEmpty($v) && "@" != substr($v, 0, 1)) {
// Convert to target character set
$v = $this->characet($v, $charset);
if ($i == 0) {
$stringToBeSigned .= "$k" . "=" . "$v";
} else {
$stringToBeSigned .= "&" . "$k" . "=" . "$v";
}
$i++;
}
}
$stringToBeSigned = $stringToBeSigned.$app_secret;

unset ($k, $v);
return $stringToBeSigned;
}
public function checkEmpty($value) {
if (!isset($value))
return true;
if ($value === null)
return true;
if (trim($value) === "")
return true;
return false;
}

public function characet($data, $targetCharset) {
if (!empty($data)) {
$fileType = "UTF-8";
if (strcasecmp($fileType, $targetCharset) != 0) {
$data = mb_convert_encoding($data, $targetCharset, $fileType);
}
}
return $data;
}

public function send_post($url, $post_data) {
$postdata = http_build_query($post_data);
$options = array(
'http' => array(
'method' => 'POST',
'header' => 'Content-type:application/x-www-form-urlencoded',
'content' => $postdata,
'timeout' => 15 * 60 // timeout (unit: s)
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
return $result;

}

}

?>

Page({
data: {},
onLoad: function () {

},
pay: function () {
tt.request({
url: 'https://www.juxunapp.com/alipay2/aop/test/index.php', // target server url
success: function (res) {
console.log(res.data.data)
tt.requestPayment({
data:res.data.data,
success(res) {
console.log(res);
},
fail(res) {
console.log(res);
}
})
}
});

}
});

<<:  Why do we say “third-rate plays with products, second-rate plays with traffic, and first-rate plays with users”!

>>:  When planning a hit event, you need to pay attention to 3 issues

Recommend

Where does pufferfish's toxicity come from and how can we eat them safely?

Hilsa shad, razor fish and puffer fish are collec...

Wang Xuan: The tactical life of the "contemporary Bi Sheng"

Wang Xuan is a famous computer scientist in my co...

Operations are not based on intuition, but are driven by data.

Many college students who are about to graduate w...

How to design a high conversion landing page? Focus on these three aspects

Previously, the editor summarized 4 misunderstand...

This old friend was born in the polar day and I meet him every winter.

Many years later, in the winter, facing the whoop...

After upgrading to 4G, which mobile phone has the fastest internet speed?

Of course not Xiaomi phones. Although Lei Jun’s s...

The conversion rate is too low! How to write product introduction copy?

Recently I saw an activity called “100-Day Progre...

What should I do if my memory continues to decline?

《Cotton Swab Medical Science Popularization》 Beij...