当前位置:网站首页>Creating wechat voucher process with PHP
Creating wechat voucher process with PHP
2022-04-23 03:03:00 【Stupid little attendant】
-
Create wechat voucher interface document address
https://pay.weixin.qq.com/wiki/doc/apiv3/wxpay/marketing/convention/chapter3_1.shtml -
Be careful : All voucher interfaces must comply with api-v3 Interface rules
api-v3 Interface rule address
https://wechatpay-api.gitbook.io/wechatpay-api-v3/ -
Signature verification rules can be downloaded from wechat PHP-SDK, There is a specific implementation
PHP-SDK Download address
https://pay.weixin.qq.com/wiki/doc/apiv3/wechatpay/wechatpay6_0.shtml -
Specific code ( Reference resources )
<?php
/** * Create wechat payment vouchers * @param Request $request * @return \Illuminate\Http\JsonResponse * @throws \GuzzleHttp\Exception\GuzzleException */
public function createWeChatPayCashCoupon(Request $request){
$requestAllData = $request->all();
$merchantId = $this->merchantId;
// Merchant number
$merchantId = $merchantId;
// Merchant API Certificate serial number
$merchantSerialNumber = $this->serial_no;
// Merchants private key
$merchantPrivateKey = PemUtil::loadPrivateKey($this->privateKeyPath);
// Wechat payment platform certificate
$wechatpayCertificate = PemUtil::loadCertificate($this->weChatPayCertificateKeyPath);
// Construct a WechatPayMiddleware
$wechatpayMiddleware = WechatPayMiddleware::builder()
->withMerchant($merchantId, $merchantSerialNumber, $merchantPrivateKey) // Incoming merchant related configuration
->withWechatPay([ $wechatpayCertificate ]) // Multiple wechat payment platform certificates can be imported , Parameter type is array
->build();
// take WechatPayMiddleware Add to Guzzle Of HandlerStack in
$stack = HandlerStack::create();
$stack->push($wechatpayMiddleware, 'wechatpay');
// establish Guzzle HTTP Client when , take HandlerStack Pass in
$client = new Client(['handler' => $stack]);
try {
$out_request_no = $this->getOutRequestNo();
$available_begin_time = $this->dateTransformationTimezone($requestAllData['available_begin_time']);
$available_end_time = $this->dateTransformationTimezone($requestAllData['available_end_time']);
$couponData['store_id'] = $requestAllData['store_id'];
$couponData['coupon_type'] = 2;
$couponData['coupon_stock_name'] = isset($requestAllData['coupon_stock_name']) ? $requestAllData['coupon_stock_name'] : " vouchers ";
$couponData['coupon_belong_merchant'] = $this->merchantId;
$couponData['coupon_comment'] = isset($requestAllData['coupon_comment']) ? $requestAllData['coupon_comment'] : " vouchers ";
$couponData['goods_name'] = " Single product use ";
$couponData['coupon_stock_type'] = "NORMAL";
$couponData['out_request_no'] = $out_request_no;
$couponData['coupon_code_mode'] = "WECHATPAY_MODE";
$couponData['available_begin_time'] = $available_begin_time;
$couponData['available_end_time'] = $available_end_time;
$couponData['discount_amount'] = intval($requestAllData['discount_amount']);
$couponData['transaction_minimum'] = intval($requestAllData['transaction_minimum']);
$couponData['use_method'] = 3;
$couponData['max_amount'] = $requestAllData['max_coupons'] * $requestAllData['discount_amount'];
$couponData['max_coupons'] = intval($requestAllData['max_coupons']);
$couponData['max_coupons_per_user'] = intval($requestAllData['max_coupons_per_user']) ?? 1;
// For the request
$resp = $client->request('POST', 'https://api.mch.weixin.qq.com/v3/marketing/favor/coupon-stocks', [
'json' => [
'stock_name' => $couponData['coupon_stock_name'],
'belong_merchant' => $merchantId,
'comment' => $couponData['coupon_comment'],
'available_begin_time' => $available_begin_time,
'available_end_time' => $available_end_time,
'stock_use_rule' => [
'max_coupons' => $couponData['max_coupons'], // Total quantity
'max_amount' => $couponData['max_coupons'] * $couponData['discount_amount'], // general budget
'max_coupons_per_user' => $couponData['max_coupons_per_user'],
'natural_person_limit' => false,
'prevent_api_abuse' => false,
],
'coupon_use_rule' => [
'fixed_normal_coupon' => [
'coupon_amount' => $couponData['discount_amount'],
'transaction_minimum' => $couponData['transaction_minimum'],
],
'available_merchants' => [
$weixinStoreInfo->wx_sub_merchant_id
],
],
'no_cash' => true,
'stock_type' => 'NORMAL',
'out_request_no' => $out_request_no
],
'headers' => ['Accept' => 'application/json']
]);
$coupon = json_decode($resp->getBody(),true);
if(isset($coupon['stock_id']) && !empty($coupon['stock_id'])){
$couponData['stock_id'] = $coupon['stock_id'];
$couponData['available_begin_time'] = $requestAllData['available_begin_time'];
$couponData['available_end_time'] = $requestAllData['available_end_time'];
date_default_timezone_set("Asia/Shanghai");
$couponData['created_at'] = $this->timezoneTransformationDate($coupon['create_time']);
// Create card data
$customerAppletsCouponsModel = new CustomerAppletsCoupons();
$result = $customerAppletsCouponsModel->createCoupon($couponData);
if($result['status'] == 200){
// return $this->responseDataJson($resp->getStatusCode(),$resp->getReasonPhrase(),$coupon);
return $this->responseDataJson(1,' Create success ',$coupon);
}else{
return $this->responseDataJson(202," Card coupon data storage failed ");
}
}else{
return $this->responseDataJson(202," Create failure ");
}
} catch (RequestException $e) {
$errorData = json_decode($e->getResponse()->getBody(),true);
return $this->responseDataJson($e->getResponse()->getStatusCode(),$errorData['message'],$errorData);
}
}
版权声明
本文为[Stupid little attendant]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220630238454.html
边栏推荐
- Résumé du gestionnaire de projet du système d'information Chapitre VI gestion des ressources humaines du projet
- Thoughts on the 2022 national network security competition of the national secondary vocational group (only one idea for myself) - network security competition questions (8)
- Traversée de l'arbre L2 - 006
- C# 读写二进制文件
- tf. keras. layers. Timedistributed function
- Golden nine silver ten interview season, you are welcome to take away the interview questions (with detailed answer analysis)
- Use of MySQL command line client and common commands
- VirtualBox virtual machine (Oracle VM)
- Encapsulation of ele table
- Publish to NPM?
猜你喜欢

Summary of software test interview questions

Binary tree

L2-006 樹的遍曆(中後序確定二叉樹&層序遍曆)

Cloud computing learning 1 - openstack cloud computing installation and deployment steps with pictures and texts (Xiandian 2.2)

Traversal of l2-006 tree (middle and later order determination binary tree & sequence traversal)

Response processing of openfeign

PDH optical transceiver 4-way E1 + 4-way 100M Ethernet 4-way 2m optical transceiver FC single fiber 20km rack type

Summary of interface automation interview questions for software testing

Linux redis - redis database caching service

tf. keras. layers. Timedistributed function
随机推荐
REINFORCE
JS learning notes
How to write the expected salary on your resume to double your salary during the interview?
Plug in for vscode
MYSQL04_ Exercises corresponding to arithmetic, logic, bit, operator and operator
Numpy append function
Face longitude:
Chapter IV project cost management of information system project manager summary
MAUI初体验:爽
腾讯视频涨价:一年多赚74亿!关注我领取腾讯VIP会员,周卡低至7元
eventBus
使用两种方法来解决“最大回文数乘积”问题
【新版发布】ComponentOne 新增 .NET 6 和 Blazor 平台控件支持
Q-Learning & Sarsa
[Euler plan question 13] sum of large numbers
Summary of interface automation interview questions for software testing
ASP.NET 6 中间件系列 - 执行顺序
Processes and threads
Guangcheng cloud service can fill in a daily report regularly every day
Publish to NPM?